#!/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; ?>