jsonfix-llm 0.1.0__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.
- jsonfix_llm-0.1.0/LICENSE +21 -0
- jsonfix_llm-0.1.0/PKG-INFO +118 -0
- jsonfix_llm-0.1.0/README.md +88 -0
- jsonfix_llm-0.1.0/pyproject.toml +59 -0
- jsonfix_llm-0.1.0/setup.cfg +4 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/__init__.py +5 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/cli/__init__.py +0 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/cli/main.py +75 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/extract/__init__.py +0 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/extract/extract_code.py +48 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/extract/extract_json.py +89 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/models.py +9 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/__init__.py +0 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/brackets.py +33 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/commas.py +35 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/comments.py +50 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/control_chars.py +29 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/literals.py +45 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/markdown.py +11 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/pipeline.py +77 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/quotes.py +95 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/repair/values.py +18 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/validators/__init__.py +0 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm/validators/validator.py +11 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm.egg-info/PKG-INFO +118 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm.egg-info/SOURCES.txt +41 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm.egg-info/dependency_links.txt +1 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm.egg-info/entry_points.txt +2 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm.egg-info/requires.txt +7 -0
- jsonfix_llm-0.1.0/src/jsonfix_llm.egg-info/top_level.txt +1 -0
- jsonfix_llm-0.1.0/tests/test_cli.py +154 -0
- jsonfix_llm-0.1.0/tests/test_extract_code.py +78 -0
- jsonfix_llm-0.1.0/tests/test_extract_json.py +101 -0
- jsonfix_llm-0.1.0/tests/test_pipeline.py +95 -0
- jsonfix_llm-0.1.0/tests/test_repair_brackets.py +60 -0
- jsonfix_llm-0.1.0/tests/test_repair_commas.py +41 -0
- jsonfix_llm-0.1.0/tests/test_repair_comments.py +72 -0
- jsonfix_llm-0.1.0/tests/test_repair_control_chars.py +39 -0
- jsonfix_llm-0.1.0/tests/test_repair_literals.py +69 -0
- jsonfix_llm-0.1.0/tests/test_repair_markdown.py +30 -0
- jsonfix_llm-0.1.0/tests/test_repair_quotes.py +92 -0
- jsonfix_llm-0.1.0/tests/test_repair_values.py +22 -0
- jsonfix_llm-0.1.0/tests/test_validator.py +34 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Your Name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jsonfix-llm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Repair broken JSON from LLM outputs
|
|
5
|
+
Author-email: shashi kundan <shashikundan0001@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/shashi3070/jsonfix-llm
|
|
8
|
+
Project-URL: Repository, https://github.com/shashi3070/jsonfix-llm
|
|
9
|
+
Project-URL: BugTracker, https://github.com/shashi3070/jsonfix-llm/issues
|
|
10
|
+
Keywords: json,llm,repair,fix,ai
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Topic :: Text Processing :: General
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: pydantic>=2.0
|
|
24
|
+
Requires-Dist: typer>=0.9
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4; extra == "dev"
|
|
28
|
+
Requires-Dist: ruff>=0.1; extra == "dev"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# jsonfix-llm
|
|
32
|
+
|
|
33
|
+
Repair broken JSON from LLM outputs. Stop fighting malformed JSON.
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install jsonfix-llm
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from jsonfix_llm import repair_json
|
|
45
|
+
|
|
46
|
+
fixed = repair_json("""{
|
|
47
|
+
'name': 'shashi'
|
|
48
|
+
'age': 25
|
|
49
|
+
}""")
|
|
50
|
+
# {"name": "shashi", "age": 25}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Features
|
|
54
|
+
|
|
55
|
+
- **Markdown fence stripping** — removes ` ```json ... ``` ` wrappers
|
|
56
|
+
- **Comment stripping** — removes `//` and `/* */` comments
|
|
57
|
+
- **Quote fixing** — single quotes → double quotes, unquoted keys → quoted
|
|
58
|
+
- **Literal fixing** — `True/False/None` → `true/false/null`
|
|
59
|
+
- **Comma fixing** — inserts missing commas, removes trailing commas
|
|
60
|
+
- **Value fixing** — fills missing values, fixes missing colons
|
|
61
|
+
- **Control char escaping** — escapes literal newlines/tabs in strings
|
|
62
|
+
- **Bracket auto-close** — appends missing `}` or `]`, handles truncation
|
|
63
|
+
- **Code extraction** — extracts code from fenced, indented, inline, and XML blocks
|
|
64
|
+
- **CLI tool** — quick repair from the terminal
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from jsonfix_llm import repair_json, extract_code, extract_json
|
|
70
|
+
|
|
71
|
+
# Simple repair
|
|
72
|
+
fixed = repair_json(text)
|
|
73
|
+
|
|
74
|
+
# Rich result
|
|
75
|
+
result = repair_json(text, rich=True)
|
|
76
|
+
print(result.fixed)
|
|
77
|
+
print(result.was_repaired)
|
|
78
|
+
print(result.fixes)
|
|
79
|
+
print(result.error_count)
|
|
80
|
+
print(result.errors)
|
|
81
|
+
|
|
82
|
+
# Extract code (default: first Python block)
|
|
83
|
+
code = extract_code(text)
|
|
84
|
+
# language-specific: extract_code(text, language="python")
|
|
85
|
+
# all blocks: extract_code(text, language="json", all=True)
|
|
86
|
+
|
|
87
|
+
# Extract JSON blocks
|
|
88
|
+
json_block = extract_json(text)
|
|
89
|
+
json_blocks = extract_json(text, all=True)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## CLI
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# Repair JSON file
|
|
96
|
+
jsonfix broken.json
|
|
97
|
+
|
|
98
|
+
# Read from stdin
|
|
99
|
+
echo '{"a":1' | jsonfix
|
|
100
|
+
|
|
101
|
+
# Write to file
|
|
102
|
+
jsonfix in.json -o fixed.json
|
|
103
|
+
|
|
104
|
+
# Show repair stats
|
|
105
|
+
jsonfix in.json --stats
|
|
106
|
+
|
|
107
|
+
# Extract code blocks
|
|
108
|
+
jsonfix extract file.md --language python
|
|
109
|
+
jsonfix extract file.md --language python -o output.py
|
|
110
|
+
jsonfix extract file.md --language json --all
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Development
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
pip install -e ".[dev]"
|
|
117
|
+
pytest tests/ -v
|
|
118
|
+
```
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# jsonfix-llm
|
|
2
|
+
|
|
3
|
+
Repair broken JSON from LLM outputs. Stop fighting malformed JSON.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install jsonfix-llm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from jsonfix_llm import repair_json
|
|
15
|
+
|
|
16
|
+
fixed = repair_json("""{
|
|
17
|
+
'name': 'shashi'
|
|
18
|
+
'age': 25
|
|
19
|
+
}""")
|
|
20
|
+
# {"name": "shashi", "age": 25}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Markdown fence stripping** — removes ` ```json ... ``` ` wrappers
|
|
26
|
+
- **Comment stripping** — removes `//` and `/* */` comments
|
|
27
|
+
- **Quote fixing** — single quotes → double quotes, unquoted keys → quoted
|
|
28
|
+
- **Literal fixing** — `True/False/None` → `true/false/null`
|
|
29
|
+
- **Comma fixing** — inserts missing commas, removes trailing commas
|
|
30
|
+
- **Value fixing** — fills missing values, fixes missing colons
|
|
31
|
+
- **Control char escaping** — escapes literal newlines/tabs in strings
|
|
32
|
+
- **Bracket auto-close** — appends missing `}` or `]`, handles truncation
|
|
33
|
+
- **Code extraction** — extracts code from fenced, indented, inline, and XML blocks
|
|
34
|
+
- **CLI tool** — quick repair from the terminal
|
|
35
|
+
|
|
36
|
+
## API
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from jsonfix_llm import repair_json, extract_code, extract_json
|
|
40
|
+
|
|
41
|
+
# Simple repair
|
|
42
|
+
fixed = repair_json(text)
|
|
43
|
+
|
|
44
|
+
# Rich result
|
|
45
|
+
result = repair_json(text, rich=True)
|
|
46
|
+
print(result.fixed)
|
|
47
|
+
print(result.was_repaired)
|
|
48
|
+
print(result.fixes)
|
|
49
|
+
print(result.error_count)
|
|
50
|
+
print(result.errors)
|
|
51
|
+
|
|
52
|
+
# Extract code (default: first Python block)
|
|
53
|
+
code = extract_code(text)
|
|
54
|
+
# language-specific: extract_code(text, language="python")
|
|
55
|
+
# all blocks: extract_code(text, language="json", all=True)
|
|
56
|
+
|
|
57
|
+
# Extract JSON blocks
|
|
58
|
+
json_block = extract_json(text)
|
|
59
|
+
json_blocks = extract_json(text, all=True)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## CLI
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Repair JSON file
|
|
66
|
+
jsonfix broken.json
|
|
67
|
+
|
|
68
|
+
# Read from stdin
|
|
69
|
+
echo '{"a":1' | jsonfix
|
|
70
|
+
|
|
71
|
+
# Write to file
|
|
72
|
+
jsonfix in.json -o fixed.json
|
|
73
|
+
|
|
74
|
+
# Show repair stats
|
|
75
|
+
jsonfix in.json --stats
|
|
76
|
+
|
|
77
|
+
# Extract code blocks
|
|
78
|
+
jsonfix extract file.md --language python
|
|
79
|
+
jsonfix extract file.md --language python -o output.py
|
|
80
|
+
jsonfix extract file.md --language json --all
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Development
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pip install -e ".[dev]"
|
|
87
|
+
pytest tests/ -v
|
|
88
|
+
```
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "jsonfix-llm"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Repair broken JSON from LLM outputs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "shashi kundan", email = "shashikundan0001@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
24
|
+
"Topic :: Text Processing :: General",
|
|
25
|
+
]
|
|
26
|
+
keywords = ["json", "llm", "repair", "fix", "ai"]
|
|
27
|
+
|
|
28
|
+
dependencies = [
|
|
29
|
+
"pydantic>=2.0",
|
|
30
|
+
"typer>=0.9",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/shashi3070/jsonfix-llm"
|
|
35
|
+
Repository = "https://github.com/shashi3070/jsonfix-llm"
|
|
36
|
+
BugTracker = "https://github.com/shashi3070/jsonfix-llm/issues"
|
|
37
|
+
|
|
38
|
+
[project.scripts]
|
|
39
|
+
jsonfix = "jsonfix_llm.cli.main:app"
|
|
40
|
+
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
dev = [
|
|
43
|
+
"pytest>=7",
|
|
44
|
+
"pytest-cov>=4",
|
|
45
|
+
"ruff>=0.1",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
testpaths = ["tests"]
|
|
50
|
+
|
|
51
|
+
[tool.ruff]
|
|
52
|
+
line-length = 100
|
|
53
|
+
target-version = "py39"
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint]
|
|
56
|
+
select = ["E", "F", "I"]
|
|
57
|
+
|
|
58
|
+
[tool.coverage.run]
|
|
59
|
+
source = ["src/jsonfix_llm"]
|
|
File without changes
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
import typer
|
|
4
|
+
from typer.core import TyperGroup
|
|
5
|
+
|
|
6
|
+
from jsonfix_llm import extract_code, repair_json
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class NaturalOrderGroup(TyperGroup):
|
|
10
|
+
def parse_args(self, ctx, args):
|
|
11
|
+
if args and args[0] in self.commands:
|
|
12
|
+
return super().parse_args(ctx, args)
|
|
13
|
+
return super().parse_args(ctx, ["repair", *args])
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
app = typer.Typer(cls=NaturalOrderGroup)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@app.command()
|
|
20
|
+
def repair(
|
|
21
|
+
file: str = typer.Argument(None, help="JSON file to repair (stdin if omitted)"),
|
|
22
|
+
output: str = typer.Option(None, "-o", "--output", help="Write output to file"),
|
|
23
|
+
stats: bool = typer.Option(False, "--stats", help="Show repair statistics"),
|
|
24
|
+
):
|
|
25
|
+
if file:
|
|
26
|
+
with open(file, encoding="utf-8") as f:
|
|
27
|
+
text = f.read()
|
|
28
|
+
else:
|
|
29
|
+
text = sys.stdin.read()
|
|
30
|
+
|
|
31
|
+
result = repair_json(text, rich=stats)
|
|
32
|
+
if stats:
|
|
33
|
+
typer.echo(result.fixed)
|
|
34
|
+
typer.echo(f"// Repaired: {result.was_repaired}", err=True)
|
|
35
|
+
fixes_summary = ", ".join(result.fixes) if result.fixes else "none"
|
|
36
|
+
typer.echo(f"// Fixes applied: {fixes_summary}", err=True)
|
|
37
|
+
typer.echo(f"// Errors: {result.error_count}", err=True)
|
|
38
|
+
if result.errors:
|
|
39
|
+
for err in result.errors:
|
|
40
|
+
typer.echo(f"// - {err}", err=True)
|
|
41
|
+
else:
|
|
42
|
+
output_text = result if isinstance(result, str) else result.fixed
|
|
43
|
+
if output:
|
|
44
|
+
with open(output, "w", encoding="utf-8") as f:
|
|
45
|
+
f.write(output_text)
|
|
46
|
+
else:
|
|
47
|
+
typer.echo(output_text)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@app.command()
|
|
51
|
+
def extract(
|
|
52
|
+
file: str = typer.Argument(None, help="File to extract from (stdin if omitted)"),
|
|
53
|
+
language: str = typer.Option("python", "--language", "-l",
|
|
54
|
+
help="Programming language to extract"),
|
|
55
|
+
output: str = typer.Option(None, "-o", "--output", help="Write output to file"),
|
|
56
|
+
all_blocks: bool = typer.Option(False, "--all", help="Extract all matching blocks"),
|
|
57
|
+
):
|
|
58
|
+
if file:
|
|
59
|
+
with open(file, encoding="utf-8") as f:
|
|
60
|
+
text = f.read()
|
|
61
|
+
else:
|
|
62
|
+
text = sys.stdin.read()
|
|
63
|
+
|
|
64
|
+
result = extract_code(text, language=language, all=all_blocks)
|
|
65
|
+
output_text = "\n---\n".join(result) if isinstance(result, list) else result
|
|
66
|
+
|
|
67
|
+
if output:
|
|
68
|
+
with open(output, "w", encoding="utf-8") as f:
|
|
69
|
+
f.write(output_text)
|
|
70
|
+
else:
|
|
71
|
+
typer.echo(output_text)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
if __name__ == "__main__": # pragma: no cover
|
|
75
|
+
app()
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _extract_fenced(text: str, language: str | None) -> list[str]:
|
|
5
|
+
if language:
|
|
6
|
+
lang_pattern = re.escape(language)
|
|
7
|
+
pattern = rf"```{lang_pattern}\s*\n(.*?)\n```"
|
|
8
|
+
else:
|
|
9
|
+
pattern = r"```\w*\s*\n(.*?)\n```"
|
|
10
|
+
return re.findall(pattern, text, re.DOTALL)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _extract_xml(text: str, tag: str) -> list[str]:
|
|
14
|
+
pattern = rf"<{tag}>(.*?)</{tag}>"
|
|
15
|
+
return re.findall(pattern, text, re.DOTALL)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _extract_indented(text: str) -> list[str]:
|
|
19
|
+
blocks = []
|
|
20
|
+
current: list[str] = []
|
|
21
|
+
for line in text.split("\n"):
|
|
22
|
+
if line.startswith(" ") or line.startswith("\t"):
|
|
23
|
+
current.append(line.lstrip())
|
|
24
|
+
elif current:
|
|
25
|
+
blocks.append("\n".join(current))
|
|
26
|
+
current = []
|
|
27
|
+
if current:
|
|
28
|
+
blocks.append("\n".join(current))
|
|
29
|
+
return blocks
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _extract_inline(text: str) -> list[str]:
|
|
33
|
+
return re.findall(r"`([^`]+)`", text)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def extract_code(text: str, language: str | None = None, all: bool = False) -> str | list[str]:
|
|
37
|
+
blocks = _extract_fenced(text, language)
|
|
38
|
+
if not blocks:
|
|
39
|
+
blocks = _extract_xml(text, "code")
|
|
40
|
+
if not blocks:
|
|
41
|
+
blocks = _extract_xml(text, "pre")
|
|
42
|
+
if not blocks:
|
|
43
|
+
blocks = _extract_indented(text)
|
|
44
|
+
if not blocks:
|
|
45
|
+
blocks = _extract_inline(text)
|
|
46
|
+
if all:
|
|
47
|
+
return blocks
|
|
48
|
+
return blocks[0] if blocks else text
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
def extract_json(text: str) -> str:
|
|
4
|
+
brace_depth = 0
|
|
5
|
+
bracket_depth = 0
|
|
6
|
+
in_string = False
|
|
7
|
+
escape = False
|
|
8
|
+
json_start = -1
|
|
9
|
+
json_end = -1
|
|
10
|
+
|
|
11
|
+
for i, char in enumerate(text):
|
|
12
|
+
if escape:
|
|
13
|
+
escape = False
|
|
14
|
+
continue
|
|
15
|
+
if char == "\\":
|
|
16
|
+
escape = True
|
|
17
|
+
continue
|
|
18
|
+
if char == '"':
|
|
19
|
+
in_string = not in_string
|
|
20
|
+
continue
|
|
21
|
+
if in_string:
|
|
22
|
+
continue
|
|
23
|
+
|
|
24
|
+
if char == "{":
|
|
25
|
+
if brace_depth == 0 and bracket_depth == 0:
|
|
26
|
+
json_start = i
|
|
27
|
+
brace_depth += 1
|
|
28
|
+
elif char == "}":
|
|
29
|
+
brace_depth -= 1
|
|
30
|
+
if brace_depth == 0 and bracket_depth == 0:
|
|
31
|
+
json_end = i + 1
|
|
32
|
+
break
|
|
33
|
+
elif char == "[":
|
|
34
|
+
if bracket_depth == 0 and brace_depth == 0:
|
|
35
|
+
json_start = i
|
|
36
|
+
bracket_depth += 1
|
|
37
|
+
elif char == "]":
|
|
38
|
+
bracket_depth -= 1
|
|
39
|
+
if bracket_depth == 0 and brace_depth == 0:
|
|
40
|
+
json_end = i + 1
|
|
41
|
+
break
|
|
42
|
+
|
|
43
|
+
if json_start >= 0 and json_end > json_start:
|
|
44
|
+
return text[json_start:json_end]
|
|
45
|
+
|
|
46
|
+
return text
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def extract_json_all(text: str) -> list[str]:
|
|
50
|
+
blocks = []
|
|
51
|
+
in_string = False
|
|
52
|
+
escape = False
|
|
53
|
+
brace_depth = 0
|
|
54
|
+
bracket_depth = 0
|
|
55
|
+
start = -1
|
|
56
|
+
|
|
57
|
+
for i, char in enumerate(text):
|
|
58
|
+
if escape:
|
|
59
|
+
escape = False
|
|
60
|
+
continue
|
|
61
|
+
if char == "\\":
|
|
62
|
+
escape = True
|
|
63
|
+
continue
|
|
64
|
+
if char == '"':
|
|
65
|
+
in_string = not in_string
|
|
66
|
+
continue
|
|
67
|
+
if in_string:
|
|
68
|
+
continue
|
|
69
|
+
|
|
70
|
+
if char == "{":
|
|
71
|
+
if brace_depth == 0 and bracket_depth == 0:
|
|
72
|
+
start = i
|
|
73
|
+
brace_depth += 1
|
|
74
|
+
elif char == "}":
|
|
75
|
+
brace_depth -= 1
|
|
76
|
+
if brace_depth == 0 and bracket_depth == 0 and start >= 0:
|
|
77
|
+
blocks.append(text[start : i + 1])
|
|
78
|
+
start = -1
|
|
79
|
+
elif char == "[":
|
|
80
|
+
if bracket_depth == 0 and brace_depth == 0:
|
|
81
|
+
start = i
|
|
82
|
+
bracket_depth += 1
|
|
83
|
+
elif char == "]":
|
|
84
|
+
bracket_depth -= 1
|
|
85
|
+
if bracket_depth == 0 and brace_depth == 0 and start >= 0:
|
|
86
|
+
blocks.append(text[start : i + 1])
|
|
87
|
+
start = -1
|
|
88
|
+
|
|
89
|
+
return blocks
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
def auto_close_brackets(text: str) -> str:
|
|
2
|
+
in_string = False
|
|
3
|
+
escape = False
|
|
4
|
+
stack = []
|
|
5
|
+
|
|
6
|
+
for char in text:
|
|
7
|
+
if escape:
|
|
8
|
+
escape = False
|
|
9
|
+
continue
|
|
10
|
+
if char == "\\":
|
|
11
|
+
escape = True
|
|
12
|
+
continue
|
|
13
|
+
if char == '"':
|
|
14
|
+
in_string = not in_string
|
|
15
|
+
continue
|
|
16
|
+
if in_string:
|
|
17
|
+
continue
|
|
18
|
+
|
|
19
|
+
if char == "{":
|
|
20
|
+
stack.append("}")
|
|
21
|
+
elif char == "[":
|
|
22
|
+
stack.append("]")
|
|
23
|
+
elif char == "}":
|
|
24
|
+
if stack and stack[-1] == "}":
|
|
25
|
+
stack.pop()
|
|
26
|
+
elif char == "]":
|
|
27
|
+
if stack and stack[-1] == "]":
|
|
28
|
+
stack.pop()
|
|
29
|
+
|
|
30
|
+
while stack:
|
|
31
|
+
text += stack.pop()
|
|
32
|
+
|
|
33
|
+
return text
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def fix_missing_commas(text: str) -> str:
|
|
5
|
+
text = re.sub(
|
|
6
|
+
r'("\s*)\n(\s*)("(?=[^:]*:))',
|
|
7
|
+
r"\1,\n\2\3",
|
|
8
|
+
text,
|
|
9
|
+
)
|
|
10
|
+
text = re.sub(r"(\})\s*\n(\s*)(\{)", r"\1,\n\2\3", text)
|
|
11
|
+
text = re.sub(r'(\])\s*\n(\s*)("(?=[^:]*:))', r"\1,\n\2\3", text)
|
|
12
|
+
text = re.sub(r'("\s*)\n(\s*)(\{)', r"\1,\n\2\3", text)
|
|
13
|
+
text = re.sub(r"(\})\s*\n(\s*)(\[)", r"\1,\n\2\3", text)
|
|
14
|
+
text = re.sub(r"(\])\s*\n(\s*)(\{)", r"\1,\n\2\3", text)
|
|
15
|
+
text = re.sub(r'(\])\s*\n(\s*)(\[)', r"\1,\n\2\3", text)
|
|
16
|
+
text = re.sub(
|
|
17
|
+
r"(\d+|true|false|null)\s*\n(\s*)("
|
|
18
|
+
r'"(?=[^:]*:))',
|
|
19
|
+
r"\1,\n\2\3",
|
|
20
|
+
text,
|
|
21
|
+
flags=re.IGNORECASE,
|
|
22
|
+
)
|
|
23
|
+
return text
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def fix_trailing_commas(text: str) -> str:
|
|
27
|
+
text = re.sub(r",\s*\}", "}", text)
|
|
28
|
+
text = re.sub(r",\s*\]", "]", text)
|
|
29
|
+
return text
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def fix_commas(text: str) -> str:
|
|
33
|
+
text = fix_trailing_commas(text)
|
|
34
|
+
text = fix_missing_commas(text)
|
|
35
|
+
return text
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _is_inside_string(line: str, pos: int) -> bool:
|
|
5
|
+
in_double = False
|
|
6
|
+
in_single = False
|
|
7
|
+
escape = False
|
|
8
|
+
for i in range(pos):
|
|
9
|
+
ch = line[i]
|
|
10
|
+
if escape:
|
|
11
|
+
escape = False
|
|
12
|
+
continue
|
|
13
|
+
if ch == "\\":
|
|
14
|
+
escape = True
|
|
15
|
+
continue
|
|
16
|
+
if ch == '"' and not in_single:
|
|
17
|
+
in_double = not in_double
|
|
18
|
+
elif ch == "'" and not in_double:
|
|
19
|
+
in_single = not in_single
|
|
20
|
+
return in_double or in_single
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def strip_line_comments(text: str) -> str:
|
|
24
|
+
lines = text.split("\n")
|
|
25
|
+
result = []
|
|
26
|
+
for line in lines:
|
|
27
|
+
comment_pos = -1
|
|
28
|
+
i = 0
|
|
29
|
+
while i < len(line):
|
|
30
|
+
if line[i] == "/" and i + 1 < len(line) and line[i + 1] == "/":
|
|
31
|
+
if not _is_inside_string(line, i):
|
|
32
|
+
comment_pos = i
|
|
33
|
+
break
|
|
34
|
+
i += 2
|
|
35
|
+
else:
|
|
36
|
+
i += 1
|
|
37
|
+
if comment_pos >= 0:
|
|
38
|
+
line = line[:comment_pos].rstrip()
|
|
39
|
+
result.append(line)
|
|
40
|
+
return "\n".join(result)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def strip_block_comments(text: str) -> str:
|
|
44
|
+
return re.sub(r"/\*[\s\S]*?\*/", "", text)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def strip_comments(text: str) -> str:
|
|
48
|
+
text = strip_block_comments(text)
|
|
49
|
+
text = strip_line_comments(text)
|
|
50
|
+
return text
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
def fix_control_chars(text: str) -> str:
|
|
2
|
+
result = []
|
|
3
|
+
in_string = False
|
|
4
|
+
escape = False
|
|
5
|
+
for char in text:
|
|
6
|
+
if escape:
|
|
7
|
+
result.append(char)
|
|
8
|
+
escape = False
|
|
9
|
+
continue
|
|
10
|
+
if char == "\\":
|
|
11
|
+
result.append(char)
|
|
12
|
+
escape = True
|
|
13
|
+
continue
|
|
14
|
+
if char == '"':
|
|
15
|
+
in_string = not in_string
|
|
16
|
+
result.append(char)
|
|
17
|
+
continue
|
|
18
|
+
if in_string:
|
|
19
|
+
if char == "\n":
|
|
20
|
+
result.append("\\n")
|
|
21
|
+
elif char == "\t":
|
|
22
|
+
result.append("\\t")
|
|
23
|
+
elif char == "\r":
|
|
24
|
+
result.append("\\r")
|
|
25
|
+
else:
|
|
26
|
+
result.append(char)
|
|
27
|
+
else:
|
|
28
|
+
result.append(char)
|
|
29
|
+
return "".join(result)
|