summaryrefslogtreecommitdiff
path: root/examples/gen_example_doxy.php
blob: 701bbd30d72f2ef6017e9103376f2856ba7da8b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/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.
 */


<?php

/* This script converts markdown to doxygen htmlonly syntax, nesting the
 * content inside a \page. It expects input on stdin and outputs on stdout.
 *
 * Usage: gen_example_doxy.php <page_identifier> "<page description>"
 */


$geshi_path = dirname($argv[0])."/includes/geshi/geshi/"; // Language files
$tmp_token  = '<!-- I wanna rock you, Chaka Khan -->';

// 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 <math> elements in <p>, since Markdown doesn't.
$page_body = preg_replace('/\n(<math.*<\/math>)\n/', '<p class="eq_para">$1</p>', $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 = '|<pre><code>(.*?)<\/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|", '<div class="codeblock">'.$block_new.'</div>', $page_body);
}

// Run GeSHi over code blocks
$regexp   = '|<div class="codeblock">(.*?)<\/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&nbsp;<\/pre>|', '</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 ('|&amp;|'
                      ,'|&lt;|'
                      ,'|&gt;|'
                      );
  $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 <math> blocks.
 *
 * @PARAM page_body str - The <body> element of an
 * XHTML page to transform.
 *
 **********************************************************/

function fix_asciiMath($page_body)
{
  $out = FALSE;

  // Remove linefeeds and whitespace in <math> elements
  $tags_bad  = array('/(<math.*?>)\n*\s*/'
                    , '/(<mstyle.*?>)\n*\s*/'
                    , '/(<\/mstyle>)\n*\s*/'
                    , '/(<mrow.*?>)\n*\s*/'
                    , '/(<\/mrow>)\n*\s*/'
                    , '/(<mo.*?>)\n*\s*/'
                    , '/(<\/mo>)\n*\s*/'
                    , '/(<mi.*?>)\n*\s*/'
                    , '/(<\/mi>)\n*\s*/'
                    , '/(<mn.*?>)\n*\s*/'
                    , '/(<\/mn>)\n*\s*/'
                    , '/(<mtext.*?>)\n*\s*/'
                    , '/(<\/mtext>)\n*\s*/'
                    , '/(<msqrt.*?>)\n*\s*/'
                    , '/(<\/msqrt>)\n*\s*/'
                    , '/(<mfrac.*?>)\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;

}