Metadata-Version: 2.4
Name: myform
Version: 2.0.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: POSIX :: Linux
Classifier: Framework :: Pytest
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup :: Markdown
Classifier: Typing :: Typed
Requires-Dist: mdformat>=1.0,<2
Requires-Dist: mdformat-deflist>=0.1.4
Requires-Dist: mdformat-footnote>=0.1.3
Requires-Dist: mdformat-front-matters>=2.0.0
Requires-Dist: mdformat-gfm>=1.0.0
Requires-Dist: mdit-py-plugins>=0.5.0
Requires-Dist: more-itertools>=10.7.0
Requires-Dist: my-basis>=1.0.0
Requires-Dist: pydantic>=2.11.7
Requires-Dist: regex>=2024.4.28
Requires-Dist: srsly>=2.5.3
Requires-Dist: wcwidth>=0.2.13
Requires-Dist: wikiparse>=1.0.2,<2 ; extra == 'wikitext'
Provides-Extra: wikitext
License-File: LICENSE
Summary: A typed translator that reads and writes MyST, GFM, Sphinx rST, and Typst.
Author-email: Robb Doering <robb@doering.ai>
License-Expression: MPL-2.0
Requires-Python: >=3.13
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://myform.docs.doering.ai/
Project-URL: Homepage, https://gitlab.com/doering-ai/libs/myform
Project-URL: Issues, https://gitlab.com/doering-ai/libs/myform/-/issues
Project-URL: Repository, https://gitlab.com/doering-ai/libs/myform

# myform

![logo](assets/logo_512.png)

> Released versions are available from PyPI.
> This checkout may contain unreleased changes.

[![Pipeline Status](https://img.shields.io/gitlab/pipeline-status/doering-ai/libs/myform?branch=main)](https://gitlab.com/doering-ai/libs/myform/-/pipelines?ref=main) [![Test Coverage](https://img.shields.io/gitlab/pipeline-coverage/doering-ai/libs/myform?branch=main)](https://gitlab.com/doering-ai/libs/myform/-/graphs/main/charts) [![License](https://img.shields.io/gitlab/license/doering-ai/libs/myform)](LICENSE) [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) [![pyrefly](https://img.shields.io/endpoint?url=https://pyrefly.org/badge.json)](https://github.com/facebook/pyrefly) [![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

`myform` is a universal docs-format translator built around one absolute, typed document tree.
It reads MyST Markdown, GitHub Flavored Markdown (GFM), Obsidian-Flavored Markdown, Sphinx reStructuredText, and Typst, then writes idiomatic MyST, GFM, Obsidian, Sphinx reStructuredText, Typst, or a bounded MediaWiki wikitext subset.
Formatting is the identity case: `myform.convert(text, format, format)` runs through the same reader, typed tree, and writer as a cross-format conversion.

| Format                     | Read | Write |
| :------------------------- | :--: | :---: |
| MyST Markdown              | Yes  |  Yes  |
| GFM                        | Yes  |  Yes  |
| Obsidian-Flavored Markdown | Yes  |  Yes  |
| Sphinx reStructuredText    | Yes  |  Yes  |
| Typst                      | Yes  |  Yes  |
| MediaWiki wikitext         | Yes  |  Yes  |

The two Markdown identity conversions remain the compatibility rail.
They produce byte-for-byte the same text as the existing mdformat pipelines, including the MyST opinions developed in this repository.

A sixth registered dialect, `plane` (`myform.convert(text, src, 'plane')`), is a different kind of target: it emits and reads Plane's stored `description_html` -- an empirically reverse-engineered ProseMirror/Tiptap-schema HTML subset -- for exact wire fidelity to one vendor's storage, not idiomatic semantic HTML.
It is intentionally absent from the table above.

**Documentation:** [myform.docs.doering.ai](https://myform.docs.doering.ai/)

## Install

Install the public release from PyPI:

```sh
pip install myform
```

To develop from the checked-out source with `uv`:

```sh
uv sync --all-extras
uv run myform --help
```

`myform 1.0.0` includes the native Typst reader, so Typst reads as well as writes.
That reader is backed by the `_typst_native` ABI3 extension over `typst-syntax`, and its source builds require Rust 1.92. The release pipeline intentionally produces one audited `cp313-abi3-manylinux_2_28_x86_64` wheel; macOS, Windows, non-x86_64 Linux, and older-glibc Linux systems must build from source until a verified wheel matrix is added.
`import myform` itself remains pure Python and loads the extension only when a Typst document is parsed.

## Translate documents

```sh
uv run myform convert guide.md --from myst --to rst -o guide.rst
printf '# Hello\n' | uv run myform convert - --from gfm --to typst
uv run myform convert guide.md --from myst --to gfm --report json >guide.gfm
uv run myform convert guide.md --from myst --to wikitext -o guide.wiki
```

Use `-` as the input path to read stdin.
Output goes to stdout unless `-o/--output` names a file.
Conversion reports always go to stderr, keeping document output safe for pipelines.
Choose `--report summary` (the default), `--report json`, or `--report none`.

## Format documents

The `format` verb performs an identity conversion:

```sh
uv run myform format README.md
uv run myform format README.md --format myst -o README.formatted.md
```

The CLI and Python calls with `target_path` share the Markdown engine and nearest-file `.mdformat.toml` discovery: `number`, `wrap`, `end_of_line`, and `[plugin.*]` apply, and excluded Markdown paths pass through unchanged.
Myform configuration selects the typed pipeline and overrides explicitly configured MyST options.
Explicit API options and CLI flags take precedence.
String-only Python calls keep their supplied options and myform configuration; pass the document path to share an editor or CLI configuration.
Optional shared defaults live at `$XDG_CONFIG_HOME/myform/.mdformat.toml` (normally `~/.config/myform/.mdformat.toml`).
The nearest local `.mdformat.toml` overrides shared behavior by key and plugin option.
Exclusions from both remain active.
An inherited `number = true` is a shared default, so Markdown list precedent still decides unless the project or caller requests numbered output explicitly.

The Python API exposes the same contract:

```python
import myform

rst = myform.convert(source, 'myst', 'rst')
formatted = myform.convert(source, 'myst', 'myst')
wikitext = myform.convert(source, 'myst', 'wikitext')
```

Extensions that preserve unsupported nodes out of band use `myform.convert_artifact(...)`.
Its `ConversionArtifact` keeps target text, the `ConversionReport`, and deterministic typed `EnclaveRecord` carriers together.
A carrier path is `enclaves/<full-sha256>.json`; every `ENCLOSED` report row binds its exact source occurrence to one carrier, and the target writer must recover a final typed link to that exact path.
The legacy `convert()` and `convert_with_report()` shapes raise `ArtifactRequiredError` rather than silently discarding a real carrier.
Built-in writers do not yet claim `ENCLAVE`; each adoption requires a target-specific marker/carrier contract and tests.

Wikitext reading uses the optional [`wikiparse`](https://gitlab.com/doering-ai/apps/wikiparse) adapter (`myform[wikitext]` extra) and requires the `pandoc` executable on `PATH`.
The adapter translates wikitext through wikiparse's document profile into Markdown, then feeds that through the existing MyST reader to produce the typed `Doc`.
Templates are extracted as standalone citation records by the upstream parser; non-citation templates are converted to text or carried.
Install the extra and pandoc to read wikitext:

```sh
pip install myform[wikitext]
```

## Configure translation

`myform` discovers the nearest `myform.yaml`, `.myform.yaml`, or `[tool.myform]` table by walking upward from the target path.
Within one directory, that order is also the precedence order.
The nearest directory wins rather than merging several files.

```yaml
degrade:
  mode: annotate
  comments: true
assist:
  enabled: false
typst:
  postformat: off
extensions: []
```

### Definition preservation

MyST, GFM, and Obsidian preserve unused footnote and link definitions by default, including through the mdformat `myst` facade.
A fragment can contain a definition whose call is in another fragment.
Formatting does not diagnose unused definitions over an assumed complete document.

A label that is defined more than once keeps every definition exactly where it is written, and the rest of the document is formatted as usual; see [Constructs left as written](#constructs-left-as-written).
Fenced examples are literal content.
Explicit orphan removal is suspended for a document that carries any construct as written, because a reference inside such a block is a use that removal cannot see.

For deliberate removal over a complete document, use `myform format --no-keep-footnote-orphans`, `options={'keep_orphans': False}`, or top-level `keep_orphans: false` in myform configuration.
The legacy `.mdformat.toml` setting `[plugin.footnote] keep_orphans = false` controls both definition kinds.
`--keep-footnote-orphans` forces preservation.
Preservation does not make arbitrary formatting lossless; see the remaining limitations below.

### Constructs left as written

Some constructs cannot be rewritten without discarding text, so formatting leaves each one exactly as written and formats everything around it.
No text of them is lost, and formatting the output again changes nothing.
The one to three spaces that may indent a construct's first line are structure, not text; they are removed, so that re-indenting a neighboring list cannot absorb the construct.

- A table row with more cells than its header keeps the whole table as written.
  The table grammar would otherwise drop the extra cells.
- Footnote or link definitions that share a label keep every definition in place, in the order written.
  CommonMark keeps the first definition of a link label, so none of them can start to win in its place.
  Unrelated definitions are still gathered after the text.

The command line names each such construct on stderr under `--report summary` (the default) or `--report json`; `--report none` stays silent.
The conversion report holds one `preserved` entry per construct, which is not a degradation, so `lossy` stays false.
Converting to a format other than MyST, GFM, or Obsidian carries the construct as literal code and reports that as a replacement.

### Fold-aware tables

MyST and GFM table writers size columns from visible inline content by default.
Only typically-hidden material -- link destinations, image URLs, and MyST role targets -- is folded out of the measurement; rendered markup such as emphasis, code, math, and HTML still counts at its serialized width.
Markdown source remains unchanged apart from padding, so pipe positions can vary when a folded cell's serialized form is longer than its measured width.

Disable the policy per target when source-character alignment is required:

```yaml
myst:
  fold_aware_tables: false
gfm:
  fold_aware_tables: false
```

The MyST mdformat facade exposes the same default as `[plugin.myst] fold_aware_tables = true`; `--no-fold-aware-tables` disables it for one invocation.
Bare `mdformat --extensions gfm` retains the upstream GFM formatter, while `myform.format(..., 'gfm')` uses this policy.

### Safe Setext headings

MyST, GFM, and Obsidian formatting rewrite ordinary Setext headings to ATX form by default.
A list label followed by a single nested dash is preserved as an empty nested list rather than being mistaken for an H2 underline.
Set `myst.setext_headings: false` (or use `--no-setext-headings` in the MyST facade) to retain the stock CommonMark precedence for MyST; the universal API accepts `options={"setext_headings": false}` for any CommonMark-family reader.

Every non-native conversion is represented in the conversion report.
The default `annotate` degradation mode also leaves a provenance comment when the target supports comments; `silent` suppresses comments but not reports, while `strict` refuses carrying, dropping, or enclosing unsupported constructs.

The optional LM assist is degradation-only and disabled by default, which makes zero network calls.
When enabled, it uses the configured local OpenAI-compatible gateway and retains a deterministic fallback.

The committed [`myform.schema.json`](myform.schema.json) and [`myst-options.schema.json`](myst-options.schema.json) schemas describe the translator and mdformat-facade configuration surfaces.
Refresh or verify both with:

```sh
uv run myform-write-schema
uv run myform-write-schema --check
```

## Use the MyST mdformat facade

The original plugin remains available through mdformat's `myst` extension entry point:

```sh
uv run mdformat README.md
```

This repository began as a ground-up rewrite of [`mdformat-myst`](https://github.com/executablebooks/mdformat-myst), written to make uncompiled MyST documents look closer to the hand-formatted sources maintained by the MyST team.
That facade remains useful on its own:

1. [Targets](https://myst-parser.readthedocs.io/en/latest/syntax/cross-referencing.html#creating-explicit-targets) hug the headings or blocks they identify instead of being separated by an extra blank line.

2. [Directive options](https://myst-parser.readthedocs.io/en/latest/syntax/roles-and-directives.html) prefer the readable `:key: value` prefix form and escalate to a YAML block only when necessary.
   Newly quoted single-line values use YAML-compatible string encoding, preserving apostrophes, embedded quotes and literal backslashes.

3. Directive contents are formatted as first-class Markdown rather than treated as opaque text.

4. Parser and renderer rules are explicit, decorator-registered examples of a modern mdformat extension.

5. YAML values use `srsly`'s maintained `ruamel.yaml` integration.

6. The optional `sembr` mode reflows paragraphs to [one sentence per line](https://sembr.org/) without changing their rendered output:

   ```toml
   [plugin.myst]
   sembr = true
   ```

   Enable it per invocation with `mdformat --sembr <filename>`; `--no-sembr` overrides the TOML setting.

   The splitter holds back wherever only a citation can start or continue: abbreviations (`cf.`, `nn.`, `vgl.`), labels with a number (`Prop. 17`, `art. 3`), roman numerals (`II. Def. vi`), and locators (`chap. 7 and 8`, `12:10`).
   List items keep their prose on one line, so a short bullet stays compact, unless `sembr_list_items = true` is set as well; each continuation line then sits under its item's marker. An item inside a block quote always stays on one line.

7. Markdown formatting follows the document's list and table style.
   The first sibling gap at each depth of a root list determines its item spacing.
   A blank between blocks within one item can still require a loose list.
   Sibling lists with different markers stay separate, and each table keeps compact or aligned padding according to its own rows.
   The first two numbers of each ordered list normally establish repeated or consecutive numbering.
   A list that begins with a different pattern keeps its numbers.
   To keep every written number, including a deliberate gap later in the list, use:

   ```toml
   [plugin.myst]
   list_numbering = "preserve"
   ```

   `mdformat --list-numbering preserve <filename>` does the same for one invocation.
   An explicit `--number` or local `number = true` selects mdformat's consecutive zero-padded numbering unless `list_numbering` sets a mode.

   If a trait is absent in the root list, myform reads the first example at the same depth elsewhere in the file, then the document's `myform:` front matter, then project configuration, then its default.
   Declare project fallback traits inside `precedent` in `myform.yaml`:

   ```yaml
   precedent:
     spacing: loose
     depths:
       1:
         indent_step: 4
   ```

   A document can supply the same trait keys under its `myform:` front matter key.
   Emphasis and strong marks follow the first occurrence of their kind in the file when the replacement still parses the same way.

8. MyST's `attrs_block` and `amsmath` extensions are read as a site that enables them reads them.
   A line holding only an attribute list (`{.glossary}`, `{#id}`, `{attribution="Immanuel Kant"}`) is a block of its own.
   It stays directly above the block it describes (a definition list, heading, quote, fence, table) and stays apart from it when it began apart.
   A bare `\begin{align*}` ... `\end{align*}` environment is display math and comes back exactly as written, indentation included, instead of escaped as prose.
   Braces that do not hold a valid attribute list (`{name}`, `{u = 0}`, `{Footnote 1: See § xviii.}`) stay text, and the line never interrupts a paragraph.

The facade also preserves established skip-formatting comments:

```md
<!-- myform-ignore -->

This next block is left exactly as written.
```

```md
<!-- myform-ignore-start -->

Every block in this range is preserved.

<!-- myform-ignore-end -->
```

```md
<!-- myform-ignore-file -->
```

The markers act at the document top level.
Their strings remain configurable through `[plugin.myst]`.

### Format a library of scholarly files

OCR-derived scholarly chapters are long, citation-dense, and full of numbered prose.
This `.mdformat.toml` formats a whole file of that kind without renumbering lists or re-wrapping text:

```toml
wrap = "keep"                # line breaks stay as written; sembr alone decides them
# number is deliberately unset: list_numbering owns ordered lists

[plugin.myst]
list_numbering = "preserve"  # 2. 3. 4. stay 2. 3. 4.
sembr = true                 # optional: one sentence per line
sembr_list_items = true      # optional: numbered prose items too
```

Formatting is meant to change layout, not text, but mdformat and the CommonMark rules behind it make a few exceptions that matter for scanned books.
Check these before a library-wide run:

- A table row with more cells than its header loses the extra cells. GFM drops them when the table is parsed, so the formatter never sees them. Widen the header, or fence the table off with `<!-- myform-ignore -->`.
- Footnote definitions are written in the order of their first reference, not the order of the source.
- A link destination is percent-encoded when it holds a non-ASCII character or a backslash.
- An entity such as `&lt;` comes back as the character it names.
- A `$$...$$` pair inside a paragraph reads as inline math between two literal dollar signs, and the dollar signs come back escaped (`\$$...$\$`).
- A tangle of `*` runs (OCR'd bold and italic fragments) can re-nest differently on the first format and is not stable until the second. Clean such passages in the source.
- A role cannot hold a `{` in its name or attribute list. A stray `{` in prose therefore stays text rather than swallowing everything up to the next role.
- `sembr` preserves parsed link/image titles and wiki syntax. Escaped literal wiki-shaped text can still fail a two-pass fixed-point check; inspect the output and retain the source when that check fails.

## Documentation

- [Documentation front door](https://myform.docs.doering.ai/)
- [API reference](https://myform.docs.doering.ai/api.html)
- [Universal translator specification](https://myform.docs.doering.ai/spec/myform-spec.html)
- [Release procedure](RELEASING.md)

