mathfence 0.1.3__tar.gz → 0.1.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. {mathfence-0.1.3 → mathfence-0.1.4}/CHANGELOG.md +7 -0
  2. {mathfence-0.1.3 → mathfence-0.1.4}/PKG-INFO +9 -9
  3. {mathfence-0.1.3 → mathfence-0.1.4}/README.ko.md +5 -5
  4. {mathfence-0.1.3 → mathfence-0.1.4}/README.md +8 -8
  5. mathfence-0.1.4/docs/images/05-bench.png +0 -0
  6. {mathfence-0.1.3 → mathfence-0.1.4}/pyproject.toml +1 -1
  7. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/__init__.py +1 -1
  8. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/delimiters.py +16 -1
  9. {mathfence-0.1.3 → mathfence-0.1.4}/tests/corpus/cases.json +18 -0
  10. {mathfence-0.1.3 → mathfence-0.1.4}/uv.lock +1 -1
  11. mathfence-0.1.3/docs/images/05-bench.png +0 -0
  12. {mathfence-0.1.3 → mathfence-0.1.4}/.github/ISSUE_TEMPLATE/bug.yml +0 -0
  13. {mathfence-0.1.3 → mathfence-0.1.4}/.github/ISSUE_TEMPLATE/question.yml +0 -0
  14. {mathfence-0.1.3 → mathfence-0.1.4}/.github/workflows/ci.yml +0 -0
  15. {mathfence-0.1.3 → mathfence-0.1.4}/.gitignore +0 -0
  16. {mathfence-0.1.3 → mathfence-0.1.4}/LICENSE +0 -0
  17. {mathfence-0.1.3 → mathfence-0.1.4}/SECURITY.md +0 -0
  18. {mathfence-0.1.3 → mathfence-0.1.4}/bench/baselines.py +0 -0
  19. {mathfence-0.1.3 → mathfence-0.1.4}/bench/compare.py +0 -0
  20. {mathfence-0.1.3 → mathfence-0.1.4}/docs/demo/before-after.en.html +0 -0
  21. {mathfence-0.1.3 → mathfence-0.1.4}/docs/demo/before-after.ko.html +0 -0
  22. {mathfence-0.1.3 → mathfence-0.1.4}/docs/demo/streaming.en.html +0 -0
  23. {mathfence-0.1.3 → mathfence-0.1.4}/docs/demo/streaming.ko.html +0 -0
  24. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/01-raw-answer.png +0 -0
  25. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/02-normalized.png +0 -0
  26. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/03-explain.ko.png +0 -0
  27. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/03-explain.png +0 -0
  28. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/04-stream.png +0 -0
  29. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/before-after.ko.png +0 -0
  30. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/before-after.png +0 -0
  31. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/streaming.ko.png +0 -0
  32. {mathfence-0.1.3 → mathfence-0.1.4}/docs/images/streaming.png +0 -0
  33. {mathfence-0.1.3 → mathfence-0.1.4}/examples/answer.md +0 -0
  34. {mathfence-0.1.3 → mathfence-0.1.4}/examples/render_compare.py +0 -0
  35. {mathfence-0.1.3 → mathfence-0.1.4}/examples/stream_demo.py +0 -0
  36. {mathfence-0.1.3 → mathfence-0.1.4}/examples/streamlit_chat.py +0 -0
  37. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/cli.py +0 -0
  38. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/core.py +0 -0
  39. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/dollars.py +0 -0
  40. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/i18n.py +0 -0
  41. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/py.typed +0 -0
  42. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/regions.py +0 -0
  43. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/rules.py +0 -0
  44. {mathfence-0.1.3 → mathfence-0.1.4}/src/mathfence/stream.py +0 -0
  45. {mathfence-0.1.3 → mathfence-0.1.4}/tests/corpus/streams.json +0 -0
  46. {mathfence-0.1.3 → mathfence-0.1.4}/tests/fuzzgen.py +0 -0
  47. {mathfence-0.1.3 → mathfence-0.1.4}/tests/judge.py +0 -0
  48. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_bench.py +0 -0
  49. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_cli.py +0 -0
  50. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_corpus.py +0 -0
  51. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_delimiters.py +0 -0
  52. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_dollars.py +0 -0
  53. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_i18n.py +0 -0
  54. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_invariants.py +0 -0
  55. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_package.py +0 -0
  56. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_profiles.py +0 -0
  57. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_regions.py +0 -0
  58. {mathfence-0.1.3 → mathfence-0.1.4}/tests/test_stream.py +0 -0
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/).
5
5
 
