summaryrefslogtreecommitdiff
path: root/scripts/html_wiki.py
blob: 1c84643d0a116822daa0eae0b123cabaa32fa29c (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
#! /usr/bin/env python3
#===============================
#
# html_wiki
#
# 2021/02/09 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
#===============================
import textile
import sys
import re

import html_base
from html_base import html
#====================================
#
# periject_html
#
#====================================
class periject_html(html_base.myhtml):

    def __init__(self):
        super().__init__()

    #--------------------
    # print
    #--------------------
    def print(self, argv):
        # remove this script
        argv.pop(0)

        with open(argv[0], "r") as f:
            text = f.read()

            # parse http[s]
            text = re.sub(r'(\s+)http://(.*)(\s+)',  r'\1"http://\2":http://\2\3',  text)
            text = re.sub(r'(\s+)https://(.*)(\s+)', r'\1"https://\2":https://\2\3', text)

            # parse img
            text = re.sub(r'!(\S+)!', r'!../../wiki/\1!', text)

            # [[foo bar]]
            # -> "foo bar":foo_bar.html
            while 1:
                hit = re.search(r'\[\[.*\]\]', text)

                if (not hit):
                    break;

                link = text[hit.start()+2:hit.end()-2]
                link = "\"{}\":{}.html".format(link, re.sub(r' ', r'_', link))

                text = text[:hit.start()] + link + text[hit.end():]

        # textile
        self.print_css("html/wiki")
        with html("body"):
            print(textile.textile(text))

#====================================
#
# As command
#
#====================================
if __name__=='__main__':
    # html_wiki.py wiki/xxx.wiki
    periject_html().print(sys.argv)