nanoc-toolbox 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +7 -4
- data/README.md +3 -0
- data/lib/nanoc/toolbox/filters.rb +1 -0
- data/lib/nanoc/toolbox/filters/add_sections.rb +92 -0
- data/lib/nanoc/toolbox/helpers/navigation.rb +21 -25
- data/lib/nanoc/toolbox/version.rb +1 -1
- data/spec/data/filters/item_without_sections.html +25 -0
- data/spec/filters/add_sections_spec.rb +38 -0
- metadata +6 -3
data/CHANGELOG.md
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
3
|
## developement
|
4
|
+
* NEW: add_section Filter
|
5
|
+
* CHANGED: bugfix on navigation helper
|
6
|
+
|
4
7
|
|
5
8
|
## Release 0.0.3
|
6
9
|
|
7
|
-
* NEW:
|
10
|
+
* NEW: Gravatar Helper
|
8
11
|
* CHANGED: Better Yardoc documentation
|
9
|
-
* NEW:
|
10
|
-
* NEW:
|
11
|
-
* NEW:
|
12
|
+
* NEW: RSpec tests
|
13
|
+
* NEW: HTML Tag Helper
|
14
|
+
* NEW: HtlmTidy Filter
|
12
15
|
|
13
16
|
## Release 0.0.2
|
14
17
|
|
data/README.md
CHANGED
@@ -0,0 +1,92 @@
|
|
1
|
+
module Nanoc::Toolbox::Filters
|
2
|
+
|
3
|
+
# This filters adds section div’s based on headers. For example, this:
|
4
|
+
# @example
|
5
|
+
# h1
|
6
|
+
# p
|
7
|
+
# foo
|
8
|
+
# p
|
9
|
+
# h2
|
10
|
+
# p
|
11
|
+
# p
|
12
|
+
# h2
|
13
|
+
# p
|
14
|
+
# p
|
15
|
+
# h1
|
16
|
+
# p
|
17
|
+
#
|
18
|
+
# section
|
19
|
+
# h1
|
20
|
+
# p
|
21
|
+
# foo
|
22
|
+
# p
|
23
|
+
# section
|
24
|
+
# h2
|
25
|
+
# p
|
26
|
+
# p
|
27
|
+
# section
|
28
|
+
# h2
|
29
|
+
# p
|
30
|
+
# p
|
31
|
+
# section
|
32
|
+
# p
|
33
|
+
# @author Denis Defreyne <denis.defreyne@stoneship.org>
|
34
|
+
class AddSections < Nanoc3::Filter
|
35
|
+
|
36
|
+
identifiers :add_sections
|
37
|
+
|
38
|
+
def run(content, arguments={})
|
39
|
+
require 'nokogiri'
|
40
|
+
|
41
|
+
# Parse
|
42
|
+
doc = Nokogiri::HTML.fragment(content)
|
43
|
+
|
44
|
+
# Add sections for all headers
|
45
|
+
(1..6).each do |level|
|
46
|
+
# For each header on this level
|
47
|
+
doc.css("h#{level}").each do |header|
|
48
|
+
# Get all siblings
|
49
|
+
siblings = header.parent.children
|
50
|
+
|
51
|
+
# Remove previous siblings
|
52
|
+
siblings_after = []
|
53
|
+
should_include = false
|
54
|
+
siblings.each do |sibling|
|
55
|
+
if sibling == header
|
56
|
+
should_include = true
|
57
|
+
elsif should_include
|
58
|
+
siblings_after << sibling
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Remove next siblings that should not be part of this section
|
63
|
+
siblings_in_between = []
|
64
|
+
siblings_after.each do |sibling|
|
65
|
+
if sibling.name =~ /^h(\d)/ && $1.to_i <= level
|
66
|
+
break
|
67
|
+
else
|
68
|
+
siblings_in_between << sibling
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Create section
|
73
|
+
section = Nokogiri::XML::Node.new('div', doc)
|
74
|
+
section['class'] = 'section'
|
75
|
+
section['id'] = header.content.downcase.gsub(/\W+/, '-').gsub(/^-|-$/, '')
|
76
|
+
header.add_previous_sibling(section)
|
77
|
+
|
78
|
+
# Move children into section
|
79
|
+
header.unlink
|
80
|
+
section.add_child(header)
|
81
|
+
siblings_in_between.each do |sibling|
|
82
|
+
sibling.unlink
|
83
|
+
section.add_child(sibling)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Done
|
89
|
+
doc.to_s
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -18,17 +18,10 @@ module Nanoc::Toolbox::Helpers
|
|
18
18
|
#
|
19
19
|
# @param [String] identifier - the identifier string of the root element
|
20
20
|
# @param [Hash] options - The Optional parameters
|
21
|
-
# @option options
|
22
|
-
# @option options [String] :collection_tag ('ol') collection englobing tag name
|
23
|
-
# @option options [String] :item_tag ('li') item englobing tag name
|
21
|
+
# @option options (see #render_menu)
|
24
22
|
#
|
25
23
|
# @return [String] The output ready to be displayed by the caller
|
26
24
|
def navigation_for(identifier, options={})
|
27
|
-
# Parse options or set to default values
|
28
|
-
options[:depth] ||= 3
|
29
|
-
options[:collection_tag] ||= 'ol'
|
30
|
-
options[:item_tag] ||= 'li'
|
31
|
-
|
32
25
|
# Get root item for which we need to draw the navigation
|
33
26
|
root = @items.find { |i| i.identifier == identifier }
|
34
27
|
|
@@ -46,9 +39,7 @@ module Nanoc::Toolbox::Helpers
|
|
46
39
|
#
|
47
40
|
# @param [String] item_rep - the representation of desired item
|
48
41
|
# @param [Hash] options - The Optional parameters
|
49
|
-
# @option options
|
50
|
-
# @option options [String] :collection_tag ('ol') collection englobing tag name
|
51
|
-
# @option options [String] :item_tag ('li') item englobing tag name
|
42
|
+
# @option options (see #render_menu)
|
52
43
|
#
|
53
44
|
# @return [String] The output ready to be displayed by the caller
|
54
45
|
#
|
@@ -57,9 +48,6 @@ module Nanoc::Toolbox::Helpers
|
|
57
48
|
require 'nokogiri'
|
58
49
|
|
59
50
|
# Parse options or set to default values
|
60
|
-
options[:depth] ||= 3
|
61
|
-
options[:collection_tag] ||= 'ol'
|
62
|
-
options[:item_tag] ||= 'li'
|
63
51
|
options[:path] ||= 'div[@class="section"]'
|
64
52
|
|
65
53
|
# Retreive the parsed content and init nokogiri
|
@@ -80,8 +68,7 @@ module Nanoc::Toolbox::Helpers
|
|
80
68
|
#
|
81
69
|
# @param [String] identifier - the identifier string of element
|
82
70
|
# @param [Hash] options - The Optional parameters
|
83
|
-
# @option options
|
84
|
-
# @option options [String] :item_tag ('li') item englobing tag name
|
71
|
+
# @option options (see #render_menu)
|
85
72
|
#
|
86
73
|
# @return [String] The output ready to be displayed by the caller
|
87
74
|
#
|
@@ -90,8 +77,7 @@ module Nanoc::Toolbox::Helpers
|
|
90
77
|
|
91
78
|
# Parse options or set to default values
|
92
79
|
options[:collection_tag] ||= 'ul'
|
93
|
-
|
94
|
-
|
80
|
+
|
95
81
|
# Retreive the breadcrumbs trail and format them
|
96
82
|
sections = find_breadcrumbs_trail(identifier)
|
97
83
|
render_menu(sections, options)
|
@@ -122,8 +108,11 @@ module Nanoc::Toolbox::Helpers
|
|
122
108
|
#
|
123
109
|
# @param [Array] items - The array of links that need to be rendered
|
124
110
|
# @param [Hash] options - The Optional parameters
|
125
|
-
# @option options [
|
126
|
-
# @option options [String] :
|
111
|
+
# @option options [Interger] :depth (3) maximum depth of the rendered menu
|
112
|
+
# @option options [String] :collection_tag ('ol') tag englobing collection of items
|
113
|
+
# @option options [String] :item_tag ('li') tag englobing item
|
114
|
+
# @option options [String] :title_tag ('h2') tag englobing the title
|
115
|
+
# @option options [String] :title ('') Title of the menu, if nil will not display title
|
127
116
|
#
|
128
117
|
# @return [String] The output ready to be displayed by the caller
|
129
118
|
def render_menu(items, options={})
|
@@ -132,7 +121,14 @@ module Nanoc::Toolbox::Helpers
|
|
132
121
|
options[:depth] ||= 3
|
133
122
|
options[:collection_tag] ||= 'ol'
|
134
123
|
options[:item_tag] ||= 'li'
|
135
|
-
|
124
|
+
options[:title_tag] ||= 'h2'
|
125
|
+
options[:title] ||= nil
|
126
|
+
|
127
|
+
# Parse the title and remove it from the options
|
128
|
+
title = options[:title] ? content_tag(options[:title_tag], options[:title]) : ''
|
129
|
+
options.delete(:title_tag)
|
130
|
+
options.delete(:title)
|
131
|
+
|
136
132
|
# Decrease the depth level
|
137
133
|
options[:depth] -= 1
|
138
134
|
|
@@ -148,7 +144,8 @@ module Nanoc::Toolbox::Helpers
|
|
148
144
|
|
149
145
|
end.join()
|
150
146
|
|
151
|
-
|
147
|
+
|
148
|
+
title + content_tag(options[:collection_tag], rendered_menu) unless rendered_menu.strip.empty?
|
152
149
|
end
|
153
150
|
|
154
151
|
private
|
@@ -163,12 +160,11 @@ module Nanoc::Toolbox::Helpers
|
|
163
160
|
# For each section found call the find_toc_sections on it with an
|
164
161
|
# increased header level (ex: h1 => h2) and then generate the hash res
|
165
162
|
sections = section.xpath(section_xpath).map do |subsection|
|
166
|
-
header = subsection.
|
163
|
+
header = subsection.css("h1, h2, h3, h4, h5, h6").first
|
167
164
|
sub_id = subsection['id']
|
168
|
-
sub_title = header.inner_html
|
165
|
+
sub_title = header ? header.inner_html : 'untitled'
|
169
166
|
subsections = {}
|
170
167
|
|
171
|
-
|
172
168
|
if subsection.xpath("#{section_xpath}") && title_level <= 6
|
173
169
|
subsections = find_toc_sections(subsection, "#{section_xpath}", title_level+1)
|
174
170
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<div>
|
2
|
+
<h1>Title</h1>
|
3
|
+
|
4
|
+
<h2>Sub title1</h2>
|
5
|
+
<p>Lorem Ipsum</p>
|
6
|
+
|
7
|
+
<h3>Sub Sub Title1</h3>
|
8
|
+
<p>Lorem Ipsum</p>
|
9
|
+
|
10
|
+
<h3>Sub Sub Title2</h3>
|
11
|
+
<p>Lorem Ipsum</p>
|
12
|
+
|
13
|
+
<h3>Sub Sub Title3</h3>
|
14
|
+
<p>Lorem Ipsum</p>
|
15
|
+
|
16
|
+
|
17
|
+
<h2>Sub title2</h2>
|
18
|
+
<p>Lorem Ipsum</p>
|
19
|
+
|
20
|
+
<h2>Sub title3</h2>
|
21
|
+
<p>Lorem Ipsum</p>
|
22
|
+
|
23
|
+
<h2>Sub title4</h2>
|
24
|
+
<p>Lorem Ipsum</p>
|
25
|
+
</div>
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# <div>
|
2
|
+
# <h1>Title</h1>
|
3
|
+
#
|
4
|
+
# <h2>Sub title1</h2>
|
5
|
+
# <p>Lorem Ipsum</p>
|
6
|
+
#
|
7
|
+
# <h3>Sub Sub Title1</h3>
|
8
|
+
# <p>Lorem Ipsum</p>
|
9
|
+
#
|
10
|
+
# <h3>Sub Sub Title2</h3>
|
11
|
+
# <p>Lorem Ipsum</p>
|
12
|
+
#
|
13
|
+
# <h3>Sub Sub Title3</h3>
|
14
|
+
# <p>Lorem Ipsum</p>
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# <h2>Sub title2</h2>
|
18
|
+
# <p>Lorem Ipsum</p>
|
19
|
+
#
|
20
|
+
# <h2>Sub title3</h2>
|
21
|
+
# <p>Lorem Ipsum</p>
|
22
|
+
#
|
23
|
+
# <h2>Sub title4</h2>
|
24
|
+
# <p>Lorem Ipsum</p>
|
25
|
+
# </div>
|
26
|
+
|
27
|
+
require "spec_helper"
|
28
|
+
require "nokogiri"
|
29
|
+
|
30
|
+
describe Nanoc::Toolbox::Filters::AddSections do
|
31
|
+
before(:each) do
|
32
|
+
@filter = described_class.new
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".run" do
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Anouar ADLANI
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-17 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- Rakefile
|
98
98
|
- lib/nanoc/toolbox.rb
|
99
99
|
- lib/nanoc/toolbox/filters.rb
|
100
|
+
- lib/nanoc/toolbox/filters/add_sections.rb
|
100
101
|
- lib/nanoc/toolbox/filters/html_tidy.rb
|
101
102
|
- lib/nanoc/toolbox/helpers.rb
|
102
103
|
- lib/nanoc/toolbox/helpers/gravatar.rb
|
@@ -104,6 +105,8 @@ files:
|
|
104
105
|
- lib/nanoc/toolbox/helpers/navigation.rb
|
105
106
|
- lib/nanoc/toolbox/version.rb
|
106
107
|
- nanoc-toolbox.gemspec
|
108
|
+
- spec/data/filters/item_without_sections.html
|
109
|
+
- spec/filters/add_sections_spec.rb
|
107
110
|
- spec/filters/html_tidy_spec.rb
|
108
111
|
- spec/helpers/gravatar_spec.rb
|
109
112
|
- spec/helpers/html_tag_spec.rb
|