Skip to main content

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 the raw attribute]): "# 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markup_front_matter_parser-0.6.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markup_front_matter_parser-0.6.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file markup_front_matter_parser-0.6.0.tar.gz.

File metadata

File hashes

Hashes for markup_front_matter_parser-0.6.0.tar.gz
Algorithm Hash digest
SHA256 8e5e3d7cc9b1f45d1bef8d92e9af3ff3a2de510cf8eff377eecd6e280dbf553c
MD5 8200dfbb19c283fdd8dcfa5dfefb502b
BLAKE2b-256 54faf0698cb1255a6f95b987853e0ca795871ab6584e5ae4942bc75a4d973e6b

See more details on using hashes here.

File details

Details for the file markup_front_matter_parser-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markup_front_matter_parser-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbdb1d1114d000bfb42bd90a37f388f809d66987d3ee4f9fbe08de16427198c8
MD5 f0b5ad6c2a3c08f632e35d4b961e7b4a
BLAKE2b-256 10cadc7f4fe5cbd67252025c43c33568a4ba4b0e03e9a012c52968b43b2d518a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page