From 96884a516021293b9b7adb04098e964b2467c019 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 20 Mar 2016 14:05:55 +0000
Subject: [PATCH] Implement templating in parser-parsedown.

Although variables are parsed, they currently aren't used.
---
 Changelog.md                 |  3 +-
 build/index.php              | 69 +++++++++++++++++++++++++++++++++++-
 module_index.json            |  4 +--
 modules/parser-parsedown.php | 69 +++++++++++++++++++++++++++++++++++-
 4 files changed, 140 insertions(+), 5 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 9e131d8..80816ef 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -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.
diff --git a/build/index.php b/build/index.php
index eee411a..8272d80 100644
--- a/build/index.php
+++ b/build/index.php
@@ -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)
diff --git a/module_index.json b/module_index.json
index 96707d4..292e9e5 100644
--- a/module_index.json
+++ b/module_index.json
@@ -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
     }
 ]
\ No newline at end of file
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index e5efa31..048e5fd 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -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)
