Implement templating in parser-parsedown.

Although variables are parsed, they currently aren't used.
This commit is contained in:
Starbeamrainbowlabs 2016-03-20 14:05:55 +00:00
parent 5af09f20dc
commit 96884a5160
4 changed files with 140 additions and 5 deletions

View File

@ -7,7 +7,8 @@
- Added recent changes page under the action `recent-changes`. A link can be found in the "More..." menu by default.
- Changed the cursor when hovering over a time to indicate that the tooltip contains more information.
- Added icons to the "More..." menu
- Help section to parsedown parser.
- Added help section to parsedown parser.
- Added more information to the dev help page.
## Changed
- Improved appearance of the all pages list.

View File

@ -3788,7 +3788,74 @@ class PeppermintParsedown extends ParsedownExtra
// Prioritise our image parser over the regular image parser
array_unshift($this->InlineTypes["!"], "ExtendedImage");
//$this->inlineMarkerList .= "{";
$this->inlineMarkerList .= "{";
if(!is_array($this->InlineTypes["{"]))
$this->InlineTypes["{"] = [];
$this->InlineTypes["{"][] = "Template";
}
protected function inlineTemplate($fragment)
{
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, $paths;
$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
$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++;
}
}
$templateFilePath = $paths->storage_prefix . $pageindex->$templatePagename->filename;
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
return [
"name" => "div",
"text" => $parsedTemplateSource,
"attributes" => [
"class" => "template"
]
];
}
protected function inlineInternalLink($fragment)

View File

@ -185,7 +185,7 @@
"author": "Johnny Broadway & Starbeamrainbowlabs",
"description": "The *old* default parser for Pepperminty Wiki. Based on Johnny Broadway's Slimdown (with more than a few modifications). This parser's features are documented in the help page. Superceded by a customised extension of parsedown extra.",
"id": "parser-default-old",
"lastupdate": false,
"lastupdate": 1457796247,
"optional": true
},
{
@ -194,7 +194,7 @@
"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",
"lastupdate": 1458411754,
"lastupdate": 1458482496,
"optional": false
}
]

View File

@ -70,7 +70,74 @@ class PeppermintParsedown extends ParsedownExtra
// Prioritise our image parser over the regular image parser
array_unshift($this->InlineTypes["!"], "ExtendedImage");
//$this->inlineMarkerList .= "{";
$this->inlineMarkerList .= "{";
if(!is_array($this->InlineTypes["{"]))
$this->InlineTypes["{"] = [];
$this->InlineTypes["{"][] = "Template";
}
protected function inlineTemplate($fragment)
{
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, $paths;
$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
$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++;
}
}
$templateFilePath = $paths->storage_prefix . $pageindex->$templatePagename->filename;
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
return [
"name" => "div",
"text" => $parsedTemplateSource,
"attributes" => [
"class" => "template"
]
];
}
protected function inlineInternalLink($fragment)