Overview
Comment:Ditch ParsedownExtra and use our own footnotes implementation as ParsedownExtra is buggy and we only need footnotes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA3-256: 1116ef5677ab9650e179fdb0d7025b4eb3d8c48c5970f0385d527318361ce1e0
User & Date: bohwaz on 2021-05-20 18:06:24
Other Links: manifest | tags
Context
2021-05-20
18:26
Fix skriv rendering issue check-in: cee432377f user: bohwaz tags: trunk, stable
18:06
Ditch ParsedownExtra and use our own footnotes implementation as ParsedownExtra is buggy and we only need footnotes check-in: 1116ef5677 user: bohwaz tags: trunk, stable
16:16
Refactor File class to add generic markdown support check-in: da6e8ed150 user: bohwaz tags: trunk, stable
Changes

Modified src/Makefile from [c52b2382ef] to [7915fce3ed].

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

	rm -rf "include/lib/KD2"
	unzip "${TMP_KD2}/kd2.zip" -d include/lib

	rm -rf ${TMP_KD2}

	wget -O "include/lib/Parsedown.php" "https://raw.githubusercontent.com/erusev/parsedown/1.7.x/Parsedown.php"
	wget -O "include/lib/ParsedownExtra.php" "https://raw.githubusercontent.com/erusev/parsedown-extra/0.8.x/ParsedownExtra.php"

dev-server:
	php -S localhost:8082 -t www www/_route.php

test:
	find . -name '*.php' -print0 | xargs -0 -n1 php -l > /dev/null








<







9
10
11
12
13
14
15

16
17
18
19
20
21
22

	rm -rf "include/lib/KD2"
	unzip "${TMP_KD2}/kd2.zip" -d include/lib

	rm -rf ${TMP_KD2}

	wget -O "include/lib/Parsedown.php" "https://raw.githubusercontent.com/erusev/parsedown/1.7.x/Parsedown.php"


dev-server:
	php -S localhost:8082 -t www www/_route.php

test:
	find . -name '*.php' -print0 | xargs -0 -n1 php -l > /dev/null

Modified src/include/lib/Garradin/Web/Render/Parsedown.php from [2ee604cfb3] to [af58530681].

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
<?php

namespace Garradin\Web\Render;

//use Parsedown;
use ParsedownExtra;

use Garradin\Entities\Files\File;

use Garradin\Utils;

/**
 * Custom Parsedown extension to enable the use of Skriv extensions inside Markdown markup


 *
 * @see https://github.com/erusev/parsedown/wiki/Tutorial:-Create-Extensions
 */
class Parsedown extends ParsedownExtra
{
	protected $skriv;
	protected $rawTextTOC;

	function __construct(?File $file)
	{
		$this->BlockTypes['<'][] = 'SkrivExtension';
		$this->BlockTypes['['][]= 'TOC';



		parent::__construct();



		$this->skriv = new Skriv($file);
	}

	protected function blockSkrivExtension(array $line): ?array
	{
		$line = $line['text'];





<
|







>
>



|


|






>
>
|
>
>
>







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
<?php

namespace Garradin\Web\Render;


use Parsedown as Parent_Parsedown;

use Garradin\Entities\Files\File;

use Garradin\Utils;

/**
 * Custom Parsedown extension to enable the use of Skriv extensions inside Markdown markup
 *
 * Also adds support for footnotes and Table of Contents
 *
 * @see https://github.com/erusev/parsedown/wiki/Tutorial:-Create-Extensions
 */
class Parsedown extends Parent_Parsedown
{
	protected $skriv;
	protected $toc = [];

	function __construct(?File $file)
	{
		$this->BlockTypes['<'][] = 'SkrivExtension';
		$this->BlockTypes['['][]= 'TOC';

		# identify footnote definitions before reference definitions
		array_unshift($this->BlockTypes['['], 'Footnote');

		# identify footnote markers before before links
		array_unshift($this->InlineTypes['['], 'FootnoteMarker');

		$this->skriv = new Skriv($file);
	}

	protected function blockSkrivExtension(array $line): ?array
	{
		$line = $line['text'];

112
113
114
115
116
117
118




























































































119
120
121
122
123
124
125
126
127
128
129
130
131
			$out .= str_repeat('</ol>', $level);
		}

		$out .= '</div>';

		return $out;
	}





























































































	public function text($text)
	{
		$out = parent::text($text);

		if (false !== strpos($out, '<toc></toc>')) {
			$toc = $this->buildTOC();
			$out = str_replace('<toc></toc>', $toc, $out);
		}

		return $out;
	}
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
			$out .= str_repeat('</ol>', $level);
		}

