Completed
Push — master ( 9ebe42...c941be )
by Josh
19:16 queued 39s
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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' => ['required'    => false   ],
89
				'url'   => ['filterChain' => ['#url']]
90
			],
91
			'template' => '<a href="{@url}"><xsl:copy-of select="@title"/><xsl:apply-templates/></a>'
92
		]
93
	];
94
95
	/**
96
	* {@inheritdoc}
97
	*/
98 8
	protected function setUp()
99
	{
100 8
		$this->configurator->rulesGenerator->append('ManageParagraphs');
101
102 8
		foreach ($this->tags as $tagName => $tagConfig)
103
		{
104
			// Skip this tag if it already exists
105 8
			if (isset($this->configurator->tags[$tagName]))
106
			{
107 1
				continue;
108
			}
109
110
			// If the tag's config is a single string, it's really its default template
111 8
			if (is_string($tagConfig))
112
			{
113 8
				$tagConfig = ['template' => $tagConfig];
114
			}
115
116
			// Add this tag
117 8
			$this->configurator->tags->add($tagName, $tagConfig);
118
		}
119 8
	}
120
121
	/**
122
	* {@inheritdoc}
123
	*/
124 2
	public function asConfig()
125
	{
126 2
		return ['decodeHtmlEntities' => (bool) $this->decodeHtmlEntities];
127
	}
128
129
	/**
130
	* {@inheritdoc}
131
	*/
132 2
	public function getJSHints()
133
	{
134 2
		return ['LITEDOWN_DECODE_HTML_ENTITIES' => (int) $this->decodeHtmlEntities];
135
	}
136
137
	/**
138
	* {@inheritdoc}
139
	*/
140 1
	public function getJSParser()
141
	{
142 1
		$js = file_get_contents(__DIR__ . '/Parser/ParsedText.js') . "\n"
143 1
		    . file_get_contents(__DIR__ . '/Parser/LinkAttributesSetter.js');
144
145
		$passes = [
146 1
			'Blocks',
147
			'LinkReferences',
148
			'InlineCode',
149
			'Images',
150
			'Links',
151
			'Strikethrough',
152
			'Superscript',
153
			'Emphasis',
154
			'ForcedLineBreaks'
155
		];
156 1
		foreach ($passes as $pass)
157
		{
158
			$js .= "\n(function(){\n"
159 1
			     . file_get_contents(__DIR__ . '/Parser/Passes/' . $pass . '.js') . "\n"
160 1
			     . "parse();\n"
161 1
			     . "})();";
162
		}
163
164 1
		return $js;
165
	}
166
}