Overview
Comment:Add support for Skriv extensions inside Markdown to manage images and files
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e26abf675cee4de7bad8781f2d43fd25163acfcbc1f16ed9ca2a2855b4d80934
User & Date: bohwaz on 2021-05-20 04:15:34
Other Links: manifest | tags
Context
2021-05-20
11:11
Implement our own lighter TOC function instead of using ParsedownToc check-in: 153b3b51b5 user: bohwaz tags: trunk
04:15
Add support for Skriv extensions inside Markdown to manage images and files check-in: e26abf675c user: bohwaz tags: trunk
03:31
Add markdown support (with TOC) check-in: 2965992701 user: bohwaz tags: trunk
Changes

Modified src/include/lib/Garradin/Web/Render/Markdown.php from [da302a81c5] to [9115b0905f].

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use Garradin\Entities\Files\File;

use Garradin\Plugin;
use Garradin\Utils;
use Garradin\Files\Files;
use Garradin\UserTemplate\CommonModifiers;

use Parsedown;
use ParsedownExtra;
use ParsedownToc;

use const Garradin\{ADMIN_URL, WWW_URL};

class Markdown
{
	use AttachmentAwareTrait;

	static protected $parsedown;

	public function render(?File $file, ?string $content = null, array $options = []): string
	{
		if (!self::$parsedown)
		{
			self::$parsedown = new ParsedownToc;
			self::$parsedown->setBreaksEnabled(true);
			self::$parsedown->setUrlsLinked(true);
			self::$parsedown->setSafeMode(true);
		}

		if ($file) {
			$this->isRelativeTo($file);
		}

		$str = $content ?? $file->fetch();

		$str = preg_replace_callback('/\(#file:([^\]\h]+)\)/', function ($match) {
			return sprintf('(%s)', $this->resolveAttachment($match[1]));
		}, $str);

		$str = self::$parsedown->text($str);

		$str = CommonModifiers::typo($str);

		$str = preg_replace_callback(';<a href="((?!https?://|\w+:|#).+)">;i', function ($matches) {
			var_dump($matches);
			return sprintf('<a href="%s" target="_parent">', $this->resolveLink($matches[1]));
		}, $str);

		return sprintf('<div class="web-content">%s</div>', $str);
	}
}







<
<
<
<






<
<


<
<
|
|
|
|
<











|











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
41
42
43
44
45
46
use Garradin\Entities\Files\File;

use Garradin\Plugin;
use Garradin\Utils;
use Garradin\Files\Files;
use Garradin\UserTemplate\CommonModifiers;





use const Garradin\{ADMIN_URL, WWW_URL};

class Markdown
{
	use AttachmentAwareTrait;



	public function render(?File $file, ?string $content = null, array $options = []): string
	{


		$parsedown = new Parsedown($file);
		$parsedown->setBreaksEnabled(true);
		$parsedown->setUrlsLinked(true);
		$parsedown->setSafeMode(true);


		if ($file) {
			$this->isRelativeTo($file);
		}

		$str = $content ?? $file->fetch();

		$str = preg_replace_callback('/\(#file:([^\]\h]+)\)/', function ($match) {
			return sprintf('(%s)', $this->resolveAttachment($match[1]));
		}, $str);

		$str = $parsedown->text($str);

		$str = CommonModifiers::typo($str);

		$str = preg_replace_callback(';<a href="((?!https?://|\w+:|#).+)">;i', function ($matches) {
			var_dump($matches);
			return sprintf('<a href="%s" target="_parent">', $this->resolveLink($matches[1]));
		}, $str);

		return sprintf('<div class="web-content">%s</div>', $str);
	}
}

Added src/include/lib/Garradin/Web/Render/Parsedown.php version [e5c3ae5f78].









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
41
42
43
44
45
46
47
48
49
50
51
52
<?php

namespace Garradin\Web\Render;

//use Parsedown;
//use ParsedownExtra;
use ParsedownToc;

use Garradin\Entities\Files\File;

class Parsedown extends ParsedownToc
{
	protected $skriv;

	function __construct(?File $file)
	{
		$this->BlockTypes['<'][] = 'SkrivExtension';
		parent::__construct();
		$this->skriv = new Skriv;

		if ($file) {
			$this->skriv->isRelativeTo($file);
		}
	}

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

		if (strpos($line, '<<') === 0 && preg_match('/^<<<?([a-z_]+)((?:(?!>>>?).)*?)(>>>?$|$)/i', trim($line), $match)) {
			$text = $this->skriv->callExtension($match);

			return [
				'char' => $line[0],
				'element' => array(
					'name' => 'div',
					'rawHtml' => $text,
					'allowRawHtmlInSafeMode' => true,
				),
				'complete' => true,
			];
		}

		return null;
	}

	protected function blockSkrivExtensionComplete(array $block): ?array
	{
		var_dump($block);
		return $block;
	}
}

Modified src/include/lib/Garradin/Web/Render/Skriv.php from [4fb5fbc98a] to [00ed3e4836].

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
..
47
48
49
50
51
52
53







