Skip to content

Instantly share code, notes, and snippets.

@pi-chan
Created March 5, 2014 07:19
Show Gist options
  • Save pi-chan/9362644 to your computer and use it in GitHub Desktop.
Save pi-chan/9362644 to your computer and use it in GitHub Desktop.
MediaWikiからエクスポートした記事をまあまあの感じでMarkdownにコンバートする
require "Nokogiri"
xml = File.open("./data.xml").read
doc = Nokogiri.XML(xml)
def convert_to_markdown(mw)
mw.gsub!(/^# /, '1. ')
mw.gsub!(/^\* /, '- ')
mw.gsub!(/^====(.+)====$/, '#### \1')
mw.gsub!(/^===(.+)===$/, '### \1')
mw.gsub!(/^==(.+)==$/, '## \1')
mw.gsub!(/^=(.+)=$/, '# \1')
mw.gsub!(/\[([\S]+) (.+)\]/, '[\2](\1)')
mw
end
doc.search("page").each do |page|
title = page.search("title").first.children.first.text
body = page.search("text").first.children.first.text
body = convert_to_markdown(body)
f = File.open("#{title}.md", "w")
f.write("#{title}\n\n")
f.write(body)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment