Python bindings for mq, a jq-like command-line tool for Markdown processing
Project description
mq-python
Python bindings for the mq Markdown processor.
Installation
pip install markdown-query
Usage
Basic Usage
Use the run function to process Markdown with mq queries:
import mq
# Extract all level 1 headings
result = mq.run(".h1", "# Hello World\n\n## Heading2\n\nText")
print(result.values) # ['# Hello World']
# Extract all level 2 headings
result = mq.run(".h2", "# Main Title\n\n## Section A\n\n## Section B")
print(result.values) # ['## Section A', '## Section B']
# Get all results as a single string
print(result.text) # '## Section A\n## Section B'
Filtering and Transforming
Use mq query syntax to filter and transform Markdown:
import mq
markdown = """
# Product
## Features
Great features here.
## Installation
Install instructions.
"""
# Filter headings containing specific text
result = mq.run('.h2 | select(contains("Feature"))', markdown)
print(result.values) # ['## Features']
# Extract list items
result = mq.run(".[]", "# List\n\n- Item 1\n- Item 2\n- Item 3")
print(result.values) # ['- Item 1', '- Item 2', '- Item 3']
# Extract code blocks
result = mq.run(".code", "# Code\n\n```python\nprint('Hello')\n```")
print(result.values) # ["```python\nprint('Hello')\n```"]
Input Formats
mq supports multiple input formats:
import mq
# Markdown (default)
options = mq.Options()
options.input_format = mq.InputFormat.MARKDOWN
result = mq.run(".h1", "# Heading", options)
# MDX (Markdown with JSX)
options = mq.Options()
options.input_format = mq.InputFormat.MDX
result = mq.run("select(is_mdx())", "# MDX\n\n<Component />", options)
print(result.values) # ['<Component />']
# HTML
options = mq.Options()
options.input_format = mq.InputFormat.HTML
result = mq.run('select(contains("Hello"))', "<h1>Hello</h1><p>World</p>", options)
print(result.values) # ['# Hello']
# Plain text
options = mq.Options()
options.input_format = mq.InputFormat.TEXT
result = mq.run('select(contains("2"))', "Line 1\nLine 2\nLine 3", options)
print(result.values) # ['Line 2']
Available input formats:
InputFormat.MARKDOWN- Standard Markdown (default)InputFormat.MDX- Markdown with JSXInputFormat.HTML- HTML contentInputFormat.TEXT- Plain textInputFormat.RAW- Raw string inputInputFormat.NULL- Null input
Rendering Options
Customize the output rendering:
import mq
options = mq.Options()
options.input_format = mq.InputFormat.MARKDOWN
options.list_style = mq.ListStyle.PLUS # Use '+' for list items
options.link_title_style = mq.TitleSurroundStyle.SINGLE # Use single quotes for link titles
options.link_url_style = mq.UrlSurroundStyle.ANGLE # Use angle brackets for URLs
result = mq.run(".", markdown, options)
Available options:
ListStyle:DASH(default),PLUS,STARTitleSurroundStyle:DOUBLE(default),SINGLE,PARENUrlSurroundStyle:NONE(default),ANGLE
HTML to Markdown Conversion
Convert HTML to Markdown:
import mq
html = "<h1>Hello World</h1><p>This is a <strong>test</strong>.</p>"
markdown = mq.html_to_markdown(html)
print(markdown) # '# Hello World\n\nThis is a **test**.'
# With conversion options
options = mq.ConversionOptions()
options.extract_scripts_as_code_blocks = True # Convert <script> tags to code blocks
options.generate_front_matter = True # Generate front matter from metadata
options.use_title_as_h1 = True # Use <title> as h1 heading
markdown = mq.html_to_markdown(html, options)
Working with Results
The run function returns an MQResult object:
import mq
result = mq.run(".h", "# H1\n\n## H2\n\n### H3")
# Get the number of results
print(len(result)) # 3
# Access individual results by index
print(result[0].text) # '# H1'
# Iterate over results
for value in result.values:
print(value)
# Get all results as a single string
print(result.text) # '# H1\n## H2\n### H3'
# Check if a value is in the result
print("# H1" in result.values) # True
Each MQValue has the following properties:
text- The string representation of the valuevalues- For arrays, returns the list of valuesmarkdown_type- The type of Markdown element (e.g.,Heading,Code,List)is_array()- Check if the value is an arrayis_markdown()- Check if the value is a Markdown element
Error Handling
Invalid queries raise a RuntimeError:
import mq
try:
result = mq.run(".invalid!!!", "# Heading")
except RuntimeError as e:
print(f"Query error: {e}")
Development
Building from Source
git clone https://github.com/harehare/mq
cd mq/crates/mq-python
pip install maturin
maturin develop
Running Tests
pytest tests/
Support
License
Licensed under the MIT License.
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 Distributions
Built Distributions
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 markdown_query-0.5.15-pp311-pypy311_pp73-win_amd64.whl.
File metadata
- Download URL: markdown_query-0.5.15-pp311-pypy311_pp73-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: PyPy, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8e1656b89ace13cbded89c7a66936032068d9098a1486525b25293caf7b0e19
|
|
| MD5 |
4af6102cc8d77e33d9c46429c120b51b
|
|
| BLAKE2b-256 |
ff71e184f9cd86ef7b19c55d5aeb7225b3ff0539c98ff9977ffb4fa6ccfe3d9c
|
File details
Details for the file markdown_query-0.5.15-cp313-cp313t-win_amd64.whl.
File metadata
- Download URL: markdown_query-0.5.15-cp313-cp313t-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.13t, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de7b5fbefcbeef2fd3c50ff5ce0896f47b5fc2e5ccc1f82c582a6438f32ab6e5
|
|
| MD5 |
4e964eadfd0a035eaa42140bc80399b1
|
|
| BLAKE2b-256 |
79cca15a08844c5dbdb941a904d238d1810344179b7ccfb1dbaea6daccacc5ce
|
File details
Details for the file markdown_query-0.5.15-cp39-abi3-win_amd64.whl.
File metadata
- Download URL: markdown_query-0.5.15-cp39-abi3-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.9+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ebd4358e4a1f68dc8b78e68b7c8b78fea6a5ebb2be51e73112c50d39295ab62
|
|
| MD5 |
5d5e59c050b3eade21970500599577b8
|
|
| BLAKE2b-256 |
d53c59fc59922ce04754bc9d9abbc2f4be6f83a6279c183f0ea09ef3e11dbf10
|
File details
Details for the file markdown_query-0.5.15-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: markdown_query-0.5.15-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd23fb2eec72140670651c17c02e9223f5356ba1ee031e85ad252d1b1e0decb2
|
|
| MD5 |
9b0bb25cc53c4e939eaae4b946e6f7b5
|
|
| BLAKE2b-256 |
f51c16e3d19df36d7d54a9c8b8e6dcbe68198cdefec569392a53ff46904adc6c
|
File details
Details for the file markdown_query-0.5.15-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: markdown_query-0.5.15-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2273b6d8a0c9a5391e5b42d4d31ce3b873460989b33f977eb8535c26be47644a
|
|
| MD5 |
35ec909bb7386fe56ae3711c6b710bd5
|
|
| BLAKE2b-256 |
a4ae0290597fae7ab65200e033afb48e06b40ae3c803b3dfb1c6782dae1d1246
|
File details
Details for the file markdown_query-0.5.15-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: markdown_query-0.5.15-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68827f75627d838e9bb8837f231a57ffd9acf7863b2af21f2630ac5bc5ebd8fa
|
|
| MD5 |
122e4d7c8c14f6f3e5a65010a997cc56
|
|
| BLAKE2b-256 |
1160daefef4b0cf4ae958ae43ae4931652c6cbe6d66e649ef0c913d2abb37ee4
|
File details
Details for the file markdown_query-0.5.15-cp39-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: markdown_query-0.5.15-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ee308b4997a6c416dfde34e7d275774ff94b6cf270058e8eb19ee0bf3c5a8c7
|
|
| MD5 |
c95943838a50ab6ef0728ee350566c18
|
|
| BLAKE2b-256 |
f042795741a8d31fa8c96e3d2d7dd019f0291d178b34ca10726670171deb5247
|