PHP Classes

File: markdown_to_html.php

Recommend this page to a friend!
  Classes of Manuel Lemos   PHP Markdown Parser   markdown_to_html.php   Download  
File: markdown_to_html.php
Role: Example script
Content type: text/plain
Description: Example of how to generate HTML from markdown parsing results
Class: PHP Markdown Parser
Parse Markdown documents and generate HTML tags
Author: By
Last change: Added the original test cases from John Gruber Markdown test suite.
Date: 10 years ago
Size: 2,461 bytes
 

Contents

Class file image Download
<?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>';
?>