Introduction
Every blog engine, documentation site and chat app converts Markdown into HTML. This converter handles the most-used features with a line-by-line block parser and regular expressions for inline formatting, and escapes HTML so user text can't inject tags.
Problem statement
code spans, [text](url) links, unordered lists (- or *), ordered lists (1.), multi-line > blockquotes, fenced ``` code blocks with an optional language class and horizontal rules (---). Escape <, > and & everywhere, and don't treat '2 * 3 * 4' as italic.Objectives
- Write a block-level parser with state
- Apply inline rules with regular expressions in the right order
- Escape HTML safely
- Handle edge cases such as stray asterisks
Features
- Headings, paragraphs and horizontal rules
- Bold, italic, inline code and links
- Ordered and unordered lists
- Blockquotes and fenced code blocks with language
- HTML escaping everywhere
Concepts demonstrated
- Parsing
- Regular expressions with lookarounds
- Escaping and security
- State machines
System architecture
convert walks the lines, keeping the current paragraph and list type as state; helper closures flush paragraphs and close lists when a different block starts. Fenced code is copied verbatim (escaped) until the closing fence. inline splits out backtick code spans first, escapes the remaining text and then applies the bold, italic and link patterns; the italic pattern uses lookarounds so spaced asterisks aren't treated as emphasis.
Folder structure
Complete source code
main.py
What each file does
- main.py
- The block parser, the inline formatter and the command-line entry point.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# Learning Python
Python is **easy to read** and *fun to write*.
It runs almost everywhere.
## Why learn it?
- Huge standard library
- Great for `data` and *automation*
- Friendly community: [python.org](https://www.python.org)
### Getting started
1. Install Python
2. Open a terminal
3. Run `python --version`
> Simple is better than complex.
> Readability counts.
```python
def greet(name):
return f"Hello, {name} <3"
```
Special characters like <tags> & ampersands are escaped. 2 * 3 * 4 is not italic.
---
Thanks for reading!Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - Two lines of the first paragraph are joined into one <p>.
- The code block keeps its indentation, gets class="language-python" and has its quotes and < escaped.
- '<tags> & ampersands' are escaped and '2 * 3 * 4' stays plain text.
Common errors
Text inside `code` gets bold or italic
Split out code spans before applying the inline patterns.
User input can inject <script> tags
Escape text with html.escape before adding any markup.
Possible improvements
- Support nested lists
- Add tables
- Wrap the output in a full HTML document with a stylesheet
Additional challenges
- 1Generate a table of contents with anchor links from the headings.
- 2Support images: .
- 3Write unit tests for every Markdown feature.
Built and tested it yourself? Mark it to update your dashboard.