Skip to content

Instantly share code, notes, and snippets.

@oppara
Forked from yuumi3/redmine_textile2md.rb
Last active October 28, 2019 07:22
Show Gist options
  • Save oppara/20a434ebb1df9a7ea406b351734eddcc to your computer and use it in GitHub Desktop.
Save oppara/20a434ebb1df9a7ea406b351734eddcc 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!(/"(.*?)":([^ ]+)( ?)/, "[\\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 << "![](#{$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
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)
@oppara
Copy link
Author

oppara commented Mar 22, 2019

このコードを以下のように runner で実行するとTextileがMarkdownに変換されます。

$ rails runner -e production tools/textile2md.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment