Skip to main content

Python bindings for mq, a jq-like command-line tool for Markdown processing

Project description

mq-python

PyPI ci GitHub Release codecov CodSpeed Badge

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 JSX
  • InputFormat.HTML - HTML content
  • InputFormat.TEXT - Plain text
  • InputFormat.RAW - Raw string input
  • InputFormat.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, STAR
  • TitleSurroundStyle: DOUBLE (default), SINGLE, PAREN
  • UrlSurroundStyle: 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 value
  • values - For arrays, returns the list of values
  • markdown_type - The type of Markdown element (e.g., Heading, Code, List)
  • is_array() - Check if the value is an array
  • is_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


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

markdown_query-0.5.14-pp311-pypy311_pp73-win_amd64.whl (1.2 MB view details)

Uploaded PyPyWindows x86-64

markdown_query-0.5.14-cp313-cp313t-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.13tWindows x86-64

markdown_query-0.5.14-cp39-abi3-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.9+Windows x86-64

markdown_query-0.5.14-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

markdown_query-0.5.14-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

markdown_query-0.5.14-cp39-abi3-macosx_11_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

markdown_query-0.5.14-cp39-abi3-macosx_10_12_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file markdown_query-0.5.14-pp311-pypy311_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-pp311-pypy311_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 3e2f283d9ade1c34a33df0d5f64d10d4f6a5fc1bb3d2ca8903a2d343b308a8db
MD5 c7f791f218a048f6f2052f0b7516a8fb
BLAKE2b-256 bbca100fd87e5666e203727cb922a9e0bff91f6dcec68ca0cb7eecda2843e33d

See more details on using hashes here.

File details

Details for the file markdown_query-0.5.14-cp313-cp313t-win_amd64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 41a6b7828f4908129fb6171922e5c3c84eff665850677b62ac9c8da02a28c67d
MD5 768e6ac09ebde62ba9129106ac4c3ceb
BLAKE2b-256 cc25468b83a4cc915bcd532bf2f76011d758b6ea08c4ac6e46cf6aa99fa61ade

See more details on using hashes here.

File details

Details for the file markdown_query-0.5.14-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6f8cf1d2097196e991fc6c90d9fa99d628452712d5dc0e12af41454c705a45cf
MD5 fdbcbee51a9e37ee2ab30c4470861ec0
BLAKE2b-256 1ddf0e92ec07ceb75e1cc411a1faffcf76df1e22fadda22d7e992daf6861f197

See more details on using hashes here.

File details

Details for the file markdown_query-0.5.14-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 af5fcd316df3a85647d976a7c3cca8a6911350427f8f6be9fa023fa6ba5919be
MD5 a75d0277f2daf1b446ba0791d8abe9cd
BLAKE2b-256 3f1a5e7fddc6eb6f008bf241dd0442736942720957149fe54a612bdc2cce2017

See more details on using hashes here.

File details

Details for the file markdown_query-0.5.14-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 89987f082d4ef0e2d55e68b54de8b5a39e9f57f8ccd5e5d59aeceb03335a5c61
MD5 9171898ff34cb62726371f5f27ef6332
BLAKE2b-256 fc60d22457302cd945312000b0db13417b18471c0b87514af8899cb399543bfe

See more details on using hashes here.

File details

Details for the file markdown_query-0.5.14-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b2a16dce045c89110c507fd0b2bdb7ee271867b66336f471d83b5d8c0abe9993
MD5 d9b9e62dd5d3ce522e7cff1b36fa2cf5
BLAKE2b-256 53969c36a1d60c90ff207980a3e00c53f9a02f8c39ed6e8d67f6c1e217ff4d0e

See more details on using hashes here.

File details

Details for the file markdown_query-0.5.14-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for markdown_query-0.5.14-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 041b5802c30c9f577812390e87b89602c99643469661843822ad0be8ddc7e2c1
MD5 4b5f348b7f3bb2661fb0e23996abc21c
BLAKE2b-256 b8bfdd78b042474118a1e1946ff114ec3a7d93fe0d2f588368058308a95df3a8

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