Skip to content

Instantly share code, notes, and snippets.

@agmarrugo
Created October 1, 2012 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agmarrugo/3814178 to your computer and use it in GitHub Desktop.
Save agmarrugo/3814178 to your computer and use it in GitHub Desktop.
A script for writing Day One footnotes inline.
#!/usr/bin/env ruby
def e_sh(str)
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\')
end
input = STDIN.read
footnotes = input.scan(/\(\*(.*?)\*\)/m)
# existing = input.scan(/^\[\^fn(\d+)\]: /i)
# counter = existing.empty? ? 1 : existing.uniq.sort[-1].join.to_i + 1
counter = 1
output = []
footnotes.each {|note|
output << {'orig' => note[0], 'title' => "#{counter}", 'footnote' => note[0] }
counter += 1
}
o = []
output.each_with_index { |x,i|
o.push("#{x['title']}. #{x['footnote'].gsub(/\n\n(\s*.)/,"\n\n\t\\1")}")
input.gsub!(/\(\*#{e_sh x['orig']}\*\)/m,"^#{x['title']}")
}
puts input.strip + "\n\n\*\*\*\*" + "\n\n#{o.join("\n\n")}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment