html_toc 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +73 -0
- data/lib/html_toc.rb +1 -1
- data/sample/html_toc.css +43 -0
- data/sample/html_toc.js +8 -0
- metadata +11 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a29233f8816b3ee12494fea882d296d3541315f
|
4
|
+
data.tar.gz: 1ef8af28f0c1c7ffd80f07dd344abad4a63da1a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ce2bc271cc049d832a6ac0c5ce2104f4cd7703de9b5a71884f67272f81d2316746e69d0dfc4021c6e74d31cc9e9295cbff81f2c62d61c58fb6433655aac4717
|
7
|
+
data.tar.gz: 05aa67dec91573dab6b1ac296589f6ddb4bf838bdab2156a4c900d089c24d37d96f669956f5c6fab229d39a329bca1a0c0742db47c464b3f7dd87eb9a873f7ca
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
**Travis CI:** ![Latest test](https://travis-ci.org/GGadow/html_toc.svg?branch=master)
|
2
|
+
|
3
|
+
#HtmlToc
|
4
|
+
|
5
|
+
HtmlToc is a Ruby module that post-processes an HTML document to built a table of contents and insert it at a specified location. It takes in the source text of the page, and returns the modified text.
|
6
|
+
|
7
|
+
##About
|
8
|
+
|
9
|
+
The gem consists of a single module, <span style="color:#009900;">HtmlToc</span>, which exposes a single public method, <span style="color:#009900;">process</span>.
|
10
|
+
|
11
|
+
<span style="color:#009900;">#process</span> starts by performing a case-insensitive search for a token, **\[\[toc\]\]**. If no token is found, the unmodified source text is return.
|
12
|
+
|
13
|
+
If the token is found, <span style="color:#009900;">#process</span> scans for header tags falling within a provided range; if the header does not already have an id attribute, one is added. If no matching headers are found, the token is removed and the modified source text is returned.
|
14
|
+
|
15
|
+
If headers are found, a link is generated for each matching header. The link text is taken from the header text, and the link's href points to the header's id. Each link wrapped in a div tag; the div is given a class name that matches is level relative to the search range. The link divs are wrapped in a few more divs with unique ids to create the table of contents. Lastly, the table of contents replaces the token and the modified source is returned. The classes and id allow the page to be styled in accordance with the website's design context. The resulting table of contents might look like this:
|
16
|
+
|
17
|
+
<div style="border:solid 1px #000000;margin-left:1em;">
|
18
|
+
|
19
|
+
<div id='__toc'><br/>
|
20
|
+
<div id='__toc_header'>Contents</div><br/>
|
21
|
+
<div id='__toc_content' style='display:block'><br/>
|
22
|
+
<div class='__toc_level_1'><a href='#id__1'>1 First (1<sup>st</sup>) major header</a></div><br/>
|
23
|
+
<div class='__toc_level_2'><a href='#id__5'>1.1 Minor header 1</a></div><br/>
|
24
|
+
<div class='__toc_level_3'><a href='#id__11'>1.1.1 Detail the first</a></div><br/>
|
25
|
+
<div class='__toc_level_3'><a href='#already_here_1'>1.1.2 Detail the second</a></div><br/>
|
26
|
+
<div class='__toc_level_2'><a href='#already_here_2'>1.2 Minor header 2</a></div><br/>
|
27
|
+
<div class='__toc_level_1'><a href='#already_here_3'>2 Second major header</a></div><br/>
|
28
|
+
<div class='__toc_level_2'><a href='#id__2'>2.1 Minor header 3</a></div><br/>
|
29
|
+
<div class='__toc_level_3'><a href='#id__12'>2.1.1 Detail the third</a></div><br/>
|
30
|
+
<div class='__toc_level_3'><a href='#already_here_4'>2.1.2 Detail the fourth</a></div><br/>
|
31
|
+
<div class='__toc_level_2'><a href='#already_here_5'>2.2 Minor header 4</a></div><br/>
|
32
|
+
</div><br/>
|
33
|
+
</div>
|
34
|
+
|
35
|
+
</div>
|
36
|
+
|
37
|
+
##Use
|
38
|
+
|
39
|
+
**<span style="color:#009900;">HtmlToc.process</span> source:, h_tags: Range.new(2, 6), show_toggle: false, use_numbers: false**
|
40
|
+
|
41
|
+
>**source** is a string holding the HTML source.
|
42
|
+
|
43
|
+
>**h_tags** is a range of integers giving the indexes of the header tags that will be used to the table of contents. The method iterates through it to build the regular expression <span style="color:#800000;">/<h#{x}(?: .\*?)?>(.\*?)<\/h#{x}>/</span>.
|
44
|
+
|
45
|
+
>**show_toggle** flags whether or not to include a toggle button in the table of contents header. The span is programmed to call a Javascript method, ShowHideToc(). The implementing script is *not* included: it must be supplied by the programmer.
|
46
|
+
|
47
|
+
>**use_numbers** flags whether or not the links will have outlining numbers.
|
48
|
+
|
49
|
+
##CSS
|
50
|
+
|
51
|
+
These classes and ids are used by <span style="color:#009900;">HtmlToc</span> in the table of contents.
|
52
|
+
|
53
|
+
>**#__toc** - The outer frame div.
|
54
|
+
|
55
|
+
>**#__toc_header** - The header div.
|
56
|
+
|
57
|
+
>**#__toc_content** - The contents div.
|
58
|
+
|
59
|
+
>**#__toc_toggle** - The span containing the toggle.
|
60
|
+
|
61
|
+
>**.__toc_level_x** - Used on the divs holding the links, with x ranging from 1 to 6. These are applied as the header tags are found, so using the default *h_tags*, __toc_level_1 will be associated with h2 tags, __toc_level_2 with h3 tags, and so on.
|
62
|
+
|
63
|
+
|
64
|
+
##Additional files
|
65
|
+
|
66
|
+
See **sample/html_toc.css** for an example of how to style the table of contents.<br/>
|
67
|
+
See **sample/html_toc.js** for the Javascript to toggle visibility of the table of contents.
|
68
|
+
|
69
|
+
##Change log
|
70
|
+
|
71
|
+
**1.1** - Added keyword arguments.
|
72
|
+
**1.0** - Initial deployment.
|
73
|
+
|
data/lib/html_toc.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rexml/document'
|
|
3
3
|
module HtmlToc
|
4
4
|
|
5
5
|
#Primary method call
|
6
|
-
def self.process
|
6
|
+
def self.process(source:, h_tags: Range.new(2, 6), show_toggle: false, use_numbers: false)
|
7
7
|
|
8
8
|
#Search regex for {{toc}}
|
9
9
|
token = /\[\[[tT][oO][cC]\]\]/
|
data/sample/html_toc.css
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
div.__toc_level_1 {
|
2
|
+
}
|
3
|
+
|
4
|
+
div.__toc_level_2 {
|
5
|
+
padding-left:1em;
|
6
|
+
}
|
7
|
+
|
8
|
+
div.__toc_level_3 {
|
9
|
+
padding-left:2em;
|
10
|
+
}
|
11
|
+
|
12
|
+
div.__toc_level_4 {
|
13
|
+
padding-left:3em;
|
14
|
+
}
|
15
|
+
|
16
|
+
div.__toc_level_5 {
|
17
|
+
padding-left:4em;
|
18
|
+
}
|
19
|
+
|
20
|
+
div.__toc_level_6 {
|
21
|
+
padding-left:5em;
|
22
|
+
}
|
23
|
+
|
24
|
+
#__toc {
|
25
|
+
display:inline-block;
|
26
|
+
padding:0.5em;
|
27
|
+
border:solid 1px #AAAAAA;
|
28
|
+
background-color:#EEEEEE;
|
29
|
+
}
|
30
|
+
|
31
|
+
#__toc_content {
|
32
|
+
padding-top:0.75em;
|
33
|
+
}
|
34
|
+
|
35
|
+
#__toc_header {
|
36
|
+
font-weight:bold;
|
37
|
+
}
|
38
|
+
|
39
|
+
#__toc_toggle {
|
40
|
+
cursor:pointer;
|
41
|
+
font-size:0.8em;
|
42
|
+
font-weight:normal;
|
43
|
+
}
|
data/sample/html_toc.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
var TOCState = 'block';
|
2
|
+
|
3
|
+
function ShowHideToc() {
|
4
|
+
TOCState = (TOCState == 'none') ? 'block' : 'none';
|
5
|
+
var newText = (TOCState == 'none') ? 'Show' : 'Hide';
|
6
|
+
document.getElementById('__toc_toggle').innerHTML = newText;
|
7
|
+
document.getElementById('__toc_content').style.display = TOCState;
|
8
|
+
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_toc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory Gadow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: "This gem is intended to be used in Rails pre-processing, after the page
|
14
14
|
has been generated but before it is delivered to the requestor. \n\nIt does a case-insensitive
|
@@ -24,9 +24,15 @@ description: "This gem is intended to be used in Rails pre-processing, after the
|
|
24
24
|
email: gpg@gregory-gadow.net
|
25
25
|
executables: []
|
26
26
|
extensions: []
|
27
|
-
extra_rdoc_files:
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.md
|
29
|
+
- sample/html_toc.css
|
30
|
+
- sample/html_toc.js
|
28
31
|
files:
|
32
|
+
- README.md
|
29
33
|
- lib/html_toc.rb
|
34
|
+
- sample/html_toc.css
|
35
|
+
- sample/html_toc.js
|
30
36
|
homepage: https://github.com/GGadow/html_toc
|
31
37
|
licenses:
|
32
38
|
- MIT
|
@@ -39,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
45
|
requirements:
|
40
46
|
- - ">="
|
41
47
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
48
|
+
version: '2.1'
|
43
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
51
|
- - ">="
|
@@ -50,5 +56,5 @@ rubyforge_project:
|
|
50
56
|
rubygems_version: 2.2.2
|
51
57
|
signing_key:
|
52
58
|
specification_version: 4
|
53
|
-
summary: Generate and
|
59
|
+
summary: Generate and insert a table of contents into a HTML document
|
54
60
|
test_files: []
|