mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-11-22 16:33:00 +00:00
125 lines
4.7 KiB
PHP
125 lines
4.7 KiB
PHP
<?php
|
|
register_module([
|
|
"name" => "Default Parser",
|
|
"version" => "0.7",
|
|
"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.",
|
|
"id" => "parser-default",
|
|
"code" => function() {
|
|
add_parser(function($markdown) {
|
|
return Slimdown::render($markdown);
|
|
});
|
|
}
|
|
]);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
///////////////////////////////////////// Slimdown /////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////// %slimdown% //
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
/**
|
|
* Slimdown - A very basic regex-based Markdown parser. Supports the
|
|
* following elements (and can be extended via Slimdown::add_rule()):
|
|
*
|
|
* - Headers
|
|
* - Links
|
|
* - Bold
|
|
* - Emphasis
|
|
* - Deletions
|
|
* - Quotes
|
|
* - Inline code
|
|
* - Blockquotes
|
|
* - Ordered/unordered lists
|
|
* - Horizontal rules
|
|
*
|
|
* Author: Johnny Broadway <johnny@johnnybroadway.com>
|
|
* Website: https://gist.github.com/jbroadway/2836900
|
|
* License: MIT
|
|
*/
|
|
|
|
/**
|
|
* Modified by Starbeamrainbowlabs (starbeamrainbowlabs)
|
|
*
|
|
* Changed bold to use single asterisks
|
|
* Changed italics to use single underscores
|
|
* Added one to add the heading levels (no <h1> tags allowed)
|
|
* 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
|
|
'/!\[(.*)\]\(([^\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(>|\>)(.*)/' => '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);
|
|
}
|
|
|
|
/**
|
|
* Add a rule.
|
|
*/
|
|
public static function add_rule ($regex, $replacement) {
|
|
self::$rules[$regex] = $replacement;
|
|
}
|
|
/**
|
|
* Render some Markdown into HTML.
|
|
*/
|
|
public static function render ($text) {
|
|
foreach (self::$rules as $regex => $replacement) {
|
|
if (is_callable ( $replacement)) {
|
|
$text = preg_replace_callback ($regex, $replacement, $text);
|
|
} else {
|
|
$text = preg_replace ($regex, $replacement, $text);
|
|
}
|
|
}
|
|
return trim ($text);
|
|
}
|
|
}
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
?>
|