Created
October 28, 2015 02:07
-
-
Save danarnold/639164d8b980b42e2298 to your computer and use it in GitHub Desktop.
Convert Redmine wiki and issues from pandoc to markdown, based on http://www.hitmaroc.net/216445-2249-how-convert-existing-redmine-wiki-textile-markdown.html
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 | |
markdown = convert_to_markdown textile | |
puts "-- Changing the text of wiki ##{wiki.id} to:" | |
puts markdown | |
version.update_attribute(:text, markdown) | |
end | |
end | |
Issue.all.each do |issue| | |
textile = issue.description | |
unless textile.nil? | |
markdown = convert_to_markdown textile | |
issue.update_attribute(:description, markdown) | |
puts "-- Changing the text of issue ##{issue.id} to:" | |
puts markdown | |
end | |
issue.journals.each do |journal| | |
textile = issue.notes | |
next if textile.nil? | |
markdown = convert_to_markdown textile | |
puts "-- Changing journal #{journal.id} for ##{issue.id} to:" | |
puts "-- #{markdown}" | |
journal.update_attribute(:notes, markdown) | |
end | |
end | |
end | |
def convert_to_markdown(textile) | |
src = Tempfile.new('textile') | |
src.write(textile) | |
src.close | |
dst = Tempfile.new('markdown') | |
dst.close | |
command = [ | |
"pandoc", | |
"--no-wrap", | |
"--smart", | |
"-f", | |
"textile", | |
"-t", | |
"markdown_strict", | |
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*") | |
markdown | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment