From 999c1abafd24b355ae1916d7fe91d795b5294968 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 9 Apr 2016 07:51:29 +0100
Subject: [PATCH] Add parser variable to list subpages

---
 Changelog.md                 |  6 ++++--
 build/index.php              | 30 ++++++++++++++++++++++++------
 module_index.json            |  2 +-
 modules/parser-parsedown.php | 30 ++++++++++++++++++++++++------
 4 files changed, 53 insertions(+), 15 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 24dc026..0abc018 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,7 +1,7 @@
 # Changelog
 # v0.11-dev
 # Added
- - Unlocked the uploading of any file type. Note that only the file types specified in the settings are allowed to be uploaded.7
+ - Unlocked the uploading of any file type. Note that only the file types specified in the settings are allowed to be uploaded.
 	- Uploaded video and audio files can now be viewed on their respective pages
     - The file preview tool is now aware that not everything will be an image.
  - Enhanced the recent changes page.
@@ -10,7 +10,9 @@
 	- Uploads show with an arrow next to them along with the size of the uploaded file
  - Added mathematical expression parsing between dollar signs.
  - Generated previews now have etags. This should speed up load times of subsequent requests significantly.
- - Added an extra debug variable that displays the top level page name (i.e. the page that has been requested). It can be used like this: `{{{~}}}`
+ - Added some extra built-in variables to the parser.
+    - `{{{~}}}`: Displays the top level page name (i.e. the page that has been requested).
+    - `{{{*}}}`: Displays a comma-separated list of subpages of the requested page.
  - Links to non-existent pages are now coloured red by default.
 
 # Changed
diff --git a/build/index.php b/build/index.php
index c7c3d58..bfe3eb3 100644
--- a/build/index.php
+++ b/build/index.php
@@ -4234,9 +4234,16 @@ class PeppermintParsedown extends ParsedownExtra
 		$this->InlineTypes["{"][] = "Template";
 	}
 	
+	/*
+	 * ████████ ███████ ███    ███ ██████  ██       █████  ████████ ██ ███    ██  ██████
+	 *    ██    ██      ████  ████ ██   ██ ██      ██   ██    ██    ██ ████   ██ ██
+	 *    ██    █████   ██ ████ ██ ██████  ██      ███████    ██    ██ ██ ██  ██ ██   ███
+	 *    ██    ██      ██  ██  ██ ██      ██      ██   ██    ██    ██ ██  ██ ██ ██    ██
+	 *    ██    ███████ ██      ██ ██      ███████ ██   ██    ██    ██ ██   ████  ██████
+	 */
 	protected function inlineTemplate($fragment)
 	{
-		global $env;
+		global $env, $pageindex;
 		
 		// Variable parsing
 		if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
@@ -4253,7 +4260,7 @@ class PeppermintParsedown extends ParsedownExtra
 			$variableValue = false;
 			switch ($variableKey)
 			{
-				case "@":
+				case "@": // Lists all subpages
 					if(!empty($params))
 					{
 						$variableValue = "<table>
@@ -4265,7 +4272,7 @@ class PeppermintParsedown extends ParsedownExtra
 						$variableValue .= "</table>";
 					}
 					break;
-				case "#":
+				case "#": // Shows a stack trace
 					$variableValue = "<ol start=\"0\">\n";
 					$variableValue .= "\t<li>$env->page</li>\n";
 					foreach($this->paramStack as $curStackEntry)
@@ -4274,10 +4281,21 @@ class PeppermintParsedown extends ParsedownExtra
 					}
 					$variableValue .= "</ol>\n";
 					break;
-				case "~":
+				case "~": // Show requested page's name
 					if(!empty($this->paramStack))
 						$variableValue = $this->escapeText($env->page);
-				// TODO: Add a option that displays a list of subpages here
+					break;
+				case "*": // Lists subpages
+					$subpages = get_subpages($pageindex, $env->page);
+					$variableValue = [];
+					foreach($subpages as $pagename => $depth)
+					{
+						$variableValue[] = $pagename;
+					}
+					$variableValue = implode(", ", $variableValue);
+					if(strlen($variableValue) === 0)
+						$variableValue = "<em>(none yet!)</em>";
+					break;
 			}
 			if(isset($params[$variableKey]))
 			{
@@ -4285,7 +4303,7 @@ class PeppermintParsedown extends ParsedownExtra
 				$variableValue = $this->escapeText($variableValue);
 			}
 			
-			if($variableValue)
+			if($variableValue !== false)
 			{
 				return [
 					"extent" => strlen($matches[0]),
diff --git a/module_index.json b/module_index.json
index 414b7db..9941aae 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": 1460145045,
+        "lastupdate": 1460184547,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index d09118b..d7b6a58 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -87,9 +87,16 @@ class PeppermintParsedown extends ParsedownExtra
 		$this->InlineTypes["{"][] = "Template";
 	}
 	
+	/*
+	 * ████████ ███████ ███    ███ ██████  ██       █████  ████████ ██ ███    ██  ██████
+	 *    ██    ██      ████  ████ ██   ██ ██      ██   ██    ██    ██ ████   ██ ██
+	 *    ██    █████   ██ ████ ██ ██████  ██      ███████    ██    ██ ██ ██  ██ ██   ███
+	 *    ██    ██      ██  ██  ██ ██      ██      ██   ██    ██    ██ ██  ██ ██ ██    ██
+	 *    ██    ███████ ██      ██ ██      ███████ ██   ██    ██    ██ ██   ████  ██████
+	 */
 	protected function inlineTemplate($fragment)
 	{
-		global $env;
+		global $env, $pageindex;
 		
 		// Variable parsing
 		if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
@@ -106,7 +113,7 @@ class PeppermintParsedown extends ParsedownExtra
 			$variableValue = false;
 			switch ($variableKey)
 			{
-				case "@":
+				case "@": // Lists all subpages
 					if(!empty($params))
 					{
 						$variableValue = "<table>
@@ -118,7 +125,7 @@ class PeppermintParsedown extends ParsedownExtra
 						$variableValue .= "</table>";
 					}
 					break;
-				case "#":
+				case "#": // Shows a stack trace
 					$variableValue = "<ol start=\"0\">\n";
 					$variableValue .= "\t<li>$env->page</li>\n";
 					foreach($this->paramStack as $curStackEntry)
@@ -127,10 +134,21 @@ class PeppermintParsedown extends ParsedownExtra
 					}
 					$variableValue .= "</ol>\n";
 					break;
-				case "~":
+				case "~": // Show requested page's name
 					if(!empty($this->paramStack))
 						$variableValue = $this->escapeText($env->page);
-				// TODO: Add a option that displays a list of subpages here
+					break;
+				case "*": // Lists subpages
+					$subpages = get_subpages($pageindex, $env->page);
+					$variableValue = [];
+					foreach($subpages as $pagename => $depth)
+					{
+						$variableValue[] = $pagename;
+					}
+					$variableValue = implode(", ", $variableValue);
+					if(strlen($variableValue) === 0)
+						$variableValue = "<em>(none yet!)</em>";
+					break;
 			}
 			if(isset($params[$variableKey]))
 			{
@@ -138,7 +156,7 @@ class PeppermintParsedown extends ParsedownExtra
 				$variableValue = $this->escapeText($variableValue);
 			}
 			
-			if($variableValue)
+			if($variableValue !== false)
 			{
 				return [
 					"extent" => strlen($matches[0]),
