sg-text-forge 0.3.2__tar.gz → 0.3.3__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.
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/PKG-INFO +1 -1
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/pyproject.toml +1 -1
- sg_text_forge-0.3.3/text_forge/migrate_curation.py +334 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/wtd_books.py +123 -39
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/wtd_books_build.py +6 -4
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/uv.lock +1 -1
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/copilot-instructions.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/skills/check-i18n/SKILL.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/skills/check-i18n/references/example_translations.json +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/skills/check-i18n/references/test_cases.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/skills/check-i18n/scripts/check_i18n.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/workflows/publish-pypi.yml +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.gitignore +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/AGENTS.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/LICENSE +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/Makefile +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/README.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/action.yml +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/docs/hindsight-memory/SKILL.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/docs/hindsight-memory.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/epub/book_meta.yml +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/epub/epub.css +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/hooks/nobr_emoticons.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/assets/js/editor.js +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/assets/js/translations.json +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/assets/stylesheets/editor.css +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/assets/stylesheets/text-forge.css +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/main.html +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/partials/comments.html +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/partials/downloads.html +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/partials/editor.html +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/partials/header.html +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/partials/source-file.html +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/hindsight-ingest.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/hindsight-wipe-documents-by-tag.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/hindsight-wtd-ingest-wrapper.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/hindsight_common.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/mkdocs-combine.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/mkdocs_common.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/process-epub-meta.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/pymdown-pandoc.lua +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/wtd-inventory.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/scripts/wtd_sections_common.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/__init__.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/anchor_ids.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/build.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/cli.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/livelib.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/__init__.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/hotkeys.json +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/plugins/text-forge/main.js +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/plugins/text-forge/manifest.json +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/scripts/insert_block.js +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/scripts/insert_image.js +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/scripts/insert_link.js +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/templater.json +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/templates/insert_block.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/templates/insert_image.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/templates/insert_link.md +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/plugin.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/wtd_books_p3.py +0 -0
- {sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/wtd_links.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: sg-text-forge
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: MkDocs plugin and build pipeline for Markdown text projects with EPUB generation
|
|
5
5
|
Project-URL: Homepage, https://github.com/shared-goals/text-forge
|
|
6
6
|
Project-URL: Repository, https://github.com/shared-goals/text-forge
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"""Migrate the old JSONC curation file to the markdown sections file.
|
|
2
|
+
|
|
3
|
+
python -m text_forge.migrate_curation <in.jsonc> <out.md>
|
|
4
|
+
|
|
5
|
+
One-off, but repeatable: the same input always yields the same bytes, so a
|
|
6
|
+
re-run diffs empty and the migration stays auditable after the fact.
|
|
7
|
+
|
|
8
|
+
What it does:
|
|
9
|
+
|
|
10
|
+
* reads the old file with its own JSONC scanner — the loader's comment stripper
|
|
11
|
+
was deleted when the format changed, and this module is the only place that
|
|
12
|
+
syntax still has a right to exist;
|
|
13
|
+
* multiplies every `weight` by 10, which is why the migration exists at all:
|
|
14
|
+
1..35 leaves no room to insert a book between two neighbours, 10..350 does;
|
|
15
|
+
* carries each entry's title across: `// title` above a key, or `// title`
|
|
16
|
+
trailing a one-line entry, becomes the heading's eye text;
|
|
17
|
+
* orders sections by weight ascending, then the weightless entries by id, so the
|
|
18
|
+
file reads as a ranked list with an unranked tail;
|
|
19
|
+
* drops `my_mark` (it reaches the frontmatter from LiveLib shelf data, never
|
|
20
|
+
from curation) and any section other than `books`.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import argparse
|
|
24
|
+
import json
|
|
25
|
+
import re
|
|
26
|
+
import sys
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
|
|
29
|
+
#: old jsonc key -> new bullet key, in the order bullets are emitted
|
|
30
|
+
CURATION_MERGE_ORDER = ("weight", "author", "status", "merges")
|
|
31
|
+
|
|
32
|
+
#: the weight factor: the whole point of migrating, so it is a named constant
|
|
33
|
+
#: rather than a `* 10` someone has to look up
|
|
34
|
+
WEIGHT_FACTOR = 10
|
|
35
|
+
|
|
36
|
+
#: the header every migrated file carries: the format's only documentation,
|
|
37
|
+
#: written for a reader who does not program. It carries no bullets on purpose:
|
|
38
|
+
#: the loader reads a bullet only inside a `## <id>` section, so a bullet here
|
|
39
|
+
#: would be a hard error in the very file this module writes.
|
|
40
|
+
DOC_BLOCK = """# Curation
|
|
41
|
+
|
|
42
|
+
Одна секция — одна редакция книги.
|
|
43
|
+
|
|
44
|
+
Заголовок секции — это id издания с LiveLib: `## <id> — <название>`.
|
|
45
|
+
id слева от тире читает сборка; название справа — только для глаз, сборка его
|
|
46
|
+
не трогает. Строки ниже — поля секции.
|
|
47
|
+
|
|
48
|
+
Что можно писать под заголовком секции (любой набор, в любом порядке).
|
|
49
|
+
weight — вес книги, целое число. Вес умножен на 10 по сравнению со старым
|
|
50
|
+
файлом: между соседними книгами остаются дырки, чтобы новую книгу можно было
|
|
51
|
+
вставить между ними, не перекладывая половину списка (15, 25, 110).
|
|
52
|
+
merges — id других изданий той же работы, через запятую. Их цитаты и отзыв
|
|
53
|
+
достаются этой заметке. author — автор, если LiveLib его не указал или указал
|
|
54
|
+
неверно. status — read / reading / stop, если полка LiveLib врёт.
|
|
55
|
+
|
|
56
|
+
Поля `my_mark` здесь нет: оценку сборка берёт с полки LiveLib, ей и верить.
|
|
57
|
+
Пустая секция (без полей) тоже законна: id уже говорит «эта книга в списке».
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def scan_jsonc_lines(text: str) -> list:
|
|
62
|
+
"""Per line of the file: (json data, raw with trailing comment, whole-line
|
|
63
|
+
comment text).
|
|
64
|
+
|
|
65
|
+
`data` is the line with every comment removed, so the file's line numbers
|
|
66
|
+
survive and a json error still points at the real line. `raw` puts back a
|
|
67
|
+
`//` comment that trailed the data: that is the one comment a key can name
|
|
68
|
+
its own entry with (`"1": {"weight": 1} // Заголовок`). `whole` is the text
|
|
69
|
+
of a comment occupying the line on its own, which names the entry whose key
|
|
70
|
+
sits BELOW it.
|
|
71
|
+
|
|
72
|
+
A `//` inside a string is data, so this walks characters instead of regexing
|
|
73
|
+
the text, and `\\"` does not toggle string state: an escaped quote cannot leave
|
|
74
|
+
a following `//` looking like data. A `/* */` span names no entry at all —
|
|
75
|
+
it is prose — but the lines it covered keep their place in the file. (This
|
|
76
|
+
is the scanner the old loader stripped with; it moved here when the loader
|
|
77
|
+
stopped reading JSONC.)
|
|
78
|
+
"""
|
|
79
|
+
rows = []
|
|
80
|
+
data_buf, comment_buf = [], []
|
|
81
|
+
# what the current line carries: "" (no comment), "//", or "*"
|
|
82
|
+
marker = [""]
|
|
83
|
+
i, n = 0, len(text)
|
|
84
|
+
in_string = False
|
|
85
|
+
|
|
86
|
+
def flush():
|
|
87
|
+
"""Hand over the line built so far and start the next one empty."""
|
|
88
|
+
data = "".join(data_buf)
|
|
89
|
+
comment = "".join(comment_buf)
|
|
90
|
+
trails_data = marker[0] == "//" and bool(data.strip())
|
|
91
|
+
whole = comment[2:].strip() if (marker[0] == "//" and not data.strip()) else ""
|
|
92
|
+
rows.append((data, data + (comment if trails_data else ""), whole))
|
|
93
|
+
data_buf.clear()
|
|
94
|
+
comment_buf.clear()
|
|
95
|
+
marker[0] = ""
|
|
96
|
+
|
|
97
|
+
while i < n:
|
|
98
|
+
ch = text[i]
|
|
99
|
+
if ch == "\n":
|
|
100
|
+
flush()
|
|
101
|
+
i += 1
|
|
102
|
+
continue
|
|
103
|
+
if in_string:
|
|
104
|
+
if ch == "\\":
|
|
105
|
+
data_buf.append(text[i:i + 2])
|
|
106
|
+
i += 2
|
|
107
|
+
continue
|
|
108
|
+
data_buf.append(ch)
|
|
109
|
+
if ch == '"':
|
|
110
|
+
in_string = False
|
|
111
|
+
i += 1
|
|
112
|
+
continue
|
|
113
|
+
if ch == '"':
|
|
114
|
+
in_string = True
|
|
115
|
+
data_buf.append(ch)
|
|
116
|
+
i += 1
|
|
117
|
+
continue
|
|
118
|
+
if text[i:i + 2] == "//" and not marker[0]:
|
|
119
|
+
marker[0] = "//"
|
|
120
|
+
while i < n and text[i] != "\n":
|
|
121
|
+
comment_buf.append(text[i])
|
|
122
|
+
i += 1
|
|
123
|
+
continue
|
|
124
|
+
if text[i:i + 2] == "/*" and not marker[0]:
|
|
125
|
+
# a block comment names no entry: swallow it whole, but let the lines
|
|
126
|
+
# it covered keep their place in the file
|
|
127
|
+
marker[0] = "*"
|
|
128
|
+
end = text.find("*/", i + 2)
|
|
129
|
+
stop = n if end == -1 else end + 2
|
|
130
|
+
while i < stop:
|
|
131
|
+
if text[i] == "\n":
|
|
132
|
+
flush()
|
|
133
|
+
i += 1
|
|
134
|
+
continue
|
|
135
|
+
data_buf.append(ch)
|
|
136
|
+
i += 1
|
|
137
|
+
if data_buf or comment_buf:
|
|
138
|
+
flush()
|
|
139
|
+
return rows
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def strip_jsonc_comments(text: str) -> str:
|
|
143
|
+
"""The file with comments removed, line numbers intact — for json.loads.
|
|
144
|
+
|
|
145
|
+
One line per input line, so a syntax error points at the real line. The
|
|
146
|
+
walking lives in scan_jsonc_lines, the module's one implementation.
|
|
147
|
+
"""
|
|
148
|
+
return "\n".join(data for data, _raw, _whole in scan_jsonc_lines(text)) + "\n"
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _title_of(block: list) -> str:
|
|
152
|
+
"""The title among the comment lines of one entry's block.
|
|
153
|
+
|
|
154
|
+
The old file wrote the title first and its url as a footnote after it:
|
|
155
|
+
|
|
156
|
+
// Моменты счастья — Алекс Дубас
|
|
157
|
+
// @url:`https://www.livelib.ru/book/1003047920`
|
|
158
|
+
|
|
159
|
+
so the title is the FIRST line that is not an `@url:` footnote.
|
|
160
|
+
"""
|
|
161
|
+
for body in block:
|
|
162
|
+
if not body.startswith("@url"):
|
|
163
|
+
return body
|
|
164
|
+
return ""
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _own_title(key: str, raw: str, trailing: str) -> str:
|
|
168
|
+
"""The title a key takes from the comment trailing its own line, or ''.
|
|
169
|
+
|
|
170
|
+
`raw` still shows the marker, so the key's tail can be read: the comment
|
|
171
|
+
names the key only when that tail is the key's complete value — closed off
|
|
172
|
+
by the end of the line — and not a value still open (`"1": {`).
|
|
173
|
+
"""
|
|
174
|
+
if not trailing:
|
|
175
|
+
return ""
|
|
176
|
+
tail = raw[raw.index(f'"{key}"'):]
|
|
177
|
+
tail = tail.split("//", 1)[0]
|
|
178
|
+
closed = (tail.count("{") + tail.count("[")) <= (tail.count("}") + tail.count("]"))
|
|
179
|
+
return _title_of([trailing]) if closed else ""
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _titles_from_comments(text: str) -> dict:
|
|
183
|
+
"""Map an edition id to the title held by the comment naming it.
|
|
184
|
+
|
|
185
|
+
The old file put a title in either of two places, and both are carried over:
|
|
186
|
+
|
|
187
|
+
// Моменты счастья — Алекс Дубас <- whole-line block above a key
|
|
188
|
+
// @url:`https://www.livelib.ru/book/1`
|
|
189
|
+
"1003047920": {"weight": 1},
|
|
190
|
+
"1000699994": {"status": "read"} // Зона — Сергей Довлатов
|
|
191
|
+
|
|
192
|
+
so an entry takes its own trailing comment when it is written as one line,
|
|
193
|
+
and otherwise the block above its key. `/* */` asides name nothing, and real
|
|
194
|
+
data ends a block. The same look-up names each `merges` member sitting
|
|
195
|
+
inside the array.
|
|
196
|
+
"""
|
|
197
|
+
views = scan_jsonc_lines(text)
|
|
198
|
+
n = len(views)
|
|
199
|
+
|
|
200
|
+
def block_above(index: int) -> list:
|
|
201
|
+
"""The whole-line `//` comments naming the id at `index`, top first."""
|
|
202
|
+
block = []
|
|
203
|
+
j = index - 1
|
|
204
|
+
while j >= 0:
|
|
205
|
+
data, _raw, whole = views[j]
|
|
206
|
+
if whole:
|
|
207
|
+
block.append(whole)
|
|
208
|
+
elif data.strip():
|
|
209
|
+
break # real data: anything above names some other entry
|
|
210
|
+
j -= 1
|
|
211
|
+
return list(reversed(block))
|
|
212
|
+
|
|
213
|
+
titles: dict = {}
|
|
214
|
+
for i, (data, raw, _whole) in enumerate(views):
|
|
215
|
+
keys = list(re.finditer(r'"(\d+)"\s*:', data))
|
|
216
|
+
for key in keys:
|
|
217
|
+
if key.group(1) in titles:
|
|
218
|
+
continue # a merges member names itself, see below
|
|
219
|
+
# a key takes its title from the comment trailing its own line (the
|
|
220
|
+
# one place a key can name itself), else from the whole-line block
|
|
221
|
+
# written above it
|
|
222
|
+
trailing = raw.split("//", 1)[1].strip() if "//" in raw else ""
|
|
223
|
+
own = _own_title(key.group(1), raw, trailing) if key is keys[-1] else ""
|
|
224
|
+
titles[key.group(1)] = own if own else _title_of(block_above(i))
|
|
225
|
+
|
|
226
|
+
# a merges array may break over lines with a comment above each member:
|
|
227
|
+
# walk the lines it spans and let a line holding an id and nothing else
|
|
228
|
+
# take the title of the block above it
|
|
229
|
+
if '"merges"' not in data:
|
|
230
|
+
continue
|
|
231
|
+
last = i
|
|
232
|
+
while data.count("[") > data.count("]") and last + 1 < n:
|
|
233
|
+
last += 1
|
|
234
|
+
for k in range(i + 1, last + 1):
|
|
235
|
+
member = re.fullmatch(r'\s*(\d+)\s*,?\s*', views[k][0])
|
|
236
|
+
if member:
|
|
237
|
+
titles.setdefault(member.group(1), _title_of(block_above(k)))
|
|
238
|
+
return titles
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _section_sort_key(item):
|
|
242
|
+
"""Weight ascending, then the weightless entries by id, then id as the
|
|
243
|
+
tiebreaker — a total order, so the file is a function of the data."""
|
|
244
|
+
entry_id, entry = item
|
|
245
|
+
weight = entry.get("weight")
|
|
246
|
+
weight = int(weight) * WEIGHT_FACTOR if weight is not None else None
|
|
247
|
+
return (weight is None, weight if weight is not None else 0, entry_id)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _eye(ident: str, title: str) -> str:
|
|
251
|
+
"""An id plus whatever the eye should read beside it.
|
|
252
|
+
|
|
253
|
+
The loader reads only the part before the em dash, so a title (or a run of
|
|
254
|
+
titles, when the old file kept several for one id) goes after it and can
|
|
255
|
+
never steer the build.
|
|
256
|
+
"""
|
|
257
|
+
return f"{ident} — {title}" if title else ident
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def _section(entry_id: str, entry: dict, titles: dict) -> str:
|
|
261
|
+
"""One `## <id> — <title>` heading plus its bullets.
|
|
262
|
+
|
|
263
|
+
A title only ever lands in the eye part of a line: the heading's text after
|
|
264
|
+
the em dash, or the ids' own annotation inside the `merges` bullet. That is
|
|
265
|
+
what makes the file safe to hand-edit — a wrong or missing title cannot steer
|
|
266
|
+
the build, because the loader reads ids and keys, never the eye text.
|
|
267
|
+
"""
|
|
268
|
+
heading = "## " + _eye(entry_id, titles.get(entry_id, ""))
|
|
269
|
+
lines = [heading]
|
|
270
|
+
|
|
271
|
+
for key in CURATION_MERGE_ORDER:
|
|
272
|
+
value = entry.get(key)
|
|
273
|
+
if value is None:
|
|
274
|
+
continue
|
|
275
|
+
if key == "weight":
|
|
276
|
+
lines.append(f"- weight: {int(value) * WEIGHT_FACTOR}")
|
|
277
|
+
elif key == "merges":
|
|
278
|
+
# ids ONLY: the loader parses this value as the id list, so no title
|
|
279
|
+
# may be annotated into it — extra text would make the ids unreadable
|
|
280
|
+
lines.append("- merges: " + ", ".join(str(int(m)) for m in value))
|
|
281
|
+
else:
|
|
282
|
+
lines.append(f"- {key}: {value}")
|
|
283
|
+
|
|
284
|
+
return "\n".join(lines)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def migrate_text(jsonc_text: str) -> str:
|
|
288
|
+
"""Convert old JSONC curation text to markdown sections text."""
|
|
289
|
+
data = json.loads(strip_jsonc_comments(jsonc_text))
|
|
290
|
+
if not isinstance(data, dict):
|
|
291
|
+
raise ValueError("curation: expected an object at the top level")
|
|
292
|
+
|
|
293
|
+
titles = _titles_from_comments(jsonc_text)
|
|
294
|
+
entries = data.get("books") or {}
|
|
295
|
+
if not isinstance(entries, dict):
|
|
296
|
+
raise ValueError("curation: `books` must be an object of edition id -> entry")
|
|
297
|
+
|
|
298
|
+
sections = [_section(entry_id, entry, titles)
|
|
299
|
+
for entry_id, entry in sorted(entries.items(), key=_section_sort_key)]
|
|
300
|
+
return DOC_BLOCK + "\n" + "\n\n".join(sections) + ("\n" if sections else "")
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def migrate_file(src, out) -> Path:
|
|
304
|
+
"""Read <src>.jsonc, write <out>.md, return the output path."""
|
|
305
|
+
text = Path(src).read_text(encoding="utf-8")
|
|
306
|
+
target = Path(out)
|
|
307
|
+
target.write_text(migrate_text(text), encoding="utf-8")
|
|
308
|
+
return target
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def main(argv=None) -> int:
|
|
312
|
+
parser = argparse.ArgumentParser(
|
|
313
|
+
prog="python -m text_forge.migrate_curation",
|
|
314
|
+
description="Convert the old curation.jsonc into Curation.md "
|
|
315
|
+
"(markdown sections, weights ×10).")
|
|
316
|
+
parser.add_argument("input", help="path to the old curation.jsonc")
|
|
317
|
+
parser.add_argument("output", help="path to write the new Curation.md")
|
|
318
|
+
args = parser.parse_args(argv)
|
|
319
|
+
|
|
320
|
+
try:
|
|
321
|
+
target = migrate_file(args.input, args.output)
|
|
322
|
+
except (OSError, ValueError) as exc:
|
|
323
|
+
# nothing partial is left behind: a half-written Curation.md would drop
|
|
324
|
+
# curation decisions from the next build without saying so
|
|
325
|
+
print(f"curation migration failed: {exc}", file=sys.stderr)
|
|
326
|
+
return 1
|
|
327
|
+
|
|
328
|
+
entries = target.read_text(encoding="utf-8").count("\n## ")
|
|
329
|
+
print(f"curation: wrote {target} ({entries} sections, weights ×{WEIGHT_FACTOR})")
|
|
330
|
+
return 0
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
if __name__ == "__main__":
|
|
334
|
+
sys.exit(main())
|
|
@@ -100,46 +100,129 @@ def _edition_id(url: str) -> str:
|
|
|
100
100
|
return m.group(1) if m else ""
|
|
101
101
|
|
|
102
102
|
|
|
103
|
+
#: the curation keys a section may carry, and how each value reads
|
|
104
|
+
CURATION_INT_KEYS = ("weight",)
|
|
105
|
+
CURATION_TEXT_KEYS = ("author", "status")
|
|
106
|
+
CURATION_ID_LIST_KEYS = ("merges",)
|
|
107
|
+
CURATION_KEYS = CURATION_INT_KEYS + CURATION_TEXT_KEYS + CURATION_ID_LIST_KEYS
|
|
108
|
+
|
|
109
|
+
# a `## ` heading: a hash pair on its own or followed by the section's text —
|
|
110
|
+
# but NOT a deeper `###`/`####` heading, and NOT a closing Setext underline
|
|
111
|
+
# (`#####` under a title, which is prose). Whatever precedes the em dash is the
|
|
112
|
+
# id; whatever follows it is for the eye only.
|
|
113
|
+
CURATION_HEADING_RE = re.compile(r"^##(?=[\s\u2014-]|$)(.*?)\s*$")
|
|
114
|
+
# a bullet: a list marker at (or within) the section's indentation
|
|
115
|
+
CURATION_BULLET_RE = re.compile(r"^\s*[-*+]\s+(\S.*)$")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class CurationError(ValueError):
|
|
119
|
+
"""A curation file the build refuses to guess at.
|
|
120
|
+
|
|
121
|
+
Every message names the edition id it tripped on, plus the line it sits on:
|
|
122
|
+
a silently dropped entry would still build a plausible-looking corpus, and
|
|
123
|
+
that is the failure this format is not allowed to have.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _curation_error(lineno: int, entry_id: str, why: str, line: str) -> CurationError:
|
|
128
|
+
where = f"line {lineno}"
|
|
129
|
+
# "- " is the shape every accepted bullet has, so a rejected one reads back
|
|
130
|
+
# in the same shape: what the eye wrote is what the message shows
|
|
131
|
+
who = f" (entry {entry_id})" if entry_id else " (no `## <id>` section above)"
|
|
132
|
+
return CurationError(
|
|
133
|
+
f"curation: {where}{who}: {why}\n {line}\n"
|
|
134
|
+
"A curation section is `## <edition id> — <title for the eye>` followed "
|
|
135
|
+
"by `- key: value` bullets, keys: " + ", ".join(CURATION_KEYS) + "."
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _curation_int(lineno: int, entry_id: str, key: str, raw: str, line: str) -> int:
|
|
140
|
+
if not raw.isdigit():
|
|
141
|
+
raise _curation_error(lineno, entry_id, f"{key} must be a whole number, got {raw!r}", line)
|
|
142
|
+
return int(raw)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _curation_ids(lineno: int, entry_id: str, key: str, raw: str, line: str) -> list[int]:
|
|
146
|
+
ids = []
|
|
147
|
+
for part in raw.split(","):
|
|
148
|
+
part = part.strip()
|
|
149
|
+
if not part.isdigit():
|
|
150
|
+
raise _curation_error(
|
|
151
|
+
lineno, entry_id, f"{key} must be comma-separated edition ids, got {part!r}", line)
|
|
152
|
+
ids.append(int(part))
|
|
153
|
+
if not ids:
|
|
154
|
+
raise _curation_error(lineno, entry_id, f"{key} names no id", line)
|
|
155
|
+
return ids
|
|
156
|
+
|
|
157
|
+
|
|
103
158
|
def load_curation(path) -> dict:
|
|
104
|
-
"""Parse a .
|
|
159
|
+
"""Parse a Curation.md file: one `## <id>` section per livelib edition.
|
|
160
|
+
|
|
161
|
+
The heading's digits ARE the entry key; everything after the em dash (and
|
|
162
|
+
any prose line) is for the eye only, the same property the old jsonc `//`
|
|
163
|
+
comments had. A section carries `- key: value` bullets, any subset of
|
|
164
|
+
`weight`/`author`/`status`/`merges`, and reads back the mapping build_books
|
|
165
|
+
consumed before: {"books": {id: {weight: int, merges: [int], ...}}}.
|
|
105
166
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
also keeps `\"` from pretending to close a string.
|
|
167
|
+
Malformed input raises CurationError naming the id and the line; a line that
|
|
168
|
+
is neither a heading, a bullet, nor blank is the old JSONC shape and raises
|
|
169
|
+
too — the previous format is gone, and it must not be read by accident.
|
|
110
170
|
"""
|
|
111
171
|
text = pathlib.Path(path).read_text(encoding="utf-8")
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
172
|
+
|
|
173
|
+
# tombstone for the previous format: braces mean JSON/JSONC, and that file
|
|
174
|
+
# needs migrating (text_forge.migrate_curation), not guessing. Without this
|
|
175
|
+
# check such a file would read as ZERO entries and drop every curation
|
|
176
|
+
# decision while still building a plausible corpus.
|
|
177
|
+
if re.search(r"^\s*[{}\[\]]\s*$", text, re.MULTILINE):
|
|
178
|
+
raise CurationError(
|
|
179
|
+
f"curation: {path} is the old JSONC format, not markdown sections — "
|
|
180
|
+
"run `python -m text_forge.migrate_curation <in.jsonc> <out.md>`"
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
entries: dict = {}
|
|
184
|
+
entry_id = ""
|
|
185
|
+
for lineno, line in enumerate(text.splitlines(), 1):
|
|
186
|
+
heading = CURATION_HEADING_RE.match(line)
|
|
187
|
+
if heading:
|
|
188
|
+
# the heading's id keys the entry; the text after the em dash is
|
|
189
|
+
# eye-only, so a wrong title can never steer the build
|
|
190
|
+
entry_id = heading.group(1).lstrip("#").split("\u2014")[0].strip()
|
|
191
|
+
if not entry_id.isdigit():
|
|
192
|
+
raise _curation_error(lineno, "", f"section id must be digits, got {entry_id!r}", line)
|
|
193
|
+
if entry_id in entries:
|
|
194
|
+
raise _curation_error(lineno, entry_id, "duplicate section for this edition id", line)
|
|
195
|
+
entries[entry_id] = {}
|
|
135
196
|
continue
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
197
|
+
|
|
198
|
+
bullet = CURATION_BULLET_RE.match(line)
|
|
199
|
+
if not bullet:
|
|
200
|
+
# nothing else in the file carries data: blank lines, the doc block's
|
|
201
|
+
# prose, and any other heading are for the eye
|
|
139
202
|
continue
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
203
|
+
|
|
204
|
+
key, sep, raw = bullet.group(1).partition(":")
|
|
205
|
+
if not sep:
|
|
206
|
+
raise _curation_error(lineno, entry_id, "bullet needs `key: value`", line)
|
|
207
|
+
key = key.strip()
|
|
208
|
+
raw = raw.strip()
|
|
209
|
+
if key not in CURATION_KEYS:
|
|
210
|
+
raise _curation_error(lineno, entry_id, f"unknown key {key!r}", line)
|
|
211
|
+
if not entry_id:
|
|
212
|
+
raise _curation_error(lineno, "", "bullet outside a `## <id>` section", line)
|
|
213
|
+
if key in entries[entry_id]:
|
|
214
|
+
raise _curation_error(lineno, entry_id, f"key {key!r} given twice", line)
|
|
215
|
+
if key in CURATION_INT_KEYS:
|
|
216
|
+
value = _curation_int(lineno, entry_id, key, raw, line)
|
|
217
|
+
elif key in CURATION_ID_LIST_KEYS:
|
|
218
|
+
value = _curation_ids(lineno, entry_id, key, raw, line)
|
|
219
|
+
else:
|
|
220
|
+
if not raw:
|
|
221
|
+
raise _curation_error(lineno, entry_id, f"{key} is empty", line)
|
|
222
|
+
value = raw
|
|
223
|
+
entries[entry_id][key] = value
|
|
224
|
+
|
|
225
|
+
return {"books": entries}
|
|
143
226
|
|
|
144
227
|
|
|
145
228
|
|
|
@@ -845,9 +928,10 @@ def build_books(books_path, quotes_path, reviews_path, out_dir, *, curation_path
|
|
|
845
928
|
work_id_by_edition=None, fetch=None) -> dict:
|
|
846
929
|
"""Build books/<slug>.md notes from LiveLib export JSON files.
|
|
847
930
|
|
|
848
|
-
curation_path — ONE curation
|
|
849
|
-
edition id
|
|
850
|
-
|
|
931
|
+
curation_path — the ONE curation file (Curation.md by default, or a mapping
|
|
932
|
+
already built): sections keyed by livelib edition id, read back as {"books":
|
|
933
|
+
{id: ...}}, each entry carrying weight?/status?/author?/merges?. Anything
|
|
934
|
+
livelib can tell us is not stored.
|
|
851
935
|
|
|
852
936
|
Reference books need no section of their own: pass `text_dir` and the build
|
|
853
937
|
scans the texts for livelib edition ids no note would declare, resolving
|
|
@@ -1159,8 +1243,8 @@ def _note_index(books_dir) -> dict:
|
|
|
1159
1243
|
|
|
1160
1244
|
def _curation_section(path_or_map, section):
|
|
1161
1245
|
"""Read one section from a curation source given as a path to a consolidated
|
|
1162
|
-
.
|
|
1163
|
-
`link_map`)."""
|
|
1246
|
+
file (Curation.md, or a json side-car the build writes) or as the mapping
|
|
1247
|
+
itself (the form build_books emits, e.g. its `link_map`)."""
|
|
1164
1248
|
if path_or_map is None:
|
|
1165
1249
|
return None
|
|
1166
1250
|
if isinstance(path_or_map, (dict, list)):
|
|
@@ -7,8 +7,8 @@ Single entry point for the `make books` target (whattodo/Makefile):
|
|
|
7
7
|
Steps:
|
|
8
8
|
1. Export fresh data (shelves incl. statuses, quotes, reviews) into
|
|
9
9
|
<root>/livelib-export/ — anonymous API + SSR, no credentials needed.
|
|
10
|
-
2. build_books() with the ONE curation file
|
|
11
|
-
|
|
10
|
+
2. build_books() with the ONE curation file <root>/Curation.md — markdown
|
|
11
|
+
sections, one per livelib edition id — and the texts themselves passed
|
|
12
12
|
as `text_dir` so a work a chapter cites but no shelf holds still gets its
|
|
13
13
|
note (title/author from the edition card JSON).
|
|
14
14
|
3. Replace <root>/text/books/*.md with the generated set (_curation/ untouched).
|
|
@@ -121,8 +121,10 @@ def main(root_str: str, user_id: int = 345497) -> int:
|
|
|
121
121
|
print("export:", counts)
|
|
122
122
|
|
|
123
123
|
tmp = Path(tempfile.mkdtemp(prefix="wtd-books-"))
|
|
124
|
-
#
|
|
125
|
-
|
|
124
|
+
# ONE curation file, at the corpus root: markdown sections, one per livelib
|
|
125
|
+
# edition id. (It used to sit beside the notes as
|
|
126
|
+
# text/books/_curation/curation.jsonc; the link map below still does.)
|
|
127
|
+
curation = root / "Curation.md"
|
|
126
128
|
if not curation.exists():
|
|
127
129
|
print(f"FAIL: curation file not found: {curation}")
|
|
128
130
|
return 1
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/.github/skills/check-i18n/references/test_cases.md
RENAMED
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/mkdocs/overrides/assets/stylesheets/text-forge.css
RENAMED
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{sg_text_forge-0.3.2 → sg_text_forge-0.3.3}/text_forge/obsidian/plugins/text-forge/manifest.json
RENAMED
|
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
|