This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SyntaxHighlighter.php
107 lines (88 loc) · 2.87 KB
/
SyntaxHighlighter.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* SyntaxhigHlighter class file.
*
* @author Giovanni Derks
* @license MIT License
* http://derks.me.uk
*/
namespace giovdk21\yii2SyntaxHighlighter;
use Yii;
use yii\web\View;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\base\Widget as Widget;
class SyntaxHighlighter extends Widget
{
public $theme = 'default';
public $brushes = [];
public $brushAliases = [
'js' => 'JScript',
];
// config:
public $bloggerMode = false;
public $strings = [];
public $stripBrs = false;
public static $tagName = 'pre';
// defaults:
public $showToolbar = false;
public $tabSize = 4;
public $blockClassName = '';
public $autoLinks = true;
public $collapse = false;
public $showGutter = true;
public $smartTabs = true;
/**
* Publishes the assets
*/
public function publishAssets()
{
SyntaxHighlighterAsset::register($this->getView());
}
/**
* Run the widget
*/
public function run()
{
SyntaxHighlighterAsset::$extraCss[] = 'styles/shCore'.ucfirst($this->theme).'.css';
foreach ($this->brushes as $brushName) {
$brushFile = (!empty($this->brushAliases[$brushName])
? $this->brushAliases[$brushName]
: ucfirst(
$brushName
));
SyntaxHighlighterAsset::$extraJs[] = 'scripts/shBrush'.$brushFile.'.js';
}
$this->publishAssets();
$initJs = '';
$initJs .= "SyntaxHighlighter.config.bloggerMode = ".($this->bloggerMode ? 'true' : 'false').";\n";
if (!empty($this->strings)) {
$initJs .= "SyntaxHighlighter.config.strings = ".Json::encode($this->strings).";\n";
}
$initJs .= "SyntaxHighlighter.config.stripBrs = ".($this->stripBrs ? 'true' : 'false').";\n";
$initJs .= "SyntaxHighlighter.config.tagName = '".static::$tagName."';\n";
$initJs .= "SyntaxHighlighter.defaults = {
'tab-size': {$this->tabSize},
'class-name': '{$this->blockClassName}',
'auto-links': ".($this->autoLinks ? 'true' : 'false').",
'collapse': ".($this->collapse ? 'true' : 'false').",
'gutter': ".($this->showGutter ? 'true' : 'false').",
'smart-tabs': ".($this->smartTabs ? 'true' : 'false').",
'toolbar': ".($this->showToolbar ? 'true' : 'false')."
};\n";
$initJs .= 'SyntaxHighlighter.all();'."\n";
$this->getView()->registerJs($initJs, View::POS_READY, 'InitSyntaxHighlighter');
parent::run();
}
public static function getBlock($source, $type, $firstLine = 1)
{
$res = Html::tag(
static::$tagName,
htmlentities($source),
[
'class' => 'brush: '.$type.'; first-line: '.($firstLine).';',
]
);
return $res;
}
}