Metadata-Version: 2.5
Name: prosefix
Version: 0.5.0
Summary: Fix spelling, style, typos and punctuation in Markdown files, in place.
Project-URL: Homepage, https://github.com/AndrewBroz/prosefix
Project-URL: Issues, https://github.com/AndrewBroz/prosefix/issues
Author: Andrew Brož
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: End Users/Desktop
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Text Processing :: Markup :: Markdown
Requires-Python: >=3.11
Requires-Dist: adjudicate<0.4,>=0.3.9
Requires-Dist: proofix<0.11,>=0.10.0
Requires-Dist: pyyaml>=6
Requires-Dist: stylefix<0.12,>=0.11.0
Description-Content-Type: text/markdown

# prosefix

prosefix fixes spelling, style, typos and punctuation in Markdown files, in place.

```sh
prosefix draft.md             # fix one file in place
prosefix draft.md --diff      # show the changes without writing them
prosefix vault/ --check       # exit 1 if any file would change
```

prosefix runs two passes over each file with one model endpoint, one decision cache and one change list. The style pass (stylefix) settles one English variety and one style guide; the proof pass (proofix) fixes typos, punctuation, compounds and commas. Front matter, code, links, HTML and Obsidian syntax are never changed.

prosefix applies a fix only when it is always right; when judgement is needed it asks the model; anything uncertain is listed for you.

```text
Before:  We recieved the the new colour scheme -- the teams programme , which covers analysis,design and testing.
After:   We received the new colour scheme – the teams programme, which covers analysis, design and testing.
```

(A British draft, kept British, with the tools installed and no model. Words that depend on meaning, such as licence and license, are listed for review until a model decides them; see [Questions without a model](#first-run).)

## Install

```sh
brew install andrewbroz/tap/prosefix     # also installs Vale, LanguageTool and typos
uv tool install prosefix                 # or: pipx install prosefix
```

Without Homebrew, install the tools you want. prosefix runs every tier whose requirements are met:

| missing | skipped | still runs |
|---|---|---|
| [Vale](https://vale.sh) (`brew install vale`) | `variety`, `guide`, `ambiguous`, `numbers` | `serial-comma`, `abbreviations`, `punctuation`, and all of the proof pass |
| [LanguageTool](https://languagetool.org) (`brew install languagetool`) | `mechanical`, `adjudicated` | `hygiene`, `basics`, `capitals`, `confusables`, `compounds`, `commas` |
| [typos](https://github.com/crate-ci/typos) (`brew install typos-cli`) | `typos` | everything else |
| a model | decisions that need judgement, `confusables`, `commas` | rule-based tiers; undecided items are listed for review (see [Questions without a model](#first-run)) |

## First run

```sh
prosefix draft.md --diff   # see what would change
prosefix draft.md          # write it
```

The first in-place run prints `tip: prosefix --diff <path> shows changes without writing them`; it stops once prosefix has a cache directory.

**Without the tools.** A bare `uv tool install prosefix` still fixes the obvious mechanical faults with the `basics` tier, which needs nothing else: a doubled small word from a closed list (a, an, the, of, to, at, for, with, from, into, onto, and, or, but, nor, it, we, they, my, your, our, their: `the the`), a space before a comma or period (`programme ,`), and a missing space after a comma, semicolon or colon between two words (`analysis,design`). Other repeats (`that that`, `log in in the corner`, `turn it on on Monday`) go to the model or to review, as does a closed-list double LanguageTool ran over without flagging; a hyphen touching either copy (`to to-do`) makes it no double. A space before `: ; ! ?` is removed at the end of a line; with more text after it (`Press : to enter`) it is listed for review. It never touches `1,000`, `10:30`, URLs or code (fenced, inline or indented). prosefix then says once, in one line, how to get the rest:

```text
prosefix: most fixes need Vale and LanguageTool — brew install andrewbroz/tap/prosefix (or: brew install vale languagetool typos-cli)
```

**Without a model**, one more line says `prosefix: model decisions and comma placement need a model — prosefix --setup`. Set one up, and check what this machine has:

```sh
prosefix --setup      # choose and test a model endpoint
prosefix --doctor     # tools found or missing, the endpoint, the settings file for this directory
```

The endpoint comes from flags, `PROSEFIX_LLM_*` / `ADJUDICATE_LLM_*` environment variables, `~/.config/prosefix/config.toml` and `~/.config/adjudicate/config.toml`, never from a project file. Without a model, all rule-based tiers still run.

**Questions without a model.** Some items need judgement but have a likely answer: a capitalised swap (`Color` to `Colour`), a serial comma, a number the guide may spell out, an abbreviation that may end a sentence, and a comma or full stop at a closing quote. Without a model, prosefix asks about each one whenever it can store your answer, that is, whenever it uses a decision cache. The question is listed for review, and the text is left as written. `--no-cache` reads and stores nothing, so it does not ask: it applies the capitalised swap and lists the others as plain review items. Before 0.3.0, only `--llm cached` asked.

**`--llm none`** contacts no model and reads only your own answers from the cache, never the model's. It applies them and asks the same questions as `--llm cached`, so the two write the same text when the cache holds only your answers. Before 0.3.1 it read no cache at all.

### Which English

When nothing sets the variety (no `--target`, no project `target`, no front matter `prosefix: {target: …}` or `lang:`), prosefix reads the note and keeps the variety it is already written in. It counts the distinct words that only one variety spells that way (`colour`, `programme`, `finalise` against `color`, `program`, `finalize`), outside code and quotations; names, words in your accept lists and words both varieties accept do not count. Two or more British words, and at least twice as many as American ones, mean UK; the same the other way means US. A note with such words but no clear majority keeps its spellings, its outer quote marks and the order of its dates as written (no variety conversion; everything else follows the US default), and a note with no such words at all uses US. The per-file line says which:

```text
prosefix: draft.md: en-GB (detected) · 6 change(s), 4 for review
prosefix: draft.md: no clear variety (1 UK, 1 US words); variety spellings left alone — set --target or lang: · 1 change(s), 0 for review
prosefix: notes.md: no clear variety (0 UK, 0 US words); using en-US — set --target or lang: · 1 change(s), 0 for review
```

Each file is decided on its own, so a mixed vault keeps every note consistent. To choose instead, pass `--target uk` (or `us`), set `target` in the project file, or put `lang: en-GB` in a note's front matter.

### Review items

Anything prosefix will not apply on its own (no model, the model unsure, or no single right fix) is a review item, with its line and reason:

```text
draft.md:5: review — A comma may be missing after the conjunctive/linking adverb ‘However’. [proofix SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA]
```

For one file (or stdin), review items go to stderr after the run. For several files, each file's line counts them and the total line adds `(--changes lists them)`; `--changes` prints every change, with a few words of context either side, and then the file's review items. `--report FILE` writes them all to a Markdown file. `-q` prints none of this.

## Use

```sh
prosefix note.md                          # rewrite one note in place
prosefix vault/ --diff                    # show what would change in a vault
prosefix vault/ --changes                 # rewrite, listing every change and review item
prosefix content/ --check --llm cached    # CI: fail if anything would change
prosefix - < in.md > out.md               # filter
prosefix note.md --target uk --skip commas
```

Directory walks take `*.md` and `*.markdown`, skipping `.git`, `.obsidian`, `node_modules`, other dot-directories and your `exclude` globs. Exit status: 0 success; 1 from `--check` (or `--strict`) when something would change; 2 for usage, configuration, input or write errors, a file that changed on disk during the run (it is left alone), or a failed `--require`. Any per-file error is reported on stderr and gives 2, but the other files are still processed.

Writes are atomic, keep the file's mode, and follow symlinks to their target; read-only files are reported and left alone.

With LanguageTool installed, prosefix starts one local LanguageTool server for the run (on 127.0.0.1 only), shares it between files and stops it when the run ends, including on errors and Ctrl-C. If it cannot start, or stops answering mid-run, prosefix says so in one line and uses the command line for the rest of the run. Ctrl-C stops it, saves the decisions already made, prints `prosefix: interrupted` and exits 130.

## Obsidian

Run prosefix in place on a vault. Wikilinks and their aliases, embeds, `#tags`, `%%comments%%`, `^block-ids`, the `> [!note]` marker of a callout, maths, and Markdown code, links and HTML are never edited; quotes beside them are curled sensibly. Prose that only looks like syntax ("issue #42", "C#", "costs $5 and $10") is edited as prose.

Until the prosefix plugin exists, use the Shell Commands community plugin with:

```text
prosefix {{file_path:absolute}}
```

## Publishing

```yaml
- run: pipx install prosefix
- run: prosefix content/ --check --llm cached
```

Commit `.prosefix/` so CI replays your decisions: `--llm cached` applies decisions already in the cache and never contacts a model. With no settings file, decisions are cached under `$XDG_CACHE_HOME/prosefix/` (default `~/.cache/prosefix/`) instead.

## Settings

prosefix looks upward from each file for `prosefix.toml`, `.prosefix.toml`, or `[tool.prosefix]` in `pyproject.toml`. Every file it finds applies, and the nearest wins key by key (see [Release notes](#release-notes-050-next-release)):

```toml
target = "uk"                    # "us" or "uk"
style = "oxford"                 # style guide
only = ["style", "proof"]        # passes to run
skip = ["commas"]                # tiers to skip
exclude = ["drafts/**", "templates/**"]
accept = ["Obsidian", "Zettelkasten"]
accept_file = ".prosefix/accept.txt"
cache = ".prosefix/cache.json"   # comma decisions go to commas.json beside it
voice = "own"                    # "plain" (default) or "own"; see Voice and conventions
serial_comma = "always"          # a named convention, optional
```

`exclude` globs are relative to the settings file; `**` crosses directories, `*` and `?` do not, and a bare directory name does not exclude its contents (write `templates/**`). `cache` and `accept_file` must stay inside the project directory.

A note can override the project in its front matter:

```yaml
---
lang: en-GB                      # sets the target when prosefix.target is absent
prosefix:
  target: uk
  skip: [commas]
---
```

or opt out with `prosefix: off`. Only `target`, `style`, `only`, `skip`, `voice` and the conventions are allowed there. Flags beat front matter, which beats the project file; when none of them sets a target, the note's own spelling decides (see [Which English](#which-english)).

The tiers are `variety`, `guide`, `ambiguous`, `numbers`, `serial-comma`, `abbreviations`, `punctuation` (style) and `hygiene`, `mechanical`, `basics`, `typos`, `adjudicated`, `capitals`, `confusables`, `compounds`, `commas` (proof).

`capitals` is on by default. It fixes clear cases outright: a lower-case word that starts a sentence ("the meeting ran late. we left" becomes "The meeting ran late. We left"), and a proper noun whose lower-case form is in no dictionary ("paris", "june", "shakespearean", "hong kong"). The model decides ambiguous ones: a name that is also a common word ("john", "bill", "york"), a word after "etc." or "U.S.", and a sentence start it doesn't know ("npm"). It finds lower-case names LanguageTool misses from the context: after a title ("Dr. smith"), written capitalized elsewhere in the note, or joined by "and" to a name. Without a model, these become questions. It never touches code, links, tags, tables or file names. Turn it off with `--skip capitals`, `skip = ["capitals"]` or `skip: [capitals]` in a note's front matter.

`confusables` is on by default. It asks the model about its/it's, their/there/they're, your/you're, whose/who's and then/than wherever the neighbouring words leave doubt: "given that it's grading is all automated" becomes "its grading", and "Their going home" becomes "They're going home". An occurrence the neighbours make clearly right ("it's a", "its own", "there is", "more than") isn't asked about. Without a model it does nothing, since most occurrences are right. Turn it off with `--skip confusables`, `skip = ["confusables"]` or `skip: [confusables]` in a note's front matter.

## Voice and conventions

Some choices are taste, not error: the serial comma, a comma or full stop at a closing quote, numbers spelled out, compound forms (e-mail, email) and optional commas. `voice` says what to do with them.

- `plain` (the default) is for technical and professional prose. prosefix applies its confident picks, and you can undo each one on review.
- `own` is for prose with a voice of its own. Your text stands, and prosefix offers its pick as a question with a suggestion. It never removes a comma you wrote unless you answer yes. Each comma is its own question, so you can take one and refuse another in the same sentence. A comma that is plainly required is still applied.

Set it in a note's front matter, in a settings file, or with a flag:

```yaml
---
prosefix: {voice: own, serial_comma: always}
---
```

A named convention settles one matter for good. It overrides the style guide's rule, is never offered as a question, and the model only judges its exceptions.

| setting | values | flag |
|---|---|---|
| `voice` | `plain` (default), `own` | `--voice` |
| `serial_comma` | `always`, `never` | `--serial-comma` |
| `quote_punctuation` | `inside`, `logical` | `--quote-punctuation` |
| `numbers` | `words`, `numerals` | `--numbers` |

`numbers = "words"` spells out numbers below the guide's threshold (below 10 when the guide has none). `numerals` leaves numerals as written. Nothing turns words into numerals. New in 0.5.0.

## JSON

`prosefix --json` prints one document, `"schema": 1`, for editors and plugins. It holds the endpoint, the capabilities skipped and an entry for each file. Each entry has the file's status, settings, `input_sha256`, a list of changes (`id`, `pass`, `check`, `start`, `end`, `line`, `before`, `after`, `reason`, `decided_by`, `confidence`, `depends_on`), review items and the full output. The settings include `target_source`, one of `flag`, `front matter`, `lang`, `project`, `detected` or `default`, and `guide`, the style guide in use (`style` when set, else the target's default), with `style_source`: `flag`, `front matter`, `project` or `default`. They also include `voice` (`plain` or `own`) with `voice_source` (the same four values), and `conventions` when any is set. They include `detected` word counts when detection ran, and `"variety_conversion": false` when no clear variety meant the variety spellings were left alone. `input_sha256` is the SHA-256 of the input as UTF-8, so a plugin can tell whether the note changed after the run. Change offsets are UTF-16 code units into the whole input file; `line` is 1-based. A change that lists `depends_on` supersedes the changes it names. No change has `before` equal to `after`: a change a later run undid (a comma put in, then taken out) isn't listed. `decided_by` is `rule`, `model`, `cache` or `user`; `user` marks a change made from your answer. A file with `"status": "error"` has only `path`, `status`, `reason` and `settings`.

**Settled output.** prosefix runs again on its own output until a run changes nothing. So `output` is settled: a run on it reports `unchanged`, with the same questions and keys. The review items come from that last run. This happens in every mode, so an in-place write gives the same text as `output`.

**Questions.** A review item with `options` is a question. Only a question has these fields:

- `start` and `end`: UTF-16 offsets into `output`, not into the input, left out when they cannot be placed there;
- `options`: the replacements on offer (keeping the text as written is always possible, and never listed);
- `suggestion`: the model's pick even when it was too unsure to apply it, or null;
- `message`: the question in plain words, such as `Keep 'monday', or change it to 'Monday' or 'today'?`;
- `confidence` and `reason`: the model's reason, or why no model answered (`no model configured` or `model error`);
- `explain`: a sentence or two on the rule, for the person answering, when the question has one;
- `key`: what `--decide` takes.

**One line per file.** `prosefix --json-lines` writes nothing to the notes either. It prints one line per file, `"type": "file"` followed by the file's entry, as each file finishes, in any order. Then it prints one `"type": "summary"` line with `schema`, `prosefix`, `endpoint` and `skipped`. `endpoint` is null when no model is used. Otherwise it has `url`, `model`, `source` and `error`: null, or the run's first model failure, given once rather than in every question. Each line is compact JSON in ASCII. A file that fails, including a path that does not exist, gets a line with `"status": "error"`, and the exit status stays 0. A `--report` file that cannot be written makes it 2.

**Answers.** `prosefix --decide` reads answers as JSON on stdin and stores each in the cache that its note's path uses:

```json
{"decisions":[
  {"path":"/v/essay.md","key":"3b9e0c1d2a4f5e6a7b8c9d0e","choice":"licence",
   "word":"license","options":["licence","license"],"start":118,"end":125},
  {"path":"/v/essay.md","key":"…","choice":"KEEP"},
  {"path":"/v/notes.md","key":"…","choice":{"text":"licensed"}}]}
```

`choice` is one of the options, `"KEEP"` to keep the text as written, or `{"text": …}` for your own words. It prints `{"written": 3, "forgotten": 0, "keys": [...], "caches": [...]}`: `keys` names each decision's key, in order. Malformed input prints `{"error": "…"}`, stores nothing and exits 2. The next run with the cache applies the answers, for example `prosefix --json-lines --llm cached /v/essay.md`.

One answer can change the sentence of another question in the same note, and so that question's key. `word` is the text at the question's `start`:`end` in `output`. With `word` and `options`, prosefix carries such an answer over: an answer the run did not use moves to the one open question in the same note with the same word and options, and the note runs again, up to three times. An answer gets one chance to move: the next run of its note, normally the cached run right after `--decide`. An answer also settles any other question on the same place, from either pass: once you choose `licence` over `license`, or keep `license`, prosefix doesn't ask whether that word should be the other one. It answers such a question `KEEP`. `start` and `end`, the question's UTF-16 offsets in `output`, give the place. Without them, only a question on the word an answer put in, with the same options, is settled. A question's key also covers the sentences beside it, so an answer re-keys the questions next to it. When the model answered one of those earlier in the same run, prosefix stores that answer under the new key instead of asking again.

**Keeping a change undone.** When you undo one change, `{"path": …, "keep": {"check": …, "before": …, "after": …, "start": …, "end": …}}` tells prosefix not to make it again. Copy `check`, `before` and `after` from the change; `start` and `end` are the UTF-16 span of `before` in the note now. Every later run leaves the text at that place as it is, found by its offsets or, if the note changed elsewhere, by the text around it. The keep covers that place in that note only. `{"path": …, "forget": key}` removes an answer or a keep by its key, as `keys` gave it; the note itself is not changed.

Known limits: an answer to a proofix compound question applies to every occurrence of that form. An answer whose edit overlaps a stylefix edit loses to it and is dropped without notice. An answer without `word` and `options` is not carried over. An answer to a comma question, or to a question about moving a comma or full stop inside a closing quote, applies only under `voice: own`. Under `plain`, the comma tier applies the model's edits and stylefix moves the mark inside. A keep applies under either voice. An answer to a comma question doesn't apply under `--llm none`, since the comma tier has no results without its cache. After a note switches to `own`, the comma tier asks the model again about each sentence where a comma question can arise, because `own` has its own comma cache. Under `--llm cached` right after the switch, it offers no comma questions until a run with a model.

**Settings without a run.** `prosefix --settings PATH…` prints each file's target and style guide, and where each came from, without running any pass: `essay.md: uk (detected), oxford (default for uk), voice plain (default)`, with each convention set added. It takes the same paths, excludes and flags as a run, and looks for no model, so it is quick on a folder. With `--json` it prints one document, `{"schema": 1, "prosefix": …, "files": [...]}`; each file has `path`, `status` (`ok`, `skipped` or `error`), `reason` and `settings`.

**Setup.** Three commands print one JSON object each, for a plugin's setup screen:

- `prosefix --doctor --json`: `schema`, the `prosefix` and `python` versions, each tool (`name`, `found`, `version`, `path`, `adds`, `fix`), the endpoint (`found`, `url`, `model`, `source`, `error`), `config_file` and `settings_file` (null when there is none). `fix` is a Homebrew command, with Homebrew's full path when it is found.
- `prosefix --check-endpoint --json [--url URL] [--model NAME]`: tests the endpoint and gives `ok`, `url`, `model`, `models`, `model_info`, `latency_ms` and `error`. `model_info` lists each model's `id` and `created` (Unix seconds, or null) in the server's order. It exits 1 when the test fails.
- `prosefix --setup --url URL [--model NAME] [--thinking-switch] --json`: tests the endpoint and, only if the test passes, writes `~/.config/prosefix/config.toml` with mode 0600. It gives `ok`, `written`, `url`, `model` and `error`, and exits 1 when nothing was written.

`--key-stdin` reads the API key from the first line of stdin; prosefix never prints it. It works only with `--check-endpoint --json` and `--setup --url … --json`, and `--thinking-switch` only with `--setup --url`; prosefix refuses them elsewhere, with exit status 2. Without `--key-stdin`, a `--url` equal to the configured endpoint's uses its key, timeout and thinking switch, so a change of model keeps them. The configured endpoint is the one a plain run uses: from prosefix's config file, adjudicate's (`~/.config/adjudicate/config.toml`) or the `PROSEFIX_LLM_*` and `ADJUDICATE_LLM_*` variables. `--setup` writes a key that came from a variable as `api_key_env` naming it, never as its value. Another URL gets no key. The details are in the man page (`man prosefix`, or `man/prosefix.1` in the source).

## Release notes: 0.5.0

- Voice and the conventions are new; see [Voice and conventions](#voice-and-conventions).
- Settings files now layer. Every settings file from the note's folder up to the topmost applies, and the nearest wins key by key. A value is replaced, not merged, so a folder's `skip` replaces the vault's. Before, the nearest file replaced all the others.
- The topmost file is the project. A nested project's notes now use the topmost file's cache, so a per-subfolder `.prosefix/cache.json` stops being used.
- Only the topmost file may set `cache`. Another file that sets it is a settings error.
- A folder file's `exclude` and `accept_file` are relative to its own folder.
- The layering stops at a boundary, which it includes. The boundary is the nearest repository root at or above the note's folder: a folder holding `.git` (a file or a directory) or `.hg`. When there is none, it is the nearest folder holding `.obsidian`. When there is neither, it is the filesystem root. So a vault inside a repository, such as Quartz's `content/`, takes the repository's settings file, and a settings file above a plain vault doesn't apply.
- Every settings file between the note and the boundary is now read. A malformed file in a parent folder, or an older file there with a key this version doesn't know, is now an error for the notes below it.
- `prosefix --settings` and the JSON `settings` report `voice`, `voice_source` and any conventions set.

## License

MIT
