summaryrefslogtreecommitdiff
path: root/pagerender.rb
blob: ecedaae29313e930e820bd4becf9925929fd2ddf (plain)
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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rdiscount'
require 'erb'

license_urls = {
	"CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/',
	'WTFPL-2' => "http://www.wtfpl.net/txt/copying/",
}

template = 'template.erb'
input = ARGV.first

lines = File.read(input).split("\n")

footnote_start = /^\[\^/
footnote_cont = /^(\t| {2,4})/

markdown = ''
tags = {}
footnotes = {}
footnote_label = nil
for line in lines do
	if (line =~ /^:/)
		(key, val) = line.sub(/^:/, '').split(/\s+/, 2)
		tags[key] = val
	else
		if (line =~ footnote_start)
			footnote_label, footnote_body = line.split(':', 2)
			footnote_label.gsub!(/[\^\[\]:]/, '')
			footnotes[footnote_label] = footnote_body+"\n"
			markdown += "[^#{footnote_label}]: 555PHONYFOOTNOTE555#{footnote_label}\n"
		elsif (!footnote_label.nil? and line =~ footnote_cont)
			footnotes[footnote_label] += line.sub(footnote_cont, '')+"\n"
		else
			footnote_label = nil
			markdown += line+"\n"
		end
	end
end

@title = tags['title'] || lines.first
@copyright = tags['copyright'] || "Luke Shumaker"
@license = tags['license'] || "CC BY-SA-3.0"
unless license_urls[@license].nil?
	@license="<a href=\"#{license_urls[@license]}\">#{@license}</a>"
end

renderer = RDiscount.new(markdown)
renderer.footnotes = true;
@content = renderer.to_html
footnotes.each do |label, body|
	@content.gsub!("555PHONYFOOTNOTE555#{label}", RDiscount.new(body).to_html)
end

erb = ERB.new(File.read(template));
erb.filename = template
erb.run()