-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_static
executable file
·361 lines (296 loc) · 8.42 KB
/
gen_static
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env ruby
require 'getoptlong'
require 'kramdown'
require 'rouge'
require 'erb'
require 'json'
require 'set'
require 'kramdown-metadata-parsers'
def write_rouge_css()
rougecss = Rouge::Themes::Base16.mode(:dark).render(scope: 'div.highlighter-rouge')
outputpath = "#{@output}/css/rouge.css"
File.open(outputpath, 'w') { |file| file.write(rougecss) }
end
def init_output()
ret = `mkdir -p "#{@output}/"`
ret = `cp -rp public/css "#{@output}/"`
ret = `cp -rp public/js "#{@output}/"`
write_rouge_css()
end
def is_external(link)
if link =~ /^http:/
return true
end
if link =~ /^https:/
return true
end
if link =~ /^mailto:/
return true
end
if link =~ /^tel:/
return true
end
if link =~ /^file:/
return true
end
if link =~ /^ftp:/
return true
end
return false
end
def detect_fenced_code_block(line)
if line.start_with?("```")
$fenced_code_block = ! $fenced_code_block
end
end
def detect_code_line(line)
if line.start_with?(" ")
return true
end
return false
end
def tr_autolink(line)
autolink = /<(.*)>/
if line.match(autolink)
return line.gsub!(autolink) do |m|
if is_external($1)
m="<"+$1+">"
else
m="<"+$1+".htm>"
end
end
else
return line
end
end
def tr_link(line)
begin
linkHash = /\[([^\]]+)\]\(([^#]+)(#[^)]+)\)/
if line.match(linkHash)
return line.gsub!(linkHash) do |m|
if is_external($2)
m="["+$1+"]("+$2+$3+")"
else
m="["+$1+"]("+$2+".htm"+$3+")"
end
end
else
link = /\[([^\]]+)\]\(([^)]+)\)/
if line.match(link)
return line.gsub!(link) do |m|
if is_external($2)
m="["+$1+"]("+$2+")"
else
m="["+$1+"]("+$2+".htm)"
end
end
else
return line
end
end
rescue TypeError
puts "error parsing "+line
end
end
def tr_line(line)
detect_fenced_code_block(line)
if ! $fenced_code_block
if ! detect_code_line(line)
# puts "B: #{line}"
line = tr_autolink(line)
line = tr_link(line)
# puts "A: #{line}"
end
end
return line
end
def add_htm(md)
out = ""
md.each_line {|s| out += tr_line(s)}
return out
end
def get_markdown(filepath)
if not File.file?(filepath)
raise "Error: File '#{filepath}' does not exist"
end
markdown = File.read(filepath, :encoding => 'utf-8')
return add_htm(markdown)
end
def get_keywords(markdown)
words = markdown.split(/\W+/)
keywords = Set.new()
words.each do |word|
if word =~ /\w+/
keywords.add(word.downcase)
end
end
return keywords.to_a
end
def get_kramdown_options()
options={:template => "public/body.erb"}
options[:input] = @parser
#rouge syntax_highlighter outputs pygments style html with a highlight
options[:syntax_highlighter] = 'rouge'
# While Rouge comes with many formatters, using them through kramdown limits the
# availibity and the configurability of the formatters
# https://kramdown.gettalong.org/syntax_highlighter/rouge.html
sho = Hash.new
# HTMLLegacy enables PRE and CODE tags after div
sho[:formatter] = Rouge::Formatters::HTMLLegacy
options[:syntax_highlighter_opts] = sho
options[:hard_wrap] = true
options[:smart_quotes] = ["apos", "apos", "quot", "quot"]
return options
end
def set_template_params()
# Set the class variables for Object
# Since the ERB template is run from the Kramdown::Document,
# This is an easy way to set simple to use variables
@site_prefix = @site_prefix
@site_title = @title
# hijack this variable to insert date for versioning purposes
@base_tag = "<meta name='date' content='#{Time::now().to_s}'>"
@home = @home
@home_url = @home+".htm"
@alpha_url = "alpha.htm"
@mtime_url = "mtime.htm"
@search_url = "search.htm"
# Search will be populated by javascript in statically gen search
@search = ""
end
def to_title(file)
# Strip .md if it exists
title = file.gsub(/.md$/, '')
title = title.gsub(/-/, ' ')
title = title.gsub(/([A-Z][a-z])/, ' \1').strip
title = title.split.map { |word| word[0].upcase+word[1..99999] }.join(' ')
return title
end
def write_special(filenames, options, name, subtitle)
markdown = ""
filenames.each do |filename|
markdown << "1. [#{filename}](#{filename}.htm)\n"
end
doc = Kramdown::Document.new(markdown, options)
@page_title = "#{subtitle}"
@body = doc.to_html
renderer = ERB.new(File.read(@template))
html = renderer.result()
# Write HTML
outputpath = "#{@output}/#{name}"
File.open(outputpath, 'w') { |file| file.write(html) }
end
# Search helper
def gen_json(filenames, keywords)
files = []
filenames.each do |filename|
files.push({:name => filename, :keywords => keywords[filename]})
end
data = {"files" => files}
return data.to_json
end
def write_search(options, filenames, keywords)
# json data about files
files_json = gen_json(filenames, keywords)
@body = <<-EOF
<div id="results"></div>
<script src="js/search.js"></script>
<script id="data" type="application/json">
#{files_json}
</script>
EOF
renderer = ERB.new(File.read(@template))
@page_title = "Search"
search_data = renderer.result()
outputpath = "#{@output}/search.htm"
File.open(outputpath, 'w') { |file| file.write(search_data) }
end
def set_opts()
opts = GetoptLong.new(
[ '--folder', '-f', GetoptLong::REQUIRED_ARGUMENT ],
[ '--output', '-o', GetoptLong::REQUIRED_ARGUMENT ],
[ '--title', '-t', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--parser', '-p', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--home', '-h', GetoptLong::OPTIONAL_ARGUMENT ])
@template = 'public/template.erb'
@folder = nil
@output = nil
@title = 'Documentation'
@parser = 'MetadataGFM'
@home = 'Index'
opts.each do |opt, arg|
case opt
when '--folder'
@folder = arg
when '--output'
@output = arg
when '--title'
if arg != ''
@title = arg
end
when '--parser'
if arg != ''
@name = arg
end
when '--home'
if arg != ''
@home = arg
end
end
end
end
def check_opts()
# For some reason GetoptLong does not handle REQUIRED_ARGUMENT
# Checking manually
if @folder == nil
raise "folder must be specified"
end
if @output == nil
raise "output must be specified"
end
if @output == ''
raise "output must be specified"
end
if @output == '/'
raise "output must not be root"
end
if @output == ENV['HOME']
raise "output must not be home"
end
end
## main ##
set_opts()
check_opts()
set_template_params()
init_output()
options = get_kramdown_options()
filenames = []
keywords = {}
Dir["#{@folder}/*"].each do |filepath|
# Skip Folders
next if File.directory? filepath
file = File.basename(filepath)
filenames.push(file)
outputpath = "#{@output}/#{file}.htm"
puts "Writing: #{outputpath}"
markdown = get_markdown(filepath)
keywords[file] = get_keywords(markdown)
doc = Kramdown::Document.new(markdown, options)
@body = doc.to_html
@page_title = doc.root.metadata["title"]
if @page_title == nil
@page_title = to_title(file)
end
renderer = ERB.new(File.read(@template))
html = renderer.result()
# Write HTML
File.open(outputpath, 'w') { |file| file.write(html) }
end
puts "Writing alpha"
filenames.sort!
write_special(filenames, options, "alpha.htm", "Alphabetical")
puts "Writing mtime"
filenames.sort_by! { |filename| - File.mtime("#{@folder}/#{filename}").to_i }
write_special(filenames, options, "mtime.htm", "Modified Time")
puts "Writing search"
write_search(options, filenames, keywords)