Skip to content

@etc /convertnotes.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.