#!/usr/bin/env php /* * Copyright (c) 2010 The WebM project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ "" */ $geshi_path = dirname($argv[0])."/includes/geshi/geshi/"; // Language files $tmp_token = ''; // Include prerequisites or exit if(!include_once('includes/PHP-Markdown-Extra-1.2.3/markdown.php')) die("Cannot load Markdown transformer.\n"); if(!include_once('includes/PHP-SmartyPants-1.5.1e/smartypants.php')) die("Cannot load SmartyPants transformer.\n"); if(!include_once('includes/geshi/geshi.php')) die("Cannot load GeSHi transformer.\n"); // ASCIIMathPHP? // HTML::Toc? // Tidy? // Prince? /** * Generate XHTML body * */ $page_body = file_get_contents('php://stdin'); // Transform any MathML expressions in the body text $regexp = '/\[\[(.*?)\]\]/'; // Double square bracket delimiters $page_body = preg_replace_callback($regexp, 'ASCIIMathPHPCallback', $page_body); // Fix ASCIIMathPHP's output $page_body = fix_asciiMath($page_body); // Wrap block-style elements in

, since Markdown doesn't. $page_body = preg_replace('/\n()\n/', '

$1

', $page_body); // Transform the body text to HTML $page_body = Markdown($page_body); // Preprocess code blocks // Decode XML entities. GeSHi doesn't anticipate that // Markdown has already done this. $regexp = '|
(.*?)<\/code><\/pre>|si';
while (preg_match($regexp, $page_body, $matches) > 0)
{
  // Replace 1st match with token
  $page_body = preg_replace($regexp, $tmp_token, $page_body, 1);
  $block_new = $matches[1];
  // Un-encode ampersand entities
  $block_new = decode_markdown($block_new);
  // Replace token with revised string
  $page_body = preg_replace("|$tmp_token|", '
'.$block_new.'
', $page_body); } // Run GeSHi over code blocks $regexp = '|
(.*?)<\/div>|si'; $language = 'c'; while (preg_match($regexp, $page_body, $matches)) { $geshi = new GeSHi($matches[1], $language); $geshi->set_language_path($geshi_path); $block_new = $geshi->parse_code(); // Strip annoying final newline $block_new = preg_replace('|\n <\/pre>|', '
' , $block_new); // Remove style attribute (TODO: Research this in GeSHi) $block_new = preg_replace('| style="font-family:monospace;"|', '' , $block_new); $page_body = preg_replace($regexp, $block_new, $page_body, 1); unset($geshi); // Clean up } unset($block_new); // Clean up // Apply typographic flourishes $page_body = SmartyPants($page_body); /** * Generate Doxygen Body * */ $page_id=(isset($argv[1]))?$argv[1]:""; $page_desc=(isset($argv[2]))?$argv[2]:""; print "/*!\\page ".$page_id." ".$page_desc."\n\\htmlonly\n"; print $page_body; print "\\endhtmlonly\n*/\n"; // --------------------------------------------------------- /** * decode_markdown() * * Markdown encodes '&', '<' and '>' in detected code * blocks, as a convenience. This will restore the * encoded entities to ordinary characters, since a * downstream transformer (like GeSHi) may not * anticipate this. * **********************************************************/ function decode_markdown($input) { $out = FALSE; $entities = array ('|&|' ,'|<|' ,'|>|' ); $characters = array ('&' ,'<' ,'>' ); $input = preg_replace($entities, $characters, $input); $out = $input; return $out; } /** * 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]); include('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.cfg.php'); require_once('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.class.php'); 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; }