#!/usr/bin/env python3
import fnmatch
import hashlib
import json
import os
import sqlite3
import time
import urllib.parse
import trac2md
wiki_query = '''
SELECT
name,
version,
time / 1000000 AS time,
text
FROM wiki
ORDER BY
name, version
'''
attachment_query = '''
SELECT
id,
filename,
size,
author,
description,
ipnr,
time / 1000000 AS createdtime
FROM
attachment
WHERE
id = ?
AND
type = 'wiki'
ORDER BY
filename, time
'''
def isotime(t):
return None if t == 0 else time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(t))
def hashname(whatever):
return hashlib.sha1(unicode(whatever)).hexdigest()
with open("filter.json") as f:
filter = json.load(f)
def keep(name):
for k, v in filter:
assert k in "+-"
if fnmatch.fnmatch(name, v):
return k == "+"
return True
class Row(sqlite3.Row):
def __getattr__(self, name):
return self[name]
db = sqlite3.connect("trac.db")
db.row_factory = Row
for row in db.execute(wiki_query):
if keep(row.name):
slug = urllib.parse.quote(row.name, "")
print(slug, row.version)
with open("tracwiki/{}.trac".format(slug), "w") as f:
f.write(row.text)
md = trac2md.WikiToMD(row.text)
with open("markdown/{}.md".format(slug), "w") as f:
f.write(md)
db.close()