Package to slicing, parsing, and manipulating Markdown Files
Project description
mdslice
mdslice is a lightweight Python package for slicing, parsing, and manipulating Markdown files. It parses Markdown content into a structured object model, allowing you to easily access headers, paragraphs, code blocks, and other elements.
Features
- Structured Parsing: Converts Markdown into a
MarkdownDocumentcomposed ofParsedSectionobjects. - Section Categorization: Identifies various Markdown elements like headers, code blocks, lists, tables, images, and quotes.
- Flexible Input: Parse from files or directly from strings.
- Export to Dictionary: Convert parsed documents to a dictionary format for easy JSON serialization.
Installation
You can install mdslice using pip:
pip install mdslice
Or using uv:
uv add mdslice
Quick Start
Parsing a Markdown File
from pathlib import Path
from mdslice import parse_markdown_file
# Parse a file
doc = parse_markdown_file(Path("README.md"))
# Iterate through sections
for section in doc.sections:
print(f"Type: {section.type.name}, Content: {section.content[:50]}...")
Parsing Markdown Text
from mdslice import from_text
md_text = """
# My Title
This is a paragraph.
\```python
print("Hello World")
\```
"""
doc = from_text(md_text)
# Access headers
headers = doc.headers()
for header in headers:
print(f"Header (Level {header.depth}): {header.content}")
Advanced Usage
Filtering Headers by Depth
# Get only H1 and H2 headers
top_headers = doc.headers(min_depth=1, max_depth=2)
Finding a Specific Section
from mdslice import SectionType
# Find the first Python code block
python_block = doc.find(lambda s: s.type == SectionType.CODE and s.meta.get("lang") == "python")
if python_block:
print(f"Found code:\n{python_block.content}")
Converting to Dictionary
import json
doc_dict = doc.to_dict()
print(json.dumps(doc_dict, indent=2))
Models
MarkdownDocument
The main container for a parsed Markdown file.
sections: List ofParsedSectionobjects.path: OptionalPathto the source file.headers(min_depth, max_depth): Returns a filtered list of header sections.find(predicate): Finds the first section matching the predicate.to_dict(): Converts the document to a serializable dictionary.
ParsedSection
Represents a single element in the Markdown document.
type: ASectionTypeenum value.content: The raw text content of the section.depth: The header level (1-6) for headers, 0 otherwise.meta: Dictionary containing metadata (e.g.,langfor code blocks).
SectionType
An enum representing the type of section:
HEADERPARAGRAPHCODELISTTABLEIMAGEQUOTEINFONONE
Development
mdslice uses uv for dependency management and nox for multi-version testing.
License
MIT
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 mdslice-0.2.0.tar.gz.
File metadata
- Download URL: mdslice-0.2.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cc67324c69f1dc647e28d91681896ed229dcb09ccf6b94e80212cd5cc6ef038
|
|
| MD5 |
d7efac6f6e64afc69b351ca8c06b4a8f
|
|
| BLAKE2b-256 |
5fb166733f71ba25cb1457a06d1d05dbef847ad3ea0242271bd122082e55a452
|
File details
Details for the file mdslice-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mdslice-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
715259380ca7fca098ce375a062abcb3dc70ffff56d1ad39ed97b8159fbddc2e
|
|
| MD5 |
9ead007a817676b0193f3b046d644d36
|
|
| BLAKE2b-256 |
393b844df9608c61861863f08af69a77d3e22858fdf7df85f0b1ead1167f4070
|