roundabout,
created on Wednesday, 27 March 2024, 14:11:29 (1711548689),
received on Wednesday, 31 July 2024, 06:54:42 (1722408882)
Author identity: vlad <vlad.muntoiu@gmail.com>
9ec9202c443ef2f35a63fb5cb1b6c360587e6fbb
markdown.py
@@ -1,6 +1,5 @@
import re inlineRegex = r""" (?P<em>[*_]{1,7}) (?P<textEm>(?:\\[*]|[^*])*) (?P=em) # emphasis |
@@ -30,40 +29,88 @@ class Element:
return "Void block" class Heading(Element):def __init__(self, content, level):class Container(Element): def __init__(self, content):super().__init__() self.content = contentself.content = parse_line(content) def __repr__(self): return "Generic container element: " + repr(self.content) class Heading(Container): def __init__(self, content, level): super().__init__(content)self.level = level pass def __repr__(self): return f"Heading level {self.level}:\n\t" + self.contentreturn f"Heading level {self.level}:\n\t" + repr(self.content)class Paragraph(Element):def __init__(self, content):super().__init__()self.content = contentclass Paragraph(Container): def __init__(self): super().__init__("")def addLine(self, content): self.content += content.strip() + " "self.content.extend([*parse_line(content), " "])def __repr__(self): return "Paragraph:\n\t" + self.contentreturn "Paragraph:\n\t" + repr(self.content)class Emphasis(Element):class Emphasis(Container):def __init__(self, content, value): super().__init__(content) self.value = value def __repr__(self): return f"Emphasis ({self.value}): " + repr(self.content) class Code(Element): def __init__(self, content):super().__init__() self.content = content def __repr__(self): return f"Inline code: {self.content}" class Strikethrough(Container): def __init__(self, content): super().__init__(content) def __repr__(self): return f"Strikethrough: {repr(self.content)}" class Diff(Container): def __init__(self, content, value): super().__init__(content)self.value = value def __repr__(self): return f"Emphasis ({self.value}): " + self.contentreturn f"Diff ({self.value}): {self.content}" class Link(Element): def __init__(self, text, destination, image=False): super().__init__() self.text = text self.destination = destination self.image = image def __repr__(self): return f"{'Image' if self.image else 'Link'}: {self.text} -> {self.destination}"class Image(Link): def __init__(self, text, destination): super().__init__(text, destination, True) def _parse_line(source):def parse_line(source):if trailing(source, "\\") == 1: source = source.rstrip("\\") source += "\n"
@@ -82,6 +129,16 @@ def _parse_line(source):
if i.group("em"): tokens.append(Emphasis(i.group("textEm"), len(i.group("em")))) if i.group("textCode"): tokens.append(Code(i.group("textCode"))) if i.group("strike"): tokens.append(Strikethrough(i.group("textStrike"))) if i.group("diff"): tokens.append(Diff(i.group("textDiff"), i.group("diff"))) if i.group("urlText"): tokens.append(Link(i.group("urlText"), i.group("urlDestination"))) if i.group("imageFlag"): tokens.append(Image(i.group("urlText"), i.group("urlDestination")))tokens.append(source[lookup:])
@@ -99,7 +156,7 @@ def _tokenise(source):
tokens.append(current_block) current_block = Element() elif line.startswith("#") and leading(line.lstrip("#"), " ") == 1:elif line.startswith("#") and leading(line.lstrip("#"), " "):tokens.append(current_block) content = line.lstrip("#").strip()
@@ -109,34 +166,29 @@ def _tokenise(source):
# Paragraph is default tokens.append(current_block) current_block = Paragraph("")current_block = Paragraph()current_block.addLine(line)current_block.addLine(line.strip())tokens.append(current_block) return tokens for i in _tokenise("""if __name__ == '__main__': for i in _tokenise( """# Hello World! ## Title 1 ### Part 1 #### Chapter 1#### Chapter _1_##### Article 1 ###### Section 1 Lorem ipsumdolor sit amet..."""):print(repr(i))def parse_markdown(source):tokens = _tokenise(source)Lorem **i`p`sum** dolor `sit` amet... """ ): print(repr(i)) parse_markdown("")print(_parse_line("**bold** text"))