"Parsedown",
"version" => "0.6.1",
"author" => "Emanuil Rusev & Starbeamrainbowlabs",
"description" => "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https://github.com/erusev/parsedown-extra), which is licensed MIT. Please be careful, as this module adds a some weight to your installation, and also *requires* write access to the disk on first load.",
"id" => "parser-parsedown",
"code" => function() {
global $settings;
$parser = new PeppermintParsedown();
$parser->setInternalLinkBase("?page=%s");
add_parser("parsedown", function($source) use ($parser) {
$result = $parser->text($source);
return $result;
});
add_help_section("20-parser-default", "Editor Syntax",
"
$settings->sitename's editor uses an extended version of Parsedown to render pages, which is a fantastic open source Github flavoured markdown parser. You can find a quick reference guide on Github flavoured markdown here by adam-p, or if you prefer a book Mastering Markdown by KB is a good read, and free too!
Extra Syntax
$settings->sitename's editor also supports some extra custom syntax, some of which is inspired by Mediawiki.
Type this | To get this | Comments |
---|
[[Internal link]] | Internal Link | An internal link. |
[[Display Text|Internal link]] | Display Text | An internal link with some display text. |
![Alt text](http://example.com/path/to/image.png | 256x256 | right) | | An image floating to the right of the page that fits inside a 256px x 256px box, preserving aspect ratio. |
Templating
$settings->sitename also supports including one page in another page as a template. The syntax is very similar to that of Mediawiki. For example, {{Announcement banner}}
will include the contents of the \"Announcement banner\" page, assuming it exists.
You can also use variables. Again, the syntax here is very similar to that of Mediawiki - they can be referenced in the included page by surrrounding the variable name in triple curly braces (e.g. {{{Announcement text}}}
), and set when including a page with the bar syntax (e.g. {{Announcement banner | importance = high | text = Maintenance has been planned for tonight.}}
). Currently the only restriction in templates and variables is that you may not include a closing curly brace (}
) in the page name, variable name, or value.
");
}
]);
/*** Parsedown versions ***
* Parsedown Core: 1.6.0 *
* Parsedown Extra: 0.7.0 *
**************************/
$env->parsedown_paths = new stdClass();
$env->parsedown_paths->parsedown = "https://cdn.rawgit.com/erusev/parsedown/3ebbd730b5c2cf5ce78bc1bf64071407fc6674b7/Parsedown.php";
$env->parsedown_paths->parsedown_extra = "https://cdn.rawgit.com/erusev/parsedown-extra/11a44e076d02ffcc4021713398a60cd73f78b6f5/ParsedownExtra.php";
// Download parsedown and parsedown extra if they don't already exist
if(!file_exists("./Parsedown.php") || filesize("./Parsedown.php") === 0)
file_put_contents("./Parsedown.php", fopen($env->parsedown_paths->parsedown, "r"));
if(!file_exists("./ParsedownExtra.php") || filesize("./ParsedownExtra.php") === 0)
file_put_contents("./ParsedownExtra.php", fopen($env->parsedown_paths->parsedown_extra, "r"));
require_once("./Parsedown.php");
require_once("./ParsedownExtra.php");
/*
* ██████ █████ ██████ ███████ ███████ ██████ ██████ ██ ██ ███ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
* ██████ ███████ ██████ ███████ █████ ██ ██ ██ ██ ██ █ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ███████ ███████ ██████ ██████ ███ ███ ██ ████
*
* ███████ ██ ██ ████████ ███████ ███ ██ ███████ ██ ██████ ███ ██ ███████
* ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ████ ██ ██
* █████ ███ ██ █████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ███████ ██ ████ ███████ ██ ██████ ██ ████ ███████
*/
class PeppermintParsedown extends ParsedownExtra
{
private $internalLinkBase = "./%s";
protected $maxParamDepth = 0;
protected $paramStack = [];
function __construct()
{
// Prioritise our internal link parsing over the regular link parsing
array_unshift($this->InlineTypes["["], "InternalLink");
// Prioritise our image parser over the regular image parser
array_unshift($this->InlineTypes["!"], "ExtendedImage");
$this->inlineMarkerList .= "{";
if(!isset($this->InlineTypes["{"]) or !is_array($this->InlineTypes["{"]))
$this->InlineTypes["{"] = [];
$this->InlineTypes["{"][] = "Template";
}
protected function inlineTemplate($fragment)
{
global $env;
// Variable parsing
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
{
$params = [];
if(!empty($this->paramStack))
{
$stackEntry = array_slice($this->paramStack, -1)[0];
$params = !empty($stackEntry) ? $stackEntry["params"] : false;
}
$variableKey = trim($matches[1]);
$variableValue = false;
switch ($variableKey)
{
case "@":
if(!empty($params))
{
$variableValue = "
Key | Value |
\n";
foreach($params as $key => $value)
{
$variableValue .= "\t" . $this->escapeText($key) . " | " . $this->escapeText($value) . " |
\n";
}
$variableValue .= "
";
}
break;
case "#":
$variableValue = "\n";
$variableValue .= "\t- $env->page
\n";
foreach($this->paramStack as $curStackEntry)
{
$variableValue .= "\t- " . $curStackEntry["pagename"] . "
\n";
}
$variableValue .= "
\n";
break;
case "~":
if(!empty($this->paramStack))
$variableValue = $this->escapeText($env->page);
// TODO: Add a option that displays a list of subpages here
}
if(isset($params[$variableKey]))
{
$variableValue = $params[$variableKey];
$variableValue = $this->escapeText($variableValue);
}
if($variableValue)
{
return [
"extent" => strlen($matches[0]),
"markup" => $variableValue
];
}
}
else if(preg_match("/\{\{([^}]+)\}\}/", $fragment["text"], $matches))
{
$templateElement = $this->templateHandler($matches[1]);
if(!empty($templateElement))
{
return [
"extent" => strlen($matches[0]),
"element" => $templateElement
];
}
}
}
protected function templateHandler($source)
{
global $pageindex, $env;
$parts = explode("|", trim($source, "{}"));
$parts = array_map("trim", $parts);
// Extract the name of the temaplate page
$templatePagename = array_shift($parts);
// If the page that we are supposed to use as the tempalte doesn't
// exist, then there's no point in continuing.
if(empty($pageindex->$templatePagename))
return false;
// Parse the parameters
$this->maxParamDepth++;
$params = [];
$i = 0;
foreach($parts as $part)
{
if(strpos($part, "=") !== false)
{
// This param contains an equals sign, so it's a named parameter
$keyValuePair = explode("=", $part, 2);
$keyValuePair = array_map("trim", $keyValuePair);
$params[$keyValuePair[0]] = $keyValuePair[1];
}
else
{
// This isn't a named parameter
$params["$i"] = trim($part);
$i++;
}
}
// Add the parsed parameters to the parameter stack
$this->paramStack[] = [
"pagename" => $templatePagename,
"params" => $params
];
$templateFilePath = $env->storage_prefix . $pageindex->$templatePagename->filename;
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
// Remove the parsed parameters from the stack
array_pop($this->paramStack);
return [
"name" => "div",
"text" => $parsedTemplateSource,
"attributes" => [
"class" => "template"
]
];
}
protected function inlineInternalLink($fragment)
{
global $pageindex;
if(preg_match('/^\[\[([^\]]*)\]\]/', $fragment["text"], $matches))
{
$display = $linkPage = $matches[1];
if(strpos($matches[1], "|"))
{
// We have a bar character
$parts = explode("|", $matches[1], 2);
$linkPage = $parts[0];
$display = $parts[1];
}
// Construct the full url
$linkUrl = str_replace(
"%s", rawurlencode($linkPage),
$this->internalLinkBase
);
$result = [
"extent" => strlen($matches[0]),
"element" => [
"name" => "a",
"text" => $display,
"attributes" => [
"href" => $linkUrl
]
]
];
if(empty($pageindex->$linkPage))
$result["element"]["attributes"]["class"] = "redlink";
return $result;
}
return;
}
protected function inlineExtendedImage($fragment)
{
if(preg_match('/^!\[(.*)\]\(([^ |)]+)\s*(?:\|([^|)]*)(?:\|([^)]*))?)?\)/', $fragment["text"], $matches))
{
/*
* 0 - Everything
* 1 - Alt text
* 2 - Url
* 3 - First param
* 4 - Second Param (optional)
*/
$altText = $matches[1];
$imageUrl = str_replace("&", "&", $matches[2]); // Decode & to allow it in preview urls
$param1 = !empty($matches[3]) ? strtolower(trim($matches[3])) : false;
$param2 = empty($matches[4]) ? false : strtolower(trim($matches[4]));
$floatDirection = false;
$imageSize = false;
if($this->isFloatValue($param1))
{
// Param 1 is a valid css float: ... value
$floatDirection = $param1;
$imageSize = $this->parseSizeSpec($param2);
}
else if($this->isFloatValue($param2))
{
// Param 2 is a valid css float: ... value
$floatDirection = $param2;
$imageSize = $this->parseSizeSpec($param1);
}
else if($param1 === false and $param2 === false)
{
// Neither params were specified
$floatDirection = false;
$imageSize = false;
}
else
{
// Neither of them are floats, but at least one is specified
// This must mean that the first param is a size spec like
// 250x128.
$imageSize = $this->parseSizeSpec($param1);
}
$style = "";
if($imageSize !== false)
$style .= " max-width: " . $imageSize["x"] . "px; max-height: " . $imageSize["y"] . "px;";
if($floatDirection)
$style .= " float: $floatDirection;";
$urlExtension = pathinfo($imageUrl, PATHINFO_EXTENSION);
$urlType = system_extension_mime_type($urlExtension);
switch(substr($urlType, 0, strpos($urlType, "/")))
{
case "audio":
return [
"extent" => strlen($matches[0]),
"element" => [
"name" => "audio",
"text" => $altText,
"attributes" => [
"src" => $imageUrl,
"controls" => "controls",
"preload" => "metadata",
"style" => trim($style)
]
]
];
case "video":
return [
"extent" => strlen($matches[0]),
"element" => [
"name" => "video",
"text" => $altText,
"attributes" => [
"src" => $imageUrl,
"controls" => "controls",
"preload" => "metadata",
"style" => trim($style)
]
]
];
case "image":
default:
// If we can't work out what it is, then assume it's an image
return [
"extent" => strlen($matches[0]),
"element" => [
"name" => "img",
"attributes" => [
"src" => $imageUrl,
"alt" => $altText,
"style" => trim($style)
]
]
];
}
}
}
# ~
# Utility Methods
# ~
private function isFloatValue($value)
{
return in_array(strtolower($value), [ "left", "right" ]);
}
private function parseSizeSpec($text)
{
if(strpos($text, "x") === false)
return false;
$parts = explode("x", $text, 2);
if(count($parts) != 2)
return false;
array_map("trim", $parts);
array_map("intval", $parts);
if(in_array(0, $parts))
return false;
return [
"x" => $parts[0],
"y" => $parts[1]
];
}
protected function escapeText($text)
{
return htmlentities($text, ENT_COMPAT | ENT_HTML5);
}
/**
* Sets the base url to be used for internal links. '%s' will be replaced
* with a URL encoded version of the page name.
* @param string $url The url to use when parsing internal links.
*/
public function setInternalLinkBase($url)
{
$this->internalLinkBase = $url;
}
}
?>