Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismangil/ee015262e373b1d6298be87ff193d92e to your computer and use it in GitHub Desktop.
Save ismangil/ee015262e373b1d6298be87ff193d92e 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
#
# 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