PHP Classes

PHP Markdown Parser: Parse Markdown documents and generate HTML tags

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-28 (11 months ago) RSS 2.0 feedNot enough user ratingsTotal: 388 All time: 6,673 This week: 50Up
Version License PHP version Categories
markdown 1.0.11BSD License4HTML, Parsers
Description 

Author

This class can parse Markdown documents and generate HTML tags.

It can take text string or a file in Markdown format and parse it to convert the document into HTML tags.

The parsed tags can be converted to HTML or another format using a separate component.

Currently it supports Markdown tags for headings, paragraphs, ordered and unordered lists, links, images, code, strong and emphasis.

Picture of Manuel Lemos
  Performance   Level  
Name: Manuel Lemos <contact>
Classes: 45 packages by
Country: Portugal Portugal

Example

<?php
/*
 * markdown_to_html.php
 *
 * @(#) $Id: markdown_to_html.php,v 1.2 2014/07/09 05:02:41 mlemos Exp $
 *
 */

   
require_once('markdown_parser.php');
    require_once(
'markup_parser.php');

   
$message_file = ((IsSet($_SERVER['argv']) && count($_SERVER['argv'])>1) ? $_SERVER['argv'][1] : 'test/sample/sample.md');
   
$markdown=new markdown_parser_class;

   
/* Set to 1 if the you need to track line numbers of errors or element
     * positions
     */
   
$markdown->track_lines = 1;

   
$parameters=array(
       
'File'=>$message_file,

       
/* Read a markdown from a string instead of a file */
        /* 'Data'=>'Markdown data here', */
   
);

/*
 * The following lines are for testing purposes.
 * Remove these lines when adapting this example to real applications.
 */
   
if(defined('__TEST'))
    {
        if(IsSet(
$__test_options['parameters']))
           
$parameters = $__test_options['parameters'];
    }

    if((
$success = $markdown->StartParsing($parameters)))
    {
       
/*
         * Use the markup class to rewrite parsed markdown tags as HTML
         *
         * http://www.phpclasses.org/secure-html-filter
         *
         */
       
$markup = new markup_parser_class;
       
   
       
$html = '';
        do
        {
            if(!(
$success = $markdown->Parse($end, $elements)))
                break;

           
/*
             * Rewrite the parsed tags as HTML
             */
           
foreach($elements as $element)
            {
                if(!(
$success = $markup->RewriteElement($element, $element_html)))
                    break
2;
               
$html .= $element_html;
            }
        }
        while(!
$end);
        if(
$success)
           
$success = $markdown->FinishParsing();
    }
    if(!
defined('__TEST'))
        echo
'<html><head><title>', HtmlSpecialChars($message_file).'</title></head><body>';
    if(
$success)
    {
       
/*
         * Markdown parsing succeeded, lets output the converted HTML
         */
       
echo $html;
    }
    else
    {
        echo
'<h1>Markdown parsing error: '.HtmlSpecialChars($markdown->error).' at position '.$markdown->error_position;
        if(
$markdown->track_lines
       
&& $markdown->GetPositionLine($markdown->error_position, $line, $column))
            echo
' line '.$line.' column '.$column;
        echo
"</h1>\n";
    }
    for(
$warning = 0, Reset($markdown->warnings); $warning < count($markdown->warnings); Next($markdown->warnings), $warning++)
    {
       
$w = Key($markdown->warnings);
        echo
'<p>Warning: ', HtmlSpecialChars($markdown->warnings[$w]), ' at position ', $w;
        if(
$markdown->track_lines
       
&& $markdown->GetPositionLine($w, $line, $column))
            echo
' line '.$line.' column '.$column;
        echo
"</p>\n";
    }
    if(!
defined('__TEST'))
        echo
'</body></html>';
?>


  Files folder image Files (66)  
