-
Notifications
You must be signed in to change notification settings - Fork 2
完全版(単一ファイル)ビルドスクリプトを追加 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c494a5
完全版(単一ファイル)ビルドスクリプトを追加
watanabe-kohei-jp 2d1556a
build.py: script 終了タグ正規表現を堅牢化
watanabe-kohei-jp b63c455
build.py: script タグ処理を CodeQL py/bad-tag-filter 対応に変更
watanabe-kohei-jp e763b10
Merge remote-tracking branch 'origin/main' into feat/standalone-build
watanabe-kohei-jp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| #!/usr/bin/env python3 | ||
| """build.py — 各章の index.html から「完全版」(単一ファイル)を生成する。 | ||
|
|
||
| shared/ の CSS・JS と、ローカル画像(qr.svg / _assets/*.png)を HTML に | ||
| インライン化し、dist/<章名>.html として 1 ファイルにまとめる。 | ||
| リポジトリやネット接続なしで開ける、配布・オフライン閲覧用のファイル。 | ||
|
|
||
| 正本は各章の index.html(shared/ 参照版)のまま。dist/ はビルド成果物で | ||
| git 管理外。内容や shared/ を変更したら `python build.py` で再生成する。 | ||
|
|
||
| 既知の制約: | ||
| theme.css は Google Fonts を @import している。完全オフラインでは | ||
| Web フォントが読めず、システムフォントにフォールバックする | ||
| (レイアウトは崩れない)。フォント自体の埋め込みは行わない。 | ||
|
|
||
| 依存: Python 3 標準ライブラリのみ。 | ||
| 使い方: リポジトリ直下で `python build.py` | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import mimetypes | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parent | ||
| DIST = ROOT / "dist" | ||
|
|
||
| # 外部 / data URI は埋め込み対象外(そのまま残す) | ||
| _SKIP_PREFIXES = ("http://", "https://", "data:", "//") | ||
|
|
||
|
|
||
| def find_chapters() -> list[Path]: | ||
| """`NN-...` 形式で index.html を持つ章ディレクトリを返す。""" | ||
| return sorted(p.parent for p in ROOT.glob("[0-9][0-9]-*/index.html")) | ||
|
|
||
|
|
||
| def data_uri(path: Path) -> str: | ||
| mime, _ = mimetypes.guess_type(path.name) | ||
| if mime is None: | ||
| mime = "application/octet-stream" | ||
| b64 = base64.b64encode(path.read_bytes()).decode("ascii") | ||
| return f"data:{mime};base64,{b64}" | ||
|
|
||
|
|
||
| def inline(html: str, base_dir: Path) -> str: | ||
| """html 内のローカル CSS / JS / 画像参照をインライン化して返す。""" | ||
|
|
||
| def resolve(ref: str) -> Path: | ||
| return (base_dir / ref).resolve() | ||
|
|
||
| def is_local(ref: str) -> bool: | ||
| return not ref.startswith(_SKIP_PREFIXES) | ||
|
|
||
| # 1. <link rel="stylesheet" href="..."> → <style>...</style> | ||
| def css_repl(m: re.Match) -> str: | ||
| href = m.group("href") | ||
| if not is_local(href): | ||
| return m.group(0) | ||
| css = resolve(href).read_text(encoding="utf-8-sig") | ||
| return f"<style>\n{css}\n</style>" | ||
|
|
||
| html = re.sub( | ||
| r'<link\s+rel="stylesheet"\s+href="(?P<href>[^"]+)"\s*/?\s*>', | ||
| css_repl, | ||
| html, | ||
| ) | ||
|
|
||
| # 2. <script src="..."></script> → <script>...</script> | ||
| # (defer 属性は落ちるが、progress-strip.js は readyState を自前で | ||
| # ガードしており、deck-stage.js は head 配置で問題ない) | ||
| def js_repl(m: re.Match) -> str: | ||
| src = m.group("src") | ||
| if not is_local(src): | ||
| return m.group(0) | ||
| js = resolve(src).read_text(encoding="utf-8-sig") | ||
| return f"<script>\n{js}\n</script>" | ||
|
|
||
| html = re.sub( | ||
| r'<script\s+src="(?P<src>[^"]+)"[^>]*>\s*</script>', | ||
Check failureCode scanning / CodeQL Bad HTML filtering regexp High
This regular expression does not match script end tags like </script >.
|
||
| js_repl, | ||
| html, | ||
| ) | ||
|
|
||
| # 3. <img ... src="..."> のローカル画像 → data URI | ||
| def img_repl(m: re.Match) -> str: | ||
| src = m.group("src") | ||
| if not is_local(src): | ||
| return m.group(0) | ||
| return m.group(0).replace(f'src="{src}"', f'src="{data_uri(resolve(src))}"') | ||
|
|
||
| html = re.sub(r'<img\b[^>]*\bsrc="(?P<src>[^"]+)"[^>]*>', img_repl, html) | ||
|
|
||
| return html | ||
|
|
||
|
|
||
| def main() -> int: | ||
| # Windows の既定コンソール(cp932)でも日本語を出せるようにする | ||
| try: | ||
| sys.stdout.reconfigure(encoding="utf-8") | ||
| except (AttributeError, ValueError): | ||
| pass | ||
|
|
||
| chapters = find_chapters() | ||
| if not chapters: | ||
| print("章ディレクトリ(NN-.../index.html)が見つかりません。", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| DIST.mkdir(exist_ok=True) | ||
| ok = True | ||
| print(f"完全版をビルド中({len(chapters)} 章)...\n") | ||
|
|
||
| for ch in chapters: | ||
| html = (ch / "index.html").read_text(encoding="utf-8-sig") | ||
| built = inline(html, ch) | ||
|
|
||
| # 未解決のローカル参照(../ や _assets/)が残っていないか検証 | ||
| leftover = sorted(set(re.findall( | ||
| r'(?:href|src)="((?:\.\./|\./|_assets/)[^"]*)"', built))) | ||
|
|
||
| out = DIST / f"{ch.name}.html" | ||
| out.write_text(built, encoding="utf-8") | ||
| size_kb = out.stat().st_size / 1024 | ||
|
|
||
| if leftover: | ||
| ok = False | ||
| print(f" [NG] {out.relative_to(ROOT)} ({size_kb:,.0f} KB)") | ||
| print(f" 未解決のローカル参照: {leftover}") | ||
| else: | ||
| print(f" [OK] {out.relative_to(ROOT)} ({size_kb:,.0f} KB)") | ||
|
|
||
| print() | ||
| if not ok: | ||
| print("一部の参照を埋め込めませんでした。上記を確認してください。", file=sys.stderr) | ||
| return 1 | ||
| print(f"完了: dist/ に {len(chapters)} ファイルを生成しました。") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.