From b373a3a6e1702e7514bb405122a2311d16d85fcd Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 27 Aug 2016 17:36:46 -0400 Subject: Teach it to make atom:entry files --- .gitignore | 2 ++ Makefile | 30 +++++++++++++++++++++++------- pagerender.rb | 39 ++++++++++++++++++++------------------- template.atom.erb | 13 +++++++++++++ template.erb | 18 ------------------ template.html.erb | 17 +++++++++++++++++ util.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ write-ifchanged | 25 +++++++++++++++++++++++++ 8 files changed, 151 insertions(+), 44 deletions(-) create mode 100644 .gitignore create mode 100644 template.atom.erb delete mode 100644 template.erb create mode 100644 template.html.erb create mode 100644 util.rb create mode 100755 write-ifchanged 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="#{@license}" -end -unless author_urls[@author].nil? - @author="#{@author}" -end +@license = License.new(doc['license'] || "CC BY-SA-3.0") +@slug = infile.sub(/\..*$/,'').sub(/^.*\//,'') +@content = doc.to('html5') +@rights = "

The content of this page is Copyright © #{@date.year unless @date.nil?} #{@author.html}.

\n" + + "

This page is licensed under the #{@license.html} license.

" @breadcrumbs = 'Luke Shumaker » ' -if (infile =~ /.*\/index(\..*)?/) +if (@slug == 'index') @breadcrumbs += "blog" else - @breadcrumbs += 'blog » ' + infile.sub(/\..*$/,'').sub(/^.*\//,'') + @breadcrumbs += 'blog » ' + @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 @@ + + + + + + https://lukeshu.com/blog/<%= @slug %>.html + <%= @gitdate.rfc3339 %> + <%= @date.rfc3339 %> + <%= @title %> + <%= html_escape(@content) %> + <%= @author.atom %> + <%= html_escape(@rights) %> + diff --git a/template.erb b/template.erb deleted file mode 100644 index c63acf9..0000000 --- a/template.erb +++ /dev/null @@ -1,18 +0,0 @@ - - - - - <%= @title %> — Luke Shumaker - - - -
<%= @breadcrumbs %>
-
-<%= @content %> -
- - - diff --git a/template.html.erb b/template.html.erb new file mode 100644 index 0000000..af6a6f4 --- /dev/null +++ b/template.html.erb @@ -0,0 +1,17 @@ + + + + + <%= @title %> — Luke Shumaker + + + +
<%= @breadcrumbs %>
+
+<%= @content %> +
+ + + 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 "#{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 + +def html_escape(html) + html + .gsub('&', '&') + .gsub('>', '>') + .gsub('<', '<') +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 . + +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 -- cgit v1.2.3