dolibarr  19.0.0-dev
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 
32 function 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 coments
40  $content = preg_replace('/<!--.*-->/ms', '', $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  $content = $parsedown->text($content);
54  } else {
55  $content = nl2br($content);
56  }
57 
58  return $content;
59 }
60 
61 
70 function dolMd2Asciidoc($content, $parser = 'dolibarr', $replaceimagepath = null)
71 {
72  if (is_array($replaceimagepath)) {
73  foreach ($replaceimagepath as $key => $val) {
74  $keytoreplace = ']('.$key;
75  $valafter = ']('.$val;
76  $content = preg_replace('/'.preg_quote($keytoreplace, '/').'/m', $valafter, $content);
77  }
78  }
79  //if ($parser == 'dolibarr')
80  //{
81  $content = preg_replace('/<!--.*-->/msU', '', $content);
82  //}
83  //else
84  //{
85  // $content = $content;
86  //}
87 
88  return $content;
89 }
dolMd2Asciidoc($content, $parser='dolibarr', $replaceimagepath=null)
Function to parse MD content into ASCIIDOC.
Definition: parsemd.lib.php:70
dolMd2Html($content, $parser='parsedown', $replaceimagepath=null)
Function to parse MD content into HTML.
Definition: parsemd.lib.php:32