6
+ ## [Unreleased]
7
+
8
+ ## [0.1.4] - 2026-12-06
9
+
10
+ ### Fixed
11
+ - `\(a\)\(b\)`, `\(x\)\[y\]` and `\(a\)$5` no longer lose all their math: a space is kept after a converted inline formula that is directly followed by `$` or another bracket pair.
12
+
6
13
  ## [0.1.3]
7
14
 
8
15
  ### Changed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: mathfence
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: Normalize LLM math delimiters and escape currency dollar signs before Markdown rendering, without touching code or URLs.
5
5
  Project-URL: Homepage, https://github.com/LeeGyeongPil/mathfence
6
6
  Project-URL: Issues, https://github.com/LeeGyeongPil/mathfence/issues
@@ -24,7 +24,7 @@ Description-Content-Type: text/markdown
24
24
 
25
25
  # mathfence
26
26
 
27
- **English** | [한국어](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.3/README.ko.md)
27
+ **English** | [한국어](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.4/README.ko.md)
28
28
 
29
29
  Normalize LLM Markdown before it reaches a `$`-math renderer. One call, `mathfence.normalize(text)`, rewrites `\( \)` and `\[ \]` to `$ $` and `$$ $$`, escapes price dollars (`$5`) as `\$`, and leaves code, URLs and HTML untouched. A streaming helper keeps half-finished answers from flashing fake formulas. Pure Python standard library, zero runtime dependencies, plus a CLI that explains every decision.
30
30
 
@@ -227,7 +227,7 @@ Pay \$5 or \$6
227
227
 
228
228
  ## Does it do better than the usual shortcuts?
229
229
 
230
- `bench/compare.py` scores four approaches on the same 94-case corpus with one lenient judge (markdown-it-py with its dollarmath plugin):
230
+ `bench/compare.py` scores four approaches on the same 97-case corpus with one lenient judge (markdown-it-py with its dollarmath plugin):
231
231
 
232
232
  ```bash
233
233
  uv run python bench/compare.py
@@ -239,10 +239,10 @@ uv run python bench/compare.py
239
239
 
240
240
  ```text
241
241
  approach pass score broken flicker
242
- no processing 40/94 0.43 0 3
243
- escape every $ 33/94 0.35 13 0
244
- regex recipe 68/94 0.72 15 2
245
- mathfence 94/94 1.00 0 0
242
+ no processing 40/97 0.41 0 3
243
+ escape every $ 33/97 0.34 13 0
244
+ regex recipe 71/97 0.73 15 2
245
+ mathfence 97/97 1.00 0 0
246
246
  ```
247
247
 
248
248
  Read this with care: the corpus was written by the author of the rules, so the score is optimistic. `regex recipe` is a composite of common snippets, not a copy of any project.
@@ -258,7 +258,7 @@ Read this with care: the corpus was written by the author of the rules, so the s
258
258
  - It is a heuristic. An ambiguous sentence such as `$5 on the x$` is escaped (shown as text) rather than rendered as math.
259
259
  - A blank line inside `$$ ... $$` or `\[ ... \]` ends the paragraph, so that pair is not matched.
260
260
  - `StreamNormalizer` settles text at blank lines outside code, HTML and lists, and at top-level list item boundaries when the settled part has no `$` or backslash; other long runs without paragraph breaks are reprocessed on every `feed()`.
261
- - Link destinations in `[text](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.3/url)` and HTML blocks nested in lists or quotes are not protected regions.
261
+ - Link destinations in `[text](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.4/url)` and HTML blocks nested in lists or quotes are not protected regions.
262
262
  - Tested on Python 3.11, 3.13 and 3.15 locally; CI covers 3.10 to 3.13. The Gradio profile and a Node cross-check are planned for 0.2.
263
263
 
264
264
  ## Troubleshooting
@@ -274,7 +274,7 @@ Read this with care: the corpus was written by the author of the rules, so the s
274
274
 
275
275
  ## Versions
276
276
 
277
- Semantic Versioning. While the major version is 0, a minor release may change the API; changes are listed in [CHANGELOG.md](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.3/CHANGELOG.md).
277
+ Semantic Versioning. While the major version is 0, a minor release may change the API; changes are listed in [CHANGELOG.md](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.4/CHANGELOG.md).
278
278
 
279
279
  ## Development
280
280
 
@@ -203,7 +203,7 @@ Pay \$5 or \$6
203
203
 
204
204
  ## 흔한 지름길보다 나은가?
205
205
 
206
- `bench/compare.py` 는 같은 94건 말뭉치를 하나의 관대한 판정기(markdown-it-py 와 dollarmath 플러그인)로 네 가지 방식에 채점합니다.
206
+ `bench/compare.py` 는 같은 97건 말뭉치를 하나의 관대한 판정기(markdown-it-py 와 dollarmath 플러그인)로 네 가지 방식에 채점합니다.
207
207
 
208
208
  ```bash
