Skip to content

Instantly share code, notes, and snippets.

@farkwun
Last active February 1, 2016 21:19
Show Gist options
  • Save farkwun/8abd54fee7487462b392 to your computer and use it in GitHub Desktop.
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
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/,"&lt;")
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
@farkwun
Copy link
Author

farkwun commented Oct 2, 2015

Put this rake task into lib/tasks/convert_textile_to_markdown_extra.rake and run with


bundle exec rake convert_textile_to_markdown_extra RAILS_ENV=production

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