Created
March 5, 2014 07:19
-
-
Save pi-chan/9362644 to your computer and use it in GitHub Desktop.
MediaWikiからエクスポートした記事をまあまあの感じでMarkdownにコンバートする
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
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