Skip to content

Instantly share code, notes, and snippets.

@somebox
Created October 10, 2010 20:31
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save somebox/619537 to your computer and use it in GitHub Desktop.
Save somebox/619537 to your computer and use it in GitHub Desktop.
Convert Trac Wiki to Markdown
#!/usr/bin/env ruby
# Convert Trac DB Wiki pages to Markdown source files
# This script is based on http://github.com/seven1m/trac_wiki_to_github which
# converted all pages from a Trac DB to GitHub Wiki format (as Textile).
#
# I made two changes:
# - uses MarkDown format instead
# - uses the sqllite3-ruby gem which does not need Ruby 1.9
#
TRAC_DB_PATH = 'trac.db'
OUT_PATH = 'wiki'
GITHUB_WIKI_URL = '/somebox/myproject/wikis/'
require 'rubygems'
gem 'sqlite3-ruby'
require 'sqlite3'
db = SQLite3::Database.new(TRAC_DB_PATH)
pages = db.execute('select name, text from wiki w2 where version = (select max(version) from wiki where name = w2.name);')
pages.each do |title, body|
File.open(File.join(OUT_PATH, title.gsub(/\s/, '')+'.md'), 'w') do |file|
body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '`\1`')
body.gsub!(/\{\{\{(.+?)\}\}\}/m){|m| m.each_line.map{|x| "\t#{x}".gsub(/[\{\}]{3}/,'')}.join}
body.gsub!(/\=\=\=\=\s(.+?)\s\=\=\=\=/, '### \1')
body.gsub!(/\=\=\=\s(.+?)\s\=\=\=/, '## \1')
body.gsub!(/\=\=\s(.+?)\s\=\=/, '# \1')
body.gsub!(/\=\s(.+?)\s\=[\s\n]*/, '')
body.gsub!(/\[(http[^\s\[\]]+)\s([^\[\]]+)\]/, '[\2](\1)')
body.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1')
body.gsub!(/'''(.+)'''/, '*\1*')
body.gsub!(/''(.+)''/, '_\1_')
body.gsub!(/^\s\*/, '*')
body.gsub!(/^\s\d\./, '1.')
file.write(body)
end
end
@ismangil
Copy link

ismangil commented Mar 6, 2019

I'm trying to convert Trac milestone description to migrate to GitHub via REST API.

The conversion went well but I can't put the resulting string into json to send to rest API - unprocessable entity. How do I escapify the result?

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