Skip to content

Instantly share code, notes, and snippets.

@khapota
Forked from yuumi3/redmine_textile2md.rb
Created February 25, 2016 07:50
Show Gist options
  • Save khapota/1bce92f26d1b7843e0b8 to your computer and use it in GitHub Desktop.
Save khapota/1bce92f26d1b7843e0b8 to your computer and use it in GitHub Desktop.
Convert Textile to Markdown contents in Redmin
def textile_to_markdown(textile)
d = []
pre = false
table_header = false
text_line = false
textile.each_line do |s|
s.chomp!
if pre
if s =~ /<\/pre>/
d << "~~~"
pre = false
else
d << s
end
next
end
s.gsub!(/(^|\s)\*([^\s\*].*?)\*(\s|$)/, " **\\2** ")
s.gsub!(/(^|\s)@([^\s].*?)@(\s|$)/, " `\\2` ")
s.gsub!(/(^|\s)-([^\s].*?)-(\s|$)/, " ~~\\2~~ ")
s.gsub!(/"(.*?)":(.*?)\.html/, " [\\1](\\2.html) ")
d << "" if text_line
text_line = false
case s
when /^<pre>/
d << "~~~"
pre = true
when /^\*\*\* (.*)$/
d << " * " + $1
when /^\*\* (.*)$/
d << " * " + $1
when /^\* (.*)$/
d << "* " + $1
when /^\#\#\# (.*)$/
d << " 1. " + $1
when /^\#\# (.*)$/
d << " 1. " + $1
when /^\# (.*)$/
d << "1. " + $1
when /^h(\d)\. (.*)$/
d << "#" * $1.to_i + " " + $2
when /^!(.*?)!/
d << "![](#{$1})"
when /^\|_\./
d << s.gsub("|_.", "| ")
table_header = true
when /^\|/
d << s.gsub(/\=\..+?\|/, ":---:|").gsub(/\s+.+?\s+\|/, "---|") if table_header
table_header = false
d << s.gsub("|=.", "| ")
when /^\s*$/
d << s
else
d << s
text_line = true
end
end
d.join("\n") + "\n"
end
def update_content(model, attrbute)
total = model.count
step = total / 10
puts " #{model}.#{attrbute} : #{total}"
model.all.each_with_index do |rec, ix|
n = ix + 1
puts sprintf("%8d", n) if n % step == 0
rec[attrbute] = textile_to_markdown(rec[attrbute]) if rec[attrbute]
rec.save!
end
end
update_content(WikiContent, :text)
update_content(Issue, :description)
update_content(Journal, :notes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment