-
Notifications
You must be signed in to change notification settings - Fork 208
fix: Convert relative path to a file in Mardown to its URL on GitHub. #1070
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
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ee98aa0
Convert relative path to a file in Mardown to its URL on GitHub.
wangshangsam 0da45f2
fix: make layernorm_epsilon configurable in with megatron backend (#1…
ashors1 838ee44
ci: Only run build-test-publish-wheel workflow if env var set (#1047)
chtruong814 62d09f3
fix: ray.sub will exit early if any srun fails to launch (#1022)
terrykong 0b83744
fix: address double bos in eval task (#962)
ZhiyuLi-Nvidia 037ecd7
feat: add testmon support to detect when tests need to be rerun (#1056)
terrykong 5580200
Fix CI.
wangshangsam 6052183
Fix CI.
wangshangsam 4be538b
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam 9332277
Ignore sphinx from type checking.
wangshangsam a0768fb
Merge branch 'wangshangsam/fix-rel-src-link' of github.com:NVIDIA-NeM…
wangshangsam 1dd4b02
Ignore sphinx from type checking.
wangshangsam b9fa7ca
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam 5da37d7
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam b80397c
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam 6cbaa67
Fix CI and address CodeRabbit comments.
wangshangsam 9d2effa
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam 76577e1
Address CodeRabbit comments.
wangshangsam d2b8d0c
Address CodeRabbit comments.
wangshangsam 0036137
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam ed9f503
Merge branch 'main' into wangshangsam/fix-rel-src-link
wangshangsam 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,15 @@ | |
|
|
||
| import os | ||
| import sys | ||
| import urllib.parse | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import git | ||
| from docutils import nodes | ||
| from docutils.transforms import Transform | ||
| from sphinx import addnodes | ||
| from sphinx.application import Sphinx | ||
|
|
||
| project = "NeMo-RL" | ||
| copyright = "2025, NVIDIA Corporation" | ||
|
|
@@ -99,16 +108,6 @@ | |
| } | ||
| html_extra_path = ["project.json", "versions1.json"] | ||
|
|
||
| # -- Supporting rendering GitHub alerts correctly ---------------------------- | ||
| # https://github.com/executablebooks/MyST-Parser/issues/845 | ||
|
|
||
| _GITHUB_ADMONITIONS = { | ||
| "> [!NOTE]": "note", | ||
| "> [!TIP]": "tip", | ||
| "> [!IMPORTANT]": "important", | ||
| "> [!WARNING]": "warning", | ||
| "> [!CAUTION]": "caution", | ||
| } | ||
|
|
||
| # Github links are now getting rate limited from the Github Actions | ||
| linkcheck_ignore = [ | ||
|
|
@@ -117,19 +116,32 @@ | |
| ] | ||
|
|
||
|
|
||
| def convert_gh_admonitions(app, relative_path, parent_docname, contents): | ||
| def _convert_gh_admonitions( | ||
| app: Sphinx, relative_path: Path, parent_docname: str, contents: list[str] | ||
| ) -> None: | ||
| """Supporting rendering GitHub alerts correctly. | ||
wangshangsam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # https://github.com/executablebooks/MyST-Parser/issues/845 | ||
| """ | ||
| _github_admonitions = { | ||
| "> [!NOTE]": "note", | ||
| "> [!TIP]": "tip", | ||
| "> [!IMPORTANT]": "important", | ||
| "> [!WARNING]": "warning", | ||
| "> [!CAUTION]": "caution", | ||
| } | ||
| # loop through content lines, replace github admonitions | ||
| for i, orig_content in enumerate(contents): | ||
| orig_line_splits = orig_content.split("\n") | ||
| replacing = False | ||
| for j, line in enumerate(orig_line_splits): | ||
| # look for admonition key | ||
| line_roi = line.lstrip() | ||
| for admonition_key in _GITHUB_ADMONITIONS: | ||
| for admonition_key in _github_admonitions: | ||
| if line_roi.startswith(admonition_key): | ||
| line = line.replace( | ||
| admonition_key, | ||
| "```{" + _GITHUB_ADMONITIONS[admonition_key] + "}", | ||
| "```{" + _github_admonitions[admonition_key] + "}", | ||
| ) | ||
| # start replacing quotes in subsequent lines | ||
| replacing = True | ||
|
|
@@ -153,5 +165,75 @@ def convert_gh_admonitions(app, relative_path, parent_docname, contents): | |
| contents[i] = "\n".join(orig_line_splits) | ||
|
|
||
|
|
||
| def setup(app): | ||
| app.connect("include-read", convert_gh_admonitions) | ||
| class _GitHubLinkTransform(Transform): | ||
| """Converting the relative path to a file in a Markdown to the URL of that file on GitHub.""" | ||
|
|
||
| default_priority = 500 # type: ignore[bad-override] | ||
|
|
||
| @staticmethod | ||
| def _get_github_source_url(repo: git.Repo) -> str: | ||
| # Find out which remote GitHub repo should be the source. | ||
| if "origin" in repo.remotes: | ||
| url = repo.remotes.origin.url | ||
| elif len(repo.remotes) == 1: | ||
| url = repo.remotes[0].url | ||
| else: | ||
| raise ValueError( | ||
| "Cannot determine which remote repo on GitHub this local repo is from." | ||
| ) | ||
| # Canonicalize the URL. | ||
| if url.startswith("[email protected]:"): | ||
| url = url.replace("[email protected]:", "https://github.com/", 1) | ||
| if url.endswith(".git"): | ||
| url = url[: -len(".git")] | ||
| return url | ||
|
|
||
| def apply(self, **kwargs: Any) -> None: # type: ignore[bad-override] | ||
| try: | ||
| local_repo = git.Repo(search_parent_directories=True) | ||
| remote_repo_url = self._get_github_source_url(local_repo) | ||
| except Exception: | ||
| # Cannot figure out which source url it should be; leave links as-is. | ||
| return | ||
| if local_repo.working_tree_dir is None: | ||
| # If the local repo is a bare repo, the method below won't work. | ||
| return | ||
| wt_dir = local_repo.working_tree_dir | ||
|
|
||
| for node in self.document.traverse(addnodes.download_reference): | ||
| md_dir = Path(node["refdoc"]).parent | ||
| dst_path = md_dir / Path(node["reftarget"]) | ||
| try: | ||
| dst_path = dst_path.resolve(strict=True) | ||
| except OSError: | ||
| # If the path doesn't exist or a symlink loop is encountered. | ||
| continue | ||
| if dst_path.is_file(): | ||
| kind = "blob" | ||
| elif dst_path.is_dir(): | ||
| kind = "tree" | ||
| else: | ||
| # Cannot figure out what type of thing this path is pointing to. | ||
| continue | ||
| refuri = "/".join( | ||
| ( | ||
| remote_repo_url.rstrip("/"), | ||
| kind, | ||
| local_repo.head.object.hexsha, | ||
| urllib.parse.quote(dst_path.relative_to(wt_dir).as_posix()), | ||
| ) | ||
| ) | ||
| new_node = nodes.reference(rawsource=node.rawsource, refuri=refuri) | ||
| # Preserve styling and title if present. | ||
| if "classes" in node: | ||
| new_node["classes"] = list(node["classes"]) | ||
| if "title" in node: | ||
| new_node["title"] = node["title"] | ||
| if node.children: | ||
| new_node += node.children | ||
| node.replace_self(new_node) | ||
|
|
||
|
|
||
| def setup(app: Sphinx) -> None: | ||
| app.add_transform(_GitHubLinkTransform) | ||
| app.connect("include-read", _convert_gh_admonitions) | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.