Skip to content

Commit

Permalink
New approach
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jun 13, 2024
1 parent 7154321 commit e9b64b7
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 35 deletions.
16 changes: 10 additions & 6 deletions scripts/docs/create_release_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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"),
Expand All @@ -185,6 +188,7 @@ def main(unused_argv):
root_dir=root_dir,
toc_path=toc_path,
output_path=output_path,
release_dir=release_dir,
)


Expand Down
23 changes: 15 additions & 8 deletions scripts/docs/rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,34 @@
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)


_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)


Expand All @@ -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)
Expand All @@ -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 = {
Expand All @@ -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.
Expand All @@ -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
2 changes: 1 addition & 1 deletion scripts/docs/rewriter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 4 additions & 0 deletions scripts/docs/testdata/expected_output/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions scripts/docs/testdata/expected_output/markdown_with_html.md
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
2 changes: 2 additions & 0 deletions scripts/docs/testdata/input/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions scripts/docs/testdata/input/markdown_with_html.md
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
59 changes: 39 additions & 20 deletions site/en/_buttons.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,44 @@
View source<span class="material-icons icon-after" aria-hidden="true">open_in_new</span>
</a>
{% dynamic endif %}
{% dynamic if request.path|length < 10 or request.path|slice:":10" != '/versions/' %}
<devsite-language-selector>
<ul role="presentation">
<li role="presentation">
<a role="menuitem" href="/versions/7.2.0{%dynamic print request.path}">7.2.0</a>
</li>
<li role="presentation">
<a role="menuitem" href="/versions/7.1.0{%dynamic print request.path}">7.1.0</a>
</li>
<li role="presentation">
<a role="menuitem" href="/versions/7.0.0{%dynamic print request.path}">7.0.0</a>
</li>
<li role="presentation">
<a role="menuitem" href="/versions/6.5.0{%dynamic print request.path}">6.5.0</a>
</li>
<li role="presentation">
<a role="menuitem" href="/versions/6.4.0{%dynamic print request.path}">6.4.0</a>
</li>
</ul>
</devsite-language-selector>
{% dynamic if not setvar.original_path %}
{% dynamic setvar original_path %}{% dynamic print request.path %}{% dynamic endsetvar %}
{% dynamic endif %}
<span style="float: right; line-height: 36px">
{% dynamic if not setvar.version %}
<strong>Nightly</strong>
{% dynamic else %}
<a href="{% dynamic print setvar.original_path %}">Nightly</a>
{% dynamic endif %}
·
{% dynamic if setvar.version == "7.2.0" %}
<strong>7.2</strong>
{% dynamic else %}
<a href="/versions/7.2.0/{% dynamic print setvar.original_path %}">7.2</a>
{% dynamic endif %}
·
{% dynamic if setvar.version == "7.1.0" %}
<strong>7.1</strong>
{% dynamic else %}
<a href="/versions/7.1.0/{% dynamic print setvar.original_path %}">7.1</a>
{% dynamic endif %}
·
{% dynamic if setvar.version == "7.0.0" %}
<strong>7.0</strong>
{% dynamic else %}
<a href="/versions/7.0.0/{% dynamic print setvar.original_path %}">7.0</a>
{% dynamic endif %}
·
{% dynamic if setvar.version == "6.5.0" %}
<strong>6.5</strong>
{% dynamic else %}
<a href="/versions/6.5.0/{% dynamic print setvar.original_path %}">6.5</a>
{% dynamic endif %}
·
{% dynamic if setvar.version == "6.4.0" %}
<strong>6.4</strong>
{% dynamic else %}
<a href="/versions/6.4.0/{% dynamic print setvar.original_path %}">6.4</a>
{% dynamic endif %}
</span>
</p>

0 comments on commit e9b64b7

Please sign in to comment.