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

$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",
}

$url = URI::parse('https://www.andrewdm.me')

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 content ; @content ||= pandoc.to('html5 '+(pandoc['pandoc_flags']||''))           ; end
	def head    ; @head    ||= pandoc['html_head_extra']                                  ; end
	def class   ; @class   ||= pandoc['class']                                            ; end

	def tags
		if @tags.nil?
			raw = pandoc['tags'] || []
			if raw.is_a?(String)
				raw = raw.split
			end
			@tags = raw.map{|tag|Tag.new(tag)}
		end
		@tags
	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?
		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 published
		if @published.nil?
			unless _published.nil?
				@published = _published
			else
				unless _updated.nil?
					@published = _updated
				end
			end
			# sanity check
			unless _published.nil? or _updated.nil?
				if _updated < _published
					@published = _updated
				end
			end
		end
		@published
	end

	def updated
		if @updated.nil?
			unless _updated.nil?
				@updated = _updated
			else
				unless _published.nil?
					@updated = _published
				end
			end
		end
		@updated
	end

	def rights
		if published.nil? || updated.nil?
			years = ''
		else
			first = published.year
			last = updated.year

			years = `git log --date=format:'%Y' --format='%cd' -- .config/login.sh`.split('\n').map{|s|s.to_i}
			years.unshift(first)
			years.unshift(last)

			# Remove dups and git years outside of [first,last]
			# TODO: simplify year spans
			years = Set[*years.select{|i|i > first && i < last}].sort.join(', ')
		end
		@rights ||= "<p>The content of this page is Copyright © #{years} #{author.html}.</p>\n" +
					"<p>This page is licensed under the #{license.html} license.</p>"
	end

	def abssrcpath
		@srcpath ||= infile.sub(/^(src|out)\//, '/')
	end
	def absoutpath
		@outpath ||= abssrcpath.sub(/\.[^\/.]*$/, '.html').sub(/\/index[.]html$/, '')
	end

	def url
		@url ||= $url + absoutpath
	end
	def srcurl
		@srcurl ||= $url + abssrcpath
	end

	def breadcrumbs
		if @breadcrumbs.nil?
			bc = []
			u = url.path
			u = "/" if u == ""
			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

	def section
		return nil
	end
end

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

class ExternPage
	def initialize(metadata)
		@metadata = metadata
	end

	def title
		@metadata['title']
	end

	def content
		nil
	end

	def tags
		if @tags.nil?
			raw = @metadata['tags'] || []
			if raw.is_a?(String)
				raw = raw.split
			end
			@tags = raw.map{|tag|Tag.new(tag)}
		end
		return @tags
	end

	def url
		return $url + @metadata['url']
	end

	def author
		Person.new(@metadata['author'] || "Andrew Murrell")
	end

	def published
		str = @metadata['published']
		if str.nil? and ! @metadata['updated'].nil?
			str = @metadata['updated']
		end
		return Date.parse(str)
	end
	def updated
		str = @metadata['updated']
		if str.nil? and ! @metadata['published'].nil?
			str = @metadata['published']
		end
		return Date.parse(str)
	end

	def section
		return @metadata['section']
	end
end