Home | History | Annotate | Download | only in includes
      1 #!/usr/bin/env php
      2 <?php
      3 /**
      4  * vp8_doc_tools.php - Functions used when generating the
      5  *   On2 VP8 user documentation.
      6  *
      7  * Requirements
      8  *
      9  *   PHP Markdown Extra
     10  *   http://michelf.com/projects/php-markdown/extra/
     11  *
     12  *   PHP SmartyPants
     13  *   http://michelf.com/projects/php-smartypants/
     14  *
     15  *   GeSHI
     16  *   http://qbnz.com/highlighter/
     17  *
     18  *   ASCIIMathPHP
     19  *   http://tinyurl.com/asciimathphp
     20  *
     21  *   HTML::Toc
     22  *   http://search.cpan.org/~fvulto/HTML-Toc-0.91/Toc.pod
     23  *
     24  *
     25  * April 2009 - Lou Quillio <lou.quillio (at) on2.com>
     26  *
     27  **********************************************************/
     28 
     29 
     30 // Includes
     31 include_once('PHP-Markdown-Extra-1.2.3/markdown.php');
     32 include_once('PHP-SmartyPants-1.5.1e/smartypants.php');
     33 include_once('ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.cfg.php');
     34 require_once('ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.class.php');
     35 include_once('geshi/geshi.php');
     36 
     37 
     38 // Paths and Scripts
     39 $geshi_lang   = 'geshi/geshi/';       // GeSHi language files
     40 $toc_script     = './do_toc.pl';
     41 
     42 
     43 
     44 /**
     45  * ASCIIMathML parser
     46  * http://tinyurl.com/ASCIIMathPHP
     47  *
     48  * @PARAM mtch_arr array - Array of ASCIIMath expressions
     49  *   as returned by preg_replace_callback([pattern]). First
     50  *   dimension is the full matched string (with delimiter);
     51  *   2nd dimension is the undelimited contents (typically
     52  *   a capture group).
     53  *
     54  **********************************************************/
     55 
     56 function ASCIIMathPHPCallback($mtch_arr)
     57 {
     58   $txt = trim($mtch_arr[1]);
     59 
     60   static $asciimath;
     61 
     62   if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr);
     63 
     64   $math_attr_arr = array('displaystyle' => 'true');
     65 
     66   $asciimath->setExpr($txt);
     67   $asciimath->genMathML($math_attr_arr);
     68 
     69   return($asciimath->getMathML());
     70 }
     71 
     72 /**
     73  * fix_asciiMath()
     74  *
     75  * ASCIIMath pretty-prints its output, with linefeeds
     76  * and tabs. Causes unexpected behavior in some renderers.
     77  * This flattens <math> blocks.
     78  *
     79  * @PARAM page_body str - The <body> element of an
     80  * XHTML page to transform.
     81  *
     82  **********************************************************/
     83 
     84 function fix_asciiMath($page_body)
     85 {
     86   $out = FALSE;
     87 
     88   // Remove linefeeds and whitespace in <math> elements
     89   $tags_bad  = array('/(<math.*?>)\n*\s*/'
     90                     , '/(<mstyle.*?>)\n*\s*/'
     91                     , '/(<\/mstyle>)\n*\s*/'
     92                     , '/(<mrow.*?>)\n*\s*/'
     93                     , '/(<\/mrow>)\n*\s*/'
     94                     , '/(<mo.*?>)\n*\s*/'
     95                     , '/(<\/mo>)\n*\s*/'
     96                     , '/(<mi.*?>)\n*\s*/'
     97                     , '/(<\/mi>)\n*\s*/'
     98                     , '/(<mn.*?>)\n*\s*/'
     99                     , '/(<\/mn>)\n*\s*/'
    100                     , '/(<mtext.*?>)\n*\s*/'
    101                     , '/(<\/mtext>)\n*\s*/'
    102                     , '/(<msqrt.*?>)\n*\s*/'
    103                     , '/(<\/msqrt>)\n*\s*/'
    104                     , '/(<mfrac.*?>)\n*\s*/'
    105                     , '/(<\/mfrac>)\n*\s*/'
    106                     );
    107   $tags_good = array( '$1'
    108                     , '$1'
    109                     , '$1'
    110                     , '$1'
    111                     , '$1'
    112                     , '$1'
    113                     , '$1'
    114                     , '$1'
    115                     , '$1'
    116                     , '$1'
    117                     , '$1'
    118                     , '$1'
    119                     , '$1'
    120                     , '$1'
    121                     , '$1'
    122                     , '$1'
    123                     , '$1'
    124                     );
    125   $out = preg_replace($tags_bad, $tags_good, $page_body);
    126 
    127   return $out;
    128 
    129 }
    130 
    131 /**
    132  * do_geshi() - Performs GeSHi transforms on XHTML blobs
    133  *
    134  * @param $blob str  - The blob to transform
    135  * @param $open str  - Opening expression to match
    136  * @param $close str - Closing expression to match
    137  * @param $lang str  - Language file to use
    138  *
    139  **********************************************************/
    140 
    141 function do_geshi($blob, $open = '<pre>',
    142                     $close = '</pre>', $lang = 'c')
    143 {
    144   $out = FALSE;
    145   $regexp = '|' . $open . '(.*?)' . $close . '|si';
    146   echo $regexp . "\n\n";
    147 
    148   while (preg_match($regexp, $blob, $matches))
    149   {
    150     $geshi = new GeSHi($matches[1], $lang);
    151     $geshi->set_language_path($geshi_lang);
    152     $blob_new = $geshi->parse_code();
    153     // Strip annoying final <br />
    154     $blob_new  = preg_replace('/\n&nbsp;<\/pre>/', '</pre>' , $blob_new);
    155     // Fix annoying GeSHI-injected attributes
    156     $blob_new  = preg_replace('/<pre.*>/i', '<pre>' , $blob_new);
    157     $blob  = preg_replace($regexp, $blob_new, $blob, 1);
    158     unset($geshi);
    159   }
    160 
    161   return $out;
    162 
    163 }
    164 
    165 
    166 
    167 
    168 /**
    169  * prep_dd_codeblocks()
    170  *
    171  * I'm _so_ not proud of this, but don't have time to
    172  * write a proper regex.
    173  *
    174  * @TODO - Write that regex.
    175  *
    176  **********************************************************/
    177 /*
    178 function prep_dd_codeblocks($page_body)
    179 {
    180   $out = FALSE;
    181   $toggle = 0;
    182   $regexp = '/~{3,}/';
    183 
    184   while (preg_match($regexp, $page_body))
    185   {
    186     if ($toggle == 0)
    187     {
    188       $regexp = '/:\s*~{3,}\s*\n/';
    189       $page_body = preg_replace($regexp, ': <pre><code>', $page_body, 1);
    190       $toggle = 1;
    191     }
    192     else
    193     {
    194       $regexp = '/\n\s*~{3,}/';
    195       $page_body = preg_replace($regexp, '</code></pre>', $page_body, 1);
    196       $toggle = 0;
    197     }
    198   }
    199 
    200   // One more time
    201   $regexp = '/\n\s*~{3,}/';
    202   $page_body = preg_replace($regexp, '</code></pre>', $page_body, 1);
    203   $out = $page_body;
    204 
    205   return $out;
    206 }
    207 */
    208