Created
June 14, 2016 17:54
-
-
Save dmyates/3df6cbc9c9191c258f5d0d33c281b371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert all files in dir from Ghost-compatible markdown to Hugo-compatible markdown | |
# (changes footnotes and headings) | |
# | |
# Usage: ruby md2md.rb /path/to/posts | |
# #Heading --> # Heading | |
def headings(str) | |
str.gsub(/(#+)(?=[^ #])/i,'\1 ') | |
end | |
# this[^n] is[^n] footnoted[^n] --> this[^m] is[^n] footnoted[^o] | |
def footnote_refs(str) | |
thing = ('m'..'z').to_a.cycle.each | |
while str =~ /\[\^n\](?=[^:])/ | |
str.sub!(/\[\^n\](?=[^:])/,"[^#{thing.next}]") | |
end | |
str | |
end | |
# [^n]: Blah blah\n[^n]: Further blah\n[^n]: More blah --> [^m]: Blah blah\n[^n]: Further blah\n[^o]: More blah | |
def footnotes(str) | |
thing = ('m'..'z').to_a.cycle.each | |
while str =~ /\[\^n\]:/ | |
str.sub!(/\[\^n\]:/,"[^#{thing.next}]:") | |
end | |
str | |
end | |
# Do this to every file.md in-place (living dangerously) | |
Dir.glob("#{ARGV[0]}/**/*.md") do |file| | |
f = open(file).read | |
f = headings(f) | |
f = footnote_refs(f) | |
f = footnotes(f) | |
File.open(file, 'w') do |fl| | |
fl.write f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment