3 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
5 * Use of this source code is governed by a BSD-style license and patent
6 * grant that can be found in the LICENSE file in the root of the source
7 * tree. All contributing project authors may be found in the AUTHORS
8 * file in the root of the source tree.
14 /* This script converts markdown to doxygen htmlonly syntax, nesting the
15 * content inside a \page. It expects input on stdin and outputs on stdout.
17 * Usage: gen_example_doxy.php <page_identifier> "<page description>"
21 $geshi_path = dirname($argv[0])."/includes/geshi/geshi/"; // Language files
22 $tmp_token = '<!-- I wanna rock you, Chaka Khan -->';
24 // Include prerequisites or exit
25 if(!include_once('includes/PHP-Markdown-Extra-1.2.3/markdown.php'))
26 die("Cannot load Markdown transformer.\n");
27 if(!include_once('includes/PHP-SmartyPants-1.5.1e/smartypants.php'))
28 die("Cannot load SmartyPants transformer.\n");
29 if(!include_once('includes/geshi/geshi.php'))
30 die("Cannot load GeSHi transformer.\n");
41 $page_body = file_get_contents('php://stdin');
43 // Transform any MathML expressions in the body text
44 $regexp = '/\[\[(.*?)\]\]/'; // Double square bracket delimiters
45 $page_body = preg_replace_callback($regexp, 'ASCIIMathPHPCallback', $page_body);
47 // Fix ASCIIMathPHP's output
48 $page_body = fix_asciiMath($page_body);
50 // Wrap block-style <math> elements in <p>, since Markdown doesn't.
51 $page_body = preg_replace('/\n(<math.*<\/math>)\n/', '<p class="eq_para">$1</p>', $page_body);
53 // Transform the body text to HTML
54 $page_body = Markdown($page_body);
56 // Preprocess code blocks
57 // Decode XML entities. GeSHi doesn't anticipate that
58 // Markdown has already done this.
59 $regexp = '|<pre><code>(.*?)<\/code><\/pre>|si';
60 while (preg_match($regexp, $page_body, $matches) > 0)
62 // Replace 1st match with token
63 $page_body = preg_replace($regexp, $tmp_token, $page_body, 1);
64 $block_new = $matches[1];
65 // Un-encode ampersand entities
66 $block_new = decode_markdown($block_new);
67 // Replace token with revised string
68 $page_body = preg_replace("|$tmp_token|", '<div class="codeblock">'.$block_new.'</div>', $page_body);
71 // Run GeSHi over code blocks
72 $regexp = '|<div class="codeblock">(.*?)<\/div>|si';
75 while (preg_match($regexp, $page_body, $matches))
77 $geshi = new GeSHi($matches[1], $language);
78 $geshi->set_language_path($geshi_path);
79 $block_new = $geshi->parse_code();
80 // Strip annoying final newline
81 $block_new = preg_replace('|\n <\/pre>|', '</pre>' , $block_new);
82 // Remove style attribute (TODO: Research this in GeSHi)
83 $block_new = preg_replace('| style="font-family:monospace;"|', '' , $block_new);
84 $page_body = preg_replace($regexp, $block_new, $page_body, 1);
85 unset($geshi); // Clean up
87 unset($block_new); // Clean up
89 // Apply typographic flourishes
90 $page_body = SmartyPants($page_body);
94 * Generate Doxygen Body
97 $page_id=(isset($argv[1]))?$argv[1]:"";
98 $page_desc=(isset($argv[2]))?$argv[2]:"";
99 print "/*!\\page ".$page_id." ".$page_desc."\n\\htmlonly\n";
101 print "\\endhtmlonly\n*/\n";
103 // ---------------------------------------------------------
108 * Markdown encodes '&', '<' and '>' in detected code
109 * blocks, as a convenience. This will restore the
110 * encoded entities to ordinary characters, since a
111 * downstream transformer (like GeSHi) may not
114 **********************************************************/
116 function decode_markdown($input)
120 $entities = array ('|&|'
124 $characters = array ('&'
128 $input = preg_replace($entities, $characters, $input);
137 * http://tinyurl.com/ASCIIMathPHP
139 * @PARAM mtch_arr array - Array of ASCIIMath expressions
140 * as returned by preg_replace_callback([pattern]). First
141 * dimension is the full matched string (with delimiter);
142 * 2nd dimension is the undelimited contents (typically
145 **********************************************************/
147 function ASCIIMathPHPCallback($mtch_arr)
149 $txt = trim($mtch_arr[1]);
151 include('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.cfg.php');
152 require_once('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.class.php');
156 if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr);
158 $math_attr_arr = array('displaystyle' => 'true');
160 $asciimath->setExpr($txt);
161 $asciimath->genMathML($math_attr_arr);
163 return($asciimath->getMathML());
169 * ASCIIMath pretty-prints its output, with linefeeds
170 * and tabs. Causes unexpected behavior in some renderers.
171 * This flattens <math> blocks.
173 * @PARAM page_body str - The <body> element of an
174 * XHTML page to transform.
176 **********************************************************/
178 function fix_asciiMath($page_body)
182 // Remove linefeeds and whitespace in <math> elements
183 $tags_bad = array('/(<math.*?>)\n*\s*/'
184 , '/(<mstyle.*?>)\n*\s*/'
185 , '/(<\/mstyle>)\n*\s*/'
186 , '/(<mrow.*?>)\n*\s*/'
187 , '/(<\/mrow>)\n*\s*/'
188 , '/(<mo.*?>)\n*\s*/'
190 , '/(<mi.*?>)\n*\s*/'
192 , '/(<mn.*?>)\n*\s*/'
194 , '/(<mtext.*?>)\n*\s*/'
195 , '/(<\/mtext>)\n*\s*/'
196 , '/(<msqrt.*?>)\n*\s*/'
197 , '/(<\/msqrt>)\n*\s*/'
198 , '/(<mfrac.*?>)\n*\s*/'
199 , '/(<\/mfrac>)\n*\s*/'
201 $tags_good = array( '$1'
219 $out = preg_replace($tags_bad, $tags_good, $page_body);