-
-
Save oppara/20a434ebb1df9a7ea406b351734eddcc 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!(/"(.*?)":([^ ]+)( ?)/, "[\\1](\\2)\\3") | |
d << "" if text_line | |
text_line = false | |
case s | |
when /^<pre><code class="(\w+)">/ | |
d << "~~~" + $1 | |
pre = true | |
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 << "" | |
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 | |
id = rec[:id] | |
puts " #{model}.#{id} : #{n}/#{total}" | |
rec[attrbute] = textile_to_markdown(rec[attrbute]) if rec[attrbute] | |
rec.save! :validate => false | |
end | |
end | |
update_content(WikiContent, :text) | |
update_content(Project, :description) | |
update_content(Issue, :description) | |
update_content(Journal, :notes) | |
# update_content(News, :description) | |
# update_content(Document, :description) | |
# update_content(Message, :content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
このコードを以下のように runner で実行するとTextileがMarkdownに変換されます。
$ rails runner -e production tools/textile2md.rb