fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 1 | # Lint as: python3 |
| 2 | # pylint: disable=g-direct-third-party-import |
| 3 | # Copyright 2022 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | """Module for fixing links in Bazel release docs.""" |
| 17 | import os |
| 18 | import re |
| 19 | |
| 20 | _BASE_URL = "https://bazel.build" |
| 21 | |
| 22 | # We need to use regular expressions here since HTML can be embedded in |
| 23 | # Markdown and Yaml, thus breaking XML parsers. Moreover, our use case is |
| 24 | # simple, so regex should work (tm). |
| 25 | _HTML_LINK_PATTERN = re.compile( |
| 26 | r"((href|src)\s*=\s*[\"']({})?)/".format(_BASE_URL)) |
| 27 | |
| 28 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 29 | def _fix_html_links(content, rel_path, version): |
| 30 | del rel_path # unused |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 31 | return _HTML_LINK_PATTERN.sub(r"\1/versions/{}/".format(version), content) |
| 32 | |
| 33 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 34 | def _fix_html_metadata(content, rel_path, version): |
| 35 | del rel_path # unused |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 36 | return content.replace("value=\"/_book.yaml\"", |
| 37 | "value=\"/versions/{}/_book.yaml\"".format(version)) |
| 38 | |
| 39 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 40 | def _set_header_vars(content, rel_path, version): |
| 41 | return content.replace( |
| 42 | """{% include "_buttons.html" %}""", |
| 43 | f"""{{% dynamic setvar version "{version}" %}} |
| 44 | {{% dynamic setvar original_path "/{os.path.splitext(rel_path)[0]}" %}} |
| 45 | {{% include "_buttons.html" %}}""", |
| 46 | ) |
| 47 | |
| 48 | |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 49 | _MD_LINK_OR_IMAGE_PATTERN = re.compile( |
| 50 | r"(\!?\[.*?\]\(({})?)(/.*?)\)".format(_BASE_URL)) |
| 51 | |
| 52 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 53 | def _fix_md_links_and_images(content, rel_path, version): |
| 54 | del rel_path # unused |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 55 | return _MD_LINK_OR_IMAGE_PATTERN.sub(r"\1/versions/{}\3)".format(version), |
| 56 | content) |
| 57 | |
| 58 | |
| 59 | _MD_METADATA_PATTERN = re.compile(r"^(Book: )(/.+)$", re.MULTILINE) |
| 60 | |
| 61 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 62 | def _fix_md_metadata(content, rel_path, version): |
| 63 | del rel_path # unused |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 64 | return _MD_METADATA_PATTERN.sub(r"\1/versions/{}\2".format(version), content) |
| 65 | |
| 66 | |
Googler | 468c056 | 2023-05-30 02:31:21 -0700 | [diff] [blame] | 67 | _YAML_PATH_PATTERN = re.compile( |
| 68 | r"(((book_|image_)?path|include): ['\"]?)(/.*?)(['\"]?)$", re.MULTILINE |
| 69 | ) |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 70 | |
| 71 | _YAML_IGNORE_LIST = frozenset( |
| 72 | ["/", "/_project.yaml", "/versions/", "/versions/_toc.yaml"]) |
| 73 | |
| 74 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 75 | def _fix_yaml_paths(content, rel_path, version): |
| 76 | del rel_path # unused |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 77 | def sub(m): |
Googler | 468c056 | 2023-05-30 02:31:21 -0700 | [diff] [blame] | 78 | prefix, path, suffix = m.group(1, 4, 5) |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 79 | if path in _YAML_IGNORE_LIST: |
| 80 | return m.group(0) |
| 81 | |
| 82 | return "{}/versions/{}{}{}".format(prefix, version, path, suffix) |
| 83 | |
| 84 | return _YAML_PATH_PATTERN.sub(sub, content) |
| 85 | |
| 86 | |
| 87 | _PURE_HTML_FIXES = [_fix_html_links, _fix_html_metadata] |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 88 | _PURE_MD_FIXES = [_fix_md_links_and_images, _fix_md_metadata, _set_header_vars] |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 89 | _PURE_YAML_FIXES = [_fix_yaml_paths] |
| 90 | |
| 91 | _FIXES = { |
| 92 | ".html": _PURE_HTML_FIXES, |
| 93 | ".md": _PURE_MD_FIXES + _PURE_HTML_FIXES, |
| 94 | ".yaml": _PURE_YAML_FIXES + _PURE_HTML_FIXES, |
| 95 | } |
| 96 | |
| 97 | |
| 98 | def _get_fixes(path): |
| 99 | _, ext = os.path.splitext(path) |
| 100 | return _FIXES.get(ext) |
| 101 | |
| 102 | |
| 103 | def can_rewrite(path): |
| 104 | """Returns whether links in this file can/should be rewritten. |
| 105 | |
| 106 | Args: |
| 107 | path: Path of the file in question. |
| 108 | |
| 109 | Returns: |
| 110 | True if the file can/should be rewritten. |
| 111 | """ |
| 112 | return bool(_get_fixes(path)) |
| 113 | |
| 114 | |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 115 | def rewrite_links(path, content, rel_path, version): |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 116 | """Rewrites links in the given file to point to versioned docs. |
| 117 | |
| 118 | Args: |
| 119 | path: Absolute path of the file to be rewritten. |
| 120 | content: Content of said file, as text. |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 121 | rel_path: Relative path of the file to be rewritten. |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 122 | version: Version of the Bazel release that is being built. |
| 123 | |
| 124 | Returns: |
| 125 | The rewritten content of the file, as text. Equal to `content` |
| 126 | if no links had to be rewritten. |
| 127 | """ |
| 128 | fixes = _get_fixes(path) |
| 129 | if not fixes: |
| 130 | raise ValueError( |
| 131 | "Cannot rewrite {} due to unsupported file type.".format(path)) |
| 132 | |
| 133 | new_content = content |
| 134 | for f in fixes: |
Fabian Meumertzheim | 2562707 | 2024-06-18 05:58:09 -0700 | [diff] [blame] | 135 | new_content = f(new_content, rel_path, version) |
fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 136 | |
| 137 | return new_content |