|
1 | 1 | # scripts/generate_errors_index.py |
| 2 | +import json |
| 3 | +import re |
| 4 | +from importlib.resources import files |
2 | 5 | from pathlib import Path |
3 | 6 |
|
4 | 7 | from search_query.constants import QueryErrorCode |
@@ -75,3 +78,94 @@ def generate_rst_file(error: QueryErrorCode) -> None: |
75 | 78 | # f.write(f"- {platform.value}\n") |
76 | 79 | # # f.write(f" parser/{platform}.rst\n") |
77 | 80 | # f.write("\n") |
| 81 | + |
| 82 | + |
| 83 | +query_dir = files("search_query") / "json_db" |
| 84 | +output_dir = Path("docs/source/query_database") |
| 85 | +rst_dir = output_dir / "queries" |
| 86 | +rst_dir.mkdir(parents=True, exist_ok=True) |
| 87 | + |
| 88 | +overview = [] |
| 89 | + |
| 90 | + |
| 91 | +def slugify(name: str) -> str: |
| 92 | + return re.sub(r"[^\w\-]+", "-", name.lower()).strip("-") |
| 93 | + |
| 94 | + |
| 95 | +for file in query_dir.iterdir(): |
| 96 | + if file.suffix == ".json": |
| 97 | + with open(file, encoding="utf-8") as f: |
| 98 | + content = json.load(f) |
| 99 | + |
| 100 | + identifier = file.stem |
| 101 | + platform = content.get("platform", "") |
| 102 | + search_string = content.get("search_string", "") |
| 103 | + title = content.get("title", identifier) |
| 104 | + description = content.get("description", "") |
| 105 | + keywords = content.get("keywords", "") |
| 106 | + filename = file.name |
| 107 | + authors = " and ".join(x["name"] for x in content.get("authors", [])) |
| 108 | + license = content.get("license", "") |
| 109 | + |
| 110 | + slug = slugify(identifier) |
| 111 | + rst_filename = rst_dir / f"{slug}.rst" |
| 112 | + |
| 113 | + # Write individual .rst file |
| 114 | + with open(rst_filename, "w", encoding="utf-8") as rst: |
| 115 | + rst.write( |
| 116 | + f"""{title} |
| 117 | +{'=' * len(title)} |
| 118 | +
|
| 119 | +**Identifier**: ``{identifier}`` |
| 120 | +
|
| 121 | +**Platform**: ``{platform}`` |
| 122 | +
|
| 123 | +**Keywords**: ``{keywords}`` |
| 124 | +
|
| 125 | +**Authors**: ``{authors}`` |
| 126 | +
|
| 127 | +**License**: ``{license}`` |
| 128 | +
|
| 129 | +**License**: {description} |
| 130 | +
|
| 131 | +Load |
| 132 | +----------- |
| 133 | +
|
| 134 | +.. code-block:: python |
| 135 | +
|
| 136 | + from search_query.database import load_query |
| 137 | +
|
| 138 | + query = load_query("{filename}") |
| 139 | +
|
| 140 | + print(query.to_string()) |
| 141 | + # {search_string[:80] + "..."} |
| 142 | +
|
| 143 | +
|
| 144 | +Search String |
| 145 | +------------- |
| 146 | +
|
| 147 | +.. raw:: html |
| 148 | +
|
| 149 | + <pre style="white-space: pre-wrap; word-break: break-word;"> |
| 150 | + {search_string} |
| 151 | + </pre> |
| 152 | +
|
| 153 | +
|
| 154 | +Suggest to `improve this query <https://github.com/CoLRev-Environment/search-query/blob/main/search_query/json_db/{filename}>`_. |
| 155 | +""" |
| 156 | + ) |
| 157 | + |
| 158 | + overview.append( |
| 159 | + { |
| 160 | + "identifier": f"`{identifier} <queries/{slug}.html>`_", |
| 161 | + "platform": platform, |
| 162 | + "search_string_snippet": search_string[:80] + "...", |
| 163 | + "title": title, |
| 164 | + "description": description, |
| 165 | + "keywords": keywords, |
| 166 | + } |
| 167 | + ) |
| 168 | + |
| 169 | +# Write overview.json |
| 170 | +with open(output_dir / "query_overview.json", "w", encoding="utf-8") as f: |
| 171 | + json.dump(overview, f, indent=2) |
0 commit comments