Skip to content

Instantly share code, notes, and snippets.

@mahedihasannoman
Forked from jbroadway/Slimdown.md
Created March 10, 2022 08:22
Show Gist options
  • Select an option

  • Save mahedihasannoman/f1798a27fc099de7c4780fd5586c217d to your computer and use it in GitHub Desktop.

Select an option

Save mahedihasannoman/f1798a27fc099de7c4780fd5586c217d to your computer and use it in GitHub Desktop.

Revisions

  1. @jbroadway jbroadway revised this gist Feb 28, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Slimdown.md
    Original 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()`):

  2. @jbroadway jbroadway renamed this gist Jan 11, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @jbroadway jbroadway revised this gist Jan 11, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Slimdown.php
    Original 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><ul>/' => '', // fix extra ul
    '/<\/ol><ol>/' => '', // fix extra ol
    '/<\/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 (strpos ($trimmed, '<') === 0) {
    return $line;
    if (preg_match ('/^<\/?(ul|ol|li|h|p|bl)/', $trimmed)) {
    return "\n" . $line . "\n";
    }
    return sprintf ("\n<p>%s</p>\n", $trimmed);
    }
  4. @jbroadway jbroadway revised this gist Jan 11, 2014. 1 changed file with 22 additions and 11 deletions.
    33 changes: 22 additions & 11 deletions Slimdown.php
    Original 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 (
    '/(#+)(.*)/e' => 'self::header (\'\\1\', \'\\2\')', // headers
    '/(#+)(.*)/' => '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\*(.*)/e' => 'self::ul_list (\'\\1\')', // ul lists
    '/\n[0-9]+\.(.*)/e' => 'self::ol_list (\'\\1\')', // ol lists
    '/\n(&gt;|\>)(.*)/e' => 'self::blockquote (\'\\2\')', // blockquotes
    '/\n([^\n]+)\n/e' => 'self::para (\'\\1\')', // add paragraphs
    '/\n\*(.*)/' => 'self::ul_list', // ul lists
    '/\n[0-9]+\.(.*)/' => 'self::ol_list', // ol lists
    '/\n(&gt;|\>)(.*)/' => '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 ($line) {
    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 ($item) {
    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 ($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 ($item) {
    private static function blockquote ($regs) {
    $item = $regs[2];
    return sprintf ("\n<blockquote>%s</blockquote>", trim ($item));
    }

    private static function header ($chars, $header) {
    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) {
    $text = preg_replace ($regex, $replacement, $text);
    if (is_callable ( $replacement)) {
    $text = preg_replace_callback ($regex, $replacement, $text);
    } else {
    $text = preg_replace ($regex, $replacement, $text);
    }
    }
    return trim ($text);
    }
  5. @jbroadway jbroadway revised this gist Jun 24, 2013. 2 changed files with 9 additions and 2 deletions.
    3 changes: 2 additions & 1 deletion README.md
    Original 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...
    More text with `inline($code)` sample.

    > A block quote
    > across two lines.
    8 changes: 7 additions & 1 deletion Slimdown.php
    Original 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&gt;(.*)/e' => 'self::blockquote (\'\\1\')', // blockquotes
    '/\n(&gt;|\>)(.*)/e' => 'self::blockquote (\'\\2\')', // blockquotes
    '/\n([^\n]+)\n/e' => 'self::para (\'\\1\')', // add paragraphs
    '/<\/ul><ul>/' => '', // fix extra ul
    '/<\/ol><ol>/' => '', // fix extra ol
  6. @jbroadway jbroadway revised this gist May 31, 2012. 2 changed files with 35 additions and 28 deletions.
    35 changes: 35 additions & 0 deletions README.md
    Original 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...");

    ?>
    ```
    28 changes: 0 additions & 28 deletions usage.php
    Original file line number Diff line number Diff line change
    @@ -1,28 +0,0 @@
    <?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...");
  7. @jbroadway jbroadway revised this gist May 31, 2012. 3 changed files with 103 additions and 10 deletions.
    68 changes: 68 additions & 0 deletions README.md
    Original 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!');

    ?>
    ```
    38 changes: 28 additions & 10 deletions Slimdown.php
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,33 @@
    <?php

    /**
    * Very rudimentary (and likely buggy!) regex-based example Markdown parser.
    * 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
    '/\*\*(.*?)\*\*/' => '<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
    '/(#+)(.*)/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&gt;(.*)/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);
    7 changes: 7 additions & 0 deletions usage.php
    Original 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...");
  8. @jbroadway jbroadway created this gist May 30, 2012.
    57 changes: 57 additions & 0 deletions Slimdown.php
    Original 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);
    }
    }
    21 changes: 21 additions & 0 deletions usage.php
    Original 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...");