middleman-toc 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/middleman_toc.rb +36 -37
- data/lib/middleman_toc/link.rb +7 -0
- data/lib/middleman_toc/page.rb +95 -0
- data/lib/middleman_toc/paths.rb +31 -0
- data/lib/middleman_toc/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d92d0afc67a827ff2b546a0330868ad1d29eee94
|
4
|
+
data.tar.gz: d1103d95ecc62e2af21f8b5460d7747a0f0c6602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65fcce00a3c64d0cc071f7e3e9dbacdfc455ca0dc2b70cad3ba33cfd1b5262b8e659519c2a047c8fdda8091f5e905fdf8eca80f737062e2d32b0460b60ba001b
|
7
|
+
data.tar.gz: 652196367bf93e1274e5a8ddaa82c3fb1d6fa7665b1894df4bde3503a03d47dc747a6194b724856e51079838d692d8d9b217a7c6276c72c4076432c8a1a6fdab
|
data/lib/middleman_toc.rb
CHANGED
@@ -39,16 +39,18 @@ class MiddlemanToc
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
class
|
42
|
+
class Page
|
43
43
|
extend Forwardable
|
44
44
|
include Indent
|
45
45
|
|
46
46
|
def_delegators :@children, :empty?
|
47
|
-
attr_reader :level, :path, :children, :active
|
48
47
|
|
49
|
-
|
48
|
+
attr_reader :level, :path, :page, :children, :active
|
49
|
+
|
50
|
+
def initialize(level, path, page)
|
50
51
|
@level = level
|
51
52
|
@path = path
|
53
|
+
@page = page
|
52
54
|
@children = []
|
53
55
|
end
|
54
56
|
|
@@ -58,28 +60,22 @@ class MiddlemanToc
|
|
58
60
|
|
59
61
|
def activate(active)
|
60
62
|
children.each { |child| child.activate(active) }
|
61
|
-
@active =
|
63
|
+
@active = active == path
|
62
64
|
end
|
63
65
|
|
64
66
|
def render
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
def activate(active)
|
75
|
-
@active = active == path
|
67
|
+
if level == 1
|
68
|
+
render_children
|
69
|
+
else
|
70
|
+
html = title
|
71
|
+
html = link("/#{path}.html", html) if File.file?(filename)
|
72
|
+
html = [html, render_children].join("\n") unless children.empty?
|
73
|
+
item(html)
|
74
|
+
end
|
76
75
|
end
|
77
76
|
|
78
|
-
def
|
79
|
-
|
80
|
-
html = link("/#{path}.html", html) if File.file?(filename)
|
81
|
-
html = item(html)
|
82
|
-
html
|
77
|
+
def render_children
|
78
|
+
%(<ul#{%( class="active") if active?}>\n#{indent(children.map(&:render).join("\n"))}\n</ul>)
|
83
79
|
end
|
84
80
|
|
85
81
|
def link(href, content)
|
@@ -87,21 +83,29 @@ class MiddlemanToc
|
|
87
83
|
end
|
88
84
|
|
89
85
|
def item(content)
|
90
|
-
%(<li#{%( class="#{classes}")
|
91
|
-
end
|
92
|
-
|
93
|
-
def active?
|
94
|
-
# current == path
|
95
|
-
@active
|
86
|
+
%(<li#{%( class="#{classes}") unless classes.empty?}>#{content}</li>)
|
96
87
|
end
|
97
88
|
|
98
89
|
def classes
|
99
90
|
classes = []
|
100
91
|
classes << 'active' if active?
|
101
92
|
classes << 'directory' if directory?
|
93
|
+
classes << 'expanded' if active? && !children.empty? || expanded?
|
102
94
|
classes.join(' ')
|
103
95
|
end
|
104
96
|
|
97
|
+
def directory?
|
98
|
+
!children.empty?
|
99
|
+
end
|
100
|
+
|
101
|
+
def active?
|
102
|
+
@active
|
103
|
+
end
|
104
|
+
|
105
|
+
def expanded?
|
106
|
+
level == 1 || children.any?(&:active?)
|
107
|
+
end
|
108
|
+
|
105
109
|
def title
|
106
110
|
title = page_title if page
|
107
111
|
title || path_title
|
@@ -118,7 +122,7 @@ class MiddlemanToc
|
|
118
122
|
end
|
119
123
|
|
120
124
|
def path_title
|
121
|
-
path.sub(/[\d]{2}-/, '').titleize
|
125
|
+
File.basename(path).sub(/[\d]{2}-/, '').titleize
|
122
126
|
end
|
123
127
|
|
124
128
|
def filename
|
@@ -159,16 +163,11 @@ class MiddlemanToc
|
|
159
163
|
|
160
164
|
private
|
161
165
|
|
162
|
-
def build(
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
page = page_for("#{path}.html")
|
168
|
-
tree.children << Page.new(level, page, path, !child.empty?)
|
169
|
-
tree << child unless child.empty?
|
170
|
-
end
|
171
|
-
tree
|
166
|
+
def build(path, level = 1)
|
167
|
+
path = path.sub('./', '')
|
168
|
+
page = Page.new(level, path, page_for("#{path}.html"))
|
169
|
+
paths.select(path).each { |path| page.children << build(path, level + 1) }
|
170
|
+
page
|
172
171
|
end
|
173
172
|
|
174
173
|
def page_for(path)
|
@@ -0,0 +1,95 @@
|
|
1
|
+
class MiddlemanToc
|
2
|
+
class Page
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def_delegators :@children, :empty?
|
6
|
+
|
7
|
+
attr_reader :level, :path, :page, :children, :active
|
8
|
+
|
9
|
+
def initialize(level, path, page)
|
10
|
+
@level = level
|
11
|
+
@path = path
|
12
|
+
@page = page
|
13
|
+
@children = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def <<(child)
|
17
|
+
children << child
|
18
|
+
end
|
19
|
+
|
20
|
+
def activate(active)
|
21
|
+
children.each { |child| child.activate(active) }
|
22
|
+
@active = active == path
|
23
|
+
end
|
24
|
+
|
25
|
+
def render
|
26
|
+
if level == 1
|
27
|
+
render_children
|
28
|
+
else
|
29
|
+
html = title
|
30
|
+
html = link("/#{path}.html", html) if File.file?(filename)
|
31
|
+
html = [html, render_children].join("\n") unless children.empty?
|
32
|
+
item(html)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def render_children
|
37
|
+
%(<ul#{%( class="active") if active?}>\n#{indent(children.map(&:render).join("\n"))}\n</ul>)
|
38
|
+
end
|
39
|
+
|
40
|
+
def link(href, content)
|
41
|
+
%(<a href="#{href}">#{content}</a>)
|
42
|
+
end
|
43
|
+
|
44
|
+
def item(content)
|
45
|
+
%(<li#{%( class="#{classes}") unless classes.empty?}>#{content}</li>)
|
46
|
+
end
|
47
|
+
|
48
|
+
def classes
|
49
|
+
classes = []
|
50
|
+
classes << 'active' if active?
|
51
|
+
classes << 'directory' if directory?
|
52
|
+
classes << 'expanded' if active? && !children.empty? || expanded?
|
53
|
+
classes.join(' ')
|
54
|
+
end
|
55
|
+
|
56
|
+
def directory?
|
57
|
+
!children.empty?
|
58
|
+
end
|
59
|
+
|
60
|
+
def active?
|
61
|
+
@active
|
62
|
+
end
|
63
|
+
|
64
|
+
def expanded?
|
65
|
+
level == 1 || children.any?(&:active?)
|
66
|
+
end
|
67
|
+
|
68
|
+
def title
|
69
|
+
title = page_title if page
|
70
|
+
title || path_title
|
71
|
+
end
|
72
|
+
|
73
|
+
def page_title
|
74
|
+
if page.data.title
|
75
|
+
page.data.title
|
76
|
+
else
|
77
|
+
html = page.render(layout: false, no_images: true)
|
78
|
+
matches = html.match(/<h.+>(.*?)<\/h1>/)
|
79
|
+
matches[1] if matches
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def path_title
|
84
|
+
File.basename(path).sub(/[\d]{2}-/, '').titleize
|
85
|
+
end
|
86
|
+
|
87
|
+
def filename
|
88
|
+
"source/#{path}.md"
|
89
|
+
end
|
90
|
+
|
91
|
+
def indent(string)
|
92
|
+
string.split("\n").map { |line| "#{' ' * (level)}#{line}" }.join("\n")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class MiddlemanToc
|
2
|
+
class Paths < Struct.new(:sitemap)
|
3
|
+
def prev(path)
|
4
|
+
path = paths[index(path) - 1]
|
5
|
+
"#{path.sub('.', '')}.html" if path
|
6
|
+
end
|
7
|
+
|
8
|
+
def next(path)
|
9
|
+
path = paths[index(path) + 1]
|
10
|
+
"#{path.sub('.', '')}.html" if path
|
11
|
+
end
|
12
|
+
|
13
|
+
def select(dir)
|
14
|
+
paths.select { |path| path =~ %r(#{dir}/) }.reject { |path| path =~ %r(#{dir}/.+/) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def paths
|
18
|
+
@paths ||= begin
|
19
|
+
paths = sitemap.resources.map(&:path)
|
20
|
+
paths = paths.select { |path| path =~ /.html$/ }
|
21
|
+
paths = paths.map { |path| "./#{path.sub(/.html$/, '')}" }
|
22
|
+
paths = paths + paths.map { |path| ::File.dirname(path) } - ['.']
|
23
|
+
paths.sort.unshift('./index').uniq
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def index(path)
|
28
|
+
paths.index("./#{path.sub('.html', '')}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-toc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sven Fuchs
|
@@ -36,6 +36,9 @@ files:
|
|
36
36
|
- lib/middleman_toc.rb
|
37
37
|
- lib/middleman_toc/extension.rb
|
38
38
|
- lib/middleman_toc/helpers.rb
|
39
|
+
- lib/middleman_toc/link.rb
|
40
|
+
- lib/middleman_toc/page.rb
|
41
|
+
- lib/middleman_toc/paths.rb
|
39
42
|
- lib/middleman_toc/version.rb
|
40
43
|
- spec/spec_helper.rb
|
41
44
|
- spec/toc_spec.rb
|