convert_textile_to_markdown.rake

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

Download (1.07 KB)

 
1
# Taken from http://stackoverflow.com/questions/9782121/how-to-convert-existing-redmine-wiki-from-textile-to-markdown
2
# Modified to be compatible with newer Pandoc versions with "markdown_strict" format
3
task :convert_textile_to_markdown => :environment do
4
  require 'tempfile'
5
  WikiContent.all.each do |wiki|
6
    ([wiki] + wiki.versions).each do |version|
7
      textile = version.text
8
      src = Tempfile.new('textile')
9
      src.write(textile)
10
      src.close
11
      dst = Tempfile.new('markdown')
12
      dst.close
13

    
14
      command = [
15
        "pandoc",
16
        "--no-wrap",
17
        "--smart",
18
        "-f",
19
        "textile",
20
        "-t",
21
        "markdown_strict",
22
        src.path,
23
        "-o",
24
        dst.path,
25
      ]
26
      system(*command) or raise "pandoc failed"
27

    
28
      dst.open
29
      markdown = dst.read
30

    
31
      # remove the \ pandoc puts before * and > at begining of lines
32
      markdown.gsub!(/^((\\[*>])+)/) { $1.gsub("\\", "") }
33

    
34
      # add a blank line before lists
35
      markdown.gsub!(/^([^*].*)\n\*/, "\\1\n\n*")
36

    
37
      version.update_attribute(:text, markdown)
38
    end
39
  end
40
end