Skip to content

Instantly share code, notes, and snippets.

@metavida
Created October 6, 2008 15:44
Show Gist options
  • Save metavida/15064 to your computer and use it in GitHub Desktop.
Save metavida/15064 to your computer and use it in GitHub Desktop.
A Textmate Command that helps in the conversion of wiki documents from Trac to Redmine.
#!/usr/bin/env ruby
# In it's current state, this script is not meant as a catch-all converter
# but should give you a good start.
# Requirements:
# * Textmate <http://macromates.com/>
# Know issues:
# * Changes all [] => [[]] - even inside code blocks.
# * Doesn't add the required \n\n after h tags.
# * Doesn't convert everything (e.g. currently ignores img tags)
s = STDIN.read
# h3
s.gsub!(/^ *={3} *(.*) *={3} *$/, 'h3. \1')
# h2
s.gsub!(/^ *={2} *(.*) *={2} *$/, 'h2. \1')
# h1
s.gsub!(/^ *={1} *(.*) *={1} *$/, 'h1. \1')
# bold, italic
s.gsub!(/'{5}(.*)'{5}/,'*_\1_*')
# bold
s.gsub!(/'{3}(.*)'{3}/,'*\1*')
# italic
s.gsub!(/'{2}(.*)'{2}/,'_\1_')
# underline
s.gsub!(/_{2}(.*)_{2}/,'+\1+')
# strike-through
s.gsub!(/~{2}(.*)~{2}/,'-\1-')
# ul1
s.gsub!(/^ \*(.*)$/,'* \1')
# ul2
s.gsub!(/^ \*(.*)$/,'** \1')
# ul3
s.gsub!(/^ \*(.*)$/,'*** \1')
# ol1
s.gsub!(/^ \d+\.(.*)$/,'# \1')
# ol2
s.gsub!(/^ \d+\.(.*)$/,'## \1')
# ol3
s.gsub!(/^ \d+\.(.*)$/,'### \1')
# code block
s.gsub!(/^ *\{{3} *$/,'<pre>')
s.gsub!(/^ *\}{3} *$/,'</pre>')
# inline code
s.gsub!(/((`)|(\{{3})(\}{3}))(.*)((`)|(\{{3})(\}{3}))/,'*@\1@*')
# links
s.gsub!(/\[(https?:\/\/[^\s\]]*)\]/,'\1')
s.gsub!(/\[(https?:\/\/[^\s\]]*) ([^\]]*)\]/,'"\2":\1')
s.gsub!(/\[(wiki:)?(.*)\]/,'[[\2]]')
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment