84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
<?php
|
|
|
|
class ParsedownHelper extends \Prefab {
|
|
|
|
static public function render($args)
|
|
{
|
|
|
|
# inline <parsedown inline="true">#markdown here</parsedown>
|
|
if(isset($args['@attrib']) && isset($args['@attrib']['inline']) &&
|
|
$args['@attrib']['inline'] === 'true'){
|
|
|
|
return self::instance()->inline($args);
|
|
|
|
}
|
|
|
|
# href <parsedown href="filename"></parsedown>
|
|
if(isset($args['@attrib']) && isset($args['@attrib']['href']))
|
|
{
|
|
return self::instance()->load_href($args);
|
|
}
|
|
|
|
|
|
# token <parsedown>{@variable}</parsedown>
|
|
$content = $args[0];
|
|
$content_token = \Template::instance()->token($content);
|
|
|
|
return '
|
|
<parsedown_rendered class="content">
|
|
<?php echo \ParsedownHelper::instance()->build('.$content_token.'); ?>
|
|
</parsedown_rendered>';
|
|
}
|
|
|
|
function build($content){
|
|
return \ParsedownTableExtension::instance()->text($content);
|
|
}
|
|
|
|
|
|
static private function inline($args){
|
|
$return = \Parsedown::instance()->text($args[0]);
|
|
|
|
return '<!-- tableextension -->
|
|
<div class="content">
|
|
<parsedown_rendered>'.$return.'</parsedown_rendered>
|
|
</div>
|
|
';
|
|
}
|
|
|
|
static private function load_href($args){
|
|
$href= $args['@attrib']['href'] ?? '';
|
|
if(!$href){
|
|
return '';
|
|
}
|
|
|
|
$ui = Base::instance()->get('UI');
|
|
$dirs = preg_split('#[;]+#', $ui, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
// look for the file in each UI dir
|
|
$file = '';
|
|
foreach ($dirs as $dir) {
|
|
// normalize trailing slash
|
|
$base = rtrim($dir, '/').'/';
|
|
// resolve relative paths
|
|
$candidate = realpath($base . $href) ?: $base . $href;
|
|
// print_r("<p>".$candidate . "</p>");
|
|
if (is_readable($candidate)) {
|
|
$file = $candidate;
|
|
break;
|
|
}
|
|
}
|
|
if(!$file) {
|
|
return "<p><em>File not found: {$href}</em></p>";
|
|
}
|
|
|
|
$text = file_get_contents($file);
|
|
$md = \Parsedown::instance()->text($text);
|
|
|
|
return '
|
|
<parsedown_rendered class="content">
|
|
'.$md.'
|
|
</parsedown_rendered>';
|
|
}
|
|
}
|
|
|
|
\Template::instance()->extend('parsedown', 'ParsedownHelper::render'); |