forked from giovdk21/yii2-syntaxhighlighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxHighlighter.php
169 lines (144 loc) · 5.75 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* SyntaxhigHlighter class file.
*
* @author Giovanni Derks
* @license MIT License
* http://derks.me.uk
*/
namespace niremizov\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';
private $_assetUrl;
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;
public $firstLine = 1;
public $gutter = true;
/**
* Publishes the assets
*/
public function publishAssets()
{
$this->_assetUrl = SyntaxHighlighterAsset::register($this->getView())->baseUrl;
}
/**
* 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';
}
if (empty($this->brushes)) {
// If no brushes was selected, force Asset to include autoloader js.
SyntaxHighlighterAsset::$extraJs[] = 'scripts/shAutoloader.js';
}
$this->publishAssets();
$this->registerConfig();
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;
}
protected function registerConfig() {
if (empty($this->brushes)) {
// Add autoloader path callback if no brushes was selected.
$this->registerAutoloaderConfig();
}
$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').",
'first-line': ". $this->firstLine .",
'gutter': ".($this->gutter ? 'true' : 'false')."
};\n";
$initJs .= 'SyntaxHighlighter.all();'."\n";
$this->getView()->registerJs($initJs, View::POS_READY, 'InitSyntaxHighlighter');
}
protected function registerAutoloaderConfig() {
$initJs = 'function path()
{
var args = arguments,
result = [];
for(var i = 0; i < args.length; i++)
result.push(args[i].replace("@", "' . $this->_assetUrl . '/scripts/"));
return result
};
SyntaxHighlighter.autoloader.apply(null, path(
"applescript @shBrushAppleScript.js",
"actionscript3 as3 @shBrushAS3.js",
"bash shell @shBrushBash.js",
"coldfusion cf @shBrushColdFusion.js",
"cpp c @shBrushCpp.js",
"c# c-sharp csharp @shBrushCSharp.js",
"css @shBrushCss.js",
"delphi pascal @shBrushDelphi.js",
"diff patch pas @shBrushDiff.js",
"erl erlang @shBrushErlang.js",
"groovy @shBrushGroovy.js",
"java @shBrushJava.js",
"jfx javafx @shBrushJavaFX.js",
"js jscript javascript @shBrushJScript.js",
"perl pl @shBrushPerl.js",
"php @shBrushPhp.js",
"text plain @shBrushPlain.js",
"py python @shBrushPython.js",
"ruby rails ror rb @shBrushRuby.js",
"sass scss @shBrushSass.js",
"scala @shBrushScala.js",
"sql @shBrushSql.js",
"vb vbnet @shBrushVb.js",
"xml xhtml xslt html @shBrushXml.js"
));
SyntaxHighlighter.all();
SyntaxHighlighter.config.stripBrs = true;
SyntaxHighlighter.all();';
$this->getView()->registerJs($initJs, View::POS_READY, 'InitSyntaxHighlighterAutoloader');
}
}