3 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
5 * Use of this source code is governed by a BSD-style license
6 * that can be found in the LICENSE file in the root of the source
7 * tree. An additional intellectual property rights grant can be found
8 * in the file PATENTS. All contributing project authors may
9 * be found in the AUTHORS file in the root of the source tree.
15 /* This script converts markdown to doxygen htmlonly syntax, nesting the
16 * content inside a \page. It expects input on stdin and outputs on stdout.
18 * Usage: gen_example_doxy.php <page_identifier> "<page description>"
22 $geshi_path = dirname($argv[0])."/includes/geshi/geshi/"; // Language files
23 $tmp_token = '<!-- I wanna rock you, Chaka Khan -->';
25 // Include prerequisites or exit
26 if(!include_once('includes/PHP-Markdown-Extra-1.2.3/markdown.php'))
27 die("Cannot load Markdown transformer.\n");
28 if(!include_once('includes/PHP-SmartyPants-1.5.1e/smartypants.php'))
29 die("Cannot load SmartyPants transformer.\n");
30 if(!include_once('includes/geshi/geshi.php'))
31 die("Cannot load GeSHi transformer.\n");
42 $page_body = file_get_contents('php://stdin');
44 // Transform any MathML expressions in the body text
45 $regexp = '/\[\[(.*?)\]\]/'; // Double square bracket delimiters
46 $page_body = preg_replace_callback($regexp, 'ASCIIMathPHPCallback', $page_body);
48 // Fix ASCIIMathPHP's output
49 $page_body = fix_asciiMath($page_body);
51 // Wrap block-style <math> elements in <p>, since Markdown doesn't.
52 $page_body = preg_replace('/\n(<math.*<\/math>)\n/', '<p class="eq_para">$1</p>', $page_body);
54 // Transform the body text to HTML
55 $page_body = Markdown($page_body);
57 // Preprocess code blocks
58 // Decode XML entities. GeSHi doesn't anticipate that
59 // Markdown has already done this.
60 $regexp = '|<pre><code>(.*?)<\/code><\/pre>|si';
61 while (preg_match($regexp, $page_body, $matches) > 0)
63 // Replace 1st match with token
64 $page_body = preg_replace($regexp, $tmp_token, $page_body, 1);
65 $block_new = $matches[1];
66 // Un-encode ampersand entities
67 $block_new = decode_markdown($block_new);
68 // Replace token with revised string
69 $page_body = preg_replace("|$tmp_token|", '<div class="codeblock">'.$block_new.'</div>', $page_body);
72 // Run GeSHi over code blocks
73 $regexp = '|<div class="codeblock">(.*?)<\/div>|si';
76 while (preg_match($regexp, $page_body, $matches))
78 $geshi = new GeSHi($matches[1], $language);
79 $geshi->set_language_path($geshi_path);
80 $block_new = $geshi->parse_code();
81 // Strip annoying final newline
82 $block_new = preg_replace('|\n <\/pre>|', '</pre>' , $block_new);
83 // Remove style attribute (TODO: Research this in GeSHi)
84 $block_new = preg_replace('| style="font-family:monospace;"|', '' , $block_new);
85 $page_body = preg_replace($regexp, $block_new, $page_body, 1);
86 unset($geshi); // Clean up
88 unset($block_new); // Clean up
90 // Apply typographic flourishes
91 $page_body = SmartyPants($page_body);
95 * Generate Doxygen Body
98 $page_id=(isset($argv[1]))?
$argv[1]:"";
99 $page_desc=(isset($argv[2]))?
$argv[2]:"";
100 print "/*!\\page ".$page_id." ".$page_desc."\n\\htmlonly\n";
102 print "\\endhtmlonly\n*/\n";
104 // ---------------------------------------------------------
109 * Markdown encodes '&', '<' and '>' in detected code
110 * blocks, as a convenience. This will restore the
111 * encoded entities to ordinary characters, since a
112 * downstream transformer (like GeSHi) may not
115 **********************************************************/
117 function decode_markdown($input)
121 $entities = array ('|&|'
125 $characters = array ('&'
129 $input = preg_replace($entities, $characters, $input);
138 * http://tinyurl.com/ASCIIMathPHP
140 * @PARAM mtch_arr array - Array of ASCIIMath expressions
141 * as returned by preg_replace_callback([pattern]). First
142 * dimension is the full matched string (with delimiter);
143 * 2nd dimension is the undelimited contents (typically
146 **********************************************************/
148 function ASCIIMathPHPCallback($mtch_arr)
150 $txt = trim($mtch_arr[1]);
152 include('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.cfg.php');
153 require_once('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.class.php');
157 if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr);
159 $math_attr_arr = array('displaystyle' => 'true');
161 $asciimath->setExpr($txt);
162 $asciimath->genMathML($math_attr_arr);
164 return($asciimath->getMathML());
170 * ASCIIMath pretty-prints its output, with linefeeds
171 * and tabs. Causes unexpected behavior in some renderers.
172 * This flattens <math> blocks.
174 * @PARAM page_body str - The <body> element of an
175 * XHTML page to transform.
177 **********************************************************/
179 function fix_asciiMath($page_body)
183 // Remove linefeeds and whitespace in <math> elements
184 $tags_bad = array('/(<math.*?>)\n*\s*/'
185 , '/(<mstyle.*?>)\n*\s*/'
186 , '/(<\/mstyle>)\n*\s*/'
187 , '/(<mrow.*?>)\n*\s*/'
188 , '/(<\/mrow>)\n*\s*/'
189 , '/(<mo.*?>)\n*\s*/'
191 , '/(<mi.*?>)\n*\s*/'
193 , '/(<mn.*?>)\n*\s*/'
195 , '/(<mtext.*?>)\n*\s*/'
196 , '/(<\/mtext>)\n*\s*/'
197 , '/(<msqrt.*?>)\n*\s*/'
198 , '/(<\/msqrt>)\n*\s*/'
199 , '/(<mfrac.*?>)\n*\s*/'
200 , '/(<\/mfrac>)\n*\s*/'
202 $tags_good = array( '$1'
220 $out = preg_replace($tags_bad, $tags_good, $page_body);