From db7ee5bc1cd91198206495a2c3f53ac6f0302152 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 4 Oct 2015 15:20:47 +0100 Subject: [PATCH] Baked image size / floating support into parsedown --- build/index.php | 51 +++++++++++++++++++++++++++++++++--- module_index.json | 2 +- modules/parser-parsedown.php | 51 +++++++++++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/build/index.php b/build/index.php index b86feb8..3fa8b0b 100644 --- a/build/index.php +++ b/build/index.php @@ -2074,10 +2074,6 @@ class Parsedown_Slimdown_Extensions { public static $rules = array ( '/\r\n/' => "\n", // new line normalisation -// '/!\[(.*)\]\(([^\s]+)\s(\d+.+)\s(left|right)\)/' => '\1', // images with size -// '/!\[(.*)\]\(([^\s]+)\s(\d+.+)\)/' => '\1', // images with size -// '/!\[(.*)\]\((.*)\)/' => '\1', // basic images - '/\[\[([a-zA-Z0-9\_\- ]+)\|([a-zA-Z0-9\_\- ]+)\]\]/' => '\2', //internal links with display text '/\[\[([a-zA-Z0-9\_\- ]+)\]\]/' => '\1', //internal links ); @@ -3247,7 +3243,46 @@ class Parsedown $Excerpt['text']= substr($Excerpt['text'], 1); +// var_dump($Excerpt); + + // %addition% + // Addition by Starbeamrainbowlabs that adds support for image sizing + // and floating + + $url_start = strpos($Excerpt["text"], "("); + $url_end = strpos($Excerpt["text"], ")"); + $url = substr($Excerpt["text"], $url_start, $url_end - $url_start); + $style = ""; + if(preg_match("/\s+/", $url) === 1) + { + // We have spaces in the url - there are parameters lying around! + $parts = preg_split("/\s+/", $url); + $part_url = $parts[0]; + $part_size = $parts[1]; + $part_float = false; + if(isset($parts[2])) + $part_float = $parts[2]; + + // Calculate the length of the bit that we removed + $parameter_length = strlen($url) - strlen($part_url); + + // Add a filler string onto the url to hide the parameters + $part_url .= str_repeat("x", $parameter_length); + + // Remove the parameters so that the link parser doesn't get confused + $Excerpt["text"] = str_replace($url, $part_url, $Excerpt["text"]); + + // Build a string of CSS to represent the parameters + $style .= "max-width: $part_size; max-height: $part_size;"; + if($part_float !== false) + $style .= " float: $part_float;"; + + } + $Link = $this->inlineLink($Excerpt); + // %addition_end% + +// var_dump($Link); if ($Link === null) { @@ -3264,6 +3299,14 @@ class Parsedown ), ), ); + + // %addition% + // Add the style to the image element + if(strlen($style) > 0) + $Inline["element"]["attributes"]["style"] = $style; + // Remove the filler from the src + $Inline["element"]["attributes"]["src"] = substr($Inline["element"]["attributes"]["src"], 0, strlen($url) - $parameter_length - 1); + // %addition_end% $Inline['element']['attributes'] += $Link['element']['attributes']; diff --git a/module_index.json b/module_index.json index 9b5b8f9..20603a2 100644 --- a/module_index.json +++ b/module_index.json @@ -158,7 +158,7 @@ "author": "Johnny Broadway, Emanuil Rusev & Starbeamrainbowlabs", "description": "An upgraded parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Also uses a modified Slimdown engine by Johnny Broadway in order to add support for internal links etc. Please be careful, as this module adds a _ton_ of weight to your installation.", "id": "parser-parsedown", - "lastupdate": 1443964436, + "lastupdate": 1443968417, "optional": true } ] \ No newline at end of file diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php index e4c32dc..ed7f548 100644 --- a/modules/parser-parsedown.php +++ b/modules/parser-parsedown.php @@ -53,10 +53,6 @@ class Parsedown_Slimdown_Extensions { public static $rules = array ( '/\r\n/' => "\n", // new line normalisation -// '/!\[(.*)\]\(([^\s]+)\s(\d+.+)\s(left|right)\)/' => '\1', // images with size -// '/!\[(.*)\]\(([^\s]+)\s(\d+.+)\)/' => '\1', // images with size -// '/!\[(.*)\]\((.*)\)/' => '\1', // basic images - '/\[\[([a-zA-Z0-9\_\- ]+)\|([a-zA-Z0-9\_\- ]+)\]\]/' => '\2', //internal links with display text '/\[\[([a-zA-Z0-9\_\- ]+)\]\]/' => '\1', //internal links ); @@ -1226,7 +1222,46 @@ class Parsedown $Excerpt['text']= substr($Excerpt['text'], 1); +// var_dump($Excerpt); + + // %addition% + // Addition by Starbeamrainbowlabs that adds support for image sizing + // and floating + + $url_start = strpos($Excerpt["text"], "("); + $url_end = strpos($Excerpt["text"], ")"); + $url = substr($Excerpt["text"], $url_start, $url_end - $url_start); + $style = ""; + if(preg_match("/\s+/", $url) === 1) + { + // We have spaces in the url - there are parameters lying around! + $parts = preg_split("/\s+/", $url); + $part_url = $parts[0]; + $part_size = $parts[1]; + $part_float = false; + if(isset($parts[2])) + $part_float = $parts[2]; + + // Calculate the length of the bit that we removed + $parameter_length = strlen($url) - strlen($part_url); + + // Add a filler string onto the url to hide the parameters + $part_url .= str_repeat("x", $parameter_length); + + // Remove the parameters so that the link parser doesn't get confused + $Excerpt["text"] = str_replace($url, $part_url, $Excerpt["text"]); + + // Build a string of CSS to represent the parameters + $style .= "max-width: $part_size; max-height: $part_size;"; + if($part_float !== false) + $style .= " float: $part_float;"; + + } + $Link = $this->inlineLink($Excerpt); + // %addition_end% + +// var_dump($Link); if ($Link === null) { @@ -1243,6 +1278,14 @@ class Parsedown ), ), ); + + // %addition% + // Add the style to the image element + if(strlen($style) > 0) + $Inline["element"]["attributes"]["style"] = $style; + // Remove the filler from the src + $Inline["element"]["attributes"]["src"] = substr($Inline["element"]["attributes"]["src"], 0, strlen($url) - $parameter_length - 1); + // %addition_end% $Inline['element']['attributes'] += $Link['element']['attributes'];