		$out .= '</div>';

		return $out;
	}

	/**
	 * Footnotes implementation, inspired by ParsedownExtra
	 * We're not using ParsedownExtra as it's buggy and unmaintained
	 */
	protected function blockFootnote(array $line): ?array
	{
		if (preg_match('/^\[\^(.+?)\]:[ ]?(.*)$/', $line['text'], $matches))
		{
			$block = array(
				'footnotes' => [$matches[1] => $matches[2]],
			);

			return $block;
		}

		return null;
	}

	protected function blockFootnoteContinue(array $line, array $block): ?array
	{
		if ($line['text'][0] === '[' && preg_match('/^\[\^(.+?)\]: ?(.*)$/', $line['text'], $matches))
		{
			$block['footnotes'][$matches[1]] = $matches[2];
			return $block;
		}

		end($block['footnotes']);
		$last = key($block['footnotes']);

		if (isset($block['interrupted']))
		{
			if ($line['indent'] >= 4)
			{
				$block['footnotes'][$last] .= "\n\n" . $line['text'];

				return $block;
			}
		}
		else
		{
			$block['footnotes'][$last] .= "\n" . $line['text'];

			return $block;
		}
	}

	protected function blockFootnoteComplete(array $in)
	{
		$html = '';

		foreach ($in['footnotes'] as $name => $value) {
			$html .= sprintf('<dt id="fn-%s"><a href="#fn-ref-%1$s">%1$s</a></dt><dd>%s</dd>', htmlspecialchars($name), $this->text($value));
		}

		$out = [
			'element' => [
				'name'                   => 'dl',
				'attributes'             => ['class' => 'footnotes'],
				'rawHtml'                => $html,
				'allowRawHtmlInSafeMode' => true,
			],
		];

		return $out;
	}


	protected function inlineFootnoteMarker($Excerpt)
	{
		if (preg_match('/^\[\^(.+?)\]/', $Excerpt['text'], $matches))
		{
			$name = htmlspecialchars($matches[1]);

			$Element = array(
				'name' => 'sup',
				'attributes' => ['id' => 'fn-ref-'.$name],
				'handler' => 'element',
				'text' => array(
					'name' => 'a',
					'attributes' => array('href' => '#fn-'.$name, 'class' => 'footnote-ref'),
					'text' => $name,
				),
			);

			return [
				'extent' => strlen($matches[0]),
				'element' => $Element,
			];
		}
	}


	public function text($text)
	{
		$out = parent::text($text);

		if (false !== strpos($out, '<toc></toc>')) {
			$toc = $this->buildTOC();
			$out = str_replace('<toc></toc>', $toc, $out);
		}

		return $out;
	}
}

Modified src/www/skel-dist/content.css from [1b25319e11] to [dda2f569aa].

27
28
29
30
31
32
33


































34
35
36
37
38
39
40
}

.web-content blockquote {
    font-size: 1.1em;
    padding-left: 3rem;
    margin: 1rem 0;
}



































.web-content table {
    border-collapse: collapse;
}

.web-content table th, .web-content table td {
    border: 1px solid #999;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
}

.web-content blockquote {
    font-size: 1.1em;
    padding-left: 3rem;
    margin: 1rem 0;
}

.web-content a.footnote-ref, .web-content .footnotes dt a {
    color: blue;
}

.web-content a.footnote-ref::before, .web-content .footnotes dt a::before {
    content: "[";
}

.web-content a.footnote-ref::after, .web-content .footnotes dt a::after {
    content: "]";
}

.web-content dl.footnotes {
    display: grid;
    grid-template-columns: .1fr .9fr;
    border-top: 2px solid #999;
    margin-top: 1rem;
    padding-top: 1rem;
}

.web-content code {
    background: rgba(100, 100, 100, 0.2);
    padding: .2rem;
}

.web-content dl.footnotes dd {
    margin: 0;
    margin-bottom: 1rem;
}

.web-content dl.footnotes dd p {
    margin: 0;
    margin-bottom: 1rem;

.web-content table {
    border-collapse: collapse;
}

.web-content table th, .web-content table td {
    border: 1px solid #999;