summaryrefslogtreecommitdiff
path: root/fsm.py
blob: 40f9c2d9ef9bf6b2082804b7e2acdf57988a2fcd (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys

class StackFSM(object):
    """
    Implement a finite state machine that uses a stack to
    manage state.
    """

    def __init__(self):
        self._state_stack = []

    def _current_state(self):
        if len(self._state_stack) > 0:
            return self._state_stack[-1]
        return None

    def update(self):
        fn = self._current_state()
        if fn is not None:
            fn()

    def push_state(self, state):
        self._state_stack.append(state)

    def pop_state(self):
        return self._state_stack.pop()

class Parser(object):

    def __init__(self, document, output=None):
        self._document = document
        if output is None:
            output = sys.stdout
        self._output = output
        self._offset = 0
        self._blanks = 0
        self._fsm = StackFSM()

    def parse(self):
        self._fsm.push_state(self.text_state)
        while self._fsm._current_state() is not None:
            self._fsm.update()

    def text_state(self):
        if len(self._document) <= self._offset:
            self._fsm.pop_state()
            return
        line = self._document[self._offset]
        if line.strip() == '':
            self._blanks += 1
        else:
            self._blanks = 0
        if line.strip() == '```':
            self._fsm.pop_state()
            self._fsm.push_state(self.pre_state)
            self._output.write('<pre>\n')
            self._offset += 1
        elif line.startswith('* '):
            self._fsm.pop_state()
            self._fsm.push_state(self.list_state)
            self._output.write('<ul>\n')
        elif line.startswith('=>'):
            self._fsm.pop_state()
            self._fsm.push_state(self.link_state)
            self._output.write('<ul>\n')
        else:
            if line.startswith('# '):
                self._output.write('<h1>{}</h1>\n'.format(line[2:]))
            elif line.startswith('## '):
                self._output.write('<h2>{}</h2>\n'.format(line[3:]))
            elif line.startswith('### '):
                self._output.write('<h3>{}</h3>\n'.format(line[4:]))
            elif line.startswith('> '):
                self._output.write('<blockquote>{}</blockquote>\n'.format(line[2:]))
            elif line.strip() == '':
                if self._blanks > 1:
                    self._output.write('<br/>\n')
            else:
                self._output.write('<p>{}</p>\n'.format(line))
            self._offset += 1

    def pre_state(self):
        if len(self._document) < self._offset:
            self.pop_state()
            return
        line = self._document[self._offset]
        if line.strip() == '```':
            self._fsm.pop_state()
            self._fsm.push_state(self.text_state)
            self._output.write('</pre>\n')
            self._offset += 1
        elif line.startswith('* '):
            self._fsm.pop_state()
            self._fsm.push_state(self.list_state)
            self._output.write('<ul>\n')
        elif line.startswith('=>'):
            self._fsm.pop_state()
            self._fsm.push_state(self.link_state)
            self._output.write('<ul>\n')
        else:
            self._output.write(line + '\n')
            self._offset += 1

    def list_state(self):
        if len(self._document) < self._offset:
            self.pop_state()
            return
        line = self._document[self._offset]
        if line.startswith('* '):
            self._output.write('<li>{}</li>\n'.format(line[2:]))
            self._offset += 1
        else:
            self._fsm.pop_state()
            self._fsm.push_state(self.text_state)
            self._output.write('</ul>\n')

    def link_state(self):
        if len(self._document) < self._offset:
            self.pop_state()
            return
        line = self._document[self._offset]
        if line.startswith('=>'):
            parts = line[2:].split(None, 2)
            if len(parts) == 1:
                self._output.write('<li><a href="{}">{}</a></li>\n'.format(parts[0], parts[0]))
            else:
                self._output.write('<li><a href="{}">{}</a></li>\n'.format(parts[0], parts[1]))
            self._offset += 1
        else:
            self._fsm.pop_state()
            self._fsm.push_state(self.text_state)
            self._output.write('</ul>\n')

document = """
# h1
hello
hello

## h2
```
code
code
```
### h3
hello


hello

### lists
* hello
* two
* three

text

* one
* two
* three

### links
=>https://example.com hello
=> https://example.com two
=>  https://example.com three

text

=>https://example.com
=> https://example.com
=>  https://example.com
"""

p = Parser(document.split('\n'))
p.parse()