summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
authorCharlotte Koch <dressupgeekout@gmail.com>2023-08-17 09:43:03 -0700
committerCharlotte Koch <dressupgeekout@gmail.com>2023-08-17 09:44:00 -0700
commitf4f8589d49f4ab946ff9bd5d734418e1d3998935 (patch)
tree8f6506b808b715d1e65f9348b91445ee09f6f415 /script
parentac1e192a52c37bba1787ad9e1f07d07d9765d39b (diff)
Render different frontmatter for different editions
Diffstat (limited to 'script')
-rw-r--r--script/erbber.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/script/erbber.rb b/script/erbber.rb
new file mode 100644
index 0000000..bb79095
--- /dev/null
+++ b/script/erbber.rb
@@ -0,0 +1,55 @@
+#!/usr/bin/env ruby
+#
+# WilloraPDF template renderer
+# Charlotte Koch <dressupgeekout@gmail.com>
+#
+
+require 'erb'
+require 'optparse'
+
+input = nil
+defines = {}
+
+# Parse command line options.
+parser = OptionParser.new do |opts|
+ opts.on("-i", "--input FILE") { |path| input = File.expand_path(path) }
+ opts.on("-D", "--define KEY=VALUE") { |arg| d = arg.split("="); defines[d[0]] = d[1]; }
+end
+parser.parse!(ARGV)
+
+# Validate the command line options.
+if not File.file?(input)
+ $stderr.puts("FATAL: no such file: #{input}")
+ exit 1
+end
+
+defines.each do |k, v|
+ if v && !k
+ $stderr.puts("pair is missing a key: #{[k,v].inspect}")
+ exit 1
+ end
+
+ if k && !v
+ $stderr.puts("pair is missing a value: #{[k,v].inspect}")
+ exit 1
+ end
+end
+
+# Set up the variable binding sandbox which will be given to the template.
+class OurEnvironment
+ attr_reader :var
+
+ def initialize(data)
+ @var = data
+ end
+
+ def get_binding
+ return binding
+ end
+end
+
+our_env = OurEnvironment.new(defines)
+
+# Render the template to standard output.
+result = ERB.new(File.read(input)).result(our_env.get_binding)
+$stdout.puts(result)