#!/usr/bin/ruby -W0
# Author: Vincent Landgraf <
[email protected]>
index, footnodes = {}, {}
# read the file and seperate the file by FILE_SEP
body, trailer = open(ARGV[0], 'r') { |f| f.read }.split('@footnote:')
# build a index that holds contains the order like {"[4]"=>2, "[1]"=>1}
body.scan(/\[\d+\]/).uniq.each { |r| index[r] = index.size + 1 }
# scan the tailer and build footnodes table. e.g.
# {5=>"Annoying Link.",
# 1=>"Al Fabetus: \"on characters and animals\", 1888, self published" }
# raise an error if a footnode is not defined but referenced
trailer.scan(/^(\[\d+\])\s+(.*)$/) do
raise "Error: Footnode #{$1} is not defined" if index[$1].nil?
footnodes[index[$1]] = $2
end
# print body, '@footnote:' and the footnode table
puts body.gsub!(/(\[\d+\])/) { '[' + index[$1].to_s + ']' }
puts '@footnote:'
footnodes.keys.sort.each { |k| puts '[' + k.to_s + '] ' + footnodes[k] }