Skip to main content

pygments-carve

CI

Carve lexer for Pygments.

Installing it is enough. Pygments discovers the lexer through the pygments.lexers entry point, so carve and crv become working fence words everywhere Pygments is the highlighter - MkDocs, Sphinx, zensical, pygmentize, and anything built on them.

There is no release on PyPI yet, so install from the repository:

pip install git+https://github.com/markup-carve/pygments-carve
```carve
# Heading /italic/ *bold*
```

Why Carve needs its own lexer

Carve is a lightweight markup language whose inline delimiters deliberately differ from Markdown's. A Markdown lexer does not merely under-highlight a Carve document, it highlights it wrongly:

Carve means Markdown reads it as
*bold* strong emphasis
/italic/ emphasis literal slashes
_under_ underline emphasis
~strike~ strikethrough subscript, or literal
{=mark=} highlight literal braces
{^sup^} superscript literal braces

What it covers

Every construct in Carve's shared grammar inventory: front matter, headings, lists (numeric, alphabetic, roman and bare-dot markers, task items in all their states, markers carrying glued attributes), tables with header and alignment markers, blockquotes, fenced and raw blocks, container divs and figure groups, captions, definition lists, comments in both the line and fence spellings, and the definition markers for footnotes, link references and abbreviations.

Inline: the emphasis families in Carve's spelling plus their braced forced forms, code and inline literals, math, links, images, spans, attributes, footnotes and inline footnotes, citations, cross-references, autolinks, extensions (:name[...]), symbols, code callouts in a fence, mentions, tags, escapes, hard breaks, and smart typography - all nineteen alternatives the spec grammar names under arrow, comparison and typographic_symbol plus the dash and ellipsis runs, which is the set Prism and highlight.js carry. Two productions of smart_typography are not scoped and are named as such in tests/test_typography.py: smart_quote, which is per-character contextual substitution rather than a run, and the braced en dash {--}.

Inside a span the content is lexed too, so {=a *b* c=} marks a strong inside the highlight, and a link's text holds its own emphasis.

Fence bodies in their own language

A fence naming a language Pygments knows is highlighted in that language, and a raw block's =FORMAT names its language the same way (=html reads as HTML). csv and tsv fences use built-in rules for separators, quoted fields, and doubled-quote escapes. Code callout markers (<1>) and, behind a quote, each line's > keep their own scope. A language Pygments does not know, or no language, leaves the body verbatim. This is the handlecodeblocks option of Pygments' Markdown lexer, with the same default; pass handlecodeblocks=False to read every body verbatim:

from pygments_carve import CarveLexer

lexer = CarveLexer(handlecodeblocks=False)

Deliberate limits

Block openers are not anchored at column 0. Carve opens a block at column 0 or at an enclosing container's content column - nowhere in between - so # H at document level is a paragraph while the same opener inside a list item is a real heading. Distinguishing them needs a container model. This lexer keeps the same trade-off the Prism and highlight.js grammars make (match at any indent, over-colour the rare invalid case) so the three surfaces agree; the TextMate grammar in carve-grammars is the surface that makes the distinction.

A fence is matched whole, and an unpaired opener claims nothing. A code fence, a raw block, front matter and a comment fence are each matched by ONE rule spanning opener, body and closer, because a Pygments state cannot hold a body: the stack resets at every newline no rule matches, so a state pushed on the opener's newline is abandoned before one body character is read (markup-carve/pygments-carve#32). Whole is also what carries the opener's width, character and column into the search for its closer. The trade is at the other end: an opener with no closer ahead really does open a code block that runs to the end of what encloses it, and here it colours its own line and claims nothing under it - the reading the line-based sibling grammars take, because a body that ran to end of input inside a container would swallow the container's own closer and every block after it. An opener whose info string is outside the three shapes the grammar admits does not pair either, because such a line opens no block at all and the corpus renders it as a paragraph. Nor does a body longer than 512 lines; that bound is what keeps a file of openers that can never pair from costing a scan of the document per line.

A fence opened inside a container pairs too: on a list item's or a description's marker line, with its closer at that item's content column, and behind a quote marker, with every line up to the closer carrying the marker. The same 512-line bound applies.

A paragraph is recognized where one starts after a blank line. List markers and captions do not interrupt a paragraph, so para followed by - x is one paragraph and the second line is not scoped as a list. A regex cannot see what the line above was, so a paragraph that follows a heading or a closed block with no blank line between them keeps the block reading on its later lines.

Pygments' stripnl and ensurenl are off. They strip a document's leading and trailing newlines and append one that is not there, which is reasonable for a programming language and not for Carve: a fence keeps the blank line at the end of its content, and the spec forbids encoding a blank payload line and no payload line identically. A caller who wants the Pygments behavior can still pass either option (markup-carve/pygments-carve#33).

An attribute block is one token. {#id .cls key="v"} is emitted whole as Name.Attribute rather than split into parts, matching how the sibling grammars treat it.

The grammar is maintained across surfaces in carve-grammars (Prism, highlight.js, TextMate, Tiptap), which is where the construct inventory lives. highlightjs-carve is the highlight.js side. A chroma lexer for Hugo can be generated from this one with chroma's own pygments2chroma_xml.py.

Development

Contributor setup, testing, and maintenance notes are in the development guide.

Metadata

Release files for pygments-carve 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygments-carve 0.1.2
File Size Uploaded Provenance Actions
pygments_carve-0.1.2.tar.gz 103.3 kB With provenance Details for pygments_carve-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pygments-carve 0.1.2
File Interpreter ABI Platform Actions
pygments_carve-0.1.2-py3-none-any.whl Python 3 none any Details for pygments_carve-0.1.2-py3-none-any.whl

Total release size: 138.1 kB

Release files / pygments_carve-0.1.2.tar.gz

Download URL pygments_carve-0.1.2.tar.gz
Size 103.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b91281cdfc1b545b0ebb4d620de86cda1e1b6ce1c4b56cd00734b4401bb833b6
BLAKE2b-256 checksum
How to use checksums
da20f97a59223a392bb919c0f9456b4d50e29768ff6d06ebef7d3e91494b94e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Release files / pygments_carve-0.1.2-py3-none-any.whl

Download URL pygments_carve-0.1.2-py3-none-any.whl
Size 34.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c9053130948e12eddc5a30963f2af065b33aaf54e230d0738cf60674ba40266
BLAKE2b-256 checksum
How to use checksums
c8502b8174c599729d4d36e88c8b5275b32eb991a0e8f93a5a06af1521451445
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Security for pygments-carve 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pygments_carve-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page