PHP Classes

File: markdown_preview.php

Recommend this page to a friend!
  Classes of Manuel Lemos   PHP Markdown Parser   markdown_preview.php   Download  
File: markdown_preview.php
Role: Application script
Content type: text/plain
Description: Markdown render preview form script
Class: PHP Markdown Parser
Parse Markdown documents and generate HTML tags
Author: By
Last change: Adjusted CSS styles.
Date: 3 years ago
Size: 4,360 bytes
 

Contents

Class file image Download
<?php
/*
 * markdown_preview.php
 *
 * @(#) $Id: markdown_preview.php,v 1.1 2016/02/03 06:23:45 mlemos Exp $
 *
 */

   
require('forms.php');
    require(
'form_layout_vertical.php');
    require(
'markup_parser.php');
    require(
'markdown_parser.php');

   
$form = new form_class;
   
$form->NAME = 'preview_form';
   
$form->METHOD = 'POST';
   
$form->ACTION = '';
   
$form->debug = 'trigger_error';
   
$form->ShowAllErrors = 1;
   
$form->InvalidCLASS = 'invalid';
   
$form->AddInput(array(
       
'TYPE'=>'textarea',
       
'NAME'=>'data',
       
'ID'=>'data',
       
'ROWS'=>10,
       
'COLS'=>60,
       
'ValidateAsNotEmpty'=>1,
       
'ValidationErrorMessage'=>
           
'It was not specified any markdown to preview.',
       
'LABEL'=>'<u>D</u>ata',
       
'ACCESSKEY'=>'D'
   
));
   
$form->AddInput(array(
       
'TYPE'=>'textarea',
       
'NAME'=>'html',
       
'ID'=>'html',
       
'ROWS'=>10,
       
'COLS'=>60,
       
'LABEL'=>'HTML',
    ));
   
$form->AddInput(array(
       
'TYPE'=>'submit',
       
'NAME'=>'preview',
       
'ID'=>'preview',
       
'VALUE'=>'Preview',
       
'ACCESSKEY'=>'P'
   
));
   
$error = $warnings = $output = '';
   
$form->LoadInputValues($form->WasSubmitted('preview'));
   
$verify=array();
    if(
$form->WasSubmitted('preview'))
    {
        if((
$error_message = $form->Validate($verify)) === '')
        {
           
$markdown = new markdown_parser_class;
           
$parameters=array(
               
'Data'=>$form->GetInputValue('data'),
            );
            if((
$success = $markdown->StartParsing($parameters)))
            {
               
$markup = new markup_parser_class;
                do
                {
                    if(!(
$success = $markdown->Parse($end, $elements)))
                        break;
                   
$te = count($elements);
                    for(
$e = 0; $e < $te; ++$e)
                    {
                        if(!(
$success = $markup->RewriteElement($elements[$e], $html)))
                            break;
                       
$output .= $html;
                    }
                }
                while(!
$end);
                if(
$success)
                   
$success = $markdown->FinishParsing();
               
$done = 1;
            }
            if(
$success)
               
$form->SetInputValue('html', $output);
            else
            {
               
$error = $markdown->error.' at position '.$markdown->error_position;
                if(
$markdown->track_lines
               
&& $markdown->GetPositionLine($markdown->error_position, $line, $column))
                   
$error .= ' line '.$line.' column '.$column;
            }
            for(
$warning = 0, Reset($markdown->warnings); $warning < count($markdown->warnings); Next($markdown->warnings), $warning++)
            {
               
$w = Key($markdown->warnings);
               
$warnings .= $markdown->warnings[$w].' at position '.$w;
                if(
$markdown->track_lines
               
&& $markdown->GetPositionLine($w, $line, $column))
                   
$warnings .= ' line '.$line.' column '.$column;
               
$warnings .= "\n";
            }
        }
        else
        {
           
$done = 0;
           
$error_message = HtmlEntities($error_message);
        }
    }
    else
    {
       
$error_message = '';
       
$done = 0;
    }
   
$form->AddInput(array(
       
'ID'=>'layout',
       
'NAME'=>'layout',
       
'TYPE'=>'custom',
       
'CustomClass'=>'form_layout_vertical_class',
       
'Inputs'=>array(
           
'data',
           
'error',
           
'warnings',
           
'html',
           
'rendered',
           
'preview',
        ),
       
'Data'=>array(
           
'error'=>'<tr><td>Error:</td><td class="invalid">'.HtmlSpecialChars($error).'</td></tr>',
           
'warnings'=>'<tr><td>Warnings:</td><td class="invalid">'.nl2br(HtmlSpecialChars($warnings)).'</td></tr>',
           
'rendered'=>'<tr><td>Rendered:</td><td class="html">'.$output.'</td></tr>'
       
),
       
'Properties'=>array(
           
'rendered'=>array(
               
'Visible'=>$done,
            ),
           
'error'=>array(
               
'Visible'=>(strlen($error) && $done),
            ),
           
'warnings'=>array(
               
'Visible'=>(strlen($warnings) && $done),
            ),
           
'rendered'=>array(
               
'Visible'=>(strlen($output) && $done),
            ),
        ),
       
'InvalidMark'=>'[Verify]',
    ));

    if(!
$done)
    {
        if(
strlen($error_message))
        {
           
Reset($verify);
           
$focus=Key($verify);
        }
        else
           
$focus='data';
       
$form->ConnectFormToInput($focus, 'ONLOAD', 'Focus', array());
    }

   
$onload=HtmlSpecialChars($form->PageLoad());

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Markdown Preview</title>
<style type="text/css"><!--
.invalid { border-color: #ff0000; background-color: #ffcccc }
.html { background-color: #ffffff ; border-width: 1px; border-style: solid; padding: 2px; margin: 1px; }
body { background-color: #cccccc; }
.center { text-align: center; }
// --></style>
</head>
<body onload="<?php echo $onload; ?>">
<h1 class="center">Markdown Preview</h1>
<div align="center" style="max-width: 60">
<?php
    $form
->StartLayoutCapture();
   
$form->AddInputPart('layout');
   
$form->EndLayoutCapture();
   
$form->DisplayOutput();
?>
</div>
</body>
</html>