Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Trac Wiki to GitHub Wiki

A rough start of a script to help convert the wiki syntax of Trac pages to GitHub-friendly syntax.

Some TLC is still needed on each output page, but better than doing it all by hand.

Project lives here: github.com/seven1m/trac_wiki_to_github

#!/usr/bin/env ruby
 
TRAC_DB_PATH = 'trac.db'
OUT_PATH = 'wiki'
GITHUB_WIKI_URL = '/seven1m/onebody/wikis/'
 
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/, '')), 'w') do |file|
    body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '<code>\1</' + 'code>')
    body.gsub!(/\{\{\{(.+?)\}\}\}/m, '<pre><code>\1</' + 'code></pre>')
    body.gsub!(/====\s(.+?)\s====/, 'h4. \1')
    body.gsub!(/===\s(.+?)\s===/, 'h3. \1')
    body.gsub!(/==\s(.+?)\s==/, 'h2. \1')
    body.gsub!(/=\s(.+?)\s=[\s\n]*/, '')
    body.gsub!(/\[(http[^\s\[\]]+)\s([^\[\]]+)\]/, '"\2":\1')
    body.gsub!(/\[([^\s]+)\s(.+)\]/, '"\2":' + GITHUB_WIKI_URL + '\1')
    body.gsub!(/([^"\/\!])(([A-Z][a-z0-9]+){2,})/, '\1[[\2]]')
    body.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1')
    body.gsub!(/'''(.+)'''/, '*\1*')
    body.gsub!(/''(.+)''/, '_\1_')
    body.gsub!(/^\s\*/, '*')
    body.gsub!(/^\s\d\./, '#')
    file.write(body)
  end
end

Trac Subversion Repository scraper

Ruby script to pull code from a Trac repo browser. Useful if you are behind a firewall/proxy and the server has no http access to the subversion repo. (I'm sure its not the prettiest/best/rubiest way to do it, but it worked for me)

#!/usr/bin/env ruby

require 'rubygems'
require 'hpricot'
require 'open-uri'


#a Trac repo scraper. pass the url to scrape (the root of a repo)
#  and optionally the local path to write to.  defaults to .

class TracRepoScraper

  def initialize(trac_url, local_path='.')
    @trac_url = trac_url

    trac_url =~ /(http:\/\/.*?)\//
    @trac_server = $1
    @local_path =  local_path
  end

  def getallfiles(url,cur_localpath)
    if cur_localpath != '.'
      Dir.mkdir(cur_localpath)
    end
    doc = Hpricot(open(url).read)
    doc.search("//tbody//tr//td//a[@class='file']").each do |file_anchor|
      #get the file as curpath+/file_name

      #following gives us absolute path (excluding domain)
      actual_file_url = @trac_server + file_anchor['href']+'?format=raw'
      #temp
      puts "Saving #{actual_file_url} to #{cur_localpath}/#{file_anchor.inner_html}"
      #read the file and write to a file in the correct directory
      File.open(cur_localpath+"/"+file_anchor.inner_html, 'w') do |f|
        remote_file = open(actual_file_url)
        remote_file.each { |line|
          f.puts(line)
        }
      end
    end

    doc.search("//tbody//tr//td//a[@class='dir']").each do |dir_anchor|
      #go into the directory
      dir_url = @trac_server + dir_anchor['href']
      puts "*** stepping into #{dir_url}"
      #dir_anchor.inner_html is the name of the subdirectory (relative)
      getallfiles(dir_url, cur_localpath+"/"+dir_anchor.inner_html)
    end
  end

  def start
    getallfiles(@trac_url, @local_path)
  end
end


#### main

trac_url = ARGV[0]
localpath = '.'
if ARGV[1]
  if !ARGV[1].strip.empty?
    localpath = ARGV[1].strip
  end
end

TracRepoScraper.new(trac_url, localpath).start

Trac installation on its own subdomain

Most installation documents of Trac show how to install to a directory in the main site's domain. For installation into a subdomain, the Trac data and CGI script should be installed into trac.domain.com/htdocs/trac/, with the following in the Apache config file:

<Directory /path/to/trac.domain.com/htdocs/trac/>
  AllowOverride None
  Options ExecCGI
  AddHandler cgi-script .cgi
  Order allow,deny
  Allow from all
</Directory>

<VirtualHost 1.2.3.4>
  ServerName trac.domain.com
  DocumentRoot /path/to/trac.domain.com/htdocs/

  RewriteEngine On
  RewriteCond %{REQUEST_URI} !^/cgi-bin/
  RewriteCond %{REQUEST_URI} !^/trac/
  RewriteRule ^/(.*) /trac/trac.cgi/$1 [L]

  <Location />
    SetEnv TRAC_ENV "/var/lib/trac/projname">
  </Location>
</VirtualHost>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS