Last active
August 3, 2024 14:51
-
-
Save yuumi3/d54ce99d1f15077ebaf8 to your computer and use it in GitHub Desktop.
Convert Textile to Markdown contents in Redmin
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
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