X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Ftoc_formatter%2Ftoc_formatter.module;fp=web%2Fmodules%2Fcontrib%2Ftoc_formatter%2Ftoc_formatter.module;h=9dc60e5fbc8f9dbf8f27df47eb626c0be3470bba;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website
diff --git a/web/modules/contrib/toc_formatter/toc_formatter.module b/web/modules/contrib/toc_formatter/toc_formatter.module
new file mode 100755
index 000000000..9dc60e5fb
--- /dev/null
+++ b/web/modules/contrib/toc_formatter/toc_formatter.module
@@ -0,0 +1,96 @@
+ array(
+ 'variables' => array('content' => NULL, 'title' => NULL, 'type' => 'ul'),
+ ),
+ 'toc_formatter_move_to_top_link' => array(
+ 'variables' => array('content' => NULL),
+ ),
+ );
+
+ return $theme;
+}
+
+
+/**
+ * Theming functions for 'Move to top' links.
+ */
+function theme_toc_formatter_move_to_top_link($variables) {
+ $content = $variables['content'];
+ $path_alias = request_uri();
+ $link = '
';
+ $link .= l(t('Move to top'), $path_alias, array('fragment' => 'table-of-contents'));
+ $link .= '
';
+
+ $output = str_ireplace('', ''. $link, $content);
+ return $output;
+}
+
+/**
+ * Generate table of contents, add anchors to headings, and add links back to top of page.
+ *
+ **/
+function theme_toc_formatter_table_of_contents($variables) {
+
+ $title = $variables['title'];
+ $content = $variables['content'];
+ $type = $variables['type'];
+
+ $path = request_uri();
+ $links = array();
+ $anchor = 0;
+ $output = '';
+
+ $dom_document = new DOMDocument('1.0','utf-8');
+ @$dom_document->loadHTML(''. $content .'
');
+
+ $headers = $dom_document->getElementsByTagName('h2');
+
+ foreach ($headers as $header) {
+ $anchor++;
+
+ // TOC links.
+ $label = trim($header->nodeValue);
+ $links[] = l($label, $path, array('fragment' => 'toc-'. $anchor));
+
+ // Headings, add class.
+ $header->setAttribute('class', 'toc-headings');
+
+ // Anchors above headings.
+ $anchor_div = $dom_document->createElement('div');
+ $anchor_div->setAttribute('class', 'toc-item-anchor');
+ $target = $dom_document->createElement('a');
+ $target->setAttribute('name', 'toc-'. $anchor);
+ $anchor_div->appendChild($target);
+ $header->parentNode->insertBefore($anchor_div, $header);
+ }
+
+ if (isset($links) && !empty($links)) {
+ // Output DOM to a string.
+ // Unfortunately below PHP 5.3.6 saveHTML() doesn't expect a parameter.
+ $content_updated = $dom_document->saveHTML();
+ // So we have to remove wrapping tags ourseleves.
+ $content_fragments = explode('', $content_updated);
+ $content_inner = str_replace('