diff options
author | RedMatrix <info@friendica.com> | 2015-01-15 11:22:57 +1100 |
---|---|---|
committer | RedMatrix <info@friendica.com> | 2015-01-15 11:22:57 +1100 |
commit | b37915a40e4e049072698e604e61798445a339de (patch) | |
tree | 8442b35b92920a759f6b9e67030b8f151114e648 | |
parent | d6ae124bbd549629ec7d76bb1b1555d1344ac8b4 (diff) | |
parent | dfab2416926631b7b08c6c89b62528ca2520f130 (diff) | |
download | volse-hubzilla-b37915a40e4e049072698e604e61798445a339de.tar.gz volse-hubzilla-b37915a40e4e049072698e604e61798445a339de.tar.bz2 volse-hubzilla-b37915a40e4e049072698e604e61798445a339de.zip |
Merge pull request #850 from einervonvielen/toc
Added a table of content as bbcode element [toc] for webpages
-rw-r--r-- | doc/bbcode.html | 3 | ||||
-rw-r--r-- | include/bbcode.php | 4 | ||||
-rw-r--r-- | library/tableofcontents/jquery.toc.js | 96 | ||||
-rw-r--r-- | view/js/main.js | 2 | ||||
-rw-r--r-- | view/php/theme_init.php | 1 |
5 files changed, 105 insertions, 1 deletions
diff --git a/doc/bbcode.html b/doc/bbcode.html index fac768879..f0b61b33c 100644 --- a/doc/bbcode.html +++ b/doc/bbcode.html @@ -65,7 +65,8 @@ <li>[rpost=title]Text to post[/rpost] The observer will be returned to their home hub to enter a post with the specified title and body. Both are optional <br /> <li>[qr]text to post[/qr] - create a QR code.<br /> -<br /> +<li>[toc] - create a table of content in a webpage (level h1,...,h4).<br /> +<br /><br /> </ul> <p>These require a suitable map plugin/addon such as openstreetmap or else the result will be blank</p> diff --git a/include/bbcode.php b/include/bbcode.php index 030a1fb49..33017c011 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -575,6 +575,10 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) { if (strpos($Text,'[h6]') !== false) { $Text = preg_replace("(\[h6\](.*?)\[\/h6\])ism",'<h6>$1</h6>',$Text); } + // Check for table of content + if (strpos($Text,'[toc]') !== false) { + $Text = preg_replace("/\[toc\]/ism",'<ul id="toc"></ul>',$Text); + } // Check for centered text if (strpos($Text,'[/center]') !== false) { $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text); diff --git a/library/tableofcontents/jquery.toc.js b/library/tableofcontents/jquery.toc.js new file mode 100644 index 000000000..d136f5aab --- /dev/null +++ b/library/tableofcontents/jquery.toc.js @@ -0,0 +1,96 @@ +/* + * Table of Contents jQuery Plugin - jquery.toc + * + * Copyright 2013 Nikhil Dabas + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations + * under the License. + * + * The original script was modified to work within the red#martrix (added var pathname). + */ + +(function ($) { + "use strict"; + + // Builds a list with the table of contents in the current selector. + // options: + // content: where to look for headings + // headings: string with a comma-separated list of selectors to be used as headings, ordered + // by their relative hierarchy level + var toc = function (options) { + return this.each(function () { + var root = $(this), + data = root.data(), + thisOptions, + stack = [root], // The upside-down stack keeps track of list elements + listTag = this.tagName, + currentLevel = 0, + headingSelectors; + + // Defaults: plugin parameters override data attributes, which override our defaults + thisOptions = $.extend( + {content: "body", headings: "h1,h2,h3"}, + {content: data.toc || undefined, headings: data.tocHeadings || undefined}, + options + ); + headingSelectors = thisOptions.headings.split(","); + + // Set up some automatic IDs if we do not already have them + $(thisOptions.content).find(thisOptions.headings).attr("id", function (index, attr) { + // Generate a valid ID: must start with a letter, and contain only letters and + // numbers. All other characters are replaced with underscores. + return attr || + $(this).text().replace(/^[^A-Za-z]*/, "").replace(/[^A-Za-z0-9]+/g, "_"); + }).each(function () { + // What level is the current heading? + var elem = $(this), level = $.map(headingSelectors, function (selector, index) { + return elem.is(selector) ? index : undefined; + })[0]; + + if (level > currentLevel) { + // If the heading is at a deeper level than where we are, start a new nested + // list, but only if we already have some list items in the parent. If we do + // not, that means that we're skipping levels, so we can just add new list items + // at the current level. + // In the upside-down stack, unshift = push, and stack[0] = the top. + var parentItem = stack[0].children("li:last")[0]; + if (parentItem) { + stack.unshift($("<" + listTag + "/>").appendTo(parentItem)); + } + } else { + // Truncate the stack to the current level by chopping off the 'top' of the + // stack. We also need to preserve at least one element in the stack - that is + // the containing element. + stack.splice(0, Math.min(currentLevel - level, Math.max(stack.length - 1, 0))); + } + // the variable pathname was added to the original script. + var pathname = window.location.pathname; + // Add the list item + $("<li/>").appendTo(stack[0]).append( + $("<a/>").text(elem.text()).attr("href", pathname + "#" + elem.attr("id")) + ); + + currentLevel = level; + }); + }); + }, old = $.fn.toc; + + $.fn.toc = toc; + + $.fn.toc.noConflict = function () { + $.fn.toc = old; + return this; + }; + + // Data API + $(function () { + toc.call($("[data-toc]")); + }); +}(window.jQuery)); diff --git a/view/js/main.js b/view/js/main.js index 9d5136c34..0c56c8d41 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -1125,6 +1125,8 @@ $(document).ready(function() { $(".autotime").timeago(); + $("#toc").toc({content: "div.page-body", headings: "h1,h2,h3,h4"}); + }); diff --git a/view/php/theme_init.php b/view/php/theme_init.php index d43a87046..b3580b81f 100644 --- a/view/php/theme_init.php +++ b/view/php/theme_init.php @@ -43,6 +43,7 @@ head_add_js('library/colorbox/jquery.colorbox-min.js'); head_add_js('library/bootstrap-tagsinput/bootstrap-tagsinput.js'); head_add_js('library/jquery.AreYouSure/jquery.are-you-sure.js'); +head_add_js('library/tableofcontents/jquery.toc.js'); /** * Those who require this feature will know what to do with it. * Those who don't, won't. |