File Role Description
Files folder imagetest (1 file, 3 directories)
Accessible without login Plain text file generate_php_developer_contract.php Example Example of rendering a the Markdown text of a PHP developer contract obtained from Google Generative API in a HTML page
Plain text file markdown_parser.php Class Markdown parser class
Accessible without login Plain text file markdown_preview.php Appl. Markdown render preview form script
Accessible without login Plain text file markdown_to_html.php Example Example of how to generate HTML from markdown parsing results
Accessible without login HTML file sample.html Data Sample output of the parsed simple.md file converted to HTML
Accessible without login Plain text file test_markdown_parser.php Example Test markdown parser class

  Files folder image Files (66)  /  test  
File Role Description
Files folder imageexpect (29 files)
Files folder imagegenerated (1 file)
Files folder imagesample (29 files)
  Accessible without login Plain text file test.php Test Main test script

  Files folder image Files (66)  /  test  /  expect  
File Role Description
  Accessible without login Plain text file Amps-and-angle-encoding.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Auto-links.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Backslash-escapes.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Blockquotes-with-code-blocks.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file code.txt Data Code test expected output
  Accessible without login Plain text file emphasis.txt Data Emphasis test expected output
  Accessible without login Plain text file Hard-wrapped-parag...ist-like-lines.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file headers.txt Data Headers test expected output
  Accessible without login Plain text file Horizontal-rules.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file images.txt Data Images test expected output
  Accessible without login Plain text file image_links.txt Data Links with image test expected output
  Accessible without login Plain text file Inline-HTML-Advanced.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Inline-HTML-comments.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Inline-HTML-Simple.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Links-inline-style.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Links-reference-style.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file links.txt Data Links test expected output
  Accessible without login Plain text file lists.txt Data Lists test expected output
  Accessible without login Plain text file lists_with_code.txt Doc. Lists with code text expected output
  Accessible without login Plain text file Literal-quotes-in-titles.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Markdown-Documentation-Basics.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Markdown-Documentation-Syntax.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Nested-blockquotes.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Ordered-and-unordered-lists.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file sample.txt Data Expected test output
  Accessible without login Plain text file Strong-and-em-together.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file strong.txt Data String test expected output
  Accessible without login Plain text file Tabs.html Data Original test HTML output file by John Gruber
  Accessible without login Plain text file Tidyness.html Data Original test HTML output file by John Gruber

  Files folder image Files (66)  /  test  /  generated  
File Role Description
  Accessible without login Plain text file .cvsignore Data CVS ignore file list

  Files folder image Files (66)  /  test  /  sample  
File Role Description
  Accessible without login Plain text file Amps-and-angle-encoding.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Auto-links.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Backslash-escapes.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Blockquotes-with-code-blocks.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file code.md Data Code markdown sample
  Accessible without login Plain text file emphasis.md Data Emphasis markdown sample
  Accessible without login Plain text file Hard-wrapped-parag...ist-like-lines.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file headers.md Data Headers markdown sample
  Accessible without login Plain text file Horizontal-rules.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file images.md Data Images markdown sample
  Accessible without login Plain text file image_links.md Data Links with image markdown sample
  Accessible without login Plain text file Inline-HTML-Advanced.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Inline-HTML-comments.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Inline-HTML-Simple.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Links-inline-style.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Links-reference-style.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file links.md Data Links markdown sample
  Accessible without login Plain text file lists.md Data Lists markdown sample
  Accessible without login Plain text file lists_with_code.md Data Lists with code markdown sample
  Accessible without login Plain text file Literal-quotes-in-titles.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Markdown-Documentation-Basics.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Markdown-Documentation-Syntax.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Nested-blockquotes.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Ordered-and-unordered-lists.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file sample.md Data Simple markdown document for testing
  Accessible without login Plain text file Strong-and-em-together.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file strong.md Data Strong markdown sample
  Accessible without login Plain text file Tabs.text Data Original test Markdown file by John Gruber
  Accessible without login Plain text file Tidyness.text Data Original test Markdown file by John Gruber

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadmarkdown-2023-11-28.zip 85KB
Downloadmarkdown-2023-11-28.tar.gz 70KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Secure HTML parser and filter Download .zip .tar.gz Convert parsed Markdown tags to HTML Conditional
 Version Control Unique User Downloads Download Rankings  
 98%
Total:388
This week:0
All time:6,673
This week:50Up