Rewrote render-liquid-template in Ruby

This commit is contained in:
Wen Kokke 2020-07-02 21:35:20 +01:00
parent 2748c8605c
commit 3c0394b8a0
4 changed files with 22 additions and 90 deletions

View file

@ -37,9 +37,6 @@ before_install:
- travis_retry curl -L https://www.stackage.org/stack/linux-x86_64 | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
# Install Pandoc
- travis_retry curl -L https://github.com/jgm/pandoc/releases/download/2.9.2.1/pandoc-2.9.2.1-1-amd64.deb -o $HOME/pandoc.deb && sudo dpkg -i $HOME/pandoc.deb
# Install Lua
- travis_retry curl -L http://www.lua.org/ftp/lua-5.4.0.tar.gz | tar zx && cd lua-5.4.0 && make linux test && sudo make install
- travis_retry curl -L https://luarocks.org/releases/luarocks-3.3.1.tar.gz | tar zx && cd luarocks-3.3.1 && ./configure && make && sudo make install
# Download and install agda, agda-stdlib, and agda2html
- make travis-setup

View file

@ -3,22 +3,12 @@ AGDA_FILES := $(shell find . -type f -and \( -path '*/src/*' -or -path '*/course
AGDAI_FILES := $(shell find . -type f -and \( -path '*/src/*' -or -path '*/courses/*' \) -and -name '*.agdai')
MARKDOWN_FILES := $(subst courses/,out/,$(subst src/,out/,$(subst .lagda.md,.md,$(AGDA_FILES))))
PLFA_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
RUBY := ruby
GEM := $(RUBY) -S gem
BUNDLE := $(RUBY) -S bundle
JEKYLL := $(BUNDLE) exec jekyll
HTML_PROOFER := $(BUNDLE) exec htmlproofer
LUA_FILES := $(shell find . -type f -and -path '*/epub/*' -and -name '*.lua')
LUA := lua
LUAROCKS := luarocks --lua-version=$(LUA_VERSION)
LUA_MODULES := lua_modules
PANDOC := /usr/bin/pandoc
ifneq ($(wildcard $(LUA_MODULES)),)
LUA_FLAGS += -l epub/set_paths
endif
ifeq ($(AGDA_STDLIB_VERSION),)
AGDA_STDLIB_URL := https://agda.github.io/agda-stdlib/
@ -67,7 +57,7 @@ out/epub/:
mkdir -p out/epub/
out/epub/plfa.epub: out/epub/ | $(AGDA_FILES) $(LUA_FILES) epub/main.css out/epub/acknowledgements.md
$(PANDOC) --strip-comments \
pandoc --strip-comments \
--css=epub/main.css \
--epub-embed-font='assets/fonts/mononoki.woff' \
--epub-embed-font='assets/fonts/FreeMono.woff' \
@ -84,7 +74,7 @@ out/epub/plfa.epub: out/epub/ | $(AGDA_FILES) $(LUA_FILES) epub/main.css out/epu
epub/index.md
out/epub/acknowledgements.md: src/plfa/acknowledgements.md _config.yml
$(LUA) $(LUA_FLAGS) epub/render-liquid-template.lua _config.yml $< $@
$(RUBY) epub/render-liquid-template.rb _config.yml $< $@
# Convert literal Agda to Markdown
@ -171,10 +161,7 @@ travis-setup:\
$(HOME)/.local/bin/acknowledgements\
$(HOME)/agda-stdlib-$(AGDA_STDLIB_VERSION)/src\
$(HOME)/.agda/defaults\
$(HOME)/.agda/libraries\
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/cjson\
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/tinyyaml.lua\
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/liquid.lua
$(HOME)/.agda/libraries
.phony: travis-setup
@ -249,22 +236,3 @@ travis-uninstall-acknowledgements:
travis-reinstall-acknowledgements: travis-uninstall-acknowledgements travis-reinstall-acknowledgements
.phony: travis-install-acknowledgements travis-uninstall-acknowledgements travis-reinstall-acknowledgements
# Lua
travis-install-lua:\
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/cjson\
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/tinyyaml.lua\
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/liquid.lua
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/cjson:
# Only this particular version works:
# https://github.com/mpx/lua-cjson/issues/56:
$(LUAROCKS) install --tree=$(LUA_MODULES) lua-cjson 2.1.0-1
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/tinyyaml.lua:
$(LUAROCKS) install --tree=$(LUA_MODULES) lua-tinyyaml
$(LUA_MODULES)/share/lua/$(LUA_VERSION)/liquid.lua:
$(LUAROCKS) install --tree=$(LUA_MODULES) liquid

View file

@ -1,52 +0,0 @@
local yaml = require 'tinyyaml'
local liquid = require 'liquid'
-- Given a file name, return its file descriptor.
-- Throws an error on failure.
local function errOpen (fname, mode)
local fd = io.open(fname, mode)
if fd == nil then
error('could not open file: ' .. fname)
end
return fd
end
-- Given a file name and some data, overwrite the file with the data.
-- Throws an error on failure.
local function errWrite (fname, data)
local fd = errOpen(fname, 'w')
local status, errstr = fd:write(data)
if status == nil then
error(fname .. ': ' .. errstr)
end
end
-- Given a file name, return that file's entire contents.
-- Throws an error on failure.
local function errReadAll (fname)
local data = errOpen(fname, 'r'):read('a')
if data == nil then
error('could not read from open file: ' .. fname)
end
return data
end
-- We must have exactly three arguments.
if #arg ~= 3 then
print('usage: ' .. arg[0] .. ' [yaml_file] [markdown_in_file] [markdown_out_file]')
os.exit(1)
end
-- 1. Read YAML metadata from file, which we nest under the key 'site'.
local metadata = { ['site'] = yaml.parse(errReadAll(arg[1])) }
-- 2. Read markdown document from file.
local document = errReadAll(arg[2])
-- 3. Render the markdown document with the YAML metadata as context.
local template = liquid.Template:parse(document)
local result = template:render(liquid.InterpreterContext:new(metadata))
-- 4. Write rendered document to output file.
errWrite(arg[3], result)

View file

@ -0,0 +1,19 @@
#!/usr/bin/env ruby
require 'yaml'
require 'liquid'
unless ARGV.length == 3
abort "Usage: render-liquid-template.rb [yaml_file] [markdown_in_file] [markdown_out_file]"
end
yaml_file, markdown_file_in, markdown_file_out = ARGV
[yaml_file, markdown_file_in].each do |file|
unless File.file? file
abort "File does not exist: '%s'" % [file]
end
end
metadata = { 'site' => (YAML.load (File.read yaml_file)) }
template = Liquid::Template.parse (File.read markdown_file_in)
File.write markdown_file_out, (template.render metadata)