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    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 388 All time: 6,674 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  

 

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

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,674
This week:50Up