209
209
  uv run python bench/compare.py
@@ -215,10 +215,10 @@ uv run python bench/compare.py
215
215
 
216
216
  ```text
217
217
  approach pass score broken flicker
218
- no processing 40/94 0.43 0 3
219
- escape every $ 33/94 0.35 13 0
220
- regex recipe 68/94 0.72 15 2
221
- mathfence 94/94 1.00 0 0
218
+ no processing 40/97 0.41 0 3
219
+ escape every $ 33/97 0.34 13 0
220
+ regex recipe 71/97 0.73 15 2
221
+ mathfence 97/97 1.00 0 0
222
222
  ```
223
223
 
224
224
  주의해서 읽으세요. 말뭉치는 규칙을 만든 사람이 썼기 때문에 점수는 낙관적입니다. `regex recipe` 는 흔한 스니펫을 합친 것이며 특정 프로젝트의 복제가 아닙니다.
@@ -1,6 +1,6 @@
1
1
  # mathfence
2
2
 
3
- **English** | [한국어](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.3/README.ko.md)
3
+ **English** | [한국어](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.4/README.ko.md)
4
4
 
5
5
  Normalize LLM Markdown before it reaches a `$`-math renderer. One call, `mathfence.normalize(text)`, rewrites `\( \)` and `\[ \]` to `$ $` and `$$ $$`, escapes price dollars (`$5`) as `\$`, and leaves code, URLs and HTML untouched. A streaming helper keeps half-finished answers from flashing fake formulas. Pure Python standard library, zero runtime dependencies, plus a CLI that explains every decision.
6
6
 
@@ -203,7 +203,7 @@ Pay \$5 or \$6
203
203
 
204
204
  ## Does it do better than the usual shortcuts?
205
205
 
206
- `bench/compare.py` scores four approaches on the same 94-case corpus with one lenient judge (markdown-it-py with its dollarmath plugin):
206
+ `bench/compare.py` scores four approaches on the same 97-case corpus with one lenient judge (markdown-it-py with its dollarmath plugin):
207
207
 
208
208
  ```bash
209
209
  uv run python bench/compare.py
@@ -215,10 +215,10 @@ uv run python bench/compare.py
215
215
 
216
216
  ```text
217
217
  approach pass score broken flicker
