Your Ad Here

Posted By

chilicuil on 09/23/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nerdfiles


parser2.php


 / Published in: PHP
 

  1. <?php
  2.  
  3. require_once('XML/Parser.php');
  4.  
  5.  
  6. define('OUT_PATH', 'out/');
  7. define('TAGS_PATH', 'out/tags');
  8.  
  9. $entities = array(
  10. '&lt;' => '<',
  11. '&gt;' => '>',
  12. '&amp;' => '&',
  13. '&quot;' => '"',
  14. '&apos;' => '"',
  15. );
  16. $tags = array();
  17.  
  18. function load_entities($path)
  19. {
  20. global $entities;
  21. $data = file_get_contents($path);
  22. preg_match_all('/<!ENTITY\s+(\S+)\s+(["\'])(.*?)\\2>/sm', $data, $regs, PREG_SET_ORDER);
  23. //print_r($regs);
  24. foreach ($regs as $r) {
  25. $entities['&' . $r[1] . ';'] = $r[3];
  26. }
  27. }
  28.  
  29. function array_top($arr)
  30. {
  31. $c = count($arr);
  32. if ($c == 0)
  33. return null;
  34. return $arr[$c-1];
  35. }
  36.  
  37. class Parser extends XML_Parser
  38. {
  39. var $_elStack = array();
  40. var $_cdata = '';
  41.  
  42. var $manual = array();
  43. var $paratext = '';
  44.  
  45. var $methodname = '';
  46. var $methodtype = '';
  47.  
  48. function Parser()
  49. {
  50. $this->folding = false;
  51. $this->XML_Parser();
  52. }
  53.  
  54. function format_function_ref($func)
  55. {
  56. return '|' . $func . '|';
  57. }
  58.  
  59. function format_param_ref($param)
  60. {
  61. return '{' . $param . '}';
  62. }
  63.  
  64. function format_text($text)
  65. {
  66. return wordwrap(
  67. preg_replace('/\s+/', ' ', trim($text)),
  68. 78
  69. );
  70. }
  71.  
  72. function format_tag($tag)
  73. {
  74. $tmp = sprintf("%78s", '*php:' . $tag . '*');
  75. $this->tag = '/^' . $tmp . '$/';
  76. return $tmp;
  77. }
  78.  
  79. function format_title($title, $desc)
  80. {
  81. return $this->format_text(preg_replace('/\s+/', ' ', trim($title . ' -- ' . $desc)));
  82. }
  83.  
  84. function format_help_tag($title)
  85. {
  86. global $tags;
  87. if (isset($tags[$title])) {
  88. $i = '#' . ++$tags[$title];
  89. }
  90. else {
  91. $tags[$title] = 1;
  92. $i = '';
  93. }
  94. return sprintf("%78s", '*' . $title . $i . '*');
  95. }
  96.  
  97. /*
  98.   function format_description($name, $desc)
  99.   {
  100.   $name = '*php:' . $name . '()*';
  101.   return sprintf("%78s\n%s", $name, wordwrap('' . trim(ucwords($desc)), 78));
  102.   }
  103.   */
  104.  
  105. function format_proto($type, $name, $param)
  106. {
  107. $text = ' ' . $type . ' ' . $name .'(';
  108. $opt = 0;
  109. $c = count($param);
  110. for ($i=0; $i<$c; $i++) {
  111. $p = $param[$i];
  112. if ($p[2]) {
  113. if ($i > 0) {
  114. $text .= ' ';
  115. }
  116. $text .= '[';
  117. $opt++;
  118. }
  119. if ($i > 0) {
  120. $text .= ', ';
  121. }
  122. $text .= $p[0] . ' ' . $p[1];
  123. }
  124. for ($i=0; $i<$opt; $i++) {
  125. $text .= ']';
  126. }
  127. $text .= ')~';
  128. return $text;
  129. }
  130.  
  131. function format_code($text)
  132. {
  133. /*
  134.   return ' ' . str_repeat(
  135.   '..', 34) . "\n : " .
  136.   preg_replace('/\r?\n/', "\n : ", trim($text));
  137.   */
  138. return preg_replace(
  139. '/^\s*<\?php\s*$/m',
  140. '<?php >',
  141. '/^\s+\?>/m',
  142. '?>',
  143. ' ' . trim(
  144. preg_replace(
  145. '/\r?\n/',
  146. "\n ",
  147. $text
  148. )
  149. )
  150. )
  151. );
  152. }
  153.  
  154. function startHandler($xp, $elem, &$attribs)
  155. {
  156. if ($this->_cdata) {
  157. $top = array_top($this->_elStack);
  158. $el = $top[0];
  159. $attr = $top[1];
  160. $this->handleElement($el, $attr, $this->_cdata, false);
  161. }
  162. array_push($this->_elStack, array($elem, $attribs));
  163. $this->_cdata = '';
  164. }
  165.  
  166. function endHandler($xp, $elem)
  167. {
  168. $top = array_top($this->_elStack);
  169. $el = $top[0];
  170. $attr = $top[1];
  171. $this->handleElement($el, $attr, $this->_cdata, true);
  172. array_pop($this->_elStack);
  173. $this->_cdata = '';
  174. }
  175.  
  176. function cdataHandler($xp, $data)
  177. {
  178. $this->_cdata .= $data;
  179. }
  180.  
  181. function inElement($name)
  182. {
  183. $c = count($this->_elStack);
  184. //print_r($this->_elStack);
  185.  
  186. for ($i=$c-1; $i>=0; $i--) {
  187. if ($this->_elStack[$i][0] == $name) {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193.  
  194. function handleElement($name, $attr, $data, $final)
  195. {
  196. if ($this->inElement('refnamediv')) {
  197. if ($name == 'refname') {
  198. $this->refname = trim($data);
  199. }
  200. elseif ($this->inElement('refpurpose')) {
  201. $this->refpurpose = isset($this->refpurpose) ? $this->refpurpose . $data : $data;
  202. }
  203. if (($name == 'refnamediv') && $final) {
  204. //$this->paragraphlist[] = $this->format_help_tag($this->refname);
  205. $this->paragraphlist[] = $this->format_title($this->refname, $this->refpurpose);
  206. }
  207. }
  208. elseif ($this->inElement('methodsynopsis')) {
  209. if ($this->inElement('methodparam')) {
  210. if ($name == 'type') {
  211. $this->paramtype = trim($data);
  212. }
  213. elseif ($name == 'parameter') {
  214. $this->paramname = trim($data);
  215. }
  216. elseif (($name == 'methodparam') && $final) {
  217. $this->paramlist[] = array(
  218. isset($this->paramtype) ? trim($this->paramtype) : '',
  219. trim($this->paramname),
  220. isset($attr['choice']) && ($attr['choice'] == 'opt')
  221. );
  222. }
  223. }
  224. elseif ($name == 'type') {
  225. $this->methodtype = trim($data);
  226. }
  227. elseif ($name == 'methodname') {
  228. $this->methodname = trim($data);
  229. }
  230. elseif (($name == 'methodsynopsis') && $final) {
  231. $this->proto = $this->format_proto(
  232. $this->methodtype,
  233. $this->methodname,
  234. isset($this->paramlist) ? $this->paramlist : array());
  235. $this->paragraphlist[] = $this->proto;
  236. }
  237. }
  238. elseif ($this->inElement('programlisting') || $this->inElement('screen') || $this->inElement('literallayout')) {
  239. if (!ctype_space($this->paratext)) {
  240. $this->paragraphlist[] = $this->format_text($this->paratext);
  241. $this->paratext = '';
  242. }
  243. $this->paragraphlist[] = $this->format_code($data);
  244. }
  245. elseif ($this->inElement('para') || $this->inElement('simpara') || $this->inElement('example')) {
  246. if ($name == 'function') {
  247. $this->paratext .= $this->format_function_ref($data);
  248. }
  249. elseif ($name == 'parameter') {
  250. $this->paratext .= $this->format_param_ref($data);
  251. }
  252. elseif (($name == 'para' || $name == 'simpara' || $name == 'example') && $final) {
  253. if (!ctype_space($data)) {
  254. $this->paratext .= $data;
  255. }
  256. if (!ctype_space($this->paratext))
  257. $this->paragraphlist[] = $this->format_text($this->paratext);
  258. $this->paratext = '';
  259. }
  260. else {
  261. $this->paratext .= $data;
  262. }
  263. }
  264. }
  265. }
  266.  
  267. function process_all($path)
  268. {
  269. if ($dir = opendir($path)) {
  270. while ($fname = readdir($dir)) {
  271. $fname2 = $path . '/' . $fname;
  272. if (is_dir($fname2) && ($fname != '.') && ($fname != '..') && ($fname != 'CVS')) {
  273. process_all($fname2);
  274. echo "recursing into $fname2\n";
  275. }
  276. elseif (preg_match('#[\/\\\\]\w+[\/\\\\]functions[\/\\\\]([a-zA-Z0-9\._-]+)\.xml$#', $fname2, $regs)) {
  277. process_file($fname2, OUT_PATH . $regs[1] . '.txt');
  278. }
  279. }
  280. closedir($dir);
  281. }
  282. }
  283.  
  284. function process_file($in, $out)
  285. {
  286. global $entities;
  287.  
  288. echo "processing $in\n";
  289. $p =& new Parser();
  290. $data = file_get_contents($in);
  291. $data = strtr($data, $entities);
  292. $data = strtr($data, $entities);
  293. /*
  294. $data = strtr($data, array(
  295. '&true;' => 'TRUE',
  296. '&false;' => 'FALSE',
  297. '&null;' => 'NULL',
  298. '&lt;' => '<',
  299. '&gt;' => '>',
  300. '&amp;' => '&',
  301. '&quot;' => '"',
  302. '&apos;' => '"',
  303. '&deg;' => '°',
  304. '&return.success;' => 'Returns TRUE on success or FALSE on failure.',
  305. '&warn.undocumented.func;' => '',
  306. '&note.gd.2;' => 'This function requires GD 2.0.1 or later.',
  307. '&php.ini;' => 'php.ini',
  308. '&url.mysql.docs.error;' => 'http://dev.mysql.com/doc/mysql/en/Error-returns.html',
  309. '&example.outputs;' => 'The above example will output something similar to:'
  310. ));
  311. */
  312.  
  313. $data = preg_replace('/\&([^;]+;)/', '&amp;\\1', $data);
  314.  
  315. $res = $p->setInputString($data);
  316. $res = $p->parse();
  317.  
  318. $fp = fopen($out, 'wb') or die("Could not open $out!");
  319.  
  320. fwrite($fp, implode("\n\n", $p->paragraphlist));
  321.  
  322. fwrite($fp, "\n\nvim:ft=help:\n");
  323. fclose($fp);
  324.  
  325. if ($p->refname) {
  326. /*
  327. if (!empty($p->proto))
  328. $tag = '/^' . $p->proto . '$/';
  329. else
  330. $tag = '1';
  331. */
  332. $tag = '/^' . $p->refname;
  333. $fp = fopen(TAGS_PATH, 'ab') or die("Could not open tags!");
  334. fwrite($fp, $p->refname . "\t" . basename($out) . "\t" . $tag . "\n");
  335. fclose($fp);
  336. }
  337. }
  338.  
  339.  
  340. $fp = fopen(TAGS_PATH, 'wb') or die("Could not open tags!");
  341. //fwrite($fp, "!_TAG_FILE_FORMAT 2\n");
  342. //fwrite($fp, "!_TAG_FILE_SORTED 1\n");
  343. fclose($fp);
  344.  
  345. load_entities('phpdoc/doc-base/entities/global.ent');
  346. load_entities('phpdoc/en/language-defs.ent');
  347. load_entities('phpdoc/en/language-snippets.ent');
  348. load_entities('phpdoc/en/livedocs.ent');
  349. load_entities('phpdoc/en/contributors.ent');
  350. //print_r($entities['&example.outputs;']);
  351. //print_r($entities);
  352. process_all('phpdoc/en/reference');
  353. //process_all('d:/htdocs/livedocs/phpdoc/en/reference');
  354.  
  355. echo "sorting tags\n";
  356.  
  357. system('sort ' . TAGS_PATH. ' -o ' . TAGS_PATH);
  358.  
  359. ?>

Report this snippet  

You need to login to post a comment.