1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the syntaxhighligter-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\SyntaxHighlighterBundle\Twig\Extension; |
13
|
|
|
|
14
|
|
|
use Twig_Extension; |
15
|
|
|
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterConfig; |
16
|
|
|
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterDefaults; |
17
|
|
|
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterStrings; |
18
|
|
|
use WBW\Library\Core\Utility\Argument\StringUtility; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract SyntaxHighlighter Twig extension. |
22
|
|
|
* |
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
24
|
|
|
* @package WBW\Bundle\SyntaxHighlighterBundle\Twig\Extension |
25
|
|
|
* @abstract |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractSyntaxHighlighterTwigExtension extends Twig_Extension { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
*/ |
32
|
|
|
protected function __construct() { |
33
|
|
|
// NOTHING TO DO. |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Displays a SyntaxHighlighter config. |
38
|
|
|
* |
39
|
|
|
* @param SyntaxHighlighterConfig $config The SyntaxHighlighter configuration. |
40
|
|
|
* @return string Returns the SyntaxHighlighter config. |
41
|
|
|
*/ |
42
|
|
|
protected function syntaxHighlighterConfig(SyntaxHighlighterConfig $config) { |
43
|
|
|
|
44
|
|
|
// Initialize the template. |
45
|
|
|
$template = []; |
46
|
|
|
|
47
|
|
|
$template[] = "SyntaxHighlighter.config.bloggerMode = " . StringUtility::parseBoolean($config->getBloggerMode()) . ";"; |
48
|
|
|
$template[] = "SyntaxHighlighter.config.stripBrs = " . StringUtility::parseBoolean($config->getStripBrs()) . ";"; |
49
|
|
|
$template[] = "SyntaxHighlighter.config.tagName = \"" . $config->getTagName() . "\";"; |
50
|
|
|
if (null !== $config->getStrings()) { |
51
|
|
|
$template[] = $this->syntaxHighlighterStrings($config->getStrings()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Return the HTML. |
55
|
|
|
return implode("\n", $template); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Displays a SyntaxHighlighter content. |
60
|
|
|
* |
61
|
|
|
* @param string $language The language. |
62
|
|
|
* @param string $content The content. |
63
|
|
|
* @return string Returns the SyntaxHightlighter content. |
64
|
|
|
*/ |
65
|
|
|
protected function syntaxHighlighterContent($tag, $language, $content) { |
66
|
|
|
|
67
|
|
|
// Initialize the template. |
68
|
|
|
$template = "<%tag% %attributes%>\n%innerHTML%\n</%tag%>"; |
69
|
|
|
|
70
|
|
|
// Initialize the attributes. |
71
|
|
|
$attributes = []; |
72
|
|
|
|
73
|
|
|
$attributes["class"][] = "brush:"; |
74
|
|
|
$attributes["class"][] = null !== $language ? $language : "php"; |
|
|
|
|
75
|
|
|
|
76
|
|
|
// Return the HTML. |
77
|
|
|
return StringUtility::replace($template, ["%tag%", "%attributes%", "%innerHTML%"], [$tag, StringUtility::parseArray($attributes), htmlentities($content)]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Displays a SyntaxHighlighter defaults. |
82
|
|
|
* |
83
|
|
|
* @param SyntaxHighlighterDefaults $defaults The SyntaxHighlighter defaults. |
84
|
|
|
* @return string Returns the SyntaxHighlighter defaults. |
85
|
|
|
*/ |
86
|
|
|
protected function syntaxHighlighterDefaults(SyntaxHighlighterDefaults $defaults) { |
87
|
|
|
|
88
|
|
|
// Initialize the template. |
89
|
|
|
$template = []; |
90
|
|
|
|
91
|
|
|
$template[] = "SyntaxHighlighter.defaults['auto-links'] = " . StringUtility::parseBoolean($defaults->getAutoLinks()) . ";"; |
92
|
|
|
$template[] = "SyntaxHighlighter.defaults['class-name'] = \"" . $defaults->getClassName() . "\";"; |
93
|
|
|
$template[] = "SyntaxHighlighter.defaults['collapse'] = " . StringUtility::parseBoolean($defaults->getCollapse()) . ";"; |
94
|
|
|
$template[] = "SyntaxHighlighter.defaults['first-line'] = " . $defaults->getFirstLine() . ";"; |
95
|
|
|
$template[] = "SyntaxHighlighter.defaults['gutter'] = " . StringUtility::parseBoolean($defaults->getGutter()) . ";"; |
96
|
|
|
$template[] = "SyntaxHighlighter.defaults['highlight'] = [" . implode(",", $defaults->getHighlight()) . "];"; |
97
|
|
|
$template[] = "SyntaxHighlighter.defaults['html-script'] = " . StringUtility::parseBoolean($defaults->getHtmlScript()) . ";"; |
98
|
|
|
$template[] = "SyntaxHighlighter.defaults['smart-tabs'] = " . StringUtility::parseBoolean($defaults->getSmartTabs()) . ";"; |
99
|
|
|
$template[] = "SyntaxHighlighter.defaults['tab-size'] = " . $defaults->getTabSize() . ";"; |
100
|
|
|
$template[] = "SyntaxHighlighter.defaults['toolbar'] = " . StringUtility::parseBoolean($defaults->getToolbar()) . ";"; |
101
|
|
|
|
102
|
|
|
// Return the HTML. |
103
|
|
|
return implode("\n", $template); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Displays a SyntaxHighlighter strings. |
108
|
|
|
* |
109
|
|
|
* @param SyntaxHighlighterStrings $strings The SyntaxHighlighter strings. |
110
|
|
|
* @return string Returns the SyntaxHighlighter strings. |
111
|
|
|
*/ |
112
|
|
|
protected function syntaxHighlighterStrings(SyntaxHighlighterStrings $strings) { |
113
|
|
|
|
114
|
|
|
// Initialize the template. |
115
|
|
|
$template = []; |
116
|
|
|
|
117
|
|
|
$template[] = "SyntaxHighlighter.config.strings.alert = \"" . $strings->getAlert() . "\";"; |
118
|
|
|
$template[] = "SyntaxHighlighter.config.strings.brushNotHtmlScript = \"" . $strings->getBrushNotHtmlScript() . "\";"; |
119
|
|
|
$template[] = "SyntaxHighlighter.config.strings.copyToClipboard = \"" . $strings->getCopyToClipboard() . "\";"; |
120
|
|
|
$template[] = "SyntaxHighlighter.config.strings.copyToClipboardConfirmation = \"" . $strings->getCopyToClipboardConfirmation() . "\";"; |
121
|
|
|
$template[] = "SyntaxHighlighter.config.strings.expandSource = \"" . $strings->getExpandSource() . "\";"; |
122
|
|
|
$template[] = "SyntaxHighlighter.config.strings.help = \"" . $strings->getHelp() . "\";"; |
123
|
|
|
$template[] = "SyntaxHighlighter.config.strings.noBrush = \"" . $strings->getNoBrush() . "\";"; |
124
|
|
|
$template[] = "SyntaxHighlighter.config.strings.print = \"" . $strings->getPrint() . "\";"; |
125
|
|
|
$template[] = "SyntaxHighlighter.config.strings.viewSource = \"" . $strings->getViewSource() . "\";"; |
126
|
|
|
|
127
|
|
|
// Return the HTML. |
128
|
|
|
return implode("\n", $template); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|