Pre-release
This release is a pre-release and may not be stable for production use.
= AsciiDocString
:toc: left
:sectnums:
:idprefix:
:idseparator: -
image:https://github.com/webmaven/asciidocstring/actions/workflows/ci.yml/badge.svg[Build Status, link=https://github.com/webmaven/asciidocstring/actions/workflows/ci.yml]
image:https://img.shields.io/pypi/v/asciidocstring.svg[PyPI Version, link=https://pypi.org/project/asciidocstring/]
image:https://img.shields.io/pypi/pyversions/asciidocstring.svg[Python Versions, link=https://pypi.org/project/asciidocstring/]
image:https://img.shields.io/pypi/l/asciidocstring.svg[License, link=https://github.com/webmaven/asciidocstring/blob/main/LICENSE]
image:https://img.shields.io/badge/coverage-99%25-success[Coverage]
A pure-Python semantic parser, extractor, and translator for Python docstrings written in AsciiDoc. Fully compatible with Python 3.14+ and WASM/Pyodide environments with zero native compiled extensions.
== Introduction
`asciidocstring` is built on top of the pure-Python `asciidoctrine` parser. It is designed to cleanly process Python docstrings written in AsciiDoc, resolve indentation, and parse them into a lossless Abstract Semantic Graph (ASG).
This parsed semantic representation can be used by downstream libraries to:
1. Render high-fidelity, Sphinx-compatible reStructuredText (reST) using `sphinx-asciidoctrine`.
2. Query and extract executable interactive doctest code blocks using `asciidoctest`.
== Installation
Initialize your project and install the library:
[source,bash]
----
pip install asciidocstring
----
To install optional developer dependencies (testing and linting tools):
[source,bash]
----
pip install "asciidocstring[test,lint]"
----
== Usage
=== Quick Start
[source,python]
----
import asciidocstring
docstring = """
= Parse Coordinates
This function processes dynamic coordinate objects.
[source,python,test]
----
assert parse_coords(10, 20) == (10, 20)
----
x (int):: The horizontal component
y (int):: The vertical component
"""
# Parse the raw docstring (automatically cleans leading docstring indentation)
doc = asciidocstring.parse(docstring)
# Translate the docstring into reStructuredText (reST) for Sphinx
rest_text = doc.to_rest()
print(rest_text)
# Extract code blocks tagged for doctesting
test_blocks = doc.extract_tests(language="python")
for block in test_blocks:
print(f"Test Code ({block.language}):")
print(block.code)
----
=== Catching Syntax and Parsing Errors
The library includes robust, structured syntax error handling. When parsing syntactically invalid AsciiDoc, an `AsciiDocStringParseError` is raised, detailing the exact location and a visual caret context.
[source,python]
----
import asciidocstring
invalid_docstring = """
= Sample Header
:: invalid-syntax
"""
try:
asciidocstring.parse(invalid_docstring)
except asciidocstring.AsciiDocStringParseError as e:
print(f"Error Message: {e}")
print(f"Error Location: Line {e.line}, Column {e.column}")
print("Caret Preview:")
print(e.context)
----
Expected output:
[source,text]
----
Error Message: AsciiDoc Parse Error: Syntax error at line 3, column 1.
:: invalid-syntax
^
Error Location: Line 3, Column 1
Caret Preview:
:: invalid-syntax
^
----
== API Reference
=== Functions
* `parse(docstring: str) -> AsciiDocStringDocument` +
Convenience function to parse a raw Python docstring into a document object.
=== Classes
* `AsciiDocStringDocument` +
The main interface representing a parsed docstring document.
** `__init__(raw_source: str)`: Cleans and parses the given docstring.
** `to_rest() -> str`: Renders the parsed document as standard Sphinx-compatible reStructuredText.
** `extract_tests(language: str = "python", requires_test_marker: bool = False) -> list[TestBlock]`: Extracts executable code blocks.
* `TestBlock` +
Represents an extracted code block designed for execution or testing.
** `code` (str): The raw code contents of the block.
** `language` (str): The code block language (e.g. `python`).
** `is_test` (bool): True if the block has been explicitly marked as a test block.
* `AsciiDocStringParseError` +
Raised when parsing an AsciiDoc docstring fails. Inherits from `ValueError`.
** `line` (int | None): The line number of the parsing error.
** `column` (int | None): The column number of the parsing error.
** `context` (str | None): A visual text block indicating the line of code and a caret highlighting the syntax error position.
== Developer Guide
Ensure you have your environment set up and dependencies installed:
[source,bash]
----
# Set up a virtual environment
python3 -m venv venv
source venv/bin/activate
# Install the package in editable mode with development dependencies
pip install -e ".[test,lint]"
----
=== Running Tests
We maintain 100% test coverage standards. To run tests and generate a coverage report:
[source,bash]
----
PYTHONPATH=src pytest --cov=src --cov-report=term-missing
----
=== Static Analysis
Run our linting and type-safety check pipeline:
[source,bash]
----
# Run Ruff code format and quality checks
ruff check src/ tests/
# Run MyPy type-safety validation
mypy src/
----
== License
This project is licensed under the Apache License, Version 2.0.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
asciidocstring-0.1.0a3.tar.gz
(16.6 kB
view details)
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 asciidocstring-0.1.0a3.tar.gz.
File metadata
- Download URL: asciidocstring-0.1.0a3.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0120ae213349cc1c70afa06bf5a12854f8343b391f52c5a6203102df0a5cd1cb
|
|
| MD5 |
1bacbdf6f3df5c0bc1f02afe59df6c21
|
|
| BLAKE2b-256 |
4b6d05cef70c526635ea5c29abc82275050dc36914d7bfb611b71b1ac290a7c1
|
File details
Details for the file asciidocstring-0.1.0a3-py3-none-any.whl.
File metadata
- Download URL: asciidocstring-0.1.0a3-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6e395a665e787d12ffa46161ae0b04e94f4849913e647ec9c46773b260c084a
|
|
| MD5 |
6fb21b0885d8c4e94cb458e17332dc64
|
|
| BLAKE2b-256 |
14690ad09d63720897ef733b0441aab49ddba1d8ec616afada3bb5424faa55d2
|