2017-03-21 16:02:54 +01:00
< ? php
2023-04-22 01:01:08 +02:00
/* Copyright ( C ) 2008 - 2023 Laurent Destailleur < eldy @ users . sourceforge . net >
2017-03-21 16:02:54 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2017-03-21 16:02:54 +01:00
*/
/**
* \file htdocs / core / lib / parsemd . lib . php
2023-04-22 01:01:08 +02:00
* \brief This file contains functions dedicated to MD parsing .
2017-03-21 16:02:54 +01:00
*/
/**
* Function to parse MD content into HTML
*
2017-03-21 16:29:31 +01:00
* @ param string $content MD content
* @ param string $parser 'parsedown' or 'nl2br'
2024-01-13 19:48:20 +01:00
* @ param string $replaceimagepath Replace path to image with another path . Example : ( 'doc/' => 'xxx/aaa/' )
2017-03-21 16:29:31 +01:00
* @ return string Parsed content
2017-03-21 16:02:54 +01:00
*/
2019-01-27 15:20:16 +01:00
function dolMd2Html ( $content , $parser = 'parsedown' , $replaceimagepath = null )
2017-03-21 16:02:54 +01:00
{
2023-04-30 15:13:04 +02:00
// Replace a HTML string with a Markdown syntax
$content = preg_replace ( '/<a href="([^"]+)">([^<]+)<\/a>/' , '[\2](\1)' , $content );
//$content = preg_replace('/<a href="([^"]+)" target="([^"]+)">([^<]+)<\/a>/', '[\3](\1){:target="\2"}', $content);
$content = preg_replace ( '/<a href="([^"]+)" target="([^"]+)">([^<]+)<\/a>/' , '[\3](\1)' , $content );
2024-01-13 19:48:20 +01:00
// Replace HTML comments
2023-11-20 12:31:30 +01:00
$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.
2023-04-30 15:13:04 +02:00
2021-02-23 22:03:23 +01:00
if ( is_array ( $replaceimagepath )) {
foreach ( $replaceimagepath as $key => $val ) {
2020-10-31 14:32:18 +01:00
$keytoreplace = '](' . $key ;
$valafter = '](' . $val ;
$content = preg_replace ( '/' . preg_quote ( $keytoreplace , '/' ) . '/m' , $valafter , $content );
}
}
2021-02-23 22:03:23 +01:00
if ( $parser == 'parsedown' ) {
2020-10-31 14:32:18 +01:00
include_once DOL_DOCUMENT_ROOT . '/includes/parsedown/Parsedown.php' ;
2023-04-22 01:01:08 +02:00
$parsedown = new Parsedown ();
$parsedown -> setSafeMode ( true ); // This will escape HTML link <a href=""> into html entities but markdown links are ok
2024-03-24 03:23:07 +01:00
// Because HTML will be HTML entity encoded, we replace tag we want to keep
$content = preg_replace ( '/<span style="([^"]+)">/' , '<!-- SPAN_STYLE_\1 -->' , $content );
$content = preg_replace ( '/<\/span>/' , '<!-- SPAN_END -->' , $content );
2023-04-22 01:01:08 +02:00
$content = $parsedown -> text ( $content );
2024-03-24 03:23:07 +01:00
$content = preg_replace ( '/<!-- SPAN_STYLE_([^-]+) -->/' , '<span style="\1">' , $content );
$content = preg_replace ( '/<!-- SPAN_END -->/' , '</span>' , $content );
2020-10-31 14:32:18 +01:00
} else {
$content = nl2br ( $content );
}
2017-03-21 16:02:54 +01:00
2020-10-31 14:32:18 +01:00
return $content ;
2017-03-21 16:02:54 +01:00
}
2019-03-17 19:33:25 +01:00
/**
* Function to parse MD content into ASCIIDOC
*
* @ param string $content MD content
* @ param string $parser 'dolibarr'
2024-01-13 19:48:20 +01:00
* @ param string $replaceimagepath Replace path to image with another path . Example : ( 'doc/' => 'xxx/aaa/' )
2019-03-17 19:33:25 +01:00
* @ return string Parsed content
*/
function dolMd2Asciidoc ( $content , $parser = 'dolibarr' , $replaceimagepath = null )
{
2021-02-23 22:03:23 +01:00
if ( is_array ( $replaceimagepath )) {
foreach ( $replaceimagepath as $key => $val ) {
2020-10-31 14:32:18 +01:00
$keytoreplace = '](' . $key ;
$valafter = '](' . $val ;
$content = preg_replace ( '/' . preg_quote ( $keytoreplace , '/' ) . '/m' , $valafter , $content );
}
}
//if ($parser == 'dolibarr')
//{
2023-12-04 12:05:28 +01:00
$content = preg_replace ( '/<!--.*-->/msU' , '' , $content );
2020-10-31 14:32:18 +01:00
//}
//else
//{
// $content = $content;
//}
2019-03-17 19:33:25 +01:00
2020-10-31 14:32:18 +01:00
return $content ;
2019-03-18 20:18:38 +01:00
}