summaryrefslogtreecommitdiff
path: root/bin/util.rb
blob: 7b4805f68ea683f54d6fba95bdadcadcc2e93ba2 (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
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
# coding: utf-8
load 'pandoc.rb'
require 'erb'
require 'date'
require 'set'

$license_urls = {
	"CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/',
	'WTFPL-2' => "http://www.wtfpl.net/txt/copying/",
}
$person_uris = {
	"Luke Shumaker" => "https://lukeshu.com/",
	"Andrew Murrell" => "https://andrewdm.me/",
}
$person_emails = {
	"Luke Shumaker" => "lukeshu@parabola.nu",
	"Andrew Murrell" => "ImFromNASA@gmail.com",
}

$tag_names = {
	"DM" => "DMing Resource",
	"ES" => "Essay",
	"FF" => "Flash Fiction",
	"HB" => "Homebrew",
	"SS" => "Short Story",
	"WP" => "WIP",
}

class Tag
	def initialize(abbr)
		@abbr = abbr
	end
	def abbr
		@abbr
	end
	def name
		$tag_names[@abbr]
	end
	def html
		return "<a class=\"tag #{abbr}\" href=\"/tags/#{abbr}.html\">#{name}</a>"
	end
end

class Person
	def initialize(name)
		@name = name
	end
	def name
		@name
	end
	def uri
		$person_uris[@name]
	end
	def email
		$person_emails[@name]
	end
	def html
		if not email.nil?
			return "<a href=\"mailto:#{email}\">#{name}</a>"
		elsif not uri.nil?
			return "<a href=\"#{uri}\">#{name}</a>"
		else
			return @name
		end
	end
	def atom
		ret = ""
		ret += "<name>#{name}</name>" unless name.nil?
		ret += "<uri>#{uri}</uri>" unless uri.nil?
		ret += "<email>#{email}</email>" unless email.nil?
	end
end

class License
	def initialize(name)
		@name = name
	end
	def name
		@name
	end
	def url
		$license_urls[@name]
	end
	def html
		"<a href=\"#{url}\">#{name}</a>"
	end
end

class Page
	def initialize(infile)
		@infile = infile
	end

	def infile ; @infile                      ; end
	def input  ; @input ||= File.read(infile) ; end
	def pandoc
		if @pandoc.nil?
			types = {
				'md' => 'markdown'
			}

			ext = File.extname(infile).gsub(/^[.]/, '')
			type = types[ext] || ext
			@pandoc = Pandoc::load(type, input)

			if @pandoc['pandoc_format']
				@pandoc = Pandoc::load(@pandoc['pandoc_format'], input)
			end
		end
		@pandoc
	end

	def title   ; @title   ||=             pandoc['title']   || input.split("\n",2).first ; end
	def showtitle ; @showtitle ||= ! pandoc['title'].nil?                                 ; end

	def author  ; @author  ||= Person.new( pandoc['author']  || "Andrew Murrell")         ; end
	def license ; @license ||= License.new(pandoc['license'] || "CC BY-SA-3.0")           ; end
	def slug    ; @slug    ||= infile.sub(/\..*$/,'').sub(/^.*\//,'')                     ; end
	def content ; @content ||= pandoc.to('html5 '+(pandoc['pandoc_flags']||''))           ; end
	def head    ; @head    ||= pandoc['html_head_extra']                                  ; end
	def tags    ; @tags    ||= (pandoc['tags'] || '').split.map{|tag|Tag.new(tag)}        ; end

	def published
		if @published.nil?
			raw = pandoc['published']
			@published = Date.parse(raw) unless raw.nil?
		end
		if @published.nil?
			raw = `git log -n1 --reverse --format='%cI' -- #{infile}`
			@published = DateTime.iso8601(raw) unless raw.empty?
			if !updated.nil? && updated < @published
				@published = updated
			end
		end
		@published
	end

	def updated
		if @updated.nil?
			raw = pandoc['updated']
			@updated = Date.parse(raw) unless raw.nil?
		end
		if @updated.nil?
			raw = `git log -n1 --format='%cI' -- #{infile}`
			@updated = DateTime.iso8601(raw) unless raw.empty?
		end
		@updated
	end

	def rights
		years = `git log --date=format:'%Y' --format='%cd' -- .config/login.sh`.split('\n').map{|s|s.to_i}
		years.unshift(published.year) unless published.nil?
		years.unshift(updated.year) unless updated.nil?
		years = Set[*years]
		# TODO: simplify year spans
		@rights ||= "<p>The content of this page is Copyright © #{years.sort.join(', ')} #{author.html}.</p>\n" +
					"<p>This page is licensed under the #{license.html} license.</p>"
	end

	def src
		@src ||= infile.sub(/^(src|out)\//, '/')
	end

	def url
		if @url.nil?
			u = src.sub(/\.[^\/.]*$/, '.html').sub(/\/index[.]html$/, '')
			@url = u == '' ? '/' : u
		end
		@url
	end

	def breadcrumbs
		if @breadcrumbs.nil?
			bc = []
			u = url
			while u != "/"
				bc.unshift("<a href=\"#{u}\">#{File.basename(u, File.extname(u))}</a>")
				u = File.dirname(u)
			end
			bc.unshift("<a href=\"/\">Andrew D. Murrell</a>")
			@breadcrumbs = bc.join(' » ')
		end
		@breadcrumbs
	end
end

def html_escape(html)
	html
		.gsub('&', '&amp;')
		.gsub('>', '&gt;')
		.gsub('<', '&lt;')
end