From: Christian Weiske Date: Wed, 3 Jun 2015 21:43:39 +0000 (+0200) Subject: (no commit message) X-Git-Url: https://git.cweiske.de/paste/199.git/commitdiff_plain/HEAD --- diff --git a/build-toc.php b/build-toc.php index f585731..73970ed 100644 --- a/build-toc.php +++ b/build-toc.php @@ -1,79 +1,79 @@ -#!/usr/bin/env php - is ignored; all other headings are used. - * - * @author Christian Weiske - * - * Alternative if you have an XSLT 2 processor: http://xmlplease.com/tocxhtml - */ -if ($argc < 2) { - echo "Please pass a file\n"; - exit(1); -} -$file = $argv[1]; -if (!file_exists($file)) { - echo "File does not exist: $file\n"; - exit(2); -} -if (substr($file, -4) != '.htm') { - echo "build-toc.php works only on htm files\n"; - exit(3); -} - -$sx = simplexml_load_file($file); -if ($sx === false) { - echo "Failed to load XML\n"; - exit(4); -} - -$body = $sx->body; -$body->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml'); -$elems = $body->xpath( - '//*[' - //XHTML - . 'self::h:h2 or self::h:h3 or self::h:h4 or self::h:h5 or self::h:h6' - //HTML - . ' or self::h2 or self::h3 or self::h4 or self::h5 or self::h6' - . ']' -); - -$data = array(); -$n = -1; -foreach ($elems as $elem) { - $name = $elem->getName(); - $level = (int) $name{1}; - $data[] = (object) array( - 'title' => (string) $elem, - 'id' => (string) $elem['id'], - 'level' => $level, - 'nextLevel' => null - ); - if (++$n >= 1) { - $data[$n - 1]->nextLevel = $level; - } -} - -$html = "\n"; -echo $html; -?> +#!/usr/bin/env php + is ignored; all other headings are used. + * + * @author Christian Weiske + * + * Alternative if you have an XSLT 2 processor: http://xmlplease.com/tocxhtml + */ +if ($argc < 2) { + echo "Please pass a file\n"; + exit(1); +} +$file = $argv[1]; +if (!file_exists($file)) { + echo "File does not exist: $file\n"; + exit(2); +} +if (substr($file, -4) != '.htm') { + echo "build-toc.php works only on htm files\n"; + exit(3); +} + +$sx = simplexml_load_file($file); +if ($sx === false) { + echo "Failed to load XML\n"; + exit(4); +} + +$body = $sx->body; +$body->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml'); +$elems = $body->xpath( + '//*[' + //XHTML + . 'self::h:h2 or self::h:h3 or self::h:h4 or self::h:h5 or self::h:h6' + //HTML + . ' or self::h2 or self::h3 or self::h4 or self::h5 or self::h6' + . ']' +); + +$data = array(); +$n = -1; +foreach ($elems as $elem) { + $name = $elem->getName(); + $level = (int) $name{1}; + $data[] = (object) array( + 'title' => (string) $elem, + 'id' => (string) $elem['id'], + 'level' => $level, + 'nextLevel' => null + ); + if (++$n >= 1) { + $data[$n - 1]->nextLevel = $level; + } +} + +$html = "
    \n"; +foreach ($data as $h) { + $i = str_repeat(' ', ($h->level -1) * 2 - 1); + $html .= $i + . '
  • ' + . htmlspecialchars($h->title) + . ''; + if ($h->nextLevel > $h->level) { + $html .= "\n" + . $i . '
      ' . "\n"; + } else { + $html .= '' . "\n"; + } + if ($h->nextLevel < $h->level) { + $html .= substr($i, 0, -1) . "
    \n" + . substr($i, 0, -2) . "
  • \n"; + } +} + +$html .= "
\n"; +echo $html; +?> diff --git a/example-output.html b/example-output.html new file mode 100644 index 0000000..ae9388c --- /dev/null +++ b/example-output.html @@ -0,0 +1,24 @@ +