1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-28 06:03:00 +00:00

Write extension of parsedown extra and change default parser.

Also automatically download parsedown and parsedownextra to the current directory dynamically instead of including in the parser-parsedown module.
This commit is contained in:
Starbeamrainbowlabs 2016-03-12 15:26:30 +00:00
parent a80a2b677f
commit f509db5785
7 changed files with 384 additions and 2333 deletions

4
.gitignore vendored
View file

@ -16,6 +16,10 @@ invindex.json
# The recent changes list # The recent changes list
recent-changes.json recent-changes.json
# The automatically downloaded parsedown files
Parsedown.php
ParsedownExtra.php
# All uploaded files # All uploaded files
build/Files/* build/Files/*

View file

@ -18,6 +18,9 @@ if(file_exists($build_env->target))
echo("*** Rebuilding module index ***\n"); echo("*** Rebuilding module index ***\n");
$modules = glob("modules/*.php"); $modules = glob("modules/*.php");
$module_index = []; $module_index = [];
// Defined just in case a module needs to reference them when we require() them
// to gain information
$env = $paths = new stdClass();
function register_module($settings) function register_module($settings)
{ {

View file

@ -93,7 +93,7 @@ $settings->maxpagesize = 135000;
// The parser to use when rendering pages. Defaults to a modified version of // The parser to use when rendering pages. Defaults to a modified version of
// slimdown, originally written by Johnny Broadway <johnny@johnnybroadway.com>. // slimdown, originally written by Johnny Broadway <johnny@johnnybroadway.com>.
$settings->parser = "default"; $settings->parser = "parsedown";
// Whether page sources should be cleaned of HTML before rendering. It is // Whether page sources should be cleaned of HTML before rendering. It is
// STRONGLY recommended that you keep this option turned on. // STRONGLY recommended that you keep this option turned on.
@ -3700,166 +3700,196 @@ register_module([
register_module([ register_module([
"name" => "Default Parser", "name" => "Parsedown",
"version" => "0.9", "version" => "0.3",
"author" => "Johnny Broadway & Starbeamrainbowlabs", "author" => "Johnny Broadway, Emanuil Rusev & Starbeamrainbowlabs",
"description" => "The 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.", "description" => "An upgraded 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-default", "id" => "parser-parsedown",
"code" => function() { "code" => function() {
global $settings; $parser = new PeppermintParsedown();
$parser->setInternalLinkBase("?page=%s");
add_parser("parsedown", function($source) use ($parser) {
$result = $parser->text($source);
add_parser("default", function($markdown) { return $result;
return Slimdown::render($markdown);
}); });
// Register the help section
if($settings->parser != "default")
return; // Don't register the help section if we aren't the currently set parser.
add_help_section("20-parser-default", "Editor Syntax", "<p>$settings->sitename's editor uses a modified version of slimdown, a flavour of markdown that is implementated using regular expressions. See the credits page for more information and links to the original source for this. A quick reference can be found below:</p>
<table>
<tr><th>Type This</th><th>To get this</th>
<tr><td><code>_italics_</code></td><td><em>italics</em></td></tr>
<tr><td><code>*bold*</code></td><td><strong>bold</strong></td></tr>
<tr><td><code>~~Strikethrough~~</code></td><td><del>Strikethough</del></td></tr>
<tr><td><code>`code`</code></td><td><code>code</code></td></tr>
<tr><td><code># Heading</code></td><td><h2>Heading</h2></td></tr>
<tr><td><code>## Sub Heading</code></td><td><h3>Sub Heading</h3></td></tr>
<tr><td><code>[[Internal Link]]</code></td><td><a href='index.php?page=Internal Link'>Internal Link</a></td></tr>
<tr><td><code>[[Display Text|Internal Link]]</code></td><td><a href='index.php?page=Internal Link'>Display Text</a></td></tr>
<tr><td><code>[Display text](//google.com/)</code></td><td><a href='//google.com/'>Display Text</a></td></tr>
<tr><td><code>&gt; Blockquote<br />&gt; Some text</code></td><td><blockquote> Blockquote<br />Some text</td></tr>
<tr><td><code> - Apples<br /> * Oranges</code></td><td><ul><li>Apples</li><li>Oranges</li></ul></td></tr>
<tr><td><code>1. This is<br />2. an ordered list</code></td><td><ol><li>This is</li><li>an ordered list</li></ol></td></tr>
<tr><td><code>
---
</code></td><td><hr /></td></tr>
<!--<tr><tds><code> - One
- Two
- Three</code></td><td><ul><li>One</li><li>Two</li><li>Three</li></ul></td></tr>-->
<tr><td><code>![Alt text](//starbeamrainbowlabs.com/favicon-small.png)</code></td><td><img src='//starbeamrainbowlabs.com/favicon-small.png' alt='Alt text' /></td></code>
</table>
<p>In addition, the following extra syntax is supported for images:</p>
<pre><code>Size the image to at most 250 pixels wide:
![Alt text](//starbeamrainbowlabs.com/favicon-small.png 250px)
Size the image to at most 120px wide and have it float at the right ahnd size of the page:
![Alt text](//starbeamrainbowlabs.com/favicon-small.png 120px right)</code></pre>");
} }
]); ]);
/*********************************************************************** /*** Parsedown versions ***
* ███████ ██ ██ ███ ███ ██████ ██████ ██ ██ ███ ██ * * Parsedown Core: 1.6.0 *
* ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ████ ██ * * Parsedown Extra: 0.7.0 *
* ███████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ * **************************/
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ * $env->parsedown_paths = new stdClass();
* ███████ ███████ ██ ██ ██ ██████ ██████ ███ ███ ██ ████ * $env->parsedown_paths->parsedown = "https://cdn.rawgit.com/erusev/parsedown/3ebbd730b5c2cf5ce78bc1bf64071407fc6674b7/Parsedown.php";
***********************************************************************/ $env->parsedown_paths->parsedown_extra = "https://cdn.rawgit.com/erusev/parsedown-extra/11a44e076d02ffcc4021713398a60cd73f78b6f5/ParsedownExtra.php";
/**
* Slimdown - A very basic regex-based Markdown parser. Supports the // Download parsedown and parsedown extra if they don't already exist
* following elements (and can be extended via Slimdown::add_rule()): if(!file_exists("./Parsedown.php") || filesize("./Parsedown.php") === 0)
file_put_contents("./Parsedown.php", fopen($env->parsedown_paths->parsedown, "r"));
if(!file_exists("./ParsedownExtra.php") || filesize("./ParsedownExtra.php") === 0)
file_put_contents("./ParsedownExtra.php", fopen($env->parsedown_paths->parsedown_extra, "r"));
require_once("./Parsedown.php");
require_once("./ParsedownExtra.php");
/*
* ██████ █████ ██████ ███████ ███████ ██████ ██████ ██ ██ ███ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
* ██████ ███████ ██████ ███████ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ███████ ███████ ██████ ██████ ███ ███ ██ ████
* *
* - Headers * ███████ ██ ██ ████████ ███████ ███ ██ ███████ ██ ██████ ███ ██ ███████
* - Links * ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ████ ██ ██
* - Bold * █████ ███ ██ █████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ███████
* - Emphasis * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* - Deletions * ███████ ██ ██ ██ ███████ ██ ████ ███████ ██ ██████ ██ ████ ███████
* - Quotes */
* - Inline code class PeppermintParsedown extends ParsedownExtra
* - Blockquotes {
* - Ordered/unordered lists private $internalLinkBase = "./%s";
* - Horizontal rules
*
* Author: Johnny Broadway <johnny@johnnybroadway.com>
* Website: https://gist.github.com/jbroadway/2836900
* License: MIT
*/
/** function __construct()
* Modified by Starbeamrainbowlabs (starbeamrainbowlabs) {
* // Prioritise our internal link parsing over the regular link parsing
* Changed bold to use single asterisks array_unshift($this->InlineTypes["["], "InternalLink");
* Changed italics to use single underscores // Prioritise our image parser over the regular image parser
* Added one to add the heading levels (no <h1> tags allowed) array_unshift($this->InlineTypes["!"], "ExtendedImage");
* Added wiki style internal link parsing
* Added wiki style internal link parsing with display text
* Added image support
*/
class Slimdown {
public static $rules = array (
'/\r\n/' => "\n", // new line normalisation
'/^(#+)(.*)/' => 'self::header', // headers
'/(\*+)(.*?)\1/' => '<strong>\2</strong>', // bold
'/(_)(.*?)\1/' => '<em>\2</em>', // emphasis
'/!\[(.*)\]\(([^\s]+)\s(\d+.+)\s(left|right)\)/' => '<img src="\2" alt="\1" style="max-width: \3; float: \4;" />', // images with size //$this->inlineMarkerList .= "{";
'/!\[(.*)\]\(([^\s]+)\s(\d+.+)\)/' => '<img src="\2" alt="\1" style="max-width: \3;" />', // images with size
'/!\[(.*)\]\((.*)\)/' => '<img src="\2" alt="\1" />', // basic images
'/\[\[([a-zA-Z0-9\_\- ]+)\|([a-zA-Z0-9\_\- ]+)\]\]/' => '<a href=\'index.php?page=\1\'>\2</a>', //internal links with display text
'/\[\[([a-zA-Z0-9\_\- ]+)\]\]/' => '<a href=\'index.php?page=\1\'>\1</a>', //internal links
'/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href=\'\2\' target=\'_blank\'>\1</a>', // links
'/\~\~(.*?)\~\~/' => '<del>\1</del>', // del
'/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote
'/`(.*?)`/' => '<code>\1</code>', // inline code
'/\n\s*(\*|-)(.*)/' => 'self::ul_list', // ul lists
'/\n[0-9]+\.(.*)/' => 'self::ol_list', // ol lists
'/\n(&gt;|\>)(.*)/' => 'self::blockquote', // blockquotes
'/\n-{3,}/' => "\n<hr />", // horizontal rule
'/\n([^\n]+)\n\n/' => 'self::para', // add paragraphs
'/<\/ul>\s?<ul>/' => '', // fix extra ul
'/<\/ol>\s?<ol>/' => '', // fix extra ol
'/<\/blockquote><blockquote>/' => "\n" // fix extra blockquote
);
private static function para ($regs) {
$line = $regs[1];
$trimmed = trim ($line);
if (preg_match ('/^<\/?(ul|ol|li|h|p|bl)/', $trimmed)) {
return "\n" . $line . "\n";
}
return sprintf ("\n<p>%s</p>\n", $trimmed);
}
private static function ul_list ($regs) {
$item = $regs[2];
return sprintf ("\n<ul>\n\t<li>%s</li>\n</ul>", trim($item));
}
private static function ol_list ($regs) {
$item = $regs[1];
return sprintf ("\n<ol>\n\t<li>%s</li>\n</ol>", trim($item));
}
private static function blockquote ($regs) {
$item = $regs[2];
return sprintf ("\n<blockquote>%s</blockquote>", trim($item));
}
private static function header ($regs) {
list ($tmp, $chars, $header) = $regs;
$level = strlen ($chars);
return sprintf ('<h%d>%s</h%d>', $level + 1, trim($header), $level + 1);
} }
/** protected function inlineInternalLink($fragment)
* Add a rule. {
*/ if(preg_match('/^\[\[(.*)\]\]/', $fragment["text"], $matches))
public static function add_rule ($regex, $replacement) { {
self::$rules[$regex] = $replacement; $display = $linkPage = $matches[1];
} if(strpos($matches[1], "|"))
/** {
* Render some Markdown into HTML. // We have a bar character
*/ $parts = explode("|", $matches[1], 2);
public static function render ($text) { $linkPage = $parts[0];
foreach (self::$rules as $regex => $replacement) { $display = $parts[1];
if (is_callable ( $replacement)) {
$text = preg_replace_callback ($regex, $replacement, $text);
} else {
$text = preg_replace ($regex, $replacement, $text);
} }
// Construct the full url
$linkUrl = str_replace(
"%s", rawurlencode($linkPage),
$this->internalLinkBase
);
return [
"extent" => strlen($matches[0]),
"element" => [
"name" => "a",
"text" => $display,
"attributes" => [
"href" => $linkUrl
]
]
];
} }
return trim ($text); return;
}
protected function inlineExtendedImage($fragment)
{
if(preg_match('/^!\[(.*)\]\(([^ |)]+)\s*\|([^|)]*)(?:\|([^)]*))?\)/', $fragment["text"], $matches))
{
/*
* 0 - Everything
* 1 - Alt text
* 2 - Url
* 3 - First param
* 4 - Second Param (optional)
*/
var_dump($matches);
$altText = $matches[1];
$imageUrl = $matches[2];
$param1 = strtolower(trim($matches[3]));
$param2 = empty($matches[4]) ? false : strtolower(trim($matches[4]));
$floatDirection = false;
$imageSize = false;
if($this->isFloatValue($param1))
{
$floatDirection = $param1;
$imageSize = $this->parseSizeSpec($param2);
}
else if($this->isFloatValue($param2))
{
$floatDirection = $param2;
$imageSize = $this->parseSizeSpec($param1);
}
else
{
$imageSize = $this->parseSizeSpec($param1);
}
// If they are both invalid then something very strange is going on
// Let the built in parsedown image handler deal with it
if($imageSize === false && $floatDirection === false)
return;
$style = "";
if($imageSize !== false)
$style .= " max-width: " . $imageSize["x"] . "; max-height: " . $imageSize["y"] . ";";
if($floatDirection)
$style .= " float: $floatDirection;";
return [
"extent" => strlen($matches[0]),
"element" => [
"name" => "img",
"attributes" => [
"src" => $imageUrl,
"alt" => $altText,
"style" => trim($style)
]
]
];
}
}
private function isFloatValue($value)
{
return in_array(strtolower($value), [ "left", "right" ]);
}
private function parseSizeSpec($text)
{
if(strpos($text, "x") === false)
return false;
$parts = explode("x", $text, 2);
if(count($parts) != 2)
return false;
array_map("trim", $parts);
array_map("intval", $parts);
if(in_array(0, $parts))
return false;
return [
"x" => $parts[0],
"y" => $parts[1]
];
}
/**
* Sets the base url to be used for internal links. '%s' will be replaced
* with a URL encoded version of the page name.
* @param string $url The url to use when parsing internal links.
*/
public function setInternalLinkBase($url)
{
$this->internalLinkBase = $url;
} }
} }
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -180,21 +180,21 @@
"optional": false "optional": false
}, },
{ {
"name": "Default Parser", "name": "Old Default Parser",
"version": "0.9", "version": "0.9",
"author": "Johnny Broadway & Starbeamrainbowlabs", "author": "Johnny Broadway & Starbeamrainbowlabs",
"description": "The 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.", "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", "id": "parser-default-old",
"lastupdate": 1451134309, "lastupdate": false,
"optional": false "optional": true
}, },
{ {
"name": "Parsedown", "name": "Parsedown",
"version": "0.1", "version": "0.3",
"author": "Johnny Broadway, Emanuil Rusev & Starbeamrainbowlabs", "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.", "description": "An upgraded 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", "id": "parser-parsedown",
"lastupdate": 1451134485, "lastupdate": 1457796170,
"optional": true "optional": false
} }
] ]

View file

@ -1,10 +1,11 @@
<?php <?php
register_module([ register_module([
"name" => "Default Parser", "name" => "Old Default Parser",
"version" => "0.9", "version" => "0.9",
"author" => "Johnny Broadway & Starbeamrainbowlabs", "author" => "Johnny Broadway & Starbeamrainbowlabs",
"description" => "The 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.", "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", "id" => "parser-default-old",
"optional" => true,
"code" => function() { "code" => function() {
global $settings; global $settings;

File diff suppressed because it is too large Load diff

View file

@ -90,7 +90,7 @@ $settings->maxpagesize = 135000;
// The parser to use when rendering pages. Defaults to a modified version of // The parser to use when rendering pages. Defaults to a modified version of
// slimdown, originally written by Johnny Broadway <johnny@johnnybroadway.com>. // slimdown, originally written by Johnny Broadway <johnny@johnnybroadway.com>.
$settings->parser = "default"; $settings->parser = "parsedown";
// Whether page sources should be cleaned of HTML before rendering. It is // Whether page sources should be cleaned of HTML before rendering. It is
// STRONGLY recommended that you keep this option turned on. // STRONGLY recommended that you keep this option turned on.