1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-12-22 13:45:02 +00:00

Implement variables. More testing needed.

This commit is contained in:
Starbeamrainbowlabs 2016-03-20 16:16:55 +00:00
parent 96884a5160
commit 10fbe52f27
3 changed files with 57 additions and 5 deletions

View file

@ -3781,6 +3781,9 @@ class PeppermintParsedown extends ParsedownExtra
{
private $internalLinkBase = "./%s";
protected $maxParamDepth = 0;
protected $paramStack = [];
function __construct()
{
// Prioritise our internal link parsing over the regular link parsing
@ -3796,7 +3799,19 @@ class PeppermintParsedown extends ParsedownExtra
protected function inlineTemplate($fragment)
{
if(preg_match("/\{\{([^}]+)\}\}/", $fragment["text"], $matches))
// Variable parsing
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
{
$variableKey = trim($matches[1]);
if(isset(array_slice($this->paramStack, -1)[0][$variableKey]))
{
return [
"extent" => strlen($matches[0]),
"markup" => array_slice($this->paramStack, -1)[0][$variableKey]
];
}
}
else if(preg_match("/\{\{([^}]+)\}\}/", $fragment["text"], $matches))
{
$templateElement = $this->templateHandler($matches[1]);
@ -3814,6 +3829,7 @@ class PeppermintParsedown extends ParsedownExtra
{
global $pageindex, $paths;
$parts = explode("|", trim($source, "{}"));
$parts = array_map(trim, $parts);
@ -3825,6 +3841,7 @@ class PeppermintParsedown extends ParsedownExtra
return false;
// Parse the parameters
$this->maxParamDepth++;
$params = [];
$i = 0;
foreach($parts as $part)
@ -3839,16 +3856,21 @@ class PeppermintParsedown extends ParsedownExtra
else
{
// This isn't a named parameter
$params[$i] = trim($part);
$params["$i"] = trim($part);
$i++;
}
}
// Add the parsed parameters to the parameter stack
$this->paramStack[] = $params;
$templateFilePath = $paths->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,
@ -3952,6 +3974,10 @@ class PeppermintParsedown extends ParsedownExtra
}
}
# ~
# Utility Methods
# ~
private function isFloatValue($value)
{
return in_array(strtolower($value), [ "left", "right" ]);

View file

@ -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": 1458482496,
"lastupdate": 1458490188,
"optional": false
}
]

View file

@ -63,6 +63,9 @@ class PeppermintParsedown extends ParsedownExtra
{
private $internalLinkBase = "./%s";
protected $maxParamDepth = 0;
protected $paramStack = [];
function __construct()
{
// Prioritise our internal link parsing over the regular link parsing
@ -78,7 +81,19 @@ class PeppermintParsedown extends ParsedownExtra
protected function inlineTemplate($fragment)
{
if(preg_match("/\{\{([^}]+)\}\}/", $fragment["text"], $matches))
// Variable parsing
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
{
$variableKey = trim($matches[1]);
if(isset(array_slice($this->paramStack, -1)[0][$variableKey]))
{
return [
"extent" => strlen($matches[0]),
"markup" => array_slice($this->paramStack, -1)[0][$variableKey]
];
}
}
else if(preg_match("/\{\{([^}]+)\}\}/", $fragment["text"], $matches))
{
$templateElement = $this->templateHandler($matches[1]);
@ -96,6 +111,7 @@ class PeppermintParsedown extends ParsedownExtra
{
global $pageindex, $paths;
$parts = explode("|", trim($source, "{}"));
$parts = array_map(trim, $parts);
@ -107,6 +123,7 @@ class PeppermintParsedown extends ParsedownExtra
return false;
// Parse the parameters
$this->maxParamDepth++;
$params = [];
$i = 0;
foreach($parts as $part)
@ -121,16 +138,21 @@ class PeppermintParsedown extends ParsedownExtra
else
{
// This isn't a named parameter
$params[$i] = trim($part);
$params["$i"] = trim($part);
$i++;
}
}
// Add the parsed parameters to the parameter stack
$this->paramStack[] = $params;
$templateFilePath = $paths->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,
@ -234,6 +256,10 @@ class PeppermintParsedown extends ParsedownExtra
}
}
# ~
# Utility Methods
# ~
private function isFloatValue($value)
{
return in_array(strtolower($value), [ "left", "right" ]);