Copy every file in the source directory to the target directory. For ``.md`` and ``.html`` files, render them with the context before copying them. ``.md`` files are transformed to HTML.
(
source_path: pathlib.Path,
target_path: pathlib.Path,
)
| 429 | |
| 430 | |
| 431 | def main( |
| 432 | source_path: pathlib.Path, |
| 433 | target_path: pathlib.Path, |
| 434 | ) -> int: |
| 435 | """ |
| 436 | Copy every file in the source directory to the target directory. |
| 437 | |
| 438 | For ``.md`` and ``.html`` files, render them with the context |
| 439 | before copying them. ``.md`` files are transformed to HTML. |
| 440 | """ |
| 441 | |
| 442 | # Sanity check: validate that versions.json is valid JSON |
| 443 | versions_path = source_path / "versions.json" |
| 444 | with versions_path.open(encoding="utf-8") as f: |
| 445 | try: |
| 446 | json.load(f) |
| 447 | except json.JSONDecodeError as e: |
| 448 | raise RuntimeError( |
| 449 | f"Invalid versions.json: {e}. Ensure it is valid JSON." |
| 450 | ) from e |
| 451 | |
| 452 | config_fname = source_path / "config.yml" |
| 453 | |
| 454 | shutil.rmtree(target_path, ignore_errors=True) |
| 455 | os.makedirs(target_path, exist_ok=True) |
| 456 | |
| 457 | sys.stderr.write("Generating context...\n") |
| 458 | context = get_context(config_fname, target_path=target_path) |
| 459 | sys.stderr.write("Context generated\n") |
| 460 | |
| 461 | templates_path = source_path / context["main"]["templates_path"] |
| 462 | jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_path)) |
| 463 | |
| 464 | for fname in get_source_files(source_path): |
| 465 | if fname.as_posix() in context["main"]["ignore"]: |
| 466 | continue |
| 467 | sys.stderr.write(f"Processing {fname}\n") |
| 468 | dirname = fname.parent |
| 469 | (target_path / dirname).mkdir(parents=True, exist_ok=True) |
| 470 | |
| 471 | extension = fname.suffix |
| 472 | if extension in (".html", ".md"): |
| 473 | with (source_path / fname).open(encoding="utf-8") as f: |
| 474 | content = f.read() |
| 475 | if extension == ".md": |
| 476 | toc = TocExtension( |
| 477 | title="Table of Contents", |
| 478 | toc_depth="2-3", |
| 479 | permalink=" #", |
| 480 | ) |
| 481 | body = markdown.markdown( |
| 482 | content, extensions=context["main"]["markdown_extensions"] + [toc] |
| 483 | ) |
| 484 | # Apply Bootstrap's table formatting manually |
| 485 | # Python-Markdown doesn't let us config table attributes by hand |
| 486 | body = body.replace("<table>", '<table class="table table-bordered">') |
| 487 | content = extend_base_template(body, context["main"]["base_template"]) |
| 488 | context["base_url"] = "../" * (len(fname.parents) - 1) |
no test coverage detected