From 87290d89f91072ff8d4f5e975bef1224b455fd41 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 3 Jun 2015 23:42:27 +0200 Subject: [PATCH] --- build-toc.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 build-toc.php diff --git a/build-toc.php b/build-toc.php new file mode 100644 index 0000000..f585731 --- /dev/null +++ b/build-toc.php @@ -0,0 +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; +?> -- 2.30.2