diff --git a/scripts/docs/create_release_docs.py b/scripts/docs/create_release_docs.py index 3aa1c2341e4fe5..7c707ec93e4ecc 100644 --- a/scripts/docs/create_release_docs.py +++ b/scripts/docs/create_release_docs.py @@ -101,7 +101,7 @@ def create_docs_tree(version, toc_path, narrative_docs_path, try_extract(narrative_docs_path, release_dir) try_extract(reference_docs_path, release_dir) - return root_dir, toc_dest_path + return root_dir, toc_dest_path, release_dir def try_extract(archive_path, output_dir): @@ -128,7 +128,7 @@ def try_extract(archive_path, output_dir): archive.extractall(output_dir) -def build_archive(version, root_dir, toc_path, output_path): +def build_archive(version, root_dir, toc_path, output_path, release_dir): """Builds a documentation archive for the given Bazel release. This function reads all documentation files from the tree rooted in root_dir, @@ -141,24 +141,27 @@ def build_archive(version, root_dir, toc_path, output_path): tree. toc_path: Absolute path of the _toc.yaml file. output_path: Absolute path where the archive should be written to. + release_dir: Absolute path of the root directory for this version. """ with zipfile.ZipFile(output_path, "w") as archive: for root, _, files in os.walk(root_dir): for f in files: src = os.path.join(root, f) dest = src[len(root_dir) + 1:] + rel_path = os.path.relpath(src, release_dir) if src != toc_path and rewriter.can_rewrite(src): - archive.writestr(dest, get_versioned_content(src, version)) + archive.writestr(dest, get_versioned_content(src, rel_path, version)) else: archive.write(src, dest) -def get_versioned_content(path, version): +def get_versioned_content(path, rel_path, version): """Rewrites links in the given file to point at versioned docs. Args: path: Absolute path of the file that should be rewritten. + rel_path: Relative path of the file that should be rewritten. version: Version of the Bazel release whose documentation is being built. Returns: @@ -167,13 +170,13 @@ def get_versioned_content(path, version): with open(path, "rt", encoding="utf-8") as f: content = f.read() - return rewriter.rewrite_links(path, content, version) + return rewriter.rewrite_links(path, content, rel_path, version) def main(unused_argv): version = validate_flag("version") output_path = validate_flag("output_path") - root_dir, toc_path = create_docs_tree( + root_dir, toc_path, release_dir = create_docs_tree( version=version, toc_path=validate_flag("toc_path"), narrative_docs_path=validate_flag("narrative_docs_path"), @@ -185,6 +188,7 @@ def main(unused_argv): root_dir=root_dir, toc_path=toc_path, output_path=output_path, + release_dir=release_dir, ) diff --git a/scripts/docs/rewriter.py b/scripts/docs/rewriter.py index a7f2aa7e0d8d10..412bc549100569 100644 --- a/scripts/docs/rewriter.py +++ b/scripts/docs/rewriter.py @@ -26,20 +26,26 @@ r"((href|src)\s*=\s*[\"']({})?)/".format(_BASE_URL)) -def _fix_html_links(content, version): +def _fix_html_links(content, _rel_path, version): return _HTML_LINK_PATTERN.sub(r"\1/versions/{}/".format(version), content) -def _fix_html_metadata(content, version): +def _fix_html_metadata(content, _rel_path, version): return content.replace("value=\"/_book.yaml\"", "value=\"/versions/{}/_book.yaml\"".format(version)) +def _set_header_vars(content, rel_path, version): + return content.replace("""{% include "_buttons.html" %}""", f"""{{% dynamic setvar version "{version}" %}} +{{% dynamic setvar original_path "/{rel_path[:-len(os.path.splitext(rel_path)[1])]}" %}} +{{% include "_buttons.html" %}}""") + + _MD_LINK_OR_IMAGE_PATTERN = re.compile( r"(\!?\[.*?\]\(({})?)(/.*?)\)".format(_BASE_URL)) -def _fix_md_links_and_images(content, version): +def _fix_md_links_and_images(content, _rel_path, version): return _MD_LINK_OR_IMAGE_PATTERN.sub(r"\1/versions/{}\3)".format(version), content) @@ -47,7 +53,7 @@ def _fix_md_links_and_images(content, version): _MD_METADATA_PATTERN = re.compile(r"^(Book: )(/.+)$", re.MULTILINE) -def _fix_md_metadata(content, version): +def _fix_md_metadata(content, _rel_path, version): return _MD_METADATA_PATTERN.sub(r"\1/versions/{}\2".format(version), content) @@ -59,7 +65,7 @@ def _fix_md_metadata(content, version): ["/", "/_project.yaml", "/versions/", "/versions/_toc.yaml"]) -def _fix_yaml_paths(content, version): +def _fix_yaml_paths(content, _rel_path, version): def sub(m): prefix, path, suffix = m.group(1, 4, 5) @@ -72,7 +78,7 @@ def sub(m): _PURE_HTML_FIXES = [_fix_html_links, _fix_html_metadata] -_PURE_MD_FIXES = [_fix_md_links_and_images, _fix_md_metadata] +_PURE_MD_FIXES = [_fix_md_links_and_images, _fix_md_metadata, _set_header_vars] _PURE_YAML_FIXES = [_fix_yaml_paths] _FIXES = { @@ -99,11 +105,12 @@ def can_rewrite(path): return bool(_get_fixes(path)) -def rewrite_links(path, content, version): +def rewrite_links(path, content, rel_path, version): """Rewrites links in the given file to point to versioned docs. Args: path: Absolute path of the file to be rewritten. + rel_path: Relative path of the file to be rewritten. content: Content of said file, as text. version: Version of the Bazel release that is being built. @@ -118,6 +125,6 @@ def rewrite_links(path, content, version): new_content = content for f in fixes: - new_content = f(new_content, version) + new_content = f(new_content, rel_path, version) return new_content diff --git a/scripts/docs/rewriter_test.py b/scripts/docs/rewriter_test.py index b13ceceeb8db99..26a7126ef02703 100644 --- a/scripts/docs/rewriter_test.py +++ b/scripts/docs/rewriter_test.py @@ -51,7 +51,7 @@ def testRewrite(self, basename): input_path, content = read_data_file(basename, "input") _, version = read_data_file("VERSION", "input") - actual = rewriter.rewrite_links(input_path, content, version) + actual = rewriter.rewrite_links(input_path, content, basename, version) _, expected = read_data_file(basename, "expected_output") diff --git a/scripts/docs/testdata/expected_output/doc.md b/scripts/docs/testdata/expected_output/doc.md index fcee50afd3ff30..dedecaab2f1ba2 100644 --- a/scripts/docs/testdata/expected_output/doc.md +++ b/scripts/docs/testdata/expected_output/doc.md @@ -3,6 +3,10 @@ Book: /versions/6.6.6/_book.yaml # Configurations +{% dynamic setvar version "6.6.6" %} +{% dynamic setvar original_path "/doc" %} +{% include "_buttons.html" %} + A build setting is a single piece of [configuration](/versions/6.6.6/rules/rules#configurations) information. Like all rules, build setting rules have [implementation functions](https://bazel.build/versions/6.6.6/rules/rules#implementation-function). diff --git a/scripts/docs/testdata/expected_output/markdown_with_html.md b/scripts/docs/testdata/expected_output/markdown_with_html.md index 4a2133668bbc0c..4ba1001028ba1a 100644 --- a/scripts/docs/testdata/expected_output/markdown_with_html.md +++ b/scripts/docs/testdata/expected_output/markdown_with_html.md @@ -1,6 +1,10 @@ Project: /_project.yaml Book: /versions/6.6.6/_book.yaml +{% dynamic setvar version "6.6.6" %} +{% dynamic setvar original_path "/markdown_with_html" %} +{% include "_buttons.html" %} + Lorem ipsum [short link](/versions/6.6.6/foo/bar). Or rather a [long link](https://bazel.build/versions/6.6.6/foo/bar)? ![Scalability graph](/versions/6.6.6/rules/scalability-graph.png "Scalability graph") diff --git a/scripts/docs/testdata/input/doc.md b/scripts/docs/testdata/input/doc.md index e86b57eadb2788..032f1daf2cc8a8 100644 --- a/scripts/docs/testdata/input/doc.md +++ b/scripts/docs/testdata/input/doc.md @@ -3,6 +3,8 @@ Book: /_book.yaml # Configurations +{% include "_buttons.html" %} + A build setting is a single piece of [configuration](/rules/rules#configurations) information. Like all rules, build setting rules have [implementation functions](https://bazel.build/rules/rules#implementation-function). diff --git a/scripts/docs/testdata/input/markdown_with_html.md b/scripts/docs/testdata/input/markdown_with_html.md index 7cf8c0f1cd3c91..538184ace4967d 100644 --- a/scripts/docs/testdata/input/markdown_with_html.md +++ b/scripts/docs/testdata/input/markdown_with_html.md @@ -1,6 +1,8 @@ Project: /_project.yaml Book: /_book.yaml +{% include "_buttons.html" %} + Lorem ipsum [short link](/foo/bar). Or rather a [long link](https://bazel.build/foo/bar)? ![Scalability graph](/rules/scalability-graph.png "Scalability graph") diff --git a/site/en/_buttons.html b/site/en/_buttons.html index bf639baa09e8ce..b79981ac483c80 100644 --- a/site/en/_buttons.html +++ b/site/en/_buttons.html @@ -10,25 +10,44 @@ View source {% dynamic endif %} -{% dynamic if request.path|length < 10 or request.path|slice:":10" != '/versions/' %} - - - +{% dynamic if not setvar.original_path %} +{% dynamic setvar original_path %}{% dynamic print request.path %}{% dynamic endsetvar %} {% dynamic endif %} + +{% dynamic if not setvar.version %} +Nightly +{% dynamic else %} +Nightly +{% dynamic endif %} +· +{% dynamic if setvar.version == "7.2.0" %} +7.2 +{% dynamic else %} +7.2 +{% dynamic endif %} +· +{% dynamic if setvar.version == "7.1.0" %} +7.1 +{% dynamic else %} +7.1 +{% dynamic endif %} +· +{% dynamic if setvar.version == "7.0.0" %} +7.0 +{% dynamic else %} +7.0 +{% dynamic endif %} +· +{% dynamic if setvar.version == "6.5.0" %} +6.5 +{% dynamic else %} +6.5 +{% dynamic endif %} +· +{% dynamic if setvar.version == "6.4.0" %} +6.4 +{% dynamic else %} +6.4 +{% dynamic endif %} +