aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/scanner_helpers.py
blob: 36d9bb5359be20a54105d72ccbe7df763b246e9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

from pathlib import Path

from bs4 import BeautifulSoup
from flask import Flask

BASE_URL = "https://dennisfink.me/blog/"
SOURCE_URL = f"{BASE_URL}example/"


def parse_html(html: str) -> BeautifulSoup:
    return BeautifulSoup(html, "html5lib")


def write_post(
    root: Path,
    slug: str,
    content: str,
    *,
    canonical: str | None = None,
    entry_url: str | None = None,
) -> Path:
    directory = root / slug
    directory.mkdir(parents=True, exist_ok=True)

    source_url = f"{BASE_URL}{slug}/"

    if canonical is None:
        canonical = source_url

    if entry_url is None:
        entry_url = source_url

    path = directory / "index.html"
    path.write_text(
        f"""
        <!doctype html>
        <html>
          <head>
            <link
              rel="canonical"
              href="{canonical}"
            >
          </head>
          <body>
            <article class="h-entry">
              <a
                class="u-url"
                href="{entry_url}"
              >
                Permalink
              </a>

              <div class="e-content">
                {content}
              </div>
            </article>
          </body>
        </html>
        """,
        encoding="utf-8",
    )

    return path


def configure_scanner(app: Flask, root: Path, *, base_url: str | None = None) -> None:
    app.config["SITE_OUTPUT_DIRECTORY"] = str(root)
    app.config["SITE_BASE_URL"] = base_url