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