Refactory: remove unused classes, imports, fields and methods.
[phpeclipse.git] / net.sourceforge.phpeclipse.help / docbook-src / guide / parse.php
1 <?php\r
2 \r
3   if (function_exists ("DebugBreak")) {\r
4     global $DBGSESSID;\r
5     $DBGSESSID = "1@clienthost:7869";\r
6     DebugBreak ();\r
7   }\r
8 \r
9   var_dump ($argv);\r
10 \r
11   echo getcwd(). "\n";\r
12 \r
13   $recode_it    = false;\r
14 \r
15   $lang         = $argv[1];                   // the language\r
16   $doc          = $argv[2];                   // the document name (xd-001 etc.)\r
17   $encoding_in  = $argv[3];\r
18   $encoding_out = $argv[4];\r
19 \r
20   echo "input encoding = $encoding_in \n";\r
21 \r
22   if ($encoding_in != $encoding_out) {\r
23     $recode_it = true;\r
24   }\r
25 \r
26   $fencoding = 'UTF-8';                       // This is the standard encoding of our XML source files\r
27 \r
28   $dirlist   = glob ("*.html");               // get the list of html files within the doc main directory\r
29 \r
30   $idlist    = array ();                      // initiate the id list array\r
31   $fidlist   = array ();                      // make a id list for every file\r
32   $bilist    = array ();                      // make a book info list\r
33 \r
34   BuildIDList ($lang, $doc, $idlist, $bilist);// build the ID List for the use of comments in xdocman\r
35 \r
36   foreach ($dirlist as $key => $file) {       // for every html file\r
37     ParseXHTML ($file, $lang, $doc, $fidlist, $idlist);\r
38   }\r
39 \r
40   WriteBookInfoList ($fidlist, $bilist);\r
41 /*\r
42   var_dump ($fidlist);\r
43 */\r
44   copy ("style.css", "tmp/style.css");        // copy the style file to the temporary directory\r
45 \r
46 /***** ParseXHTML ()                         **********************************/\r
47 /* Parse the XHTML files\r
48  *\r
49  */\r
50 \r
51   function ParseXHTML (&$file, $lang, $doc, &$fidlist, &$idlist) {\r
52     global $fencoding;\r
53     $data     = file_get_contents ($file);                        // get the xhtml-file content\r
54     $parser   = xml_parser_create ($fencoding);\r
55     $imglist  = array ();                                         // initiate the image list array\r
56 \r
57     xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);\r
58     // xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);\r
59 \r
60     xml_parse_into_struct ($parser, $data, $values, $tags);       // parse the xhtml file\r
61     xml_parser_free ($parser);\r
62 \r
63     MakePictureList ($values, $tags, $imglist, $lang, $doc);      // build the list with the used images\r
64     CopyImages ($imglist);                                        // copy the used images into the temp folder\r
65     ChangeLinks ($values, $tags, $lang, $doc);                    // changed the links, so we can use the files with xdocman\r
66     MakeFileIDList ($file, $values, $tags, $fidlist, $idlist);\r
67 \r
68 /*\r
69     echo "array: tags\n";\r
70     var_dump ($tags);\r
71     echo "array: values\n";\r
72     var_dump ($values);\r
73     echo "array: imglist\n";\r
74     var_dump ($imglist);\r
75 \r
76     echo "array:".$file." generate output\n";\r
77 */\r
78     OutputXHTML ($file, $values, $tags, 0);\r
79   }\r
80 \r
81 /***** MakeFileIDList (...)                  **********************************/\r
82 /*\r
83  */\r
84   function MakeFileIDList ($file, &$values, &$tags, &$fidlist, &$idlist) {\r
85     echo "file = $file\n";\r
86 \r
87     $fidlist[$file] = array ();\r
88 \r
89     foreach ($tags['a'] as $key => $val) {\r
90       if (isset ($values[$val]['attributes'])) {\r
91         foreach ($values[$val]['attributes'] as $tkey => $tval) {\r
92           if ($tkey == 'id') {\r
93             // $idList[$tval] = $tval;\r
94 \r
95             // echo "id =  $tval \n";\r
96 \r
97             if (isset ($idlist[$tval])) {\r
98               echo "setzen $file: $tval = $idlist[$tval]  \n";\r
99               $fidlist[$file][$tval]['title']   = $idlist[$tval]['title'];\r
100               $fidlist[$file][$tval]['element'] = $idlist[$tval]['element'];\r
101             }\r
102           }\r
103         }\r
104       }\r
105     }\r
106   }\r
107 \r
108 /***** ChangeLinks (...)                     **********************************/\r
109 /*\r
110  * changes href in <a href="..."> if href is sort of chxxx.html or index.html or ixxx.html\r
111  * to ?lang=en&doc=xd-999&file=chxxx.html\r
112  */\r
113   function ChangeLinks (&$values, &$tags, $lang, $doc) {\r
114     foreach ($tags['a'] as $key => $val) {\r
115       if (($values[$val]['type'] == 'open') || ($values[$val]['type'] == 'complete')) {\r
116         if (isset ($values[$val]['attributes']['href'])) {\r
117           $olddest = $values[$val]['attributes']['href'];\r
118 \r
119           echo "string = " . $olddest;\r
120           preg_match ("/([ch|index|ix|pr|co|ar]).*\.html.*/i", $olddest, $linkval);\r
121           // preg_match ("/#.*/i", $olddest, $idval);\r
122 \r
123           $newdest = "";\r
124           if (isset ($linkval[0])) {\r
125             // $newdest .= "manual.php?lang=$lang&amp;doc=$doc&amp;file=".$linkval[0];\r
126             // echo "                 link = " . $linkval[0];\r
127 \r
128             preg_match ("/#.*/i", $olddest, $lidval);\r
129             if (isset ($lidval[0])) {\r
130               echo " id = ". $lidval[0];\r
131               $newdest .= "manual.php?lang=$lang&amp;doc=$doc&amp;id=".str_replace ('#', "", $lidval[0])."&amp;file=".$linkval[0];\r
132             }\r
133             else {\r
134               $newdest .= "manual.php?lang=$lang&amp;doc=$doc&amp;file=".$linkval[0];\r
135             }\r
136           }\r
137 /*\r
138           else if (isset ($idval[0])) {\r
139             echo " id = ".$idval[0];\r
140 \r
141             $newdest = $olddest;\r
142           }\r
143 */\r
144           else {\r
145             $newdest = $olddest;\r
146           }\r
147 \r
148           echo "\n";\r
149           $values[$val]['attributes']['href'] = $newdest;\r
150         }\r
151       }\r
152     }\r
153   }\r
154 \r
155 /***** CopyImages (...)                      **********************************/\r
156 /*\r
157  */\r
158   function CopyImages (&$imglist) {\r
159     CreateDirectory ('tmp/img');\r
160     CreateDirectory ('tmp/img/callouts');\r
161     CreateDirectory ('tmp/img/nav');\r
162     CreateDirectory ('tmp/img/admon');\r
163 \r
164     foreach ($imglist as $key => $img) {\r
165       $path    = explode ('/', $img);\r
166 \r
167       if (($path[1] != 'callouts') && ($path[1] != 'nav') && ($path[1] != 'admon')) {\r
168         $dest = "tmp/".$path[0].'/'.$path[3];\r
169       }\r
170       else {\r
171         $dest = "tmp/".$img;\r
172       }\r
173 \r
174       copy ($img, $dest);\r
175     }\r
176   }\r
177 \r
178 /***** MakePictureList (...)                 **********************************/\r
179 /*\r
180  */\r
181   function MakePictureList (&$values, &$tags, &$imglist, &$lang, &$doc) {\r
182     //** scan every <a> tag for the onmouseover and onmouseout attribute\r
183     foreach ($tags['a'] as $key => $val) {\r
184       if (isset ($values[$val]['attributes'])) {\r
185         foreach ($values[$val]['attributes'] as $tkey => $tval) {\r
186           if (($tkey == 'onmouseover') || ($tkey == 'onmouseout')) {\r
187             //** strip everthing before the '='\r
188             $ta  = explode ('=', $tval);\r
189             $img = str_replace ("'", "", $ta[1]);\r
190 \r
191             if (!in_array ($img, $imglist)) {\r
192               array_push ($imglist, $img);\r
193             }\r
194 \r
195             $values[$val]['attributes'][$tkey] = $ta[0]."='docs/".$lang."/".$doc."/".$img."'";\r
196           }\r
197         }\r
198       }\r
199     }\r
200 \r
201     //** scan every <img> tag for the src attribute\r
202     foreach ($tags['img'] as $key => $val) {\r
203       if (isset ($values[$val]['attributes'])) {\r
204         foreach ($values[$val]['attributes'] as $tkey => $tval) {\r
205           if ($tkey == 'src') {\r
206             if (!in_array ($tval, $imglist)) {\r
207               array_push ($imglist, $tval);\r
208             }\r
209 \r
210             //** now change the image path from img/en/xxxx/xxx.png to img/xxx.png\r
211             $path    = explode ('/', $tval);\r
212 \r
213             if (($path[1] != 'callouts') && ($path[1] != 'nav') && ($path[1] != 'admon')) {\r
214               $newpath = $path[0].'/'.$path[3];\r
215 \r
216               $img = "docs/".$lang."/".$doc."/".$newpath;\r
217 \r
218               $values[$val]['attributes'][$tkey] = $img;\r
219             }\r
220             else {\r
221               $values[$val]['attributes'][$tkey] = "docs/".$lang."/".$doc."/".$tval;\r
222             }\r
223           }\r
224         }\r
225       }\r
226     }\r
227   }\r
228 \r
229 /***** OutputXHTML (...)                     **********************************/\r
230 /*\r
231  */\r
232   function OutputXHTML (&$file, &$values, &$tags, $all) {\r
233     global $fencoding;\r
234     global $recode_it;\r
235     global $encoding_in;\r
236     global $encoding_out;\r
237     $fp      = fopen ("tmp/".$file, "w");\r
238     $i       = count ($values);\r
239 \r
240     //** get start and end\r
241 \r
242     if ($all) {\r
243       $start = 0;\r
244       $end   = $i;\r
245     }\r
246     else {\r
247       $start = $tags['body'][0] + 1;\r
248       $end   = $tags['body'][1] - 1;\r
249     }\r
250 \r
251     if ($all) {\r
252       $text    = '<?xml version="1.0" encoding="'.$fencoding.'" standalone="no"?>'."\n";\r
253       $text   .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";\r
254       fwrite ($fp, $text);\r
255     }\r
256 \r
257 \r
258     if ($fp) {\r
259       foreach ($values as $key => $tag) {\r
260         if (($key < $start) || ($key > $end)) {\r
261           continue;\r
262         }\r
263 \r
264         $cdata = 0;\r
265         $open  = 0;\r
266 \r
267         if ($tag['type'] == 'open') {\r
268           $text  = "<";\r
269           $close = "\n>";\r
270           $open  = 1;\r
271         }\r
272         else if ($tag['type'] == 'close') {\r
273           $text  = "</";\r
274           $close = "\n>";\r
275         }\r
276         else if ($tag['type'] == 'cdata') {\r
277           $cdata = 1;\r
278         }\r
279         else {      //** it's complete\r
280           $text  = "<";\r
281           $close = " /\n>";\r
282         }\r
283 \r
284         if ($cdata) {\r
285           $text = htmlspecialchars ($tag['value']);\r
286         }\r
287         else {\r
288           $text .= $tag['tag'];\r
289 \r
290           if (isset ($tag['attributes'])) {\r
291             foreach ($tag['attributes'] as $key => $att) {\r
292               $text .= ' '.$key. '="' . $att . '"';\r
293             }\r
294           }\r
295 \r
296           if (isset ($tag['value'])) {\r
297             if ($open) {\r
298               $text .= ">".htmlspecialchars ($tag['value']);\r
299             }\r
300             else {\r
301               $text .= ">".htmlspecialchars ($tag['value'])."</" . $tag['tag'] . ">";\r
302             }\r
303           }\r
304           else {\r
305             if (($tag['tag'] == 'a') && ($tag['type'] == 'complete')) {\r
306               $text .= "></a>";\r
307             }\r
308             else {\r
309               $text .= $close;\r
310             }\r
311           }\r
312         }\r
313 \r
314         if ($recode_it) {\r
315           $text = mb_convert_encoding ($text, 'HTML-ENTITIES', $encoding_in);\r
316         }\r
317 \r
318         fwrite ($fp, $text);\r
319       }\r
320     }\r
321   }\r
322 \r
323 /***** CreateDirectory (...)                 **********************************/\r
324 \r
325   function CreateDirectory ($dirname) {\r
326     $path = "";\r
327     $dir  = split ('[/|\\]', $dirname);\r
328 \r
329     for ($i = 0; $i < count ($dir); $i++) {\r
330       $path .= $dir[$i]."/";\r
331 \r
332       if (!is_dir ($path)) {\r
333         @mkdir ($path, 0777);\r
334         @chmod ($path, 0777);\r
335       }\r
336     }\r
337 \r
338     if (is_dir ($dirname)) {\r
339       return 1;\r
340     }\r
341 \r
342     return 0;\r
343   }\r
344 \r
345 /***** BuildIDList (...)                     **********************************/\r
346 \r
347   function BuildIDList ($lang, $doc, &$idlist, &$bilist) {\r
348     global $fencoding;\r
349     $data     = file_get_contents ($lang."_".$doc.".xml");        // get the xml-file content\r
350     $parser   = xml_parser_create ($fencoding);\r
351 \r
352     xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);\r
353     xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);\r
354 \r
355     xml_parse_into_struct ($parser, $data, $values, $tags);       // parse the xml file\r
356     xml_parser_free ($parser);\r
357 \r
358     MakeIDList ($values, $tags, $idlist);                         // build the list with the used ids\r
359     MakeBIList ($values, $tags, $bilist);                         // build the Book Info list\r
360 \r
361     echo "array: bilist\n";\r
362     var_dump ($bilist);\r
363     echo "array: tags\n";\r
364     var_dump ($tags);\r
365     echo "array: values\n";\r
366     var_dump ($values);\r
367 \r
368   }\r
369 \r
370 /***** MakeIDList (...)                      **********************************/\r
371 \r
372   function MakeIDList (&$values, &$tags, &$idlist) {\r
373     $taglist      = array ("chapter", "sect1", "sect2", "sect3", "sect4", "figure");\r
374 \r
375     foreach ($taglist as $tlkey => $tag) {                        // for every tag with a possible id\r
376 \r
377       if (isset ($tags[$tag])) {\r
378         foreach ($tags[$tag] as $key => $val) {\r
379           if (isset ($values[$val]['attributes'])) {\r
380             foreach ($values[$val]['attributes'] as $tkey => $tval) {\r
381               if ($tkey == 'id') {                                  // we have an id, so look for the title\r
382                 if ($values[$val + 1]['tag'] == 'title') {          // if the next tag is a title\r
383                   // $idlist[$tval]          = $values[$val + 1]['value'];\r
384                   $idlist[$tval]['title']   = $values[$val + 1]['value'];\r
385                   $idlist[$tval]['element'] = $tag;                 // the element\r
386                 }\r
387               }\r
388             }\r
389           }\r
390         }\r
391       }\r
392     }\r
393   }\r
394 \r
395 /***** MakeBIList (...)                      **********************************/\r
396 \r
397   function MakeBIList (&$values, &$tags, &$bilist) {\r
398     $start = 0;\r
399     $end   = 0;\r
400 \r
401     if (isset ($tags['book'])) {\r
402       // $count = count ($tags['book']);\r
403       $start = $tags['bookinfo'][0] + 1;\r
404       $end   = $tags['bookinfo'][1] - 1;\r
405       $state = 0;\r
406     }\r
407     else if (isset ($tags['article'])) {\r
408       $start = $tags['articleinfo'][0] + 1;\r
409       $end   = $tags['articleinfo'][1] - 1;\r
410       $state = 0;\r
411     }\r
412 \r
413     // get the status attribute from book or article\r
414 \r
415     if (isset ($values[0]['attributes']['status'])) {\r
416       $status = explode ('_', $values[0]['attributes']['status']);\r
417 \r
418       if (($status[0] == 'progress') && (isset ($status[1]))) {\r
419         $bilist['status'] = $status[1];\r
420       }\r
421       else {\r
422         $bilist['status'] = $status[0];\r
423       }\r
424     }\r
425 \r
426     // search for the title\r
427 \r
428     for ($index = $start; $index <= $end; $index++) {\r
429       switch ($state) {\r
430         case 0:             // search the title tag\r
431           if ($values[$index]['tag'] == 'title') {\r
432             if (isset ($values[$index]['value'])) {\r
433               $bilist['title'] = $values[$index]['value'];\r
434             }\r
435 \r
436             if ($values[$index]['type'] == 'open') {\r
437               $state = 1;   // title is not complete, so append the other tags (i.e. from quote or emphasis)\r
438             }\r
439             else {\r
440               $state = 2;   // title is complete\r
441             }\r
442           }\r
443           break;\r
444 \r
445         case 1:             // append the values of every tag until the closing title tag\r
446           if ($values[$index]['tag'] == 'title') {\r
447             if (isset ($values[$index]['value'])) {\r
448               $bilist['title'] .= $values[$index]['value'];\r
449             }\r
450 \r
451             if ($values[$index]['type'] == 'close') {\r
452               $state = 2;     // title is complete\r
453             }\r
454           }\r
455           else {\r
456             if (isset ($values[$index]['value'])) {\r
457               $bilist['title'] .= $values[$index]['value'];\r
458             }\r
459           }\r
460           break;\r
461 \r
462         default:\r
463           break;\r
464       }\r
465 \r
466       if ($state == 2) {    // if we have the title leave the loop\r
467         break;\r
468       }\r
469     }\r
470 \r
471     // search for the revnumber and revdate within the last revision within revhistory\r
472 \r
473     if (isset ($tags['revision'])) { // ok we have a revision\r
474       $count = count ($tags['revision']);\r
475 \r
476       $start = $tags['revision'][$count - 2];\r
477       $end   = $tags['revision'][$count - 1];\r
478     }\r
479 \r
480     if (isset ($tags['revnumber'])) {\r
481       $count = count ($tags['revnumber']);\r
482 \r
483       for ($index = 0; $index < $count; $index++) {\r
484         $val_index = $tags['revnumber'][$index];\r
485 \r
486         if (($val_index > $start) && ($val_index < $end)) {\r
487           $bilist['revnumber'] = $values[$val_index]['value'];\r
488         }\r
489       }\r
490     }\r
491 \r
492     if (isset ($tags['date'])) {\r
493       $count = count ($tags['date']);\r
494 \r
495       for ($index = 0; $index < $count; $index++) {\r
496         $val_index = $tags['date'][$index];\r
497 \r
498         if (($val_index > $start) && ($val_index < $end)) {\r
499           $bilist['revdate'] = $values[$val_index]['value'];\r
500         }\r
501       }\r
502     }\r
503   }\r
504 \r
505 /***** WriteBookInfoList (...)               **********************************/\r
506 \r
507   function WriteBookInfoList (&$fidlist, &$bilist) {\r
508     global $fencoding;\r
509     global $recode_it;\r
510     global $encoding_in;\r
511 \r
512     $fp      = fopen ("tmp/docinfo.xml", "w");\r
513 \r
514     if ($fp) {\r
515       $text = "<?xml version=\"1.0\" ?>" . "\n";\r
516       fwrite ($fp, $text);\r
517 \r
518       $text = "<docinfo>\n";\r
519       fwrite ($fp, $text);\r
520 \r
521       foreach ($bilist as $tag => $value) {\r
522         $text = "  <$tag>$value</$tag>\n";\r
523         fwrite ($fp, $text);\r
524       }\r
525 \r
526       foreach ($fidlist as $filename => $file) {\r
527         $text = "  <file>\n    <name>$filename</name>\n";\r
528         fwrite ($fp, $text);\r
529 \r
530         foreach ($file as $id => $idvalue) {\r
531           $text = "    <entry>\n      <id>".$id."</id>\n";\r
532           fwrite ($fp, $text);\r
533 \r
534           $text = "      <element>".$idvalue['element']."</element>\n";\r
535           fwrite ($fp, $text);\r
536 \r
537           $text = "      <text>".$idvalue['title']."</text>\n    </entry>\n";\r
538 \r
539           if ($recode_it) {\r
540             $text = mb_convert_encoding ($text, 'HTML-ENTITIES', $encoding_in);\r
541           }\r
542 \r
543           fwrite ($fp, $text);\r
544         }\r
545 \r
546         $text = "  </file>\n";\r
547         fwrite ($fp, $text);\r
548       }\r
549 \r
550       $text = "</docinfo>";\r
551       fwrite ($fp, $text);\r
552 \r
553       fclose ($fp);\r
554     }\r
555   }\r
556 \r
557 /******************************************************************************/\r
558 \r
559 ?>\r