From 10fbe52f2750905276d4e9663cd32ec18bc7a649 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 20 Mar 2016 16:16:55 +0000 Subject: [PATCH] Implement variables. More testing needed. --- build/index.php | 30 ++++++++++++++++++++++++++++-- module_index.json | 2 +- modules/parser-parsedown.php | 30 ++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/build/index.php b/build/index.php index 8272d80..24069ee 100644 --- a/build/index.php +++ b/build/index.php @@ -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" ]); diff --git a/module_index.json b/module_index.json index 292e9e5..abe6e83 100644 --- a/module_index.json +++ b/module_index.json @@ -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 } ] \ No newline at end of file diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php index 048e5fd..4b29032 100644 --- a/modules/parser-parsedown.php +++ b/modules/parser-parsedown.php @@ -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" ]);