1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-22 04:23:01 +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"; private $internalLinkBase = "./%s";
protected $maxParamDepth = 0;
protected $paramStack = [];
function __construct() function __construct()
{ {
// Prioritise our internal link parsing over the regular link parsing // Prioritise our internal link parsing over the regular link parsing
@ -3796,7 +3799,19 @@ class PeppermintParsedown extends ParsedownExtra
protected function inlineTemplate($fragment) 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]); $templateElement = $this->templateHandler($matches[1]);
@ -3814,6 +3829,7 @@ class PeppermintParsedown extends ParsedownExtra
{ {
global $pageindex, $paths; global $pageindex, $paths;
$parts = explode("|", trim($source, "{}")); $parts = explode("|", trim($source, "{}"));
$parts = array_map(trim, $parts); $parts = array_map(trim, $parts);
@ -3825,6 +3841,7 @@ class PeppermintParsedown extends ParsedownExtra
return false; return false;
// Parse the parameters // Parse the parameters
$this->maxParamDepth++;
$params = []; $params = [];
$i = 0; $i = 0;
foreach($parts as $part) foreach($parts as $part)
@ -3839,16 +3856,21 @@ class PeppermintParsedown extends ParsedownExtra
else else
{ {
// This isn't a named parameter // This isn't a named parameter
$params[$i] = trim($part); $params["$i"] = trim($part);
$i++; $i++;
} }
} }
// Add the parsed parameters to the parameter stack
$this->paramStack[] = $params;
$templateFilePath = $paths->storage_prefix . $pageindex->$templatePagename->filename; $templateFilePath = $paths->storage_prefix . $pageindex->$templatePagename->filename;
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath)); $parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
// Remove the parsed parameters from the stack
array_pop($this->paramStack);
return [ return [
"name" => "div", "name" => "div",
"text" => $parsedTemplateSource, "text" => $parsedTemplateSource,
@ -3952,6 +3974,10 @@ class PeppermintParsedown extends ParsedownExtra
} }
} }
# ~
# Utility Methods
# ~
private function isFloatValue($value) private function isFloatValue($value)
{ {
return in_array(strtolower($value), [ "left", "right" ]); return in_array(strtolower($value), [ "left", "right" ]);

View file

@ -194,7 +194,7 @@
"author": "Emanuil Rusev & Starbeamrainbowlabs", "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.", "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", "id": "parser-parsedown",
"lastupdate": 1458482496, "lastupdate": 1458490188,
"optional": false "optional": false
} }
] ]

View file

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