dolibarr 21.0.0-alpha
parsemd.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2008-2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
32function dolMd2Html($content, $parser = 'parsedown', $replaceimagepath = null)
33{
34 // Replace a HTML string with a Markdown syntax
35 $content = preg_replace('/<a href="([^"]+)">([^<]+)<\/a>/', '[\2](\1)', $content);
36 //$content = preg_replace('/<a href="([^"]+)" target="([^"]+)">([^<]+)<\/a>/', '[\3](\1){:target="\2"}', $content);
37 $content = preg_replace('/<a href="([^"]+)" target="([^"]+)">([^<]+)<\/a>/', '[\3](\1)', $content);
38
39 // Replace HTML comments
40 $content = preg_replace('/<!--.*-->/Ums', '', $content); // We remove HTML comment that are not MD comment because they will be escaped and output when setSafeMode is set to true.
41
42 if (is_array($replaceimagepath)) {
43 foreach ($replaceimagepath as $key => $val) {
44 $keytoreplace = ']('.$key;
45 $valafter = ']('.$val;
46 $content = preg_replace('/'.preg_quote($keytoreplace, '/').'/m', $valafter, $content);
47 }
48 }
49 if ($parser == 'parsedown') {
50 include_once DOL_DOCUMENT_ROOT.'/includes/parsedown/Parsedown.php';
51 $parsedown = new Parsedown();
52 $parsedown->setSafeMode(true); // This will escape HTML link <a href=""> into html entities but markdown links are ok
53
54 // Because HTML will be HTML entity encoded, we replace tag we want to keep
55 $content = preg_replace('/<span style="([^"]+)">/', '<!-- SPAN_STYLE_\1 -->', $content);
56 $content = preg_replace('/<\/span>/', '<!-- SPAN_END -->', $content);
57
58 $content = $parsedown->text($content);
59
60 $content = preg_replace('/&lt;!-- SPAN_STYLE_([^-]+) --&gt;/', '<span style="\1">', $content);
61 $content = preg_replace('/&lt;!-- SPAN_END --&gt;/', '</span>', $content);
62 } else {
63 $content = nl2br($content);
64 }
65
66 return $content;
67}
68
69
78function dolMd2Asciidoc($content, $parser = 'dolibarr', $replaceimagepath = null)
79{
80 if (is_array($replaceimagepath)) {
81 foreach ($replaceimagepath as $key => $val) {
82 $keytoreplace = ']('.$key;
83 $valafter = ']('.$val;
84 $content = preg_replace('/'.preg_quote($keytoreplace, '/').'/m', $valafter, $content);
85 }
86 }
87 //if ($parser == 'dolibarr')
88 //{
89 $content = preg_replace('/<!--.*-->/msU', '', $content);
90 //}
91 //else
92 //{
93 // $content = $content;
94 //}
95
96 return $content;
97}
dolMd2Asciidoc($content, $parser='dolibarr', $replaceimagepath=null)
Function to parse MD content into ASCIIDOC.
dolMd2Html($content, $parser='parsedown', $replaceimagepath=null)
Function to parse MD content into HTML.