# 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 "#{name}"
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 "#{name}"
elsif not uri.nil?
return "#{name}"
else
return @name
end
end
def atom
ret = ""
ret += "#{name}" unless name.nil?
ret += "#{uri}" unless uri.nil?
ret += "#{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
"#{name}"
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 ||= "
The content of this page is Copyright © #{years.sort.join(', ')} #{author.html}.
\n" +
"This page is licensed under the #{license.html} license.
"
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("#{File.basename(u, File.extname(u))}")
u = File.dirname(u)
end
bc.unshift("Andrew D. Murrell")
@breadcrumbs = bc.join(' » ')
end
@breadcrumbs
end
end
def html_escape(html)
html
.gsub('&', '&')
.gsub('>', '>')
.gsub('<', '<')
end