MarkDoc/lib/ParsedownExtraPlus.php

255 lines
7.0 KiB
PHP
Raw Permalink Normal View History

<?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;
}
2022-04-15 12:40:52 +02:00
if ($Link['element']['text']!="video")
{
$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']);
}
else
{
$Inline = array(
'extent' => $Link['extent'] + 1,
'element' => array(
'name' => 'video',
'attributes' => array(
'src' => $Link['element']['attributes']['href'],
'id' => 'player',
'controls' => '',
'class' => 'v-vlite video-js-style',
'playsinline' => ''
),
),
);
}
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,
);
}
}