-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathestate.rb
274 lines (217 loc) · 5.19 KB
/
estate.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# estate.rb
# the erector of _why's Estate
$:.unshift '.'
require 'fileutils'
unless (2..3) === ARGV.length
puts "Usage: ruby estate.rb [--index-only] <indir> <outdir>"
exit
end
if ARGV.length == 3
if ARGV[0] == "--index-only"
index_only = true
dir_in = ARGV[1]
dir_out = ARGV[2]
else
puts "`#{ ARGV[0] }' isn't a valid option!"
exit
end
else
index_only = false
dir_in = ARGV[0]
dir_out = ARGV[1]
end
#
# helper functions for HTML output
#
module HTML
NBSP = " "
# <a href="...">
def a(text, url)
"<a href=\"#{url}\">#{text}</a>"
end
# <span class="...">
def span(klass, text)
"<span class=\"#{klass}\">#{text}</span>"
end
# <p>, <h1>, <h3>, <li>
[:p, :h1, :h3, :li].each do |tag|
# lone & neat
define_method(tag) { |text| "<#{tag}>#{text}</#{tag}>\n" }
# with id attribute
define_method("#{tag.to_s}_id") do |id, text|
"<#{tag} id=\"#{id}\">#{text}</#{tag}>\n"
end
end
end
include HTML
#
# function that interpolates variables in header.html
#
def interpolate_header(text, title, dir)
text = text.gsub("{title}", title)
text = text.gsub("{dir}", dir)
text
end
#
# class to make the HTML list
#
class Estate
def initialize
@footnote = 1
@notes = []
end
def make_list(list, horizontal = false, quiet = false)
if horizontal
out = ": "
else
out = "<ul>"
end
list.each do |item|
left, right = *item
if NOTES[left]
@notes << NOTES[left]
left << "<span class=\"footnote\"><sup><a href=\"##{@footnote}\">#{@footnote}</a></sup></span>"
@footnote += 1
end
if right.is_a? Array
out << li(left + make_list(right, right.delete(["horizontal"])))
else
if NOTES[right]
@notes << NOTES[right]
footnote = "<span class=\"footnote\"><sup><a href=\"##{@footnote}\">#{@footnote}</a></sup></span>"
@footnote += 1
else
footnote = ""
end
url = left
if url[0, 9] == "estate://"
url = url[9..-1]
domain = ""
else
domain = span("domain", "(" + domain_from_url(url) + ")")
end
domain = "" if quiet
if horizontal
out << a(right, url) + " " + domain
else
out << li(a(right, url) + footnote + " " + domain)
end
end
end
if !horizontal
out << "</ul>"
end
out
end
def domain_from_url(url)
url = url.dup
url.gsub!(/https?:\/\/www\./, "")
url.gsub!(/https?:\/\//, "")
url.split("/")[0]
end
attr_reader :notes
end
#
# get input and output dirs
#
if Dir[dir_in].empty?
puts "Input directory '#{dir_in}' doesn\'t exist."
exit
end
if Dir[dir_in + "/config.rb"].empty?
puts "Couldn't find '#{ dir_in }/config.rb"
exit
end
if Dir[dir_in + "/index.rb"].empty?
puts "Couldn't find '#{ dir_in }/index.rb"
exit
end
#
# just copy all the files to the output directory
#
unless index_only
FileUtils::rm_r(dir_out, :force => true)
FileUtils::cp_r(dir_in, dir_out)
end
#
# load some input files
#
require(dir_in + "/config.rb")
require(dir_in + "/index.rb")
if Dir[dir_in + "/header.html"].empty?
header = ''
else
header = File.read(dir_in + "/header.html")
end
if Dir[dir_in + "/footer.html"].empty?
footer = ''
else
footer = File.read(dir_in + "/footer.html")
end
#
# build index.html
#
File.open(dir_out + "/index.html", "w") do |f|
# headers
f << interpolate_header(header, TITLE, "")
f << INDEX_HEADER
# title and description
f << h1(TITLE)
f << p(DESCRIPTION)
# jump to
section_links = LIST.collect do |section|
name = section.first
a(name, "#" + name.tr(" ", "_"))
end
f << p_id("jump", "Jump to: " + section_links.join(" " + NBSP * 2)) unless section_links.empty?
# the list
estate = Estate.new
LIST.each do |section|
name, list = *section
f << h3_id(name.tr(" ", "_"), name)
desc = list.assoc('desc')
if desc
f << p(desc[1])
list.delete(desc)
end
quiet = list.delete(['quiet'])
f << estate.make_list(list, false, quiet)
end
# notes
unless estate.notes.empty?
f << h3("Notes")
estate.notes.each_index do |note|
f << "<p style=\"font-size: 10pt;\">"
f << "<span id=\"#{note + 1}\" class=\"footnote\"><sup>#{note + 1}</sup></span> "
f << estate.notes[note]
f << "</p>"
end
end
# footers
f << INDEX_FOOTER
f << footer
end
#
# convert .src.html files to .html files with headers and footers
#
unless index_only
Dir[dir_out + "/*/*.src.html"].each do |filename|
outfile = filename.gsub(/\.src\.html$/, ".html")
file_title = File.read(filename).match(/<h1>(.+?)<\/h1>/)[1]
File.open(outfile, "w") do |f|
f << interpolate_header(header, TITLE + " - " + file_title, "../")
f << "<p>back to <a href=\"../index.html\">#{TITLE}</a></p><hr />"
f << File.read(filename)
f << footer
end
end
end
#
# remove config files from output directory
#
unless index_only
to_delete = ["config.rb", "index.rb", "header.html", "footer.html"]
to_delete.collect! { |filename| dir_out + "/" + filename }
FileUtils::rm(to_delete, :force => true)
FileUtils::rm(Dir[dir_out + "/*/*.src.html"])
end