218
- no processing 40/94 0.43 0 3
219
- escape every $ 33/94 0.35 13 0
220
- regex recipe 68/94 0.72 15 2
221
- mathfence 94/94 1.00 0 0
218
+ no processing 40/97 0.41 0 3
219
+ escape every $ 33/97 0.34 13 0
220
+ regex recipe 71/97 0.73 15 2
221
+ mathfence 97/97 1.00 0 0
222
222
  ```
223
223
 
224
224
  Read this with care: the corpus was written by the author of the rules, so the score is optimistic. `regex recipe` is a composite of common snippets, not a copy of any project.
@@ -234,7 +234,7 @@ Read this with care: the corpus was written by the author of the rules, so the s
234
234
  - It is a heuristic. An ambiguous sentence such as `$5 on the x$` is escaped (shown as text) rather than rendered as math.
235
235
  - A blank line inside `$$ ... $$` or `\[ ... \]` ends the paragraph, so that pair is not matched.
236
236
  - `StreamNormalizer` settles text at blank lines outside code, HTML and lists, and at top-level list item boundaries when the settled part has no `$` or backslash; other long runs without paragraph breaks are reprocessed on every `feed()`.
237
- - Link destinations in `[text](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.3/url)` and HTML blocks nested in lists or quotes are not protected regions.
237
+ - Link destinations in `[text](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.4/url)` and HTML blocks nested in lists or quotes are not protected regions.
238
238
  - Tested on Python 3.11, 3.13 and 3.15 locally; CI covers 3.10 to 3.13. The Gradio profile and a Node cross-check are planned for 0.2.
239
239
 
240
240
  ## Troubleshooting
@@ -250,7 +250,7 @@ Read this with care: the corpus was written by the author of the rules, so the s
250
250
 
251
251
  ## Versions
252
252
 
253
- Semantic Versioning. While the major version is 0, a minor release may change the API; changes are listed in [CHANGELOG.md](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.3/CHANGELOG.md).
253
+ Semantic Versioning. While the major version is 0, a minor release may change the API; changes are listed in [CHANGELOG.md](https://github.com/LeeGyeongPil/mathfence/blob/v0.1.4/CHANGELOG.md).
254
254
 
255
255
  ## Development
256
256
 
Binary file
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "mathfence"
7
- version = "0.1.3"
7
+ version = "0.1.4"
8
8
  description = "Normalize LLM math delimiters and escape currency dollar signs before Markdown rendering, without touching code or URLs."
9
9
  readme = "README.md"
10
10
  license = { text = "MIT" }
@@ -8,7 +8,7 @@
8
8
  from .core import PROFILES, Decision, Result, analyze, normalize
9
9
  from .stream import StreamNormalizer, StreamStats
10
10
 
11
- __version__ = "0.1.3"
11
+ __version__ = "0.1.4"
12
12
 
13
13
  __all__ = [
14
14
  "PROFILES",
@@ -98,7 +98,13 @@ def convert(text: str, regions: tuple[Region, ...], *, streaming: bool = False)
98
98
  raw_body = text[m.end() : k]
99
99
  source = text[m.start() : end]
100
100
  after = text[end] if end < len(text) else ""
101
- replacement, kind, rule = _rewrite(raw_body, after, display)
101
+ # An inline result directly followed by "$" (or by another bracket
102
+ # pair that becomes "$") would glue two dollars together; a space
103
+ # keeps the boundary, and the dollar judge sees that space too.
104
+ pad = not display and _touches_dollar(text, end)
105
+ replacement, kind, rule = _rewrite(raw_body, " " if pad else after, display)
106
+ if replacement is not None and pad:
107
+ replacement += " "
102
108
  pos = end
103
109
  if replacement is None:
104
110
  if raw_body.strip():
@@ -117,6 +123,15 @@ def convert(text: str, regions: tuple[Region, ...], *, streaming: bool = False)
117
123
  return Converted("".join(out), edits, raws, display_starts)
118
124
 
119
125
 
126
+ def _touches_dollar(text: str, end: int) -> bool:
127
+ """True if the text at ``end`` is a ``$`` or an unescaped ``\\(`` / ``\\[``."""
128
+ if end >= len(text):
129
+ return False
130
+ if text[end] == "$":
131
+ return True
132
+ return text[end] == "\\" and text[end + 1 : end + 2] in ("(", "[")
133
+
134
+
120
135
  def _rewrite(raw_body: str, after: str, display: bool) -> tuple[str | None, str, str]:
121
136
  """Return ``(replacement, kind, rule)``; ``replacement`` is ``None`` to leave the brackets."""
122
137
  body = raw_body.strip()
@@ -702,6 +702,24 @@
702
702
  "src": "Funding of $2.5 billion and $300 million.",
703
703
  "expect": [],
704
704
  "verbatim": []
705
+ },
706
+ {
707
+ "id": "adj-paren-paren",
708
+ "src": "Vectors \\(a\\)\\(b\\) here.",
709
+ "expect": ["a", "b"],
710
+ "verbatim": []
711
+ },
712
+ {
713
+ "id": "adj-paren-bracket",
714
+ "src": "see \\(x\\)\\[y\\]",
715
+ "expect": ["x", "y"],
716
+ "verbatim": []
717
+ },
718
+ {
719
+ "id": "adj-paren-price",
720
+ "src": "cost \\(a\\)$5 total",
721
+ "expect": ["a"],
722
+ "verbatim": []
705
723
  }
706
724
  ]
707
725
  }
@@ -46,7 +46,7 @@ wheels = [
46
46
 
47
47
  [[package]]
48
48
  name = "mathfence"
49
- version = "0.1.3"
49
+ version = "0.1.4"
50
50
  source = { editable = "." }
51
51
 
52
52
  [package.dev-dependencies]
Binary file
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes