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-5 of 5 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

migrate us: old code snippets



http://jm3.net/wiki.pl?CodeLibrary

html table to wiki converter

import HTMLParser, re, sys
class html2wiki(HTMLParser.HTMLParser):
	def __init__(self):
		HTMLParser.HTMLParser.__init__(self)
		self.wiki = ''	  # The Wiki text
		self.wikirow = ''   # The current Wiki row of table being constructed from HTML
		self.inTD = 0	  # Used to track if we are inside or outside a <TD>...</TD> tag.
		self.inTR = 0	  # Used to track if we are inside or outside a <TR>...</TR> tag.
		self.re_multiplespaces = re.compile('\s+')  # regular expression used to remove spaces in excess
		self.rowCount = 0  # output row counter.
		self.rowspan = ''
		self.colspan = ''
		self.linebreak = '<br>'
		self.data = ''
		self.prop = ''
		
	def handle_starttag(self, tag, attrs):
		if tag == 'table': self.start_table()
		elif   tag == 'tr': self.start_tr()
		elif tag == 'td': self.start_td(attrs)
		
	def handle_endtag(self, tag):
		if tag == 'table': self.end_table();
		elif   tag == 'tr': self.end_tr()
		elif tag == 'td': self.end_td()
		
	def start_table(self):
		self.wiki += '{| border=1' + self.linebreak
		self.wiki += '|-' + self.linebreak
		
	def end_table(self):
		self.wiki += '|}' + self.linebreak
	
	def start_tr(self):
		if self.inTR: self.end_tr()  # <TR> implies </TR>
		self.inTR = 1
		
	def end_tr(self):
		if self.inTD: self.end_td()  # </TR> implies </TD>
		self.inTR = 0			
		if len(self.wikirow) > 0:
			self.wiki += self.wikirow
			self.wiki += '|-' + self.linebreak
			self.wikirow = ''
		self.rowCount += 1

	def start_td(self, attrs):
		if not self.inTR: self.start_tr() # <TD> implies <TR>
		self.data = ''
		self.prop = ''
		self.rowspan = ''
		self.colspan = ''
		for key, value in attrs:
			if key == 'rowspan':
				self.rowspan = value
			elif key == 'colspan':
				self.colspan = value			
		self.inTD = 1
		
	def end_td(self):
		if self.inTD:				
			self.wikirow += '| ' + self.prop + self.re_multiplespaces.sub(' ',self.data.replace('\t',' ').replace(self.linebreak,'').replace('\r','').replace('"','""'))+ self.linebreak;
			self.data = ''
			self.inTD = 0

	def handle_data(self, data):
		if self.inTD:
			if data.strip() != '':				
				self.prop = ''
				if self.rowspan != '':
					self.prop = ' rowspan = '+self.rowspan 
				if self.colspan != '':
					self.prop += ' colspan = '+self.colspan
				if self.prop:
					self.prop += ' | '
				self.data += data

if __name__ == '__main__':				
	parser = html2wiki()
	if len(sys.argv) == 2:
		in_file = open(sys.argv[1],"r")
		text = in_file.read()
		parser.feed(text)
		in_file.close()
		print parser.wiki
	else:
		print 'Argument - filename required'

Latex2wiki

Translate a subset of LaTeX into MoinMoin wiki syntax.


#!/usr/bin/env python

#    Copyright (C) 2003, Maxime Biais <maxime@biais.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: latex2wiki.py,v 1.1.1.1 2004/03/14 18:31:50 max Exp $

import sys, re

def dummy(d):
    pass

NONE = "__@NONE@__"

tr_list = [
    (r"\\includegraphics.*{(.*)\.eps}", "attachment::%s.png", dummy),
    (r"\\caption{.*}", "", dummy),
    (r"\\label{.*}", "", dummy),
    (r"(.*)\\emph{(.*)}(.*)", """%s'''%s'''%s""", dummy),
    (r"\\item (.*)", " * %s", dummy),
    (r"\\begin{.*}", "", dummy),
    (r"\\end{.*}", "", dummy),
    (r"(.*)``(.*)''(.*)", "%s\"%s\"%s", dummy),
    (r"\\chapter{(.*)}", NONE, dummy),
    (r"\\paragraph{(.*)}", "==== %s ====", dummy),
    (r"\\subsubsection{(.*)}", "==== %s ====", dummy),
    (r"\\subsection{(.*)}", "=== %s ===", dummy),
    (r"\\section{(.*)}", "== %s ==", dummy),
    (r"(.*)\\fig{.*}(.*)", "%s suivant %s", dummy)
    ]

in_stream  = open(sys.argv[1], "r")
if len(sys.argv) < 3:
    out_stream = sys.stdout
else:
    out_stream = open(sys.argv[2], "w")


for i in in_stream.readlines():
    cur_write = 0
    for reg in tr_list:
        m = re.search(reg[0], i)
        if m:
            reg[2](i)
            cur_write = 1
            if reg[1] == NONE:
                break
            print >> out_stream, reg[1] % m.groups()
            break
    if not cur_write:
        out_stream.write(i)

Regular Expression for WikiWords

This RegEx should return all the Wiki Words (i.e. any word in Camel Case):

[^A-Za-z]*([A-Z][a-z]+[A-Z][a-z]+([A-Z][a-z]+)*)[^A-Za-z]*
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS