1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the syntaxhighlighter-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\Environment; |
15
|
|
|
use WBW\Bundle\CoreBundle\Twig\Extension\AbstractTwigExtension; |
16
|
|
|
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterConfig; |
17
|
|
|
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterDefaults; |
18
|
|
|
use WBW\Bundle\SyntaxHighlighterBundle\API\SyntaxHighlighterStrings; |
19
|
|
|
use WBW\Library\Core\Argument\StringHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract SyntaxHighlighter Twig extension. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb/> |
25
|
|
|
* @package WBW\Bundle\SyntaxHighlighterBundle\Twig\Extension |
26
|
|
|
* @abstract |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractSyntaxHighlighterTwigExtension extends AbstractTwigExtension { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param Environment $twigEnvironment The Twig environment. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Environment $twigEnvironment) { |
36
|
|
|
parent::__construct($twigEnvironment); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Displays a SyntaxHighlighter config. |
41
|
|
|
* |
42
|
|
|
* @param SyntaxHighlighterConfig $config The configuration. |
43
|
|
|
* @return string Returns the SyntaxHighlighter config. |
44
|
|
|
*/ |
45
|
|
|
protected function syntaxHighlighterConfig(SyntaxHighlighterConfig $config) { |
46
|
|
|
|
47
|
|
|
$template = []; |
48
|
|
|
|
49
|
|
|
$template[] = "SyntaxHighlighter.config.bloggerMode = " . StringHelper::parseBoolean($config->getBloggerMode()) . ";"; |
50
|
|
|
$template[] = "SyntaxHighlighter.config.stripBrs = " . StringHelper::parseBoolean($config->getStripBrs()) . ";"; |
51
|
|
|
$template[] = "SyntaxHighlighter.config.tagName = \"" . $config->getTagName() . "\";"; |
52
|
|
|
if (null !== $config->getStrings()) { |
53
|
|
|
$template[] = $this->syntaxHighlighterStrings($config->getStrings()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return implode("\n", $template); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Displays a SyntaxHighlighter content. |
61
|
|
|
* |
62
|
|
|
* @param string $tag The tag. |
63
|
|
|
* @param string $language The language. |
64
|
|
|
* @param string $content The content. |
65
|
|
|
* @return string Returns the SyntaxHightlighter content. |
66
|
|
|
*/ |
67
|
|
|
protected function syntaxHighlighterContent($tag, $language, $content) { |
68
|
|
|
|
69
|
|
|
$attributes = []; |
70
|
|
|
|
71
|
|
|
$attributes["class"][] = "brush:"; |
72
|
|
|
$attributes["class"][] = $language; |
73
|
|
|
|
74
|
|
|
return static::coreHTMLElement($tag, implode("", ["\n", htmlentities($content), "\n"]), $attributes); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Displays a SyntaxHighlighter defaults. |
79
|
|
|
* |
80
|
|
|
* @param SyntaxHighlighterDefaults $defaults The defaults. |
81
|
|
|
* @return string Returns the SyntaxHighlighter defaults. |
82
|
|
|
*/ |
83
|
|
|
protected function syntaxHighlighterDefaults(SyntaxHighlighterDefaults $defaults) { |
84
|
|
|
|
85
|
|
|
$template = []; |
86
|
|
|
|
87
|
|
|
$template[] = "SyntaxHighlighter.defaults['auto-links'] = " . StringHelper::parseBoolean($defaults->getAutoLinks()) . ";"; |
88
|
|
|
$template[] = "SyntaxHighlighter.defaults['class-name'] = \"" . $defaults->getClassName() . "\";"; |
89
|
|
|
$template[] = "SyntaxHighlighter.defaults['collapse'] = " . StringHelper::parseBoolean($defaults->getCollapse()) . ";"; |
90
|
|
|
$template[] = "SyntaxHighlighter.defaults['first-line'] = " . $defaults->getFirstLine() . ";"; |
91
|
|
|
$template[] = "SyntaxHighlighter.defaults['gutter'] = " . StringHelper::parseBoolean($defaults->getGutter()) . ";"; |
92
|
|
|
$template[] = "SyntaxHighlighter.defaults['highlight'] = [" . implode(",", $defaults->getHighlight()) . "];"; |
93
|
|
|
$template[] = "SyntaxHighlighter.defaults['html-script'] = " . StringHelper::parseBoolean($defaults->getHtmlScript()) . ";"; |
94
|
|
|
$template[] = "SyntaxHighlighter.defaults['smart-tabs'] = " . StringHelper::parseBoolean($defaults->getSmartTabs()) . ";"; |
95
|
|
|
$template[] = "SyntaxHighlighter.defaults['tab-size'] = " . $defaults->getTabSize() . ";"; |
96
|
|
|
$template[] = "SyntaxHighlighter.defaults['toolbar'] = " . StringHelper::parseBoolean($defaults->getToolbar()) . ";"; |
97
|
|
|
|
98
|
|
|
return implode("\n", $template); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Displays a SyntaxHighlighter strings. |
103
|
|
|
* |
104
|
|
|
* @param SyntaxHighlighterStrings $strings The strings. |
105
|
|
|
* @return string Returns the SyntaxHighlighter strings. |
106
|
|
|
*/ |
107
|
|
|
protected function syntaxHighlighterStrings(SyntaxHighlighterStrings $strings) { |
108
|
|
|
|
109
|
|
|
$template = []; |
110
|
|
|
|
111
|
|
|
$template[] = "SyntaxHighlighter.config.strings.alert = \"" . $strings->getAlert() . "\";"; |
112
|
|
|
$template[] = "SyntaxHighlighter.config.strings.brushNotHtmlScript = \"" . $strings->getBrushNotHtmlScript() . "\";"; |
113
|
|
|
$template[] = "SyntaxHighlighter.config.strings.copyToClipboard = \"" . $strings->getCopyToClipboard() . "\";"; |
114
|
|
|
$template[] = "SyntaxHighlighter.config.strings.copyToClipboardConfirmation = \"" . $strings->getCopyToClipboardConfirmation() . "\";"; |
115
|
|
|
$template[] = "SyntaxHighlighter.config.strings.expandSource = \"" . $strings->getExpandSource() . "\";"; |
116
|
|
|
$template[] = "SyntaxHighlighter.config.strings.help = \"" . $strings->getHelp() . "\";"; |
117
|
|
|
$template[] = "SyntaxHighlighter.config.strings.noBrush = \"" . $strings->getNoBrush() . "\";"; |
118
|
|
|
$template[] = "SyntaxHighlighter.config.strings.print = \"" . $strings->getPrint() . "\";"; |
119
|
|
|
$template[] = "SyntaxHighlighter.config.strings.viewSource = \"" . $strings->getViewSource() . "\";"; |
120
|
|
|
|
121
|
|
|
return implode("\n", $template); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|