-
Notifications
You must be signed in to change notification settings - Fork 0
Add script to split readme and organize documentation #29
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5e13c3
✨ Feat(docs/split_readme.py): Add script to split readme and organize…
sayalaruano 7e18a09
🎨 Add split_readme script on the conf.py file and update index
sayalaruano 4993227
Merge branch 'main' into split-readme-docs
sayalaruano f55a6f3
🔧 activate admonitions in myst_nb
enryH 6984e55
Remove withe space above overview
sayalaruano 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,128 @@ | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| # Mapping section titles to their corresponding filenames | ||
| SECTION_MAPPING = { | ||
| "": "home_page.md", | ||
| "About the project": "about.md", | ||
| "Installation": "installation.md", | ||
| "Documentation": "docs.md", | ||
| "License": "license.md", | ||
| "Credits and acknowledgements": "credits.md", | ||
| "Contact and feedback": "contact.md", | ||
| } | ||
|
|
||
|
|
||
| def extract_section(readme, section_title): | ||
| """Extracts content between current section and next ## heading""" | ||
| pattern = rf"## {re.escape(section_title)}(.*?)(?=\n## |\Z)" | ||
| match = re.search(pattern, readme, flags=re.DOTALL) | ||
| return match.group(1).strip() if match else "" | ||
|
|
||
|
|
||
| def extract_links_from_readme(readme): | ||
| """Extract link references from README.md into a dictionary""" | ||
| link_pattern = r"\[([^\]]+)\]: (\S+)" | ||
| links = {} | ||
|
|
||
| matches = re.findall(link_pattern, readme) | ||
| for ref, url in matches: | ||
| links[ref] = url | ||
|
|
||
| return links | ||
|
|
||
|
|
||
| def convert_gfm_to_sphinx(content, links): | ||
| """Convert GitHub Flavored Markdown to Sphinx-style syntax.""" | ||
| # Convert GFM admonitions (like > [!IMPORTANT] and > [!NOTE]) | ||
| content = re.sub( | ||
| r"(^|\n)> \[!(\w+)\]([^\n]*)((?:\n> [^\n]*)*)", | ||
| lambda m: f"\n:::{{{m.group(2)}}}\n" # Note the curly braces here | ||
| + re.sub(r"^> ", "", m.group(4), flags=re.MULTILINE).strip() | ||
| + "\n:::\n", | ||
| content, | ||
| ) | ||
|
|
||
| # Replace link references dynamically using the links dictionary | ||
| for ref, url in links.items(): | ||
| content = re.sub(rf"\[{re.escape(ref)}\]", f"({url})", content) | ||
|
|
||
| return content | ||
|
|
||
|
|
||
| def decrease_header_levels(content): | ||
| """Decrease each Markdown header by one level.""" | ||
| lines = content.splitlines() | ||
| new_lines = [] | ||
| for line in lines: | ||
| if re.match(r"^(#{2,6})\s", line): | ||
| num_hashes = len(line.split()[0]) | ||
| new_line = "#" * (num_hashes - 1) + line[num_hashes:] | ||
| new_lines.append(new_line) | ||
| else: | ||
| new_lines.append(line) | ||
| return "\n".join(new_lines) | ||
|
|
||
|
|
||
| def clean_trailing_links(content): | ||
| """Remove trailing links and clean up extra empty lines.""" | ||
| # Remove [label]: link style | ||
| content = re.sub(r"^\[.+?\]:\s+\S+$", "", content, flags=re.MULTILINE) | ||
| # Remove (url): url style | ||
| content = re.sub( | ||
| r"^\(https?://[^\s)]+\):\s*https?://[^\s)]+$", "", content, flags=re.MULTILINE | ||
| ) | ||
| content = re.sub( | ||
| r"^\(mailto:[^\s)]+\):\s*mailto:[^\s)]+$", "", content, flags=re.MULTILINE | ||
| ) | ||
| # Remove empty lines | ||
| content = re.sub(r"\n{2,}", "\n\n", content).strip() | ||
| return content | ||
|
|
||
|
|
||
| def process_readme(readme_path, output_dir): | ||
| readme = Path(readme_path).read_text(encoding="utf-8") | ||
|
|
||
| # Extract links from README | ||
| links = extract_links_from_readme(readme) | ||
|
|
||
| # Create output directory | ||
| output_dir.mkdir(exist_ok=True, parents=True) | ||
|
|
||
| for section_title, filename in SECTION_MAPPING.items(): | ||
| content = extract_section(readme, section_title) | ||
| if content: | ||
| myst_content = ( | ||
| f"## {section_title}\n\n{convert_gfm_to_sphinx(content, links)}" | ||
| ) | ||
| if filename.lower() == "contact.md": | ||
| myst_content = clean_trailing_links(myst_content) | ||
| myst_content = decrease_header_levels(myst_content) | ||
| (output_dir / filename).write_text(myst_content) | ||
| print(f"Generated {filename}") | ||
| else: | ||
| raise ValueError(f"Section '{section_title}' not found in README") | ||
|
|
||
| # Include CONTRIBUTING.md with its own link references | ||
| contrib_path = readme_path.parent / "CONTRIBUTING.md" | ||
| try: | ||
| raw_contrib = contrib_path.read_text() | ||
| contrib_links = extract_links_from_readme(raw_contrib) | ||
|
|
||
| # Convert content | ||
| contrib_converted = convert_gfm_to_sphinx(raw_contrib, contrib_links) | ||
|
|
||
| # Remove trailing link definitions | ||
| contrib_converted = clean_trailing_links(contrib_converted) | ||
|
|
||
| # Write output | ||
| (output_dir / "contributing.md").write_text(contrib_converted) | ||
| print("Generated contributing.md") | ||
| except FileNotFoundError as e: | ||
| raise FileNotFoundError(f"CONTRIBUTING.md not found at {contrib_path}") from e | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| default_readme = Path(__file__).resolve().parent.parent / "README.md" | ||
| output_sections_readme = Path("./sections_readme") | ||
| process_readme(default_readme, output_sections_readme) |
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.