From 09c7b7abbdbf5b12f908d4a88fe5c0098094b2c7 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Fri, 14 Jan 2022 10:30:10 -0600 Subject: switch to fsm implementation of gem2html --- fsm.py | 86 +++++++++++------------------------------------------------------- 1 file changed, 14 insertions(+), 72 deletions(-) (limited to 'fsm.py') diff --git a/fsm.py b/fsm.py index 40f9c2d..901e114 100644 --- a/fsm.py +++ b/fsm.py @@ -1,4 +1,6 @@ import sys +import urllib.parse + class StackFSM(object): """ @@ -25,6 +27,7 @@ class StackFSM(object): def pop_state(self): return self._state_stack.pop() + class Parser(object): def __init__(self, document, output=None): @@ -38,29 +41,23 @@ class Parser(object): def parse(self): self._fsm.push_state(self.text_state) - while self._fsm._current_state() is not None: + while self._fsm._current_state() is not None and len(self._document) > self._offset: 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('
\n')
             self._offset += 1
         elif line.startswith('* '):
-            self._fsm.pop_state()
             self._fsm.push_state(self.list_state)
             self._output.write('
\n') self._offset += 1 - elif line.startswith('* '): - self._fsm.pop_state() - self._fsm.push_state(self.list_state) - self._output.write('\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() -- cgit v1.2.3