Initial WebM release
[platform/upstream/libvpx.git] / examples / gen_example_doxy.php
1 #!/usr/bin/env php
2 /*
3  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4  *
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.
9  */
10
11
12 <?php
13
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.
16  *
17  * Usage: gen_example_doxy.php <page_identifier> "<page description>"
18  */
19
20
21 $geshi_path = dirname($argv[0])."/includes/geshi/geshi/"; // Language files
22 $tmp_token  = '<!-- I wanna rock you, Chaka Khan -->';
23
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");
31 // ASCIIMathPHP?
32 // HTML::Toc?
33 // Tidy?
34 // Prince?
35
36 /**
37  *  Generate XHTML body
38  *
39  */
40
41 $page_body = file_get_contents('php://stdin');
42
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);
46
47 // Fix ASCIIMathPHP's output
48 $page_body = fix_asciiMath($page_body);
49
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);
52
53 // Transform the body text to HTML
54 $page_body = Markdown($page_body);
55
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)
61 {
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);
69 }
70
71 // Run GeSHi over code blocks
72 $regexp   = '|<div class="codeblock">(.*?)<\/div>|si';
73 $language = 'c';
74
75 while (preg_match($regexp, $page_body, $matches))
76 {
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&nbsp;<\/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
86 }
87 unset($block_new);  // Clean up
88
89 // Apply typographic flourishes
90 $page_body = SmartyPants($page_body);
91
92
93 /**
94  *  Generate Doxygen Body
95  *
96  */
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";
100 print $page_body;
101 print "\\endhtmlonly\n*/\n";
102
103 // ---------------------------------------------------------
104
105 /**
106  * decode_markdown()
107  *
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
112  * anticipate this.
113  *
114  **********************************************************/
115
116 function decode_markdown($input)
117 {
118   $out = FALSE;
119
120   $entities   = array ('|&amp;|'
121                       ,'|&lt;|'
122                       ,'|&gt;|'
123                       );
124   $characters = array ('&'
125                       ,'<'
126                       ,'>'
127                       );
128   $input = preg_replace($entities, $characters, $input);
129   $out = $input;
130
131   return $out;
132 }
133
134
135 /**
136  * ASCIIMathML parser
137  * http://tinyurl.com/ASCIIMathPHP
138  *
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
143  *   a capture group).
144  *
145  **********************************************************/
146
147 function ASCIIMathPHPCallback($mtch_arr)
148 {
149   $txt = trim($mtch_arr[1]);
150
151   include('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.cfg.php');
152   require_once('includes/ASCIIMathPHP-2.0/ASCIIMathPHP-2.0.class.php');
153
154   static $asciimath;
155
156   if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr);
157
158   $math_attr_arr = array('displaystyle' => 'true');
159
160   $asciimath->setExpr($txt);
161   $asciimath->genMathML($math_attr_arr);
162
163   return($asciimath->getMathML());
164 }
165
166 /**
167  * fix_asciiMath()
168  *
169  * ASCIIMath pretty-prints its output, with linefeeds
170  * and tabs. Causes unexpected behavior in some renderers.
171  * This flattens <math> blocks.
172  *
173  * @PARAM page_body str - The <body> element of an
174  * XHTML page to transform.
175  *
176  **********************************************************/
177
178 function fix_asciiMath($page_body)
179 {
180   $out = FALSE;
181
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*/'
189                     , '/(<\/mo>)\n*\s*/'
190                     , '/(<mi.*?>)\n*\s*/'
191                     , '/(<\/mi>)\n*\s*/'
192                     , '/(<mn.*?>)\n*\s*/'
193                     , '/(<\/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*/'
200                     );
201   $tags_good = array( '$1'
202                     , '$1'
203                     , '$1'
204                     , '$1'
205                     , '$1'
206                     , '$1'
207                     , '$1'
208                     , '$1'
209                     , '$1'
210                     , '$1'
211                     , '$1'
212                     , '$1'
213                     , '$1'
214                     , '$1'
215                     , '$1'
216                     , '$1'
217                     , '$1'
218                     );
219   $out = preg_replace($tags_bad, $tags_good, $page_body);
220
221   return $out;
222
223 }