markdown.py
Python script, ASCII text executable
1import re 2 3 4def leading(string, character): 5return len(string) - len(string.lstrip(character)) 6 7 8def trailing(string, character): 9return len(string) - len(string.rstrip(character)) 10 11 12class Element: 13def __init__(self): 14pass 15 16def __repr__(self): 17return "Void block" 18 19 20class Heading(Element): 21def __init__(self, content, level): 22super().__init__() 23self.content = content 24self.level = level 25pass 26 27def __repr__(self): 28return f"Heading level {self.level}:\n\t" + self.content 29 30 31class Paragraph(Element): 32def __init__(self, content): 33super().__init__() 34self.content = content 35 36def addLine(self, content): 37self.content += content.strip() + " " 38 39def __repr__(self): 40return "Paragraph:\n\t" + self.content 41 42 43def _tokenise(source): 44tokens = [] 45 46currentBlock = Element 47 48for line in source.split("\n"): 49if not line.strip(): 50# Void block 51 52tokens.append(currentBlock) 53currentBlock = Element() 54elif line.startswith("#") and leading(line.lstrip("#"), " ") == 1: 55tokens.append(currentBlock) 56 57content = line.lstrip("#").strip() 58currentBlock = Heading(content, leading(line, "#")) 59else: 60if not isinstance(currentBlock, Paragraph): 61tokens.append(currentBlock) 62currentBlock = Paragraph("") 63 64currentBlock.addLine(line) 65 66tokens.append(currentBlock) 67 68return tokens 69 70 71for i in _tokenise( 72""" 73# Hello World! 74## Title 1 75### Part 1 76#### Chapter 1 77##### Article 1 78###### Section 1 79Lorem ipsum 80dolor sit amet""" 81): 82print(repr(i)) 83 84 85def parseMarkdown(source): 86tokens = _tokenise(source) 87 88 89parseMarkdown("") 90