Skip to content

Instantly share code, notes, and snippets.

@dmyates
Created June 14, 2016 17:54
Show Gist options
  • Select an option

  • Save dmyates/3df6cbc9c9191c258f5d0d33c281b371 to your computer and use it in GitHub Desktop.

Select an option

Save dmyates/3df6cbc9c9191c258f5d0d33c281b371 to your computer and use it in GitHub Desktop.

Revisions

  1. dmyates created this gist Jun 14, 2016.
    38 changes: 38 additions & 0 deletions md2md.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    # 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