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