#!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'rdiscount' require 'erb' license_urls = { "CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/', 'WTFPL-2' => "http://www.wtfpl.net/txt/copying/", } template = 'template.erb' input = ARGV.first lines = File.read(input).split("\n") footnote_start = /^\[\^/ footnote_cont = /^(\t| {2,4})/ markdown = '' tags = {} footnotes = {} footnote_label = nil for line in lines do if (line =~ /^:/) (key, val) = line.sub(/^:/, '').split(/\s+/, 2) tags[key] = val else if (line =~ footnote_start) footnote_label, footnote_body = line.split(':', 2) footnote_label.gsub!(/[\^\[\]:]/, '') footnotes[footnote_label] = footnote_body+"\n" markdown += "[^#{footnote_label}]: 555PHONYFOOTNOTE555#{footnote_label}\n" elsif (!footnote_label.nil? and line =~ footnote_cont) footnotes[footnote_label] += line.sub(footnote_cont, '')+"\n" else footnote_label = nil markdown += line+"\n" end end end @title = tags['title'] || lines.first @copyright = tags['copyright'] || "Luke Shumaker" @license = tags['license'] || "CC BY-SA-3.0" unless license_urls[@license].nil? @license="#{@license}" end renderer = RDiscount.new(markdown) renderer.footnotes = true; @content = renderer.to_html footnotes.each do |label, body| @content.gsub!("555PHONYFOOTNOTE555#{label}", RDiscount.new(body).to_html) end erb = ERB.new(File.read(template)); erb.filename = template erb.run()