Skip to content

Instantly share code, notes, and snippets.

@etc
Created September 27, 2011 19:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save etc/1246047 to your computer and use it in GitHub Desktop.
Save etc/1246047 to your computer and use it in GitHub Desktop.
A small script to convert WP-Footnotes style footnotes to PHP Markdown Extra style footnotes.
#!/usr/bin/env ruby
# A small script to convert WP-Footnotes style footnotes to PHP Markdown Extra
# style footnotes.
#
# Possibly useful for those who are migrating from Wordpress to Jekyll, were
# using WP-Footnotes, and wish to render their new Markdown pages with Kramdown
# (which supports PHP Markdown Extra style footnotes).
#
# For example, you might first run https://github.com/thomasf/exitwp and then run
# this in your _posts directory.
#
# - Outputs changed files with extension "new.markdown".
# - Not very well tested. In fact, not really tested at all.
# - USE AT YOUR OWN RISK.
def convertnotes(directory,files)
Dir.chdir(directory)
puts "Processing directory: " << Dir.pwd
Dir.glob(files).each do |filename|
File.open(filename,'r+') do |file|
newtext = ""
footnotes = []
# Replace ((footnote)) with [^n]
# (If the footnote text finishes with a series of brackets, we
# carry them into the footnote itself)
newtext << file.read.gsub(/\s\(\((.*?)\)\)(\)*)/m) { |s|
retain = $1 + $2
footnotes << retain
"[^" << footnotes.length.to_s << "]"
}
unless footnotes.empty?
puts "File " << filename << " had " << footnotes.length.to_s << " footnotes."
# Append list of "[^n]: footnote" lines
newtext << "\n"
footnotes.each_index { |i|
# First we strip any inherited blockquotes markup
footnotes[i].gsub!(/\n\s*>/,"")
# The we strip out remaining newlines, replacing with a space if it
# is in between non-whitespace characters
footnotes[i].gsub!(/(\S)\n(\S)/,'\\1 \\2')
footnotes[i].gsub!(/\n/,"")
# Then we output the footnote
newtext << "[^" << (i+1).to_s << "]: " << footnotes[i] << "\n"
}
# Write to file
puts "Writing to file: " << filename << ".new.markdown"
newfile = File.new(filename << ".new.markdown",'a')
newfile.write(newtext)
end
end
end
end
convertnotes('.','*.markdown')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment