Skip to content

Instantly share code, notes, and snippets.

@yuumi3
Last active October 28, 2019 01:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save yuumi3/d54ce99d1f15077ebaf8 to your computer and use it in GitHub Desktop.
Save yuumi3/d54ce99d1f15077ebaf8 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