Skip to content

Commit 1d0b216

Browse files
committed
app: vendor Bootstrap and htmx; add find_static() template global.
1 parent 7912357 commit 1d0b216

13 files changed

Lines changed: 100 additions & 9 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
* text eol=lf
33
*.png -text
44
*.bat -text
5+
src/reader/_app/static/vendor/** -text -diff

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,6 @@ ENV/
103103

104104
# mypy
105105
.mypy_cache/
106+
107+
# add back some stuff
108+
!src/reader/_app/static/vendor/*/dist/

scripts/vendor_app_stuff.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pathlib
2+
import shutil
3+
4+
import requests
5+
6+
7+
STUFF = {
8+
"htmx.org@2.0.8": ["dist/htmx.min.js"],
9+
"htmx-ext-response-targets@2.0.4": ["dist/response-targets.min.js"],
10+
"bootstrap@5.3.8": [
11+
"dist/css/bootstrap.min.css",
12+
"dist/js/bootstrap.bundle.min.js",
13+
],
14+
"bootstrap-icons@1.13.1": [
15+
"font/bootstrap-icons.min.css",
16+
"font/fonts/bootstrap-icons.woff",
17+
"font/fonts/bootstrap-icons.woff2",
18+
],
19+
}
20+
21+
self_path = pathlib.Path(__file__)
22+
root_dir = self_path.parent.parent
23+
vendor_dir = root_dir / "src/reader/_app/static/vendor"
24+
url_fmt = "https://cdn.jsdelivr.net/npm/{package}/{path}"
25+
26+
shutil.rmtree(vendor_dir, ignore_errors=True)
27+
vendor_dir.mkdir(parents=True)
28+
(vendor_dir / "README").write_text(f"maintained by {self_path.name}")
29+
30+
for package, paths in STUFF.items():
31+
for path in paths:
32+
33+
src = url_fmt.format(package=package, path=path)
34+
response = requests.get(src)
35+
response.raise_for_status()
36+
37+
dst = vendor_dir.joinpath(package, *pathlib.PosixPath(path).parts)
38+
dst.parent.mkdir(parents=True, exist_ok=True)
39+
dst.write_bytes(response.content)
40+
41+
print(f"{package} {path} done")

src/reader/_app/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pathlib
12
from datetime import datetime
23
from datetime import timedelta
34
from datetime import timezone
@@ -317,6 +318,31 @@ def add_jinja_do_extension(setup_state):
317318
setup_state.app.jinja_env.add_extension('jinja2.ext.do')
318319

319320

321+
@blueprint.add_app_template_global
322+
def find_static(filename, blueprint=None):
323+
blueprint = blueprint or request.blueprint
324+
if blueprint:
325+
endpoint = f'{blueprint}.static'
326+
folder = current_app.blueprints[blueprint].static_folder
327+
else:
328+
endpoint = 'static'
329+
folder = current_app.static_folder
330+
331+
dir = pathlib.Path(folder)
332+
matches = [p.relative_to(dir) for p in dir.rglob(f'{filename}')]
333+
334+
if not matches:
335+
raise RuntimeError(f"no such static file: {filename!r}")
336+
if len(matches) > 1:
337+
sep = '\n* '
338+
raise RuntimeError(
339+
f"more than one static file for {filename!r}:\n"
340+
f"{sep}{sep.join(p.as_posix() for p in matches)}\n"
341+
)
342+
343+
return url_for(endpoint, filename=matches[0])
344+
345+
320346
def create_app(reader_config):
321347
app = Flask(__name__)
322348
app.config['SECRET_KEY'] = 'secret'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
maintained by vendor_app_stuff.py

src/reader/_app/static/vendor/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.

src/reader/_app/static/vendor/bootstrap@5.3.8/dist/css/bootstrap.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/reader/_app/static/vendor/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)