From 3c8456cda3f7b2df7d71c504758ac78c13ac0a42 Mon Sep 17 00:00:00 2001
From: Micke Nordin
\n')
tag.append('')
@@ -68,7 +72,7 @@ class GemParser:
tag.append('\n')
return tag[self.mstate]
- def is_text(self, line):
+ def is_text(self, line: str) -> bool:
"""A function that will check if this is a paragraph of text
Args:
@@ -78,13 +82,13 @@ class GemParser:
bool: True if it is text, False if it is something else
"""
return (not (line == '' or line == '\n')
- and self.mstate == State.INITIAL and
- (not line.startswith(Tag.LINK) and not line.startswith(Tag.PRE)
- and not line.startswith(Tag.HEADING)
- and not line.startswith(Tag.UNORDERED)
- and not line.startswith(Tag.QUOTE)))
+ and self.mstate == StateEnum.INITIAL and
+ (not line.startswith(TagEnum.LINK) and not line.startswith(TagEnum.PRE)
+ and not line.startswith(TagEnum.HEADING)
+ and not line.startswith(TagEnum.UNORDERED)
+ and not line.startswith(TagEnum.QUOTE)))
- def parse_heading(self, line, level=0):
+ def parse_heading(self, line: str, level: int = 0) -> str:
"""A function that will recursively call it self to get the correct
heading level
@@ -95,14 +99,14 @@ class GemParser:
Returns:
str: A html heading tag of the correct level
"""
- if line.startswith(Tag.HEADING):
+ if line.startswith(TagEnum.HEADING):
return self.parse_heading(line[1:], level + 1)
else:
return "{}{}{}".format(self.get_start_tag().format(level),
line.strip().rstrip('\n'),
self.get_end_tag().format(level))
- def parse_line(self, line):
+ def parse_line(self, line: str) -> str:
"""This subroutine will parse a single line of gemtext and enter the
correct state and output the corresponding html
@@ -112,76 +116,76 @@ class GemParser:
Returns:
str: A string of html
"""
- rstring = str()
- is_text = self.is_text(line)
- starts_with_pre = line.startswith(Tag.PRE)
- starts_with_ul = line.startswith(Tag.UNORDERED)
- starts_with_quote = line.startswith(Tag.QUOTE)
- if line == '\n' and self.mstate != State.PRETEXT:
+ rstring: str = str()
+ is_text: bool = self.is_text(line)
+ starts_with_pre: bool = line.startswith(TagEnum.PRE)
+ starts_with_ul: bool = line.startswith(TagEnum.UNORDERED)
+ starts_with_quote: bool = line.startswith(TagEnum.QUOTE)
+ if line == '\n' and self.mstate != StateEnum.PRETEXT:
return rstring
# This is blockquote end tag
- if self.mstate == State.QUOTE and not starts_with_quote:
+ if self.mstate == StateEnum.QUOTE and not starts_with_quote:
rstring += self.get_end_tag()
self.recurse = True
# This is paragraph end tag
- if self.mstate == State.TEXT and not is_text:
+ if self.mstate == StateEnum.TEXT and not is_text:
rstring += self.get_end_tag()
self.recurse = True
# This is ul end tag
- if self.mstate == State.UNORDERED and not starts_with_ul:
+ if self.mstate == StateEnum.UNORDERED and not starts_with_ul:
rstring += self.get_end_tag()
self.recurse = True
# This is a pre start tag
- if starts_with_pre and self.mstate != State.PRETEXT:
- self.mstate = State.PRETEXT
+ if starts_with_pre and self.mstate != StateEnum.PRETEXT:
+ self.mstate = StateEnum.PRETEXT
rstring += self.get_start_tag().format(' aria-label="{}"'.format(
line[4:].rstrip('\n')))
return rstring
# This is in pre formatted text
- elif self.mstate == State.PRETEXT and not starts_with_pre:
+ elif self.mstate == StateEnum.PRETEXT and not starts_with_pre:
rstring += line
return rstring
# This is a pre end tag
- elif starts_with_pre and self.mstate == State.PRETEXT:
+ elif starts_with_pre and self.mstate == StateEnum.PRETEXT:
rstring += self.get_end_tag()
- self.mstate = State.INITIAL
+ self.mstate = StateEnum.INITIAL
return rstring
else:
# This is paragraph start tag
- if is_text and self.mstate != State.TEXT:
- self.mstate = State.TEXT
+ if is_text and self.mstate != StateEnum.TEXT:
+ self.mstate = StateEnum.TEXT
rstring += self.get_start_tag()
rstring += line
return rstring
# This is in paragraph
- if is_text and self.mstate == State.TEXT:
+ if is_text and self.mstate == StateEnum.TEXT:
rstring += line
return rstring
# This is a heading
- if line.startswith(Tag.HEADING):
- self.mstate = State.HEADING
+ if line.startswith(TagEnum.HEADING):
+ self.mstate = StateEnum.HEADING
rstring += self.parse_heading(line)
- self.mstate = State.INITIAL
+ self.mstate = StateEnum.INITIAL
return rstring
# This is a link
- if line.startswith(Tag.LINK):
- self.mstate = State.LINK
+ if line.startswith(TagEnum.LINK):
+ self.mstate = StateEnum.LINK
rstring += self.parse_link(line)
- self.mstate = State.INITIAL
+ self.mstate = StateEnum.INITIAL
return rstring
# This is ul start tag
- if starts_with_ul and not self.mstate == State.UNORDERED:
- self.mstate = State.UNORDERED
+ if starts_with_ul and not self.mstate == StateEnum.UNORDERED:
+ self.mstate = StateEnum.UNORDERED
rstring += self.get_start_tag()
rstring += '