forked from railsbridge/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contents.rb
executable file
·170 lines (141 loc) · 4.04 KB
/
contents.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class Contents < Erector::Widget
attr_accessor :site_dir
attr_accessor :page_name
needs :page_name, :site_name, :site_dir => nil
def initialize options
super options
self.page_name = options[:page_name]
if options.include? :site_dir
self.site_dir = options[:site_dir]
else
here = File.expand_path File.dirname(__FILE__)
top = File.expand_path "#{here}/.."
self.site_dir = "#{top}/sites/#{@site_name}"
end
end
def site_files ext
Dir.glob("#{site_dir}/*.{#{ext}}").sort
end
def subpages_for filename
links = []
content = open("#{site_dir}/#{filename}").read()
# (markdown) links of the form: [link text](link_page)
content.scan /\[.*?\]\((.*?)\)/ do |link, _|
next if (link =~ /^http/)
next if (link =~ /(jpg|png)$/)
links.push(link) if !links.include? link
end
# (stepfiles) links of the form: link "next page"
content.scan /link\s*["'](.*?)["']/ do |link, _|
links.push(link) if !links.include? link
end
links
end
def next_step_for filename
content = open("#{site_dir}/#{filename}").read()
# (stepfiles) links of the form: stepfile "next page"
content.scan /next_step\s*["'](.*?)["']/ do |link, _|
return link
end
return nil
end
def collect_subpages page
if subpages[page] and !subpages[page].empty?
return [page] + subpages[page].map { |subpage| collect_subpages subpage }
end
return page
end
def find_next_page page
return nextpages[page] if nextpages[page]
if subpages[page]
return subpages[page].map { |subpage| find_next_page subpage }.compact.first
end
return nil
end
# Generate an array of pages on this site arranged hierarchically.
#
# The array is of the format:
# [
# "root_page",
# ["second_page",
# "subpage_of_second_page",
# "another_subpage_on_the_same_level"],
# ["third_page",
# ["subpage_of_third_page",
# "subpage_of_subpage_of_third_page"]],
# "fourth page"
# ]
# ... and so on
# This code assumes that a site is a linearly ordered series of pages
# linked by "next_step" declarations.
# Each page can contain many subpages, and the "next_step" for the outer page
# may be hidden deeply within the tree, but it must be there and it must be
# unique (one side of the subtree branch shouldn't go to next_step "this"
# while another goes to next_step "that").
def hierarchy
result = []
next_page = File.basename(site_dir)
while next_page do
this_page = next_page
result.push(collect_subpages this_page)
next_page = find_next_page this_page
end
result
end
def all_pages
site_files("mw,md,step").map { |file| File.basename(file).sub(/(\..*)$/, '') }.sort
end
def orphans
all_pages - hierarchy.flatten.uniq
end
def _page_links type="subpages"
site_files("mw,md,step").inject({}) do |result, filename|
page = File.basename(filename)
page_no_ext = page.sub(/(\..*)$/, '')
result[page_no_ext] = send("#{type}_for", page)
result
end
end
def subpages
@subpages ||= _page_links("subpages")
end
def nextpages
@nextpages ||= _page_links("next_step")
end
def toc_link page
link_text = page.split(/[-_]/).map{|s|s.capitalize}.join(' ')
path = page.start_with?('/') ? page : "/#{@site_name}/" + page
li do
if page == page_name
span link_text, class: 'current'
else
a link_text, href: path
end
yield if block_given?
end
end
def toc_list toc_items
ul do
toc_items.each do |toc_item|
if toc_item.instance_of? Array
toc_link toc_item.first do
toc_list toc_item[1..toc_item.length]
end
else
toc_link toc_item
end
end
end
end
def content
div id: "table_of_contents", class: "toc" do
toc_list hierarchy
unless orphans.empty?
h1 "Other Pages"
ul do
orphans.each { |orphan| toc_link orphan }
end
end
end
end
end