Skip to content

Commit 1890ceb

Browse files
authored
Merge pull request #84 from python-project-templates/tkp/hf
Wiki tweaks
2 parents 60b69d7 + 33e022f commit 1890ceb

File tree

1 file changed

+81
-29
lines changed

1 file changed

+81
-29
lines changed

yardang/wiki.py

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ def fix_code_block(match):
231231
# Python: assignment or call followed by new statement
232232
code = re.sub(r"(\w)\s{2,}(def |class |import |from |with |if |for |while |return |#)", r"\1\n\2", code)
233233

234+
# TOML: section header [xxx] followed by key (even single space)
235+
code = re.sub(r"(\])\s+(\w[\w-]*\s*=)", r"\1\n\2", code)
236+
237+
# TOML: section header [xxx] followed by another section [xxx]
238+
code = re.sub(r"(\])\s+(\[)", r"\1\n\n\2", code)
239+
240+
# TOML: key = "value" followed by another key
241+
code = re.sub(r'(")\s+(\w[\w-]*\s*=)', r"\1\n\2", code)
242+
243+
# TOML: key = true/false/number followed by another key
244+
code = re.sub(r"(true|false|\d)\s+(\w[\w-]*\s*=)", r"\1\n\2", code)
245+
246+
# TOML: array items - string in array followed by string
247+
code = re.sub(r'(",)\s+(")', r"\1\n\2", code)
248+
249+
# TOML: closing array ] followed by key
250+
code = re.sub(r"(\])\s+(\w[\w-]*\s*=)", r"\1\n\2", code)
251+
234252
# YAML: value followed by new key at same or lower indent level
235253
code = re.sub(r"(\]|\"|\'|\w)\s{2,}(\w+:)", r"\1\n\2", code)
236254

@@ -257,8 +275,8 @@ def cleanup_api_docs(content: str) -> str:
257275
"""Clean up API documentation for better readability.
258276
259277
Reformats dense sphinx-markdown-builder API output:
260-
- Breaks long function signatures into multiple lines
261-
- Removes escaped underscores in code contexts
278+
- Removes escaped characters
279+
- Cleans up function signature formatting
262280
- Improves parameter list formatting
263281
264282
Args:
@@ -267,26 +285,34 @@ def cleanup_api_docs(content: str) -> str:
267285
Returns:
268286
Cleaned API documentation content.
269287
"""
270-
# Remove escaped underscores in code/function contexts
271-
# Match: word\_word patterns and unescape them
272-
content = re.sub(r"(\w)\\_(\w)", r"\1_\2", content)
273-
274-
# Format long function signatures - break parameters onto separate lines
275-
def format_signature(match):
276-
prefix = match.group(1) # ### module.function(
277-
params = match.group(2) # parameters
278-
suffix = match.group(3) # )
279-
280-
# If signature is short enough, keep it
281-
if len(match.group(0)) < 80:
282-
return match.group(0)
288+
# Remove ALL escaped underscores - they don't render well in GitHub Wiki
289+
content = re.sub(r"\\_", "_", content)
290+
291+
# Remove escaped asterisks in signatures (e.g., \* for *args)
292+
content = re.sub(r"\\(\*+)", r"\1", content)
293+
294+
# Fix function signatures that got broken across lines badly
295+
# For long signatures, use function name as heading + signature in code block
296+
def format_function_signature(match):
297+
func_name = match.group(1) # e.g., "yardang.build.generate_docs_configuration"
298+
sig_content = match.group(2) # everything between ( and )
299+
300+
# Collapse any existing newlines/whitespace in signature
301+
sig_content = re.sub(r"\s+", " ", sig_content).strip()
283302

284-
# Parse parameters and format them
285-
# Split on ", " but be careful about nested brackets
303+
# Build the full signature
304+
full_sig = f"{func_name}({sig_content})"
305+
306+
# If short enough, keep as simple heading
307+
if len(full_sig) < 80:
308+
return f"### {full_sig}"
309+
310+
# For long signatures: function name as heading, full signature in code block
311+
# Parse parameters for nice formatting
286312
param_list = []
287313
current = ""
288314
bracket_depth = 0
289-
for char in params:
315+
for char in sig_content:
290316
if char in "([{":
291317
bracket_depth += 1
292318
current += char
@@ -302,18 +328,20 @@ def format_signature(match):
302328
if current.strip():
303329
param_list.append(current.strip())
304330

305-
# If few parameters, keep on one line
306-
if len(param_list) <= 2:
307-
return match.group(0)
331+
# Format params with nice indentation in code block
332+
if param_list:
333+
formatted_params = ",\n ".join(param_list)
334+
code_block = f"```python\n{func_name}(\n {formatted_params}\n)\n```"
335+
else:
336+
code_block = f"```python\n{func_name}()\n```"
308337

309-
# Format with line breaks
310-
formatted_params = ",\n ".join(param_list)
311-
return f"{prefix}\n {formatted_params}\n{suffix}"
338+
return f"### `{func_name}()`\n\n{code_block}"
312339

313-
# Match function/method signatures: ### name(params)
340+
# Match ### header with function signature (handles multi-line)
341+
# This pattern matches: ### name(\n params...\n) or ### name(params)
314342
content = re.sub(
315-
r"(###\s+[\w.]+\()((?:[^()]+|\([^()]*\))*?)(\))",
316-
format_signature,
343+
r"###\s+([\w.]+)\(\s*\n?([\s\S]*?)\n?\s*\)",
344+
format_function_signature,
317345
content,
318346
)
319347

@@ -372,8 +400,19 @@ def format_signature(match):
372400
content,
373401
)
374402

375-
# Fix "#### NOTE" / "#### WARNING" etc to be more prominent
376-
content = re.sub(r"####\s+(NOTE|WARNING|SEE ALSO|IMPORTANT)", r"> **\1**", content)
403+
# Fix "#### NOTE" / "#### WARNING" etc to use GitHub Flavored Markdown alerts
404+
def _to_gfm_alert(match: re.Match) -> str:
405+
alert_type = match.group(1).upper()
406+
# Map to GFM alert types
407+
gfm_type = {
408+
"NOTE": "NOTE",
409+
"WARNING": "WARNING",
410+
"IMPORTANT": "IMPORTANT",
411+
"SEE ALSO": "TIP",
412+
}.get(alert_type, "NOTE")
413+
return f"> [!{gfm_type}]"
414+
415+
content = re.sub(r"####\s+(NOTE|WARNING|SEE ALSO|IMPORTANT)", _to_gfm_alert, content)
377416

378417
return content
379418

@@ -738,6 +777,19 @@ def process_wiki_output(
738777
if not output_dir.exists():
739778
raise FileNotFoundError(f"Output directory not found: {output_dir}")
740779

780+
# Remove sphinx build artifacts that shouldn't be in the wiki
781+
cruft_dirs = [
782+
".doctrees",
783+
"_static",
784+
"_sphinx_design_static",
785+
"_images",
786+
"_sources",
787+
]
788+
for cruft_dir in cruft_dirs:
789+
cruft_path = output_dir / cruft_dir
790+
if cruft_path.exists() and cruft_path.is_dir():
791+
shutil.rmtree(cruft_path)
792+
741793
# Flatten directory structure
742794
page_map = flatten_directory_structure(output_dir)
743795

0 commit comments

Comments
 (0)