-
Notifications
You must be signed in to change notification settings - Fork 0
feat(docs): add nexus page BBCode generator #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| #!/usr/bin/env python3 | ||
| """Generate nexus-page BBCode from README.md user-facing content. | ||
|
|
||
| Reads the block between <!-- nexus:start --> and <!-- nexus:end --> in README.md, | ||
| converts it to BBCode, substitutes it into the <!-- generated:start/end --> region | ||
| of docs/nexus-page.md in memory, and prints the result to stdout. | ||
|
codepuncher marked this conversation as resolved.
|
||
|
|
||
| Usage: | ||
| python3 scripts/generate-nexus-page.py | ||
| python3 scripts/generate-nexus-page.py | xclip -selection clipboard | ||
| """ | ||
|
|
||
| import argparse | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| README = Path(__file__).parent.parent / "README.md" | ||
| NEXUS_TEMPLATE = Path(__file__).parent.parent / "docs" / "nexus-page.md" | ||
|
|
||
| NEXUS_START = "<!-- nexus:start -->" | ||
| NEXUS_END = "<!-- nexus:end -->" | ||
| GEN_START = "<!-- generated:start -->" | ||
| GEN_END = "<!-- generated:end -->" | ||
| BBCODE_FENCE_OPEN = "```bbcode" | ||
| BBCODE_FENCE_CLOSE = "```" | ||
|
|
||
|
|
||
| def extract_block(text: str, start_marker: str, end_marker: str) -> str: | ||
| start = text.find(start_marker) | ||
| if start == -1: | ||
| sys.exit(f"error: marker not found: {start_marker!r}") | ||
| end = text.find(end_marker, start + len(start_marker)) | ||
| if end == -1: | ||
| sys.exit(f"error: marker not found: {end_marker!r}") | ||
| return text[start + len(start_marker):end].strip() | ||
|
codepuncher marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def md_to_bbcode(md: str) -> str: | ||
| lines = md.splitlines() | ||
| output: list[str] = [] | ||
| i = 0 | ||
| first_heading = True | ||
|
|
||
| while i < len(lines): | ||
| line = lines[i] | ||
|
|
||
| # ## Heading | ||
| if line.startswith("## "): | ||
| heading = line[3:].strip() | ||
| if not first_heading: | ||
| output.append("") | ||
| output.append("[line]") | ||
| output.append("") | ||
| first_heading = False | ||
| output.append(f"[size=4][b][color=#B8953E]{heading}[/color][/b][/size]") | ||
| i += 1 | ||
| continue | ||
|
|
||
| # Unordered list block | ||
| if line.startswith("- "): | ||
| items: list[str] = [] | ||
| while i < len(lines) and lines[i].startswith("- "): | ||
| items.append(convert_inline(lines[i][2:].strip())) | ||
| i += 1 | ||
| output.append("[list]") | ||
| for item in items: | ||
| output.append(f"[*]{item}") | ||
| output.append("[/list]") | ||
| continue | ||
|
|
||
| # Ordered list block — may be preceded by a bold label line | ||
| if re.match(r"\d+\. ", line): | ||
| items = [] | ||
| while i < len(lines) and re.match(r"\d+\. ", lines[i]): | ||
| items.append(convert_inline(re.sub(r"^\d+\. ", "", lines[i]))) | ||
| i += 1 | ||
| output.append("[list=1]") | ||
| for item in items: | ||
| output.append(f"[*]{item}") | ||
| output.append("[/list]") | ||
| continue | ||
|
|
||
| # Bold label (e.g. **Mod manager (recommended):**) | ||
| if re.match(r"\*\*.+\*\*", line): | ||
| output.append(convert_inline(line)) | ||
| i += 1 | ||
| continue | ||
|
|
||
| # Blank line | ||
| if line.strip() == "": | ||
| output.append("") | ||
| i += 1 | ||
| continue | ||
|
|
||
| # Plain paragraph (pass through inline conversion) | ||
| stripped = line.strip() | ||
| if stripped and not stripped.startswith("<!--"): | ||
| output.append(convert_inline(line)) | ||
|
|
||
| i += 1 | ||
|
|
||
| # Collapse consecutive blank lines to single blank | ||
| result = re.sub(r"\n{3,}", "\n\n", "\n".join(output)) | ||
| return result.strip() | ||
|
|
||
|
|
||
| def convert_inline(text: str) -> str: | ||
| # [text](url) → [url=url]text[/url] | ||
| # Note: link text containing ] or URLs containing unbalanced ) are not supported. | ||
| text = re.sub(r"\[([^\]]+)\]\(([^)]*(?:\([^)]*\)[^)]*)*)\)", r"[url=\2]\1[/url]", text) | ||
| # **text** → [b]text[/b] | ||
| text = re.sub(r"\*\*(.+?)\*\*", r"[b]\1[/b]", text) | ||
| # `code` → [font=Courier New]code[/font] | ||
| text = re.sub(r"`([^`]+)`", r"[font=Courier New]\1[/font]", text) | ||
| return text | ||
|
|
||
|
|
||
| def extract_bbcode_fence(text: str) -> str: | ||
| start = text.find(BBCODE_FENCE_OPEN) | ||
| if start == -1: | ||
| sys.exit(f"error: {BBCODE_FENCE_OPEN!r} fence not found in nexus-page.md") | ||
| newline = text.find("\n", start) | ||
| if newline == -1: | ||
| sys.exit(f"error: no newline after {BBCODE_FENCE_OPEN!r} in nexus-page.md") | ||
| start = newline + 1 | ||
| end = text.find(f"\n{BBCODE_FENCE_CLOSE}", start) | ||
| if end == -1: | ||
| sys.exit("error: closing ``` fence not found in nexus-page.md") | ||
| return text[start:end] | ||
|
codepuncher marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def inject_generated(bbcode_content: str, generated: str) -> str: | ||
| start = bbcode_content.find(GEN_START) | ||
| if start == -1: | ||
| sys.exit(f"error: marker not found: {GEN_START!r}") | ||
| end = bbcode_content.find(GEN_END, start + len(GEN_START)) | ||
| if end == -1: | ||
| sys.exit(f"error: marker not found: {GEN_END!r}") | ||
| return ( | ||
| bbcode_content[:start] | ||
| + GEN_START + "\n" | ||
| + generated + "\n" | ||
| + bbcode_content[end:] | ||
|
codepuncher marked this conversation as resolved.
|
||
| ) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| argparse.ArgumentParser( | ||
| description=__doc__, | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ).parse_args() | ||
|
|
||
| readme = README.read_text(encoding="utf-8") | ||
| template = NEXUS_TEMPLATE.read_text(encoding="utf-8") | ||
|
|
||
| md_block = extract_block(readme, NEXUS_START, NEXUS_END) | ||
| bbcode_generated = md_to_bbcode(md_block) | ||
|
|
||
| bbcode_content = extract_bbcode_fence(template) | ||
| result = inject_generated(bbcode_content, bbcode_generated) | ||
| result = result.replace(GEN_START + "\n", "", 1).replace("\n" + GEN_END, "", 1) | ||
|
|
||
|
codepuncher marked this conversation as resolved.
|
||
| print(result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.