54
55
56
57
58
59
60

class Skriv
{
	use AttachmentAwareTrait;

	static protected $skriv;

	public function render(?File $file, ?string $content = null, array $options = []): string
	{
		if (!self::$skriv)
		{
			self::$skriv = new \KD2\SkrivLite;
			self::$skriv->registerExtension('file', [$this, 'SkrivFile']);
			self::$skriv->registerExtension('image', [$this, 'SkrivImage']);

			// Enregistrer d'autres extensions éventuellement
			Plugin::fireSignal('skriv.init', ['skriv' => self::$skriv]);
		}




		$skriv =& self::$skriv;

		if ($file) {
			$this->isRelativeTo($file);
		}

		$str = $content ?? $file->fetch();
................................................................................

		$str = preg_replace_callback(';<a href="((?!https?://|\w+:).+)">;i', function ($matches) {
			return sprintf('<a href="%s" target="_parent">', $this->resolveLink($matches[1]));
		}, $str);

		return sprintf('<div class="web-content">%s</div>', $str);
	}








	/**
	 * Callback utilisé pour l'extension <<file>> dans le wiki-texte
	 * @param array $args    Arguments passés à l'extension
	 * @param string $content Contenu éventuel (en mode bloc)
	 * @param SkrivLite $skriv   Objet SkrivLite
	 */







|










|
>
>
>







 







>
>
>
>
>
>
>







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
41
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

class Skriv
{
	use AttachmentAwareTrait;

	static protected $skriv;

	public function __construct()
	{
		if (!self::$skriv)
		{
			self::$skriv = new \KD2\SkrivLite;
			self::$skriv->registerExtension('file', [$this, 'SkrivFile']);
			self::$skriv->registerExtension('image', [$this, 'SkrivImage']);

			// Enregistrer d'autres extensions éventuellement
			Plugin::fireSignal('skriv.init', ['skriv' => self::$skriv]);
		}
	}

	public function render(?File $file, ?string $content = null, array $options = []): string
	{
		$skriv =& self::$skriv;

		if ($file) {
			$this->isRelativeTo($file);
		}

		$str = $content ?? $file->fetch();
................................................................................

		$str = preg_replace_callback(';<a href="((?!https?://|\w+:).+)">;i', function ($matches) {
			return sprintf('<a href="%s" target="_parent">', $this->resolveLink($matches[1]));
		}, $str);

		return sprintf('<div class="web-content">%s</div>', $str);
	}

	public function callExtension(array $match)
	{
		$method = new \ReflectionMethod(self::$skriv, '_callExtension');
		$method->setAccessible(true);
		return $method->invoke(self::$skriv, $match);
	}

	/**
	 * Callback utilisé pour l'extension <<file>> dans le wiki-texte
	 * @param array $args    Arguments passés à l'extension
	 * @param string $content Contenu éventuel (en mode bloc)
	 * @param SkrivLite $skriv   Objet SkrivLite
	 */

Modified src/www/admin/static/scripts/wiki_editor.js from [c9ab486754] to [25e6fd36a1].

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
			let args = new URLSearchParams(window.location.search);
			var wiki_id = args.get('p');
			g.openFrameDialog(g.admin_url + 'web/_attach.php?_dialog&p=' + wiki_id);
		};

		window.te_insertFile = function (file)
		{
			if (config.format == 'markdown') {
				var tag = '[' + file + '](#file:' + file + ')';
			}
			else {
				var tag = '<<file|'+file+'>>';
			}

			t.insertAtPosition(t.getSelection().start, tag);

			g.closeDialog();
		};

		window.te_insertImage = function (file, position, caption)
		{
			if (config.format == 'markdown') {
				var tag = '![' + caption + '](#file:' + file + '?500px)';
			}
			else {
				var tag = '<<image|' + file;

				if (position)
					tag += '|' + position;

				if (caption)
					tag += '|' + caption;

				tag += '>>';
			}

			t.insertAtPosition(t.getSelection().start, tag);

			g.closeDialog();
		};

		var EscapeEvent = function (e) {







<
<
<
<
|
<








<
<
<
<
|

|
|

|
|

|
<







100
101
102
103
104
105
106




107

108
109
110
111
112
113
114
115




116
117
118
119
120
121
122
123
124

125
126
127
128
129
130
131
			let args = new URLSearchParams(window.location.search);
			var wiki_id = args.get('p');
			g.openFrameDialog(g.admin_url + 'web/_attach.php?_dialog&p=' + wiki_id);
		};

		window.te_insertFile = function (file)
		{




			var tag = '<<file|'+file+'>>';


			t.insertAtPosition(t.getSelection().start, tag);

			g.closeDialog();
		};

		window.te_insertImage = function (file, position, caption)
		{




			var tag = '<<image|' + file;

			if (position)
				tag += '|' + position;

			if (caption)
				tag += '|' + caption;

			tag += '>>';


			t.insertAtPosition(t.getSelection().start, tag);

			g.closeDialog();
		};

		var EscapeEvent = function (e) {