/
Typographer.php
79 lines (72 loc) · 1.76 KB
/
Typographer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace andrew72ru\typographer;
use Yii;
use EMT\EMTypograph;
use yii\base\Component;
use yii\helpers\Markdown;
use Michelf\MarkdownExtra;
/**
* A text (or HTML) formatter for Yii2 with Evgeny Muravjev Typograph, http://mdash.ru
*
* @package andrew72ru\typographer
* @author Andrew Zhdanovskih <andrew72ru@gmail.com>
* @param $params Array settings of emuravjev/mdash
* @param $markdown boolean Whether to use yii\helpers\Markdown to convert text
* @param $markdownType what type of markdown use in converter
*/
class Typographer extends Component
{
/**
* @var array
*/
public $params = [
'Text.paragraphs' => 'off',
'Text.breakline' => 'off',
'OptAlign.oa_oquote' => 'on',
];
/**
* @var bool
*/
public $markdown = true;
/**
* @var string
*/
public $markdownType = 'gfm';
private $_t;
/**
* @return EMTypograph
*/
public function Setup()
{
$_t = new EMTypograph();
$_t->setup($this->params);
return $_t;
}
/**
* Process the $text using settings with explode \n to <p>
* @param $text
* @return string
*/
public function typo($text)
{
$t = $this->Setup();
if($this->markdown)
$text = MarkdownExtra::defaultTransform($text);
$t->set_text($text);
return $t->apply();
}
/**
* Process $text using settings without explode \n to <p>
* $ntext = true change \n to <br>
* @param $text
* @param bool $ntext
* @return string
*/
public function directTypo($text)
{
$t = $this->Setup();
$text = Markdown::processParagraph($text, $this->markdownType);
$t->set_text($text);
return $t->apply();
}
}