feat: separate new parsedown function in new class

This commit is contained in:
Nicolas Hordé 2021-01-01 13:41:31 +01:00
parent 0fec1fd9e3
commit f07a0b2f9b
7 changed files with 250 additions and 41 deletions

View File

@ -1,4 +1,7 @@
PHP MarkDown document manager, Free & OpenSource :heart_eyes: for easily create your documentation website** # MarkDoc
**PHP MarkDown document manager, Free & OpenSource :heart_eyes: for easily create your documentation website**
``` ```
__ __ _ _____ __ __ _ _____
| \/ | | | | __ \ | \/ | | | | __ \

View File

@ -21,6 +21,7 @@ define('LOG_DIR', HOST_DIR.LOG_FILE);
define('HISTORY_DIR', HOST_DIR.HISTORY_FILE); define('HISTORY_DIR', HOST_DIR.HISTORY_FILE);
include LIB_DIR."/Parsedown.php"; include LIB_DIR."/Parsedown.php";
include LIB_DIR."/ParsedownExtra.php"; include LIB_DIR."/ParsedownExtra.php";
include LIB_DIR."/ParsedownExtraPlus.php";
include LIB_DIR."/functions.php"; include LIB_DIR."/functions.php";
### Security ### Security

View File

@ -144,8 +144,10 @@ $(function(){
$("#save").click(function(e){ $("#save").click(function(e){
e.preventDefault(); e.preventDefault();
file="/"+$("#files").jstree("get_path",nodes.node,"/").replace(/^.+?[/]/, ''); viewfile="";
if ($("#files").jstree("is_leaf",nodes.node)) node=$("#files").jstree("get_selected");
file="/"+$("#files").jstree("get_path",node,"/").replace(/^.+?[/]/, '');
if ($("#files").jstree("is_leaf",node))
{ {
data = editor.value(); data = editor.value();
$.ajax({ $.ajax({

View File

@ -1081,11 +1081,6 @@ class Parsedown
continue; continue;
} }
if (isset($Inline['addon']))
{
$markup .= $Inline['addon'];
}
# makes sure that the inline belongs to "our" marker # makes sure that the inline belongs to "our" marker
if (isset($Inline['position']) and $Inline['position'] > $markerPosition) if (isset($Inline['position']) and $Inline['position'] > $markerPosition)
@ -1114,7 +1109,6 @@ class Parsedown
$markup .= $this->unmarkedText($unmarkedText); $markup .= $this->unmarkedText($unmarkedText);
# compile the inline # compile the inline
if (isset($Inline['element']))
$markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']); $markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
# remove the examined text # remove the examined text
@ -1254,19 +1248,6 @@ class Parsedown
), ),
); );
$modifier = explode('?', $Inline['element']['attributes']['src'], 2)[1] ?? "";
list($size,$align) = explode('-', $modifier, 2);
$width = $height = 0;
if (preg_match('/^\d+x\d+$/', $size)) {
list($width, $height) = explode('x', $size);
}
if (preg_match('/^\d+$/', $size)) {
$width = $size;
}
if($width > 0) $Inline['element']['attributes']['width'] = $width;
if($height > 0) $Inline['element']['attributes']['height'] = $height;
if($align !="" ) $Inline['element']['attributes']['align'] = $align;
$Inline['element']['attributes'] += $Link['element']['attributes']; $Inline['element']['attributes'] += $Link['element']['attributes'];
unset($Inline['element']['attributes']['href']); unset($Inline['element']['attributes']['href']);
@ -1339,20 +1320,6 @@ class Parsedown
$Element['attributes']['href'] = $Definition['url']; $Element['attributes']['href'] = $Definition['url'];
$Element['attributes']['title'] = $Definition['title']; $Element['attributes']['title'] = $Definition['title'];
} }
if ($Element['attributes']['href']=="tags")
{
$addon='<div class="flexalign flexwrap">';
$items=explode(" ",$Element['text']);
foreach($items as $item)
{
$addon.='<span class="flexalign contained particularite"><a href="javascript:search(\''.$item.'\')">'.$item.'</a></span>';
}
$addon.='</div>';
return array(
'addon' => $addon,
'extent' => $extent
);
}
return array( return array(
'extent' => $extent, 'extent' => $extent,

236
lib/ParsedownExtraPlus.php Normal file
View File

@ -0,0 +1,236 @@
<?php
/*
* MarkDoc
* PHP MarkDown document manager
* Hordé Nicolas
* https://github.com/dahut87/MarkDoc
* Release under GPLv3.0 license
* Base on Pheditor "PHP file editor" By Hamid Samak Release under MIT license
*/
class ParsedownExtraplus extends ParsedownExtra
{
public function line($text, $nonNestables=array())
{
$markup = '';
# $excerpt is based on the first occurrence of a marker
while ($excerpt = strpbrk($text, $this->inlineMarkerList))
{
$marker = $excerpt[0];
$markerPosition = strpos($text, $marker);
$Excerpt = array('text' => $excerpt, 'context' => $text);
foreach ($this->InlineTypes[$marker] as $inlineType)
{
# check to see if the current inline type is nestable in the current context
if ( ! empty($nonNestables) and in_array($inlineType, $nonNestables))
{
continue;
}
$Inline = $this->{'inline'.$inlineType}($Excerpt);
if ( ! isset($Inline))
{
continue;
}
if (isset($Inline['addon']))
{
$markup .= $Inline['addon'];
}
# makes sure that the inline belongs to "our" marker
if (isset($Inline['position']) and $Inline['position'] > $markerPosition)
{
continue;
}
# sets a default inline position
if ( ! isset($Inline['position']))
{
$Inline['position'] = $markerPosition;
}
# cause the new element to 'inherit' our non nestables
foreach ($nonNestables as $non_nestable)
{
$Inline['element']['nonNestables'][] = $non_nestable;
}
# the text that comes before the inline
$unmarkedText = substr($text, 0, $Inline['position']);
# compile the unmarked text
$markup .= $this->unmarkedText($unmarkedText);
# compile the inline
if (isset($Inline['element']))
$markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
# remove the examined text
$text = substr($text, $Inline['position'] + $Inline['extent']);
continue 2;
}
# the marker does not belong to an inline
$unmarkedText = substr($text, 0, $markerPosition + 1);
$markup .= $this->unmarkedText($unmarkedText);
$text = substr($text, $markerPosition + 1);
}
$markup .= $this->unmarkedText($text);
return $markup;
}
protected function inlineImage($Excerpt)
{
if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
{
return;
}
$Excerpt['text']= substr($Excerpt['text'], 1);
$Link = $this->inlineLink($Excerpt);
if ($Link === null)
{
return;
}
$Inline = array(
'extent' => $Link['extent'] + 1,
'element' => array(
'name' => 'img',
'attributes' => array(
'src' => $Link['element']['attributes']['href'],
'alt' => $Link['element']['text'],
),
),
);
$modifier = explode('?', $Inline['element']['attributes']['src'], 2)[1] ?? "";
list($size,$align) = explode('-', $modifier, 2);
$width = $height = 0;
if (preg_match('/^\d+x\d+$/', $size)) {
list($width, $height) = explode('x', $size);
}
if (preg_match('/^\d+$/', $size)) {
$width = $size;
}
if($width > 0) $Inline['element']['attributes']['width'] = $width;
if($height > 0) $Inline['element']['attributes']['height'] = $height;
if($align !="" ) $Inline['element']['attributes']['align'] = $align;
$Inline['element']['attributes'] += $Link['element']['attributes'];
unset($Inline['element']['attributes']['href']);
return $Inline;
}
protected function inlineLink($Excerpt)
{
$Element = array(
'name' => 'a',
'handler' => 'line',
'nonNestables' => array('Url', 'Link'),
'text' => null,
'attributes' => array(
'href' => null,
'title' => null,
),
);
$extent = 0;
$remainder = $Excerpt['text'];
if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
{
$Element['text'] = $matches[1];
$extent += strlen($matches[0]);
$remainder = substr($remainder, $extent);
}
else
{
return;
}
if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches))
{
$Element['attributes']['href'] = $matches[1];
if (isset($matches[2]))
{
$Element['attributes']['title'] = substr($matches[2], 1, - 1);
}
$extent += strlen($matches[0]);
}
else
{
if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
{
$definition = strlen($matches[1]) ? $matches[1] : $Element['text'];
$definition = strtolower($definition);
$extent += strlen($matches[0]);
}
else
{
$definition = strtolower($Element['text']);
}
if ( ! isset($this->DefinitionData['Reference'][$definition]))
{
return;
}
$Definition = $this->DefinitionData['Reference'][$definition];
$Element['attributes']['href'] = $Definition['url'];
$Element['attributes']['title'] = $Definition['title'];
}
if ($Element['attributes']['href']=="tags")
{
$addon='<div class="flexalign flexwrap">';
$items=explode(" ",$Element['text']);
foreach($items as $item)
{
$addon.='<span class="flexalign contained particularite"><a href="javascript:search(\''.$item.'\')">'.$item.'</a></span>';
}
$addon.='</div>';
return array(
'addon' => $addon,
'extent' => $extent
);
}
return array(
'extent' => $extent,
'element' => $Element,
);
}
}

View File

@ -69,7 +69,7 @@ function specialurl($url)
Based on Pheditor "PHP file editor" By Hamid Samak Release under MIT license Based on Pheditor "PHP file editor" By Hamid Samak Release under MIT license
2020 par Nicolas H.'; 2020 par Nicolas H.';
$extra = new ParsedownExtra(); $extra = new ParsedownExtraplus();
print($extra->text($content)); print($extra->text($content));
exit; exit;
case ':ADMIN': case ':ADMIN':
@ -239,7 +239,7 @@ function getcontent($url,$md=true)
} }
if ($md==true) if ($md==true)
{ {
$extra = new ParsedownExtra(); $extra = new ParsedownExtraplus();
return $extra->text($content); return $extra->text($content);
} }
else else

View File

@ -1,6 +1,6 @@
# MarkDoc # MarkDoc
PHP MarkDown document manager, Free &amp; OpenSource :heart_eyes: for easily create your documentation website** **PHP MarkDown document manager, Free &amp; OpenSource :heart_eyes: for easily create your documentation website**
``` ```
__ __ _ _____ __ __ _ _____
| \/ | | | | __ \ | \/ | | | | __ \