Project

General

Profile

Feature #22005 ยป convert_textile_to_markdown.rake

Hugues C., 2016-02-10 18:36

 
# Taken from http://stackoverflow.com/questions/9782121/how-to-convert-existing-redmine-wiki-from-textile-to-markdown
# Modified to be compatible with newer Pandoc versions with "markdown_strict" format
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",
"--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*")

version.update_attribute(:text, markdown)
end
end
end
    (1-1/1)