-
-
Save mahedihasannoman/f1798a27fc099de7c4780fd5586c217d to your computer and use it in GitHub Desktop.
Revisions
-
jbroadway revised this gist
Feb 28, 2022 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ # Slimdown > **[This project has moved to github.com/jbroadway/slimdown](https://github.com/jbroadway/slimdown)** A very basic regex-based Markdown parser. Supports the following elements (and can be extended via `Slimdown::add_rule()`): -
jbroadway renamed this gist
Jan 11, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jbroadway revised this gist
Jan 11, 2014 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,16 +33,16 @@ class Slimdown { '/\n(>|\>)(.*)/' => 'self::blockquote ', // blockquotes '/\n-{5,}/' => "\n<hr />", // horizontal rule '/\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); } -
jbroadway revised this gist
Jan 11, 2014 . 1 changed file with 22 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,50 +13,57 @@ * - Inline code * - Blockquotes * - Ordered/unordered lists * - Horizontal rules * * Author: Johnny Broadway <[email protected]> * Website: https://gist.github.com/jbroadway/2836900 * License: MIT */ class Slimdown { public static $rules = array ( '/(#+)(.*)/' => 'self::header', // headers '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href=\'\2\'>\1</a>', // links '/(\*\*|__)(.*?)\1/' => '<strong>\2</strong>', // bold '/(\*|_)(.*?)\1/' => '<em>\2</em>', // emphasis '/\~\~(.*?)\~\~/' => '<del>\1</del>', // del '/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote '/`(.*?)`/' => '<code>\1</code>', // inline code '/\n\*(.*)/' => 'self::ul_list', // ul lists '/\n[0-9]+\.(.*)/' => 'self::ol_list', // ol lists '/\n(>|\>)(.*)/' => 'self::blockquote ', // blockquotes '/\n-{5,}/' => "\n<hr />", // horizontal rule '/\n([^\n]+)\n/' => 'self::para', // add paragraphs '/<\/ul><ul>/' => '', // fix extra ul '/<\/ol><ol>/' => '', // fix extra ol '/<\/blockquote><blockquote>/' => "\n" // fix extra blockquote ); private static function para ($regs) { $line = $regs[1]; $trimmed = trim ($line); if (strpos ($trimmed, '<') === 0) { return $line; } return sprintf ("\n<p>%s</p>\n", $trimmed); } private static function ul_list ($regs) { $item = $regs[1]; 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, trim ($header), $level); } @@ -74,7 +81,11 @@ public static function add_rule ($regex, $replacement) { public static function render ($text) { $text = "\n" . $text . "\n"; 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); } -
jbroadway revised this gist
Jun 24, 2013 . 2 changed files with 9 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,7 @@ following elements (and can be extended via `Slimdown::add_rule()`): * Emphasis * Deletions * Quotes * Inline code * Blockquotes * Ordered/unordered lists @@ -92,7 +93,7 @@ One __two__ three _four_ five __six__ seven _eight_. 2. Two 3. Three More text with `inline($code)` sample. > A block quote > across two lines. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,13 @@ * - Emphasis * - Deletions * - Quotes * - Inline code * - Blockquotes * - Ordered/unordered lists * * Author: Johnny Broadway <[email protected]> * Website: https://gist.github.com/jbroadway/2836900 * License: MIT */ class Slimdown { public static $rules = array ( @@ -21,9 +26,10 @@ class Slimdown { '/(\*|_)(.*?)\1/' => '<em>\2</em>', // emphasis '/\~\~(.*?)\~\~/' => '<del>\1</del>', // del '/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote '/`(.*?)`/' => '<code>\1</code>', // inline code '/\n\*(.*)/e' => 'self::ul_list (\'\\1\')', // ul lists '/\n[0-9]+\.(.*)/e' => 'self::ol_list (\'\\1\')', // ol lists '/\n(>|\>)(.*)/e' => 'self::blockquote (\'\\2\')', // blockquotes '/\n([^\n]+)\n/e' => 'self::para (\'\\1\')', // add paragraphs '/<\/ul><ul>/' => '', // fix extra ul '/<\/ol><ol>/' => '', // fix extra ol -
jbroadway revised this gist
May 31, 2012 . 2 changed files with 35 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -66,3 +66,38 @@ echo Slimdown::render ('Check [[This Page]] out!'); ?> ``` ### A longer example ```php <?php require_once ('Slimdown.php'); echo Slimdown::render ("# Title And *now* [a link](http://www.google.com) to **follow** and [another](http://yahoo.com/). * One * Two * Three ## Subhead One **two** three **four** five. One __two__ three _four_ five __six__ seven _eight_. 1. One 2. Two 3. Three More text... > A block quote > across two lines. More text..."); ?> ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,28 +0,0 @@ -
jbroadway revised this gist
May 31, 2012 . 3 changed files with 103 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ # 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 * Blockquotes * Ordered/unordered lists ## Usage Here is the general use case: ```php <?php require_once ('Slimdown.php'); echo Slimdown::render ( "# Page title\n\nAnd **now** for something _completely_ different." ); ?> ``` ### Adding rules A simple rule to convert `:)` to an image: ```php <?php require_once ('Slimdown.php'); Slimdown::add_rule ('/(\W)\:\)(\W)/', '\1<img src="smiley.png" />\2'); echo Slimdown::render ('Know what I\'m sayin? :)'); ?> ``` In this example, we add GitHub-style internal linking (e.g., `[[Another Page]]`). ```php <?php require_once ('Slimdown.php'); function mywiki_internal_link ($title) { return sprintf ( '<a href="%s">%s</a>', preg_replace ('/[^a-zA-Z0-9_-]+/', '-', $title), $title ); } Slimdown::add_rule ('/\[\[(.*?)\]\]/e', 'mywiki_internal_link (\'\\1\')'); echo Slimdown::render ('Check [[This Page]] out!'); ?> ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,33 @@ <?php /** * 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 * - Blockquotes * - Ordered/unordered lists */ class Slimdown { public static $rules = array ( '/(#+)(.*)/e' => 'self::header (\'\\1\', \'\\2\')', // headers '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href=\'\2\'>\1</a>', // links '/(\*\*|__)(.*?)\1/' => '<strong>\2</strong>', // bold '/(\*|_)(.*?)\1/' => '<em>\2</em>', // emphasis '/\~\~(.*?)\~\~/' => '<del>\1</del>', // del '/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote '/\n\*(.*)/e' => 'self::ul_list (\'\\1\')', // ul lists '/\n[0-9]+\.(.*)/e' => 'self::ol_list (\'\\1\')', // ol lists '/\n>(.*)/e' => 'self::blockquote (\'\\1\')', // blockquotes '/\n([^\n]+)\n/e' => 'self::para (\'\\1\')', // add paragraphs '/<\/ul><ul>/' => '', // fix extra ul '/<\/ol><ol>/' => '', // fix extra ol '/<\/blockquote><blockquote>/' => "\n" // fix extra blockquote ); private static function para ($line) { @@ -32,6 +46,10 @@ private static function ol_list ($item) { return sprintf ("\n<ol>\n\t<li>%s</li>\n</ol>", trim ($item)); } private static function blockquote ($item) { return sprintf ("\n<blockquote>%s</blockquote>", trim ($item)); } private static function header ($chars, $header) { $level = strlen ($chars); return sprintf ('<h%d>%s</h%d>', $level, trim ($header), $level); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,8 +14,15 @@ One **two** three **four** five. One __two__ three _four_ five __six__ seven _eight_. 1. One 2. Two 3. Three More text... > A block quote > across two lines. More text..."); -
jbroadway created this gist
May 30, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ <?php /** * Very rudimentary (and likely buggy!) regex-based example Markdown parser. */ class Slimdown { public static $rules = array ( '/(#+)(.*)/e' => 'self::header (\'\\1\', \'\\2\')', // headers '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href=\'\2\'>\1</a>', // links '/\*\*(.*?)\*\*/' => '<strong>\1</strong>', // bold '/\*(.*?)\*/' => '<em>\1</em>', // emphasis '/\n\*(.*)/e' => 'self::ul_list (\'\\1\')', // ul lists '/\n[0-9]+\.(.*)/e' => 'self::ol_list (\'\\1\')', // ol lists '/<\/ul>\n<ul>/' => "\n", // fix extra ul '/<\/ol>\n<ol>/' => "\n", // fix extra ol '/\n([^\n]+)\n/e' => 'self::para (\'\\1\')' // add paragraphs ); private static function para ($line) { $trimmed = trim ($line); if (strpos ($trimmed, '<') === 0) { return $line; } return sprintf ("\n<p>%s</p>\n", $trimmed); } private static function ul_list ($item) { return sprintf ("\n<ul>\n\t<li>%s</li>\n</ul>", trim ($item)); } private static function ol_list ($item) { return sprintf ("\n<ol>\n\t<li>%s</li>\n</ol>", trim ($item)); } private static function header ($chars, $header) { $level = strlen ($chars); return sprintf ('<h%d>%s</h%d>', $level, trim ($header), $level); } /** * Add a rule. */ public static function add_rule ($regex, $replacement) { self::$rules[$regex] = $replacement; } /** * Render some Markdown into HTML. */ public static function render ($text) { $text = "\n" . $text . "\n"; foreach (self::$rules as $regex => $replacement) { $text = preg_replace ($regex, $replacement, $text); } return trim ($text); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ <?php require_once ('Slimdown.php'); echo Slimdown::render ("# Title And *now* [a link](http://www.google.com) to **follow** and [another](http://yahoo.com/). * One * Two * Three ## Subhead One **two** three **four** five. 1. One 2. Two 3. Three More text...");