-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathgenerate_index_keywords.py
More file actions
304 lines (237 loc) · 11.2 KB
/
generate_index_keywords.py
File metadata and controls
304 lines (237 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
Generate an A–Z documentation index that lists:
* Markdown files (by title) grouped under their starting letter.
* Keywords/phrases (from db_terms.txt) grouped under their starting letter,
each linking to all docs in which the term appears.
What's new in this version
==========================
• **Multi‑word terms supported**: Lines in `db_terms.txt` may contain spaces (e.g., "tech preview", "storage engine").
• **Original capitalization preserved**: How you write the term in `db_terms.txt` is how it appears in the generated index.
• **Case‑insensitive matching**: Terms match regardless of capitalization in Markdown content.
• **Whitespace‑flexible phrase matching**: Internal whitespace in a term (spaces/tabs/newlines) is matched with `\s+` so a term can wrap lines in Markdown and still match.
• **Code blocks ignored**: Fenced code (``` ... ```) and inline code (`...`) are stripped before matching so code examples do not create false positives.
• **Backward compatible single‑word behavior**: Single tokens in `db_terms.txt` work exactly as before.
Usage
-----
1. Put your canonical keyword/phrase list (one per line) in `db_terms.txt`.
2. Run this script from the project root (the directory that contains `docs/`).
3. The script writes `docs/index-keywords.md`.
Notes / Recommendations
-----------------------
* Blank lines and lines that start with `#` (comment) in `db_terms.txt` are ignored.
* Duplicate terms (case-insensitive) keep the first casing encountered.
* If you have overlapping terms (e.g., "engine" and "storage engine"), **both** can be listed; each is matched independently.
* Very common short words (e.g., "the") will match widely—avoid including those.
"""
import os
import re
from collections import defaultdict
from typing import Dict, List, Tuple, Pattern
DEBUG = False
DB_TERMS_FILE = "db_terms.txt"
# ---------------------------------------------------------------------------
# Loading & preparing DB terms
# ---------------------------------------------------------------------------
def load_db_terms() -> Dict[str, str]:
"""Load terms from DB_TERMS_FILE.
Returns a dict mapping lowercase term -> *original* term string (trimmed),
preserving the first capitalization seen.
Lines that are blank or start with '#' are skipped.
"""
if not os.path.exists(DB_TERMS_FILE):
print(f"'{DB_TERMS_FILE}' not found. Creating an empty file.")
open(DB_TERMS_FILE, 'w', encoding='utf-8').close()
return {}
terms: Dict[str, str] = {}
with open(DB_TERMS_FILE, 'r', encoding='utf-8') as f:
for raw in f:
line = raw.strip()
if not line:
continue
if line.startswith('#'):
continue
key = line.lower()
# keep the *first* capitalization encountered for a given lowercase key
terms.setdefault(key, line)
return terms
# ---------------------------------------------------------------------------
# Content preprocessing (strip code blocks / inline code)
# ---------------------------------------------------------------------------
def strip_code(content: str) -> str:
"""Remove fenced and inline code segments from Markdown content.
This reduces false positives when scanning for terms.
"""
# Remove fenced code blocks ```...``` (greedy across newlines)
content = re.sub(r"```[\s\S]*?```", " ", content)
# Remove inline code `...`
content = re.sub(r"`[^`]+`", " ", content)
return content
# ---------------------------------------------------------------------------
# Build regex patterns for each term
# ---------------------------------------------------------------------------
def _term_to_regex(term: str) -> str:
"""Convert a term (possibly multi-word) to a regex string.
Rules:
* Match case-insensitively (applied when compiled).
* Enforce word boundaries at start & end (\b) for alnum/underscore tokens.
* Internal whitespace in the term becomes "\s+" in regex.
* Other punctuation characters in the term are escaped literally.
Examples:
'MySQL' -> r'\bMySQL\b'
'tech preview' -> r'\btech\s+preview\b'
'xtrabackup-plugin' -> r'\bxtrabackup\-plugin\b'
NOTE: If you *intentionally* want to match inside words, include a '*' at
the start/end yourself and call this function differently. For now we keep
it simple & safe.
"""
# Split on whitespace to decide where to allow flexible spacing
parts = re.split(r"\s+", term.strip())
escaped_parts = [re.escape(p) for p in parts if p]
if not escaped_parts:
return "" # shouldn't happen; caller guards
body = r"\s+".join(escaped_parts) if len(escaped_parts) > 1 else escaped_parts[0]
# Wrap with word boundaries. We deliberately use \b; this behaves well for
# alnum/_ boundaries. If your term starts/ends with punctuation, \b may not
# behave exactly as expected, but it's still usually fine. We could enhance
# with lookarounds if needed later.
return rf"\b{body}\b"
def build_term_patterns(db_terms: Dict[str, str]) -> Dict[str, Pattern[str]]:
"""Compile regex patterns (IGNORECASE) for each term.
Returns dict mapping lowercase-term -> compiled Pattern.
"""
patterns: Dict[str, Pattern[str]] = {}
for lterm, orig in db_terms.items():
regex_str = _term_to_regex(orig)
if not regex_str:
continue
patterns[lterm] = re.compile(regex_str, re.IGNORECASE)
return patterns
# ---------------------------------------------------------------------------
# Extract title from Markdown
# ---------------------------------------------------------------------------
def extract_title(content: str, filename: str) -> str:
"""Extract the first Markdown H1 (#) heading; fallback to filename."""
match = re.search(r'^#\s+(.*)', content, re.MULTILINE)
return match.group(1).strip() if match else filename
# ---------------------------------------------------------------------------
# Keyword/phrase detection in content
# ---------------------------------------------------------------------------
def extract_keywords(content: str, db_terms: Dict[str, str], patterns: Dict[str, Pattern[str]]) -> List[str]:
"""Return a list of *original-case* db terms found in content.
Matching is case-insensitive; internal whitespace in the term is flexible.
Code blocks and inline code are ignored.
"""
text = strip_code(content)
found: List[str] = []
for lterm, pat in patterns.items():
if pat.search(text):
found.append(db_terms[lterm]) # original capitalization
# Deduplicate while preserving original order
seen = set()
unique = []
for t in found:
if t.lower() in seen:
continue
seen.add(t.lower())
unique.append(t)
return sorted(unique, key=str.lower)
# ---------------------------------------------------------------------------
# Scan docs tree
# ---------------------------------------------------------------------------
def scan_markdown_files(
docs_dir: str,
db_terms: Dict[str, str],
patterns: Dict[str, Pattern[str]],
) -> Tuple[List[Tuple[str, str]], Dict[str, List[str]], Dict[str, str]]:
"""Scan Markdown files and map found keywords to files.
Returns:
file_data : list of (title, rel_path)
keyword_map : term -> [rel_paths...]
title_map : rel_path -> title
"""
file_data: List[Tuple[str, str]] = []
keyword_map: Dict[str, List[str]] = defaultdict(list)
title_map: Dict[str, str] = {}
for root, _, files in os.walk(docs_dir):
for filename in files:
if not filename.endswith('.md'):
continue
if filename == 'index-keywords.md':
continue
rel_path = os.path.relpath(os.path.join(root, filename), docs_dir)
# Skip release notes subtree
if rel_path.startswith("release-notes" + os.sep):
continue
full_path = os.path.join(root, filename)
with open(full_path, 'r', encoding='utf-8') as f:
content = f.read()
title = extract_title(content, filename)
kws = extract_keywords(content, db_terms, patterns)
file_data.append((title, rel_path))
title_map[rel_path] = title
if DEBUG and kws:
print(f"[DEBUG] {rel_path}: {kws}")
for kw in kws:
keyword_map[kw].append(rel_path)
# Deduplicate and sort rel_paths per keyword
for kw in keyword_map:
keyword_map[kw] = sorted(set(keyword_map[kw]))
return file_data, keyword_map, title_map
# ---------------------------------------------------------------------------
# Index generation
# ---------------------------------------------------------------------------
def generate_alphabetical_index(
file_data: List[Tuple[str, str]],
keyword_map: Dict[str, List[str]],
title_map: Dict[str, str],
) -> str:
"""Generate the Markdown index grouped by initial letter."""
index_content = "# Documentation Index by Alphabet\n\n"
# Group files by first letter of title
files_by_letter: Dict[str, List[Tuple[str, str]]] = defaultdict(list)
for title, path in file_data:
letter = title[0].upper() if title else '#'
files_by_letter[letter].append((title, path))
# Group keywords by first letter (use display case; safe to index 0)
kws_by_letter: Dict[str, List[str]] = defaultdict(list)
for kw in keyword_map.keys():
letter = kw[0].upper() if kw else '#'
kws_by_letter[letter].append(kw)
for letter in (chr(c) for c in range(ord('A'), ord('Z') + 1)):
files = files_by_letter.get(letter, [])
kws = kws_by_letter.get(letter, [])
if not files and not kws:
continue
index_content += f"## {letter}\n\n"
if files:
index_content += "**Files:**\n\n"
for title, path in sorted(files, key=lambda x: x[0].lower()):
index_content += f"* [{title}]({path})\n"
index_content += "\n"
if kws:
index_content += "**Keywords:**\n\n"
for kw in sorted(kws, key=str.lower):
linked_titles = ", ".join(
f"[{title_map[p]}]({p})" for p in keyword_map[kw]
)
index_content += f"* **{kw}** — {linked_titles}\n"
index_content += "\n"
return index_content
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
docs_dir = 'docs'
if not os.path.exists(docs_dir):
print(f"Error: '{docs_dir}' not found.")
return
db_terms = load_db_terms() # lower -> original
patterns = build_term_patterns(db_terms)
file_data, keyword_map, title_map = scan_markdown_files(docs_dir, db_terms, patterns)
index_content = generate_alphabetical_index(file_data, keyword_map, title_map)
index_path = os.path.join(docs_dir, 'index-keywords.md')
with open(index_path, 'w', encoding='utf-8') as f:
f.write(index_content)
print(f"A–Z index with titles and keyword links generated: {index_path}")
if __name__ == "__main__":
main()