Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Trac Wiki to Markdown
#!/usr/bin/env ruby
# Convert Trac DB Wiki pages to Markdown source files
#
# Usage
#
# 1. Save the file somewhere and make it executable:
# chmod a+rx convert_track_wiki_to_markdown.rb
# 2. Run it like this:
# ./convert_track_wiki_to_markdown.rb /path/to/your/project/db/trac.db
#
# It will save the converted Wiki pages in a subfolder of the current path,
# named after "project" (taken from the path you provide to the script).
#
# 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) with
# the following changes:
#
# - uses MarkDown format instead
# - uses the sqllite3 gem which does not need Ruby 1.9
require 'rubygems'
require 'sqlite3'
require 'fileutils'
TRAC_DB_PATH = ARGV.first
project_name = File.basename(TRAC_DB_PATH.sub(/\/db\/trac.db$/, ''))
OUT_PATH = "#{project_name}/wiki"
puts "Reading Wiki from #{TRAC_DB_PATH}"
puts "Writing results to #{OUT_PATH}"
FileUtils.mkdir_p OUT_PATH
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.