summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-08-27 17:36:46 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-08-27 17:36:46 -0400
commitb373a3a6e1702e7514bb405122a2311d16d85fcd (patch)
tree1b8ededef1b8ac60ec3b1a8a347cfc1669f61bb1
parentd4359dc767d3524a16f529f3545d89ab558e1b8f (diff)
Teach it to make atom:entry files
-rw-r--r--.gitignore2
-rw-r--r--Makefile30
-rwxr-xr-xpagerender.rb39
-rw-r--r--template.atom.erb13
-rw-r--r--template.html.erb (renamed from template.erb)3
-rw-r--r--util.rb51
-rwxr-xr-xwrite-ifchanged25
7 files changed, 135 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9cf059f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/.var*
+/.tmp* \ No newline at end of file
diff --git a/Makefile b/Makefile
index de1cfa9..b144c89 100644
--- a/Makefile
+++ b/Makefile
@@ -5,11 +5,27 @@ articles = $(filter-out public/index,$(patsubst %.md,%,$(wildcard public/*.md)))
all: public/index.html $(addsuffix .html,$(articles))
-public/%.html: public/%.md pagerender.rb template.erb Makefile
- ./pagerender.rb $< > $@
-public/index.md: public/ $(addsuffix .md,$(articles)) index.rb Makefile
- ./index.rb $(filter-out public/ index.rb Makefile $@,$^) > $@
- touch public/
- touch $@
+public/%.html: public/%.md pagerender.rb template.html.erb util.rb Makefile
+ ./pagerender.rb html $< > $@
+public/%.atom: public/%.md pagerender.rb template.atom.erb util.rb Makefile
+ ./pagerender.rb atom $< > $@
+public/index.md: .var.articles $(addsuffix .md,$(articles)) index.rb Makefile
+ ./index.rb $(filter %.md,$^) > $@
+
+.var.%: FORCE
+ @printf '%s' $(call quote.shell,$($*)) | sed 's/^/#/' | ./write-ifchanged $@
+-include $(wildcard .var.*)
+
clean:
- rm -f -- public/*.html public/index.md
+ rm -f -- public/*.html public/*.atom public/index.md .var* .tmp*
+
+.PHONY: FORCE
+.PHONY: all clean
+
+define nl
+
+
+endef
+# I put this as the last line in the file because it confuses Emacs syntax
+# highlighting and makes the remainder of the file difficult to edit.
+quote.shell = $(subst $(nl),'$$'\n'','$(subst ','\'',$1)')
diff --git a/pagerender.rb b/pagerender.rb
index 67c5aab..3c122bc 100755
--- a/pagerender.rb
+++ b/pagerender.rb
@@ -1,21 +1,23 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
load 'pandoc.rb'
+load 'util.rb'
require 'erb'
require 'date'
-license_urls = {
+$license_urls = {
"CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/',
'WTFPL-2' => "http://www.wtfpl.net/txt/copying/",
}
-author_urls = {
- "Luke Shumaker" => "mailto:lukeshu@sbcglobal.net",
+$person_uris = {
+ "Luke Shumaker" => "https://lukeshu.com/",
+}
+$person_emails = {
+ "Luke Shumaker" => "lukeshu@sbcglobal.net",
}
-template = 'template.erb'
-infile = ARGV.first
-
-Pandoc::prog='pandoc'
+template = "template.#{ARGV[0]}.erb"
+infile = ARGV[1]
input = File.read(infile)
doc = Pandoc::load('markdown', input)
@@ -24,26 +26,25 @@ 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 = doc['author'] || "Luke Shumaker"
+@author = Person.new(doc['author'] || "Luke Shumaker")
+@gitdate = DateTime.iso8601(gitdate) unless gitdate.empty?
@date = Date.parse(doc['date']) unless doc['date'].nil?
-@license = doc['license'] || "CC BY-SA-3.0"
-unless license_urls[@license].nil?
- @license="<a href=\"#{license_urls[@license]}\">#{@license}</a>"
-end
-unless author_urls[@author].nil?
- @author="<a href=\"#{author_urls[@author]}\">#{@author}</a>"
-end
+@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 (infile =~ /.*\/index(\..*)?/)
+if (@slug == 'index')
@breadcrumbs += "blog"
else
- @breadcrumbs += '<a href=/blog>blog</a> » ' + infile.sub(/\..*$/,'').sub(/^.*\//,'')
+ @breadcrumbs += '<a href=/blog>blog</a> » ' + @slug
end
-@content = doc.to('html5')
-
erb = ERB.new(File.read(template));
erb.filename = template
erb.run()
diff --git a/template.atom.erb b/template.atom.erb
new file mode 100644
index 0000000..da52b79
--- /dev/null
+++ b/template.atom.erb
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/<%= @slug %>.md"/>
+ <link rel="alternate" type="text/application/atom+xml" href="/blog/<%= @slug %>.atom"/>
+ <link rel="alternate" type="text/html" href="/blog/<%= @slug %>.html"/>
+ <id>https://lukeshu.com/blog/<%= @slug %>.html</id>
+ <updated><%= @gitdate.rfc3339 %></updated>
+ <published><%= @date.rfc3339 %></published>
+ <title><%= @title %></title>
+ <content type="html"><%= html_escape(@content) %></content>
+ <author><%= @author.atom %></author>
+ <rights type="html"><%= html_escape(@rights) %></rights>
+</entry>
diff --git a/template.erb b/template.html.erb
index c63acf9..af6a6f4 100644
--- a/template.erb
+++ b/template.html.erb
@@ -11,8 +11,7 @@
<%= @content %>
</article>
<footer>
-<p>The content of this page is Copyright © <%= @date.year unless @date.nil? %> <%= @author %>.</p>
-<p>This page is licensed under the <%= @license %> license.</p>
+<%= @rights %>
</footer>
</body>
</html>
diff --git a/util.rb b/util.rb
new file mode 100644
index 0000000..f7bd1b9
--- /dev/null
+++ b/util.rb
@@ -0,0 +1,51 @@
+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
+
+def html_escape(html)
+ html
+ .gsub('&', '&amp;')
+ .gsub('>', '&gt;')
+ .gsub('<', '&lt;')
+end
diff --git a/write-ifchanged b/write-ifchanged
new file mode 100755
index 0000000..185ceb0
--- /dev/null
+++ b/write-ifchanged
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+# Copyright (C) 2015 Luke Shumaker
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+outfile=$1
+tmpfile="$(dirname "$outfile")/.tmp${outfile##*/}"
+
+cat > "$tmpfile" || exit $?
+if cmp -s "$tmpfile" "$outfile"; then
+ rm -f "$tmpfile" || :
+else
+ mv -f "$tmpfile" "$outfile"
+fi