#!/usr/bin/env php * **********************************************************/ // Includes include_once('PHP-Markdown-Extra-1.2.3/markdown.php'); include_once('PHP-SmartyPants-1.5.1e/smartypants.php'); include_once('ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.cfg.php'); require_once('ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.class.php'); include_once('geshi/geshi.php'); // Paths and Scripts $geshi_lang = 'geshi/geshi/'; // GeSHi language files $toc_script = './do_toc.pl'; /** * ASCIIMathML parser * http://tinyurl.com/ASCIIMathPHP * * @PARAM mtch_arr array - Array of ASCIIMath expressions * as returned by preg_replace_callback([pattern]). First * dimension is the full matched string (with delimiter); * 2nd dimension is the undelimited contents (typically * a capture group). * **********************************************************/ function ASCIIMathPHPCallback($mtch_arr) { $txt = trim($mtch_arr[1]); static $asciimath; if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr); $math_attr_arr = array('displaystyle' => 'true'); $asciimath->setExpr($txt); $asciimath->genMathML($math_attr_arr); return($asciimath->getMathML()); } /** * fix_asciiMath() * * ASCIIMath pretty-prints its output, with linefeeds * and tabs. Causes unexpected behavior in some renderers. * This flattens blocks. * * @PARAM page_body str - The element of an * XHTML page to transform. * **********************************************************/ function fix_asciiMath($page_body) { $out = FALSE; // Remove linefeeds and whitespace in elements $tags_bad = array('/()\n*\s*/' , '/()\n*\s*/' , '/(<\/mstyle>)\n*\s*/' , '/()\n*\s*/' , '/(<\/mrow>)\n*\s*/' , '/()\n*\s*/' , '/(<\/mo>)\n*\s*/' , '/()\n*\s*/' , '/(<\/mi>)\n*\s*/' , '/()\n*\s*/' , '/(<\/mn>)\n*\s*/' , '/()\n*\s*/' , '/(<\/mtext>)\n*\s*/' , '/()\n*\s*/' , '/(<\/msqrt>)\n*\s*/' , '/()\n*\s*/' , '/(<\/mfrac>)\n*\s*/' ); $tags_good = array( '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' , '$1' ); $out = preg_replace($tags_bad, $tags_good, $page_body); return $out; } /** * do_geshi() - Performs GeSHi transforms on XHTML blobs * * @param $blob str - The blob to transform * @param $open str - Opening expression to match * @param $close str - Closing expression to match * @param $lang str - Language file to use * **********************************************************/ function do_geshi($blob, $open = '
',
                    $close = '
', $lang = 'c') { $out = FALSE; $regexp = '|' . $open . '(.*?)' . $close . '|si'; echo $regexp . "\n\n"; while (preg_match($regexp, $blob, $matches)) { $geshi = new GeSHi($matches[1], $lang); $geshi->set_language_path($geshi_lang); $blob_new = $geshi->parse_code(); // Strip annoying final
$blob_new = preg_replace('/\n <\/pre>/', '' , $blob_new); // Fix annoying GeSHI-injected attributes $blob_new = preg_replace('//i', '
' , $blob_new);
    $blob  = preg_replace($regexp, $blob_new, $blob, 1);
    unset($geshi);
  }

  return $out;

}




/**
 * prep_dd_codeblocks()
 *
 * I'm _so_ not proud of this, but don't have time to
 * write a proper regex.
 *
 * @TODO - Write that regex.
 *
 **********************************************************/
/*
function prep_dd_codeblocks($page_body)
{
  $out = FALSE;
  $toggle = 0;
  $regexp = '/~{3,}/';

  while (preg_match($regexp, $page_body))
  {
    if ($toggle == 0)
    {
      $regexp = '/:\s*~{3,}\s*\n/';
      $page_body = preg_replace($regexp, ': 
', $page_body, 1);
      $toggle = 1;
    }
    else
    {
      $regexp = '/\n\s*~{3,}/';
      $page_body = preg_replace($regexp, '
', $page_body, 1); $toggle = 0; } } // One more time $regexp = '/\n\s*~{3,}/'; $page_body = preg_replace($regexp, '
', $page_body, 1); $out = $page_body; return $out; } */