From ba3b3609ddf0614485344f3ee88cf2621ec81b2f Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 3 Apr 2016 17:49:38 +0100
Subject: [PATCH] Implement math expression parsing. Closes #61.

---
 Changelog.md          |  1 +
 README.md             |  1 +
 build/index.php       | 26 +++++++++++++++++++++++++-
 core.php              | 21 ++++++++++++++++++++-
 settings.fragment.php |  5 +++++
 5 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 1053916..d5ce1be 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -6,6 +6,7 @@
     - New pages show up with an 'N' next to them (as they do in a MediaWiki installation)
     - Page deletions show up in red with a line though them
     - Uploads show with an arrow next to them along with the size of the uploaded file
+ - Added mathematical expression parsing between dollar signs.
 
 # Changed
  - Enhanced the dev help page some more
diff --git a/README.md b/README.md
index 3e392c3..e040d20 100644
--- a/README.md
+++ b/README.md
@@ -94,6 +94,7 @@ Key				| Value			| Explanation
 `editing`		| boolean		| Determines whether editing is enabled. Set to false to disable disting for all users (anonymous or otherwise).
 `maxpagesize`	| integer		| The maximum number of characters allowed on a single page. The default is 135,000 characters, which is about 50 pages.
 `clean_raw_html`| boolean		| Whether page sources should be cleaned of HTML before rendering. If set to true any raw HTML will be escaped before rendering. Note that this shouldn't affect code blocks - they should alwys be escaped. It is STRONGLY recommended that you keep this option turned on, **_ESPECIALLY_** if you allow anonymous edits as no sanitizing whatsoever is performed on the HTML. If you need a feature that the markdown parser doesn't have, please open an issue. Also note that some parsers may override this setting and escape HTML sequences anyway.
+`enable_math_rendering`	| boolean	| Whether to enable client side mathematical expression parsing. When enabled LaTeX mathematical expressions can be enclosed in dollar signs like so: `$x^2$`. Turn off if you don't use it.
 `anonedits`		| boolean		| Determines whether users who aren't logged in are allowed to edit your wiki. Set to true to allow anonymous users to edit the wiki.
 `defaultpage`	| string		| The name of the page that will act as the home page for the wiki. This page will be served if the user didn't specify a page.
 `defaultaction`	| action name	| The default action. This action will be performed if no other action is specified. It is recommended you set this to "view" - that way the user automatically views the default page (see above).
diff --git a/build/index.php b/build/index.php
index da243be..7cfd38c 100644
--- a/build/index.php
+++ b/build/index.php
@@ -100,6 +100,11 @@ $settings->parser = "parsedown";
 // STRONGLY recommended that you keep this option turned on.
 $settings->clean_raw_html = true;
 
+// Whether to enable client side rendering of methematical expressions.
+// Math expressions should be enclosed inside of dollar signs ($).
+// Turn off if you don't use it.
+$settings->enable_math_rendering = true;
+
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////// Access and Security /////////////////////////////
@@ -1035,7 +1040,7 @@ class page_renderer
 			"{sitename}" => $logo_html,
 			"v0.11-dev" => $version,
 			"{favicon-url}" => $settings->favicon,
-			"{header-html}" => self::get_css_as_html(),
+			"{header-html}" => self::get_header_html(),
 
 			"{navigation-bar}" => self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"),
 			"{navigation-bar-bottom}" => self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"),
@@ -1084,6 +1089,25 @@ class page_renderer
 		return self::render($title, $content, self::$minimal_content_template);
 	}
 	
+	public static function get_header_html()
+	{
+		global $settings;
+		$result = self::get_css_as_html();
+		
+		if(!empty($settings->enable_math_rendering))
+			$result .= "<script type='text/x-mathjax-config'>
+  MathJax.Hub.Config({
+    tex2jax: {
+      inlineMath: [ ['$','$'], ['\\\\(','\\\\)'] ],
+      processEscapes: true,
+      skipTags: ['script','noscript','style','textarea','pre','code']
+    }
+  });
+</script>
+<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML'></script>";
+		
+		return $result;
+	}
 	public static function get_css_as_html()
 	{
 		global $settings;
diff --git a/core.php b/core.php
index 5b050ac..3292f7b 100644
--- a/core.php
+++ b/core.php
@@ -650,7 +650,7 @@ class page_renderer
 			"{sitename}" => $logo_html,
 			"{version}" => $version,
 			"{favicon-url}" => $settings->favicon,
-			"{header-html}" => self::get_css_as_html(),
+			"{header-html}" => self::get_header_html(),
 
 			"{navigation-bar}" => self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"),
 			"{navigation-bar-bottom}" => self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"),
@@ -699,6 +699,25 @@ class page_renderer
 		return self::render($title, $content, self::$minimal_content_template);
 	}
 	
+	public static function get_header_html()
+	{
+		global $settings;
+		$result = self::get_css_as_html();
+		
+		if(!empty($settings->enable_math_rendering))
+			$result .= "<script type='text/x-mathjax-config'>
+  MathJax.Hub.Config({
+    tex2jax: {
+      inlineMath: [ ['$','$'], ['\\\\(','\\\\)'] ],
+      processEscapes: true,
+      skipTags: ['script','noscript','style','textarea','pre','code']
+    }
+  });
+</script>
+<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML'></script>";
+		
+		return $result;
+	}
 	public static function get_css_as_html()
 	{
 		global $settings;
diff --git a/settings.fragment.php b/settings.fragment.php
index 1b6c398..8bedc04 100644
--- a/settings.fragment.php
+++ b/settings.fragment.php
@@ -97,6 +97,11 @@ $settings->parser = "parsedown";
 // STRONGLY recommended that you keep this option turned on.
 $settings->clean_raw_html = true;
 
+// Whether to enable client side rendering of methematical expressions.
+// Math expressions should be enclosed inside of dollar signs ($).
+// Turn off if you don't use it.
+$settings->enable_math_rendering = true;
+
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////// Access and Security /////////////////////////////
