public
Last active

A small script to convert WP-Footnotes style footnotes to PHP Markdown Extra style footnotes.

  • Download Gist
convertnotes.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#!/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')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.