An OOP-y front matter parser for markup files
Project description
Markup Front Matter Parser
This is an OOP-y Python package for parsing the Front Matter of markup files (e.g. .md, .html).
Front Matter is a YAML snippet at the top of a markup file that stores metadata
that is used commonly in Jekyll static sites.
Note that this package is a simple front matter parser that will only parse single
line key-values. It currently does not support parsing multi-line front matter mapping entries (e.g. YAML lists).
Usage
Install the package with:
pip install markup-front-matter-parser
In your code import the following:
from markup_front_matter_parser import FrontMatterParserFactory, InvalidFrontMatterError
Note: If you'd like to add type-hints to objects, then you can additionally import MarkupFile, MarkupContent, and FileFormat.
To use in your code, start by creating an instance of FrontMatterParserFactory and then call get_parser() with the desired file you want parsed:
parser_factory = FrontMatterParserFactory()
parser: AbstractFrontMatterParser = parser_factory.get_parser("songs/crazy-train.md")
Obtaining the parser may raise a ValueError if the file type is incompatible and a FileNotFoundError if the file is not found.
Calling parse() on this parser instance will return a MarkupFile instance that contains the parsed front matter and other attributes:
try:
markup_file: MarkupFile = parser.parse()
except InvalidFrontMatterException as e:
[...]
For example, parsing the file crazy_train.md:
---
name: Crazy Train
artist: Ozzy Osbourne
---
# This is content.
Hooray for content
Would result in a MarkupFile object composed of:
- A front_matter attribute:
{"name": "Crazy Train", "artist": "Ozzy Osbourne"} - A content attribute (an instance of
MarkupContent[specifically therawattribute]):"# This is content.\nHooray for content\n" - A path attribute:
"crazy_train.md" - A file_format attribute:
FileFormat.MARKDOWN
The parse() method will raise an informative InvalidFrontMatterError if the file's front matter cannot be parsed.
Code Overview
This was written within the context of an in-depth sprint of learning Python, so it may not be the most practical package for the job.
The architecture of the package uses the factory pattern and was designed so that future improvements would not change the public API.
In addition, each subclass of AbstractFrontMatterParser could easily implement their own tokenizing of file-specific markup content.
In this fashion, the package could then become a holistic markup parser rather than solely just a front matter parser.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markup_front_matter_parser-0.6.0.tar.gz.
File metadata
- Download URL: markup_front_matter_parser-0.6.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e5e3d7cc9b1f45d1bef8d92e9af3ff3a2de510cf8eff377eecd6e280dbf553c
|
|
| MD5 |
8200dfbb19c283fdd8dcfa5dfefb502b
|
|
| BLAKE2b-256 |
54faf0698cb1255a6f95b987853e0ca795871ab6584e5ae4942bc75a4d973e6b
|
File details
Details for the file markup_front_matter_parser-0.6.0-py3-none-any.whl.
File metadata
- Download URL: markup_front_matter_parser-0.6.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbdb1d1114d000bfb42bd90a37f388f809d66987d3ee4f9fbe08de16427198c8
|
|
| MD5 |
f0b5ad6c2a3c08f632e35d4b961e7b4a
|
|
| BLAKE2b-256 |
10cadc7f4fe5cbd67252025c43c33568a4ba4b0e03e9a012c52968b43b2d518a
|