aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/scanner_helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tasks/scanner_helpers.py')
-rw-r--r--tests/tasks/scanner_helpers.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/tasks/scanner_helpers.py b/tests/tasks/scanner_helpers.py
new file mode 100644
index 0000000..4100028
--- /dev/null
+++ b/tests/tasks/scanner_helpers.py
@@ -0,0 +1,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["SOURCE_DIRECTORY"] = str(root)
+ app.config["SOURCE_BASE_URL"] = base_url