summaryrefslogtreecommitdiff
path: root/docs/zeearchive/mkdocs-server-configuration/export.py
blob: 76668b1d69e3be6f6599d03eaa3b32f05da76e88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
# vim:set fileencoding=utf-8 sw=2 ai:

import datetime
import re
import psycopg2

SQL = '''
  select
      name, version, to_timestamp(time/1000000)::date, author, text
    from
      wiki w
    where
      version = (select max(version) from wiki where name = w.name)
    order by version
'''

con = psycopg2.connect(database='tracdb', user='tracuser',
                    password='password')

with con:

    cur = con.cursor()
    cur.execute(SQL)

    rows = cur.fetchall()

    for row in rows:
      name = row[0]
      version = row[1]
      time = row[2]
      author = row[3]
      text = row[4]
      print(name,version,time,author)

      text = re.sub('\r\n', '\n', text)
      text = re.sub(r'{{{(.*?)}}}', r'`\1`', text)
      def indent4(m):
         return '\n    ' + m.group(1).replace('\n', '\n    ')
      text = re.sub(r'(?sm){{{\n(.*?)\n}}}', indent4, text)
      text = re.sub(r'(?m)^====\s+(.*?)\s+====$', r'#### \1', text)
      text = re.sub(r'(?m)^===\s+(.*?)\s+===$', r'### \1', text)
      text = re.sub(r'(?m)^==\s+(.*?)\s+==$', r'## \1', text)
      text = re.sub(r'(?m)^=\s+(.*?)\s+=$', r'# \1', text)
      text = re.sub(r'^       * ', r'****', text)
      text = re.sub(r'^     * ', r'***', text)
      text = re.sub(r'^   * ', r'**', text)
      text = re.sub(r'^ * ', r'*', text)
      text = re.sub(r'^ \d+. ', r'1.', text)

      a = []
      for line in text.split('\n'):
        if not line.startswith('    '):
          line = re.sub(r'\[(https?://[^\s\[\]]+)\s([^\[\]]+)\]', r'[\2](\1)', line)
          line = re.sub(r'\[(wiki:[^\s\[\]]+)\s([^\[\]]+)\]', r'[\2](/\1/)', line)
          line = re.sub(r'\!(([A-Z][a-z0-9]+){2,})', r'\1', line)
          line = re.sub(r'\'\'\'(.*?)\'\'\'', r'*\1*', line)
          line = re.sub(r'\'\'(.*?)\'\'', r'_\1_', line)
        a.append(line)
      text = '\n'.join(a)

      fp = open('%s.md' % name, 'w')
      print('<!-- Name: %s -->' % name,file=fp)
      print('<!-- Version: %d -->' % version,file=fp)
      print('<!-- Last-Modified: %s -->' % time.strftime('%Y/%m/%d %H:%M:%S'),file=fp)
      print('<!-- Author: %s -->' % author,file=fp)
      fp.write(str(text))
      fp.close()