Last active
February 1, 2016 21:19
-
-
Save farkwun/8abd54fee7487462b392 to your computer and use it in GitHub Desktop.
Convert textile to Markdown Extra - shamelessly derived from https://gist.github.com/othree/8563b7f9018730666b95
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
task :convert_textile_to_markdown_extra => :environment do | |
require 'tempfile' | |
Issue.all.each do |version| | |
if (String(version.tracker) == "Approval" && Integer(version.id) > 72400 && Integer(version.id) < 72500) | |
#if (Integer(version.id) == 73293) for testing on specific documents | |
puts 'Converting Issue...' | |
puts version.id | |
puts version.subject | |
puts version.tracker | |
textile = version.description | |
#only if your descriptions have tag-like strings that are not actually meant to be tags | |
textile.gsub!(/\<([^\.])/,'STARTTAG\\1') | |
textile.gsub!(/\>/,'ENDTAG') | |
#replaces ++ tags, or underlines, since pandoc converts them to asterisks | |
textile.gsub!(/([\s\t\r\n\f])\+(.*)\+([\s\t\r\n\f])/,"\\1UNDERLINESTART\\2UNDERLINEEND\\3") | |
src = Tempfile.new('textile') | |
src.write(textile) | |
src.open | |
src.close | |
dst = Tempfile.new('markdown') | |
dst.close | |
command = [ | |
"pandoc", | |
"--no-wrap", | |
"--smart", | |
"+RTS", | |
"-K64m", | |
"-RTS", | |
# "--strict", | |
"-f", | |
"textile", | |
"-t", | |
"markdown_phpextra", | |
src.path, | |
"-o", | |
dst.path, | |
] | |
system(*command) or raise "pandoc failed" | |
dst.open | |
markdown = dst.read | |
# remove the \ pandoc puts before * and > at begining of lines | |
markdown.gsub!(/^((\\[*> ])+)/) { $1.gsub("\\", "") } | |
# | |
# add a blank line before lists | |
markdown.gsub!(/^([^*].*)\n\*/, "\\1\n\n*") | |
# remove the strange \ character pandoc puts after | | |
markdown.gsub!(/\|\\/,"|") | |
#Create headers in tables - specifically for a three column, left-aligned table | |
markdown.gsub!(/\n\|\*\\\<\.(.*)\n/,"\n\|\*\\\<\.\\1\n\|:-\|:-\|:-\|\n") | |
# remove the header attributes that pandoc doesn't convert - *<. and _<. | |
markdown.gsub!(/\*\\<\./,"") | |
markdown.gsub!(/_\\\<\./,"") | |
# removes any solitary || with no purpose | |
markdown.gsub!(/\n\|\|\n/,"\n\n") | |
# removes | , which screws up tables | |
markdown.gsub!(/\| \n/,"\|\n") | |
#adds a line after every first line of a table | |
markdown.gsub!(/\n\n\|(.*)\|\n/,"\n\|\\1\|\n") | |
#opens up every cell that should be there but empty, as || is just ignored by markdown_extra | |
markdown.gsub!(/\|\|/,"| |") | |
markdown.gsub!(/\|\|\|/,"| | |") | |
markdown.gsub!(/\|\|\|\|/,"| | | |") | |
#catches underline tags from before, replaces them with appropriate ones | |
markdown.gsub!(/UNDERLINESTART/,"<u>") | |
markdown.gsub!(/UNDERLINEEND/,"</u>") | |
#catches tags from before, replaces them with appropriate ones, only if your descriptions have tag-like strings that are not actually meant to be tags | |
markdown.gsub!(/STARTTAG/,"<") | |
markdown.gsub!(/ENDTAG/,">") | |
#probably as a result of the operations above, indented lists don't work, this fixes them | |
# markdown.gsub!(/\n\*\*\\\*/," *") | |
markdown.gsub!(/\n\*\* (.*)\n/," * \\1\n") | |
version.update_columns(description: markdown) | |
puts 'Done' | |
else | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put this rake task into lib/tasks/convert_textile_to_markdown_extra.rake and run with