]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/markdown/markdown.php
Merge pull request #453 from ArthurHoaro/pr450
[github/shaarli/Shaarli.git] / plugins / markdown / markdown.php
1 <?php
2
3 /**
4 * Plugin Markdown.
5 *
6 * Shaare's descriptions are parsed with Markdown.
7 */
8
9 require_once 'Parsedown.php';
10
11 /**
12 * Parse linklist descriptions.
13 *
14 * @param array $data linklist data.
15 *
16 * @return mixed linklist data parsed in markdown (and converted to HTML).
17 */
18 function hook_markdown_render_linklist($data)
19 {
20 foreach ($data['links'] as &$value) {
21 $value['description'] = process_markdown($value['description']);
22 }
23
24 return $data;
25 }
26
27 /**
28 * Parse daily descriptions.
29 *
30 * @param array $data daily data.
31 *
32 * @return mixed daily data parsed in markdown (and converted to HTML).
33 */
34 function hook_markdown_render_daily($data)
35 {
36 // Manipulate columns data
37 foreach ($data['cols'] as &$value) {
38 foreach ($value as &$value2) {
39 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
40 }
41 }
42
43 return $data;
44 }
45
46 /**
47 * When link list is displayed, include markdown CSS.
48 *
49 * @param array $data includes data.
50 *
51 * @return mixed - includes data with markdown CSS file added.
52 */
53 function hook_markdown_render_includes($data)
54 {
55 if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
56 || $data['_PAGE_'] == Router::$PAGE_DAILY
57 || $data['_PAGE_'] == Router::$PAGE_EDITLINK
58 ) {
59
60 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
61 }
62
63 return $data;
64 }
65
66 /**
67 * Hook render_editlink.
68 * Adds an help link to markdown syntax.
69 *
70 * @param array $data data passed to plugin
71 *
72 * @return array altered $data.
73 */
74 function hook_markdown_render_editlink($data)
75 {
76 // Load help HTML into a string
77 $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
78 return $data;
79 }
80
81
82 /**
83 * Remove HTML links auto generated by Shaarli core system.
84 * Keeps HREF attributes.
85 *
86 * @param string $description input description text.
87 *
88 * @return string $description without HTML links.
89 */
90 function reverse_text2clickable($description)
91 {
92 return preg_replace('!<a +href="([^ ]*)">[^ ]+</a>!m', '$1', $description);
93 }
94
95 /**
96 * Remove <br> tag to let markdown handle it.
97 *
98 * @param string $description input description text.
99 *
100 * @return string $description without <br> tags.
101 */
102 function reverse_nl2br($description)
103 {
104 return preg_replace('!<br */?>!im', '', $description);
105 }
106
107 /**
108 * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
109 *
110 * @param string $description input description text.
111 *
112 * @return string $description without HTML links.
113 */
114 function reverse_space2nbsp($description)
115 {
116 return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
117 }
118
119 /**
120 * Remove '&gt;' at start of line auto generated by Shaarli core system
121 * to allow markdown blockquotes.
122 *
123 * @param string $description input description text.
124 *
125 * @return string $description without HTML links.
126 */
127 function reset_quote_tags($description)
128 {
129 return preg_replace('/^( *)&gt; /m', '$1> ', $description);
130 }
131
132 /**
133 * Render shaare contents through Markdown parser.
134 * 1. Remove HTML generated by Shaarli core.
135 * 2. Generate markdown descriptions.
136 * 3. Wrap description in 'markdown' CSS class.
137 *
138 * @param string $description input description text.
139 *
140 * @return string HTML processed $description.
141 */
142 function process_markdown($description)
143 {
144 $parsedown = new Parsedown();
145
146 $processedDescription = $description;
147 $processedDescription = reverse_text2clickable($processedDescription);
148 $processedDescription = reverse_nl2br($processedDescription);
149 $processedDescription = reverse_space2nbsp($processedDescription);
150 $processedDescription = reset_quote_tags($processedDescription);
151 $processedDescription = $parsedown
152 ->setMarkupEscaped(false)
153 ->setBreaksEnabled(true)
154 ->text($processedDescription);
155 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
156
157 return $processedDescription;
158 }