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

$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/",
}
$person_emails = {
	"Luke Shumaker" => "lukeshu@sbcglobal.net",
}

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
	attr_accessor :title, :author, :gitdate, :date, :license, :slug, :content, :rights, :breadcrumbs
	def initialize(infile)
		input = File.read(infile)
		doc = Pandoc::load('markdown', input)

		if doc['markdown_options']
			doc = Pandoc::load('markdown'+doc['markdown_options'], input)
		end

		gitdate = `git log -n1 --format='%cI' -- #{infile}`

		@title   = doc['title']   || input.split("\n",2).first
		@author  = Person.new(doc['author']  || "Luke Shumaker")
		@gitdate = DateTime.iso8601(gitdate) unless gitdate.empty?
		@date    = Date.parse(doc['date']) unless doc['date'].nil?
		@license = License.new(doc['license'] || "CC BY-SA-3.0")
		@slug    = infile.sub(/\..*$/,'').sub(/^.*\//,'')
		@content = doc.to('html5')
		@rights  = "<p>The content of this page is Copyright © #{@date.year unless @date.nil?} #{@author.html}.</p>\n" +
				   "<p>This page is licensed under the #{@license.html} license.</p>"

		@breadcrumbs = '<a href="/">Luke Shumaker</a> » '
		if (@slug == 'index')
			@breadcrumbs += "blog"
		else
			@breadcrumbs += '<a href=/blog>blog</a> » ' + @slug
		end
	end
end

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