Created
June 4, 2015 08:25
-
-
Save othree/8563b7f9018730666b95 to your computer and use it in GitHub Desktop.
redmine_convert_textile_to_markdown.rake
This file contains 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
task :convert_textile_to_markdown => :environment do | |
require 'tempfile' | |
WikiContent.all.each do |wiki| | |
([wiki] + wiki.versions).each do |version| | |
textile = version.text | |
src = Tempfile.new('textile') | |
src.write(textile) | |
src.close | |
dst = Tempfile.new('markdown') | |
dst.close | |
command = [ | |
"pandoc", | |
"-f", | |
"textile", | |
"-t", | |
"markdown", | |
src.path, | |
"-o", | |
dst.path, | |
] | |
system(*command) or raise "pandoc failed" | |
dst.open | |
markdown = dst.read | |
# remove the \ pandoc puts before * and > at begining of lines | |
markdown.gsub!(/^((\\[*>])+)/) { $1.gsub("\\", "") } | |
# add a blank line before lists | |
markdown.gsub!(/^([^*].*)\n\*/, "\\1\n\n*") | |
version.update_attribute(:text, markdown) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment