Completed
Branch wip/litedown (a8c24a)
by Josh
35:58 queued 23:44
created

Configurator::asConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Litedown;
9
10
use s9e\TextFormatter\Plugins\ConfiguratorBase;
11
12
class Configurator extends ConfiguratorBase
13
{
14
	/**
15
	* @var bool Whether to decode HTML entities in attribute values
16
	*/
17
	public $decodeHtmlEntities = false;
18
19
	/**
20
	* @var array Default tags
21
	*/
22
	protected $tags = [
23
		'C'      => '<code><xsl:apply-templates /></code>',
24
		'CODE'   => [
25
			'attributes' => [
26
				'lang' => [
27
					'filterChain' => ['#simpletext'],
28
					'required'    => false
29
				]
30
			],
31
			'template' =>
32
				'<pre>
33
					<code>
34
						<xsl:if test="@lang">
35
							<xsl:attribute name="class">
36
								<xsl:text>language-</xsl:text>
37
								<xsl:value-of select="@lang"/>
38
							</xsl:attribute>
39
						</xsl:if>
40
						<xsl:apply-templates />
41
					</code>
42
				</pre>'
43
		],
44
		'DEL'    => '<del><xsl:apply-templates/></del>',
45
		'EM'     => '<em><xsl:apply-templates/></em>',
46
		'H1'     => '<h1><xsl:apply-templates/></h1>',
47
		'H2'     => '<h2><xsl:apply-templates/></h2>',
48
		'H3'     => '<h3><xsl:apply-templates/></h3>',
49
		'H4'     => '<h4><xsl:apply-templates/></h4>',
50
		'H5'     => '<h5><xsl:apply-templates/></h5>',
51
		'H6'     => '<h6><xsl:apply-templates/></h6>',
52
		'HR'     => '<hr/>',
53
		'IMG'    => [
54
			'attributes' => [
55
				'alt'   => ['required' => false],
56
				'src'   => ['filterChain' => ['#url']],
57
				'title' => ['required' => false]
58
			],
59
			'template' => '<img src="{@src}"><xsl:copy-of select="@alt"/><xsl:copy-of select="@title"/></img>'
60
		],
61
		'LI'     => '<li><xsl:apply-templates/></li>',
62
		'LIST'   => [
63
			'attributes' => [
64
				'start' => [
65
					'filterChain' => ['#uint'],
66
					'required'    => false
67
				],
68
				'type' => [
69
					'filterChain' => ['#simpletext'],
70
					'required'    => false
71
				]
72
			],
73
			'template' =>
74
				'<xsl:choose>
75
					<xsl:when test="not(@type)">
76
						<ul><xsl:apply-templates/></ul>
77
					</xsl:when>
78
					<xsl:otherwise>
79
						<ol><xsl:copy-of select="@start"/><xsl:apply-templates/></ol>
80
					</xsl:otherwise>
81
				</xsl:choose>'
82
		],
83
		'QUOTE'  => '<blockquote><xsl:apply-templates/></blockquote>',
84
		'STRONG' => '<strong><xsl:apply-templates/></strong>',
85
		'SUP'    => '<sup><xsl:apply-templates/></sup>',
86
		'URL'    => [
87
			'attributes' => [
88
				'title' => [
89
					'required' => false
90
				],
91
				'url'   => [
92
					'filterChain' => ['#url']
93
				]
94
			],
95
			'template' => '<a href="{@url}"><xsl:copy-of select="@title"/><xsl:apply-templates/></a>'
96
		]
97
	];
98
99
	/**
100
	* {@inheritdoc}
101
	*/
102 270
	protected function setUp()
103
	{
104 270
		$this->configurator->rulesGenerator->append('ManageParagraphs');
105
106 270
		foreach ($this->tags as $tagName => $tagConfig)
107
		{
108
			// Skip this tag if it already exists
109 270
			if (isset($this->configurator->tags[$tagName]))
110 270
			{
111 1
				continue;
112
			}
113
114
			// If the tag's config is a single string, it's really its default template
115 270
			if (is_string($tagConfig))
116 270
			{
117 270
				$tagConfig = ['template' => $tagConfig];
118 270
			}
119
120
			// Replace default filters in the definition
121 270
			if (isset($tagConfig['attributes']))
122 270
			{
123 270
				foreach ($tagConfig['attributes'] as &$attributeConfig)
124
				{
125 270
					if (isset($attributeConfig['filterChain']))
126 270
					{
127 270
						foreach ($attributeConfig['filterChain'] as &$filter)
128
						{
129 270
							if (is_string($filter) && $filter[0] === '#')
130 270
							{
131 270
								$filter = $this->configurator->attributeFilters[$filter];
132 270
							}
133 270
						}
134 270
						unset($filter);
135 270
					}
136 270
				}
137 270
				unset($attributeConfig);
138 270
			}
139
140
			// Add this tag
141 270
			$this->configurator->tags->add($tagName, $tagConfig);
142 270
		}
143 270
	}
144
145
	/**
146
	* {@inheritdoc}
147
	*/
148 265
	public function asConfig()
149
	{
150 265
		return ['decodeHtmlEntities' => (bool) $this->decodeHtmlEntities];
151
	}
152
153
	/**
154
	* {@inheritdoc}
155
	*/
156 2
	public function getJSHints()
157
	{
158 2
		return ['LITEDOWN_DECODE_HTML_ENTITIES' => (int) $this->decodeHtmlEntities];
159
	}
160
161
	/**
162
	* {@inheritdoc}
163
	*/
164 263
	public function getJSParser()
165
	{
166 263
		$js = file_get_contents(__DIR__ . '/Parser/ParsedText.js') . "\n"
167 263
		    . file_get_contents(__DIR__ . '/Parser/LinkAttributesSetter.js');
168
169
		$passes = [
170 263
			'Blocks',
171 263
			'LinkReferences',
172 263
			'InlineCode',
173 263
			'Images',
174 263
			'Links',
175 263
			'Strikethrough',
176 263
			'Superscript',
177 263
			'Emphasis',
178
			'ForcedLineBreaks'
179 263
		];
180 263
		foreach ($passes as $pass)
181
		{
182
			$js .= "\n(function(){\n"
183 263
			     . file_get_contents(__DIR__ . '/Parser/Passes/' . $pass . '.js') . "\n"
184 263
			     . "parse();\n"
185 263
			     . "})();";
186 263
		}
187
188 263
		return $js;
189
	}
190
}