|
| 1 | +""" |
| 2 | +External content |
| 3 | +################ |
| 4 | +
|
| 5 | +Copyright (c) 2021 Nordic Semiconductor ASA |
| 6 | +SPDX-License-Identifier: Apache-2.0 |
| 7 | +
|
| 8 | +Introduction |
| 9 | +============ |
| 10 | +
|
| 11 | +This extension allows to import sources from directories out of the Sphinx |
| 12 | +source directory. They are copied to the source directory before starting the |
| 13 | +build. Note that the copy is *smart*, that is, only updated files are actually |
| 14 | +copied. Therefore, incremental builds detect changes correctly and behave as |
| 15 | +expected. |
| 16 | +
|
| 17 | +Links to external content not included in the generated documentation are |
| 18 | +transformed to external links as needed. |
| 19 | +
|
| 20 | +Configuration options |
| 21 | +===================== |
| 22 | +
|
| 23 | +- ``external_content_contents``: A list of external contents. Each entry is |
| 24 | + a tuple with two fields: the external base directory and a file glob pattern. |
| 25 | +- ``external_content_link_prefixes``: A list of link prefixes out of scope. |
| 26 | + All links to content with these prefixes are made external. |
| 27 | +- ``external_content_link_extensions``: A list of file extensions in scope of |
| 28 | + the documentation. All links to content without these file extensions are |
| 29 | + made external. |
| 30 | +- ``external_content_keep``: A list of file globs (relative to the destination |
| 31 | + directory) that should be kept even if they do not exist in the source |
| 32 | + directory. This option can be useful for auto-generated files in the |
| 33 | + destination directory. |
| 34 | +""" |
| 35 | + |
| 36 | +import filecmp |
| 37 | +import os |
| 38 | +from pathlib import Path |
| 39 | +import re |
| 40 | +import shutil |
| 41 | +import tempfile |
| 42 | +from typing import Dict, Any, List, Optional |
| 43 | + |
| 44 | +from sphinx.application import Sphinx |
| 45 | + |
| 46 | +__version__ = "0.1.0" |
| 47 | + |
| 48 | +DIRECTIVES = ("figure", "image", "include", "literalinclude") |
| 49 | +"""Default directives for included content.""" |
| 50 | + |
| 51 | +EXTERNAL_LINK_URL_PREFIX = ( |
| 52 | + "https://github.com/project-chip/connectedhomeip/blob/master/" |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +def adjust_includes( |
| 57 | + fname: Path, |
| 58 | + basepath: Path, |
| 59 | + encoding: str, |
| 60 | + link_prefixes: List[str], |
| 61 | + extensions: List[str], |
| 62 | + targets: List[Path], |
| 63 | + dstpath: Optional[Path] = None, |
| 64 | +) -> None: |
| 65 | + """Adjust included content paths. |
| 66 | +
|
| 67 | + Args: |
| 68 | + fname: File to be processed. |
| 69 | + basepath: Base path to be used to resolve content location. |
| 70 | + encoding: Sources encoding. |
| 71 | + link_prefixes: Prefixes of links that are made external. |
| 72 | + extensions: Filename extensions links to which are not made external. |
| 73 | + targets: List of all files that are being copied. |
| 74 | + dstpath: Destination path for fname if its path is not the actual destination. |
| 75 | + """ |
| 76 | + |
| 77 | + if fname.suffix != ".md": |
| 78 | + return |
| 79 | + |
| 80 | + dstpath = dstpath or fname.parent |
| 81 | + |
| 82 | + def _adjust_path(path): |
| 83 | + # ignore absolute paths, section links, hyperlinks and same folder |
| 84 | + if path.startswith(("/", "#", "http", "www")) or not "/" in path: |
| 85 | + return path |
| 86 | + |
| 87 | + # for files that are being copied modify reference to and out of /docs |
| 88 | + filepath = path.split("#")[0] |
| 89 | + absolute = (basepath / filepath).resolve() |
| 90 | + if absolute in targets: |
| 91 | + if "docs/" in path: |
| 92 | + path = path.replace("docs/", "") |
| 93 | + elif "../examples" in path: |
| 94 | + path = path.replace("../", "", 1) |
| 95 | + return path |
| 96 | + |
| 97 | + # otherwise change links to point to their targets' original location |
| 98 | + return Path(os.path.relpath(basepath / path, dstpath)).as_posix() |
| 99 | + |
| 100 | + def _adjust_links(m): |
| 101 | + displayed, fpath = m.groups() |
| 102 | + fpath_adj = _adjust_path(fpath) |
| 103 | + return f"[{displayed}]({fpath_adj})" |
| 104 | + |
| 105 | + def _adjust_external(m): |
| 106 | + displayed, target = m.groups() |
| 107 | + return f"[{displayed}]({EXTERNAL_LINK_URL_PREFIX}{target})" |
| 108 | + |
| 109 | + def _adjust_filetype(m): |
| 110 | + displayed, target, extension = m.groups() |
| 111 | + if extension.lower() in extensions or target.startswith("http"): |
| 112 | + return m.group(0) |
| 113 | + |
| 114 | + return f"[{displayed}]({EXTERNAL_LINK_URL_PREFIX}{target})" |
| 115 | + |
| 116 | + def _adjust_image_link(m): |
| 117 | + prefix, fpath, postfix = m.groups() |
| 118 | + fpath_adj = _adjust_path(fpath) |
| 119 | + return f"{prefix}{fpath_adj}{postfix}" |
| 120 | + |
| 121 | + rules = [ |
| 122 | + # Find any links and adjust the path |
| 123 | + (r"\[([^\[\]]*)\]\s*\((.*)\)", _adjust_links), |
| 124 | + |
| 125 | + # Find links that lead to an external folder and transform it |
| 126 | + # into an external link. |
| 127 | + ( |
| 128 | + r"\[([^\[\]]*)\]\s*\((?:\.\./)*((?:" + "|".join(link_prefixes) + r")[^)]*)\)", |
| 129 | + _adjust_external, |
| 130 | + ), |
| 131 | + |
| 132 | + # Find links that lead to a non-presentable filetype and transform |
| 133 | + # it into an external link. |
| 134 | + ( |
| 135 | + r"\[([^\[\]]*)\]\s*\((?:\.\./)*((?:[^()]+?/)*[^.()]+?(\.[^)/#]+))(?:#[^)]+)?\)", |
| 136 | + _adjust_filetype, |
| 137 | + ), |
| 138 | + |
| 139 | + # Find links that lead to a folder and transform it into an external link. |
| 140 | + ( |
| 141 | + r"\[([^\[\]]*)\]\s*\((?:\.\./)*((?:[^()]+?/)+[^).#/]+)(\))", |
| 142 | + _adjust_filetype, |
| 143 | + ), |
| 144 | + |
| 145 | + # Find image links in img tags and adjust them |
| 146 | + (r"(<img [^>]*src=[\"'])([^ >]+)([\"'][^>]*>)", _adjust_image_link) |
| 147 | + ] |
| 148 | + |
| 149 | + with open(fname, "r+", encoding=encoding) as f: |
| 150 | + content = f.read() |
| 151 | + modified = False |
| 152 | + |
| 153 | + for pattern, sub_func in rules: |
| 154 | + content, changes_made = re.subn(pattern, sub_func, content) |
| 155 | + modified = modified or changes_made |
| 156 | + |
| 157 | + if modified: |
| 158 | + f.seek(0) |
| 159 | + f.write(content) |
| 160 | + f.truncate() |
| 161 | + |
| 162 | + |
| 163 | +def sync_contents(app: Sphinx) -> None: |
| 164 | + """Synhronize external contents. |
| 165 | +
|
| 166 | + Args: |
| 167 | + app: Sphinx application instance. |
| 168 | + """ |
| 169 | + |
| 170 | + srcdir = Path(app.srcdir).resolve() |
| 171 | + to_copy = [] |
| 172 | + to_delete = set(f for f in srcdir.glob("**/*") if not f.is_dir()) |
| 173 | + to_keep = set( |
| 174 | + f |
| 175 | + for k in app.config.external_content_keep |
| 176 | + for f in srcdir.glob(k) |
| 177 | + if not f.is_dir() |
| 178 | + ) |
| 179 | + |
| 180 | + for content in app.config.external_content_contents: |
| 181 | + prefix_src, glob = content |
| 182 | + for src in prefix_src.glob(glob): |
| 183 | + if src.is_dir(): |
| 184 | + to_copy.extend( |
| 185 | + [(f, prefix_src) for f in src.glob("**/*") if not f.is_dir()] |
| 186 | + ) |
| 187 | + else: |
| 188 | + to_copy.append((src, prefix_src)) |
| 189 | + |
| 190 | + list_of_destinations = [f for f, _ in to_copy] |
| 191 | + |
| 192 | + for entry in to_copy: |
| 193 | + src, prefix_src = entry |
| 194 | + dst = (srcdir / src.relative_to(prefix_src)).resolve() |
| 195 | + |
| 196 | + if dst in to_delete: |
| 197 | + to_delete.remove(dst) |
| 198 | + |
| 199 | + if not dst.parent.exists(): |
| 200 | + dst.parent.mkdir(parents=True) |
| 201 | + |
| 202 | + # just copy if it does not exist |
| 203 | + if not dst.exists(): |
| 204 | + shutil.copy(src, dst) |
| 205 | + adjust_includes( |
| 206 | + dst, |
| 207 | + src.parent, |
| 208 | + app.config.source_encoding, |
| 209 | + app.config.external_content_link_prefixes, |
| 210 | + app.config.external_content_link_extensions, |
| 211 | + list_of_destinations, |
| 212 | + ) |
| 213 | + # if origin file is modified only copy if different |
| 214 | + elif src.stat().st_mtime > dst.stat().st_mtime: |
| 215 | + with tempfile.TemporaryDirectory() as td: |
| 216 | + # adjust origin includes before comparing |
| 217 | + src_adjusted = Path(td) / src.name |
| 218 | + shutil.copy(src, src_adjusted) |
| 219 | + adjust_includes( |
| 220 | + src_adjusted, |
| 221 | + src.parent, |
| 222 | + app.config.source_encoding, |
| 223 | + app.config.external_content_link_prefixes, |
| 224 | + app.config.external_content_link_extensions, |
| 225 | + list_of_destinations, |
| 226 | + dstpath=dst.parent, |
| 227 | + ) |
| 228 | + |
| 229 | + if not filecmp.cmp(src_adjusted, dst): |
| 230 | + dst.unlink() |
| 231 | + shutil.move(os.fspath(src_adjusted), os.fspath(dst)) |
| 232 | + |
| 233 | + # remove any previously copied file not present in the origin folder, |
| 234 | + # excepting those marked to be kept. |
| 235 | + for file in to_delete - to_keep: |
| 236 | + file.unlink() |
| 237 | + |
| 238 | + |
| 239 | +def setup(app: Sphinx) -> Dict[str, Any]: |
| 240 | + app.add_config_value("external_content_contents", [], "env") |
| 241 | + app.add_config_value("external_content_keep", [], "") |
| 242 | + app.add_config_value("external_content_link_prefixes", [], "env") |
| 243 | + app.add_config_value("external_content_link_extensions", [], "env") |
| 244 | + |
| 245 | + app.connect("builder-inited", sync_contents) |
| 246 | + |
| 247 | + return { |
| 248 | + "version": __version__, |
| 249 | + "parallel_read_safe": True, |
| 250 | + "parallel_write_safe": True, |
| 251 | + } |
0 commit comments