byggleaf 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
byggleaf/__init__.py ADDED
@@ -0,0 +1,18 @@
1
+ """ByggLeaf — a deliberately small static site generator.
2
+
3
+ The package root is only a compatibility surface: it re-exports the canonical
4
+ version from :mod:`byggleaf.core.version` and never imports an entry module.
5
+
6
+ Dependency direction (enforced by tests/test_architecture.py):
7
+
8
+ app.py -> cli.py, core/*
9
+ cli.py -> standard library
10
+ core/* -> core/*, standard library, markdown, jinja2
11
+
12
+ Nothing ever points back towards app.py. The only entry point is the installed
13
+ ``byggleaf`` console script; the package intentionally has no ``__main__.py``.
14
+ """
15
+
16
+ from .core.version import VERSION as __version__
17
+
18
+ __all__ = ["__version__"]
byggleaf/app.py ADDED
@@ -0,0 +1,226 @@
1
+ """Application assembly point.
2
+
3
+ ``app.py`` is the only module that imports both ``cli`` and ``core``. The CLI
4
+ defines the command surface and receives handlers from this file; core modules
5
+ never learn that a command line exists. This is the seam that keeps the
6
+ dependency graph pointing in one direction.
7
+
8
+ The installed ``byggleaf`` console script lands here. There is deliberately
9
+ no package-level ``__main__.py``: one entry point is enough.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import sys
15
+ from pathlib import Path
16
+
17
+ from . import cli
18
+ from .core.builder import Builder, BuildError
19
+ from .core.clean import CleanError, clean_output
20
+ from .core.config import CONFIG_FILENAME, ConfigError, load_config
21
+ from .core.jinja import TemplateError
22
+ from .core.locales import LocaleError
23
+ from .core.markdown import MarkdownError
24
+ from .core.new import NewError, create_article
25
+ from .core.scaffold import ScaffoldError, init_site
26
+ from .core.server import ServerError, serve
27
+ from .core.version import VERSION
28
+
29
+
30
+ def _display(path: Path, root: Path) -> str:
31
+ """Prefer a short workspace-relative path in terminal summaries."""
32
+
33
+ try:
34
+ return str(path.relative_to(root))
35
+ except ValueError:
36
+ return str(path)
37
+
38
+
39
+ def _cmd_init(options: cli.InitOptions) -> int:
40
+ console = cli.Console()
41
+ errors = cli.Console(stream=sys.stderr)
42
+ root = options.root.resolve()
43
+
44
+ console.heading("init")
45
+ console.item("root", str(root))
46
+
47
+ try:
48
+ result = init_site(root, force=options.force)
49
+ except (ScaffoldError, OSError) as exc:
50
+ errors.error(str(exc))
51
+ return 1
52
+
53
+ for path in result.created:
54
+ console.item("create", _display(path, root))
55
+ for path in result.skipped:
56
+ console.item("keep", _display(path, root))
57
+
58
+ console.success("The garden is ready.")
59
+ console.muted(
60
+ "Next: edit byggleaf.toml, then run `byggleaf build` and `byggleaf serve`."
61
+ )
62
+ return 0
63
+
64
+
65
+ def _cmd_clean(options: cli.CleanOptions) -> int:
66
+ console = cli.Console()
67
+ errors = cli.Console(stream=sys.stderr)
68
+ root = options.root.resolve()
69
+
70
+ console.heading("clean")
71
+
72
+ try:
73
+ result = clean_output(root)
74
+ except (CleanError, OSError) as exc:
75
+ errors.error(str(exc))
76
+ return 1
77
+
78
+ if not result.removed:
79
+ console.muted("public/ is already absent; nothing to do.")
80
+ return 0
81
+
82
+ console.item("removed", f"{_display(result.output_dir, root)}/")
83
+ console.success(f"Removed public/ ({result.kind}).")
84
+ return 0
85
+
86
+
87
+ def _cmd_new(options: cli.NewOptions) -> int:
88
+ console = cli.Console()
89
+ errors = cli.Console(stream=sys.stderr)
90
+ root = options.root.resolve()
91
+
92
+ console.heading("new")
93
+
94
+ try:
95
+ result = create_article(root)
96
+ except (NewError, OSError) as exc:
97
+ errors.error(str(exc))
98
+ return 1
99
+
100
+ console.item("create", _display(result.path, root))
101
+ console.item("date", result.published.isoformat(sep=" ", timespec="minutes"))
102
+ console.success("Created a new article.")
103
+ console.muted(
104
+ "Edit the title, author, tags, and body; set updated when you revise it."
105
+ )
106
+ return 0
107
+
108
+
109
+ def _cmd_build(options: cli.BuildOptions) -> int:
110
+ console = cli.Console()
111
+ errors = cli.Console(stream=sys.stderr)
112
+ root = options.root.resolve()
113
+ public_dir = root / "public"
114
+
115
+ console.heading("build")
116
+
117
+ try:
118
+ config = load_config(root / CONFIG_FILENAME)
119
+ console.item("site", config.site.title)
120
+ console.item("baseurl", config.site.baseurl or "(root-relative)")
121
+ result = Builder(root, config).build()
122
+ except (
123
+ ConfigError,
124
+ BuildError,
125
+ LocaleError,
126
+ MarkdownError,
127
+ TemplateError,
128
+ OSError,
129
+ ) as exc:
130
+ errors.error(str(exc))
131
+ if public_dir.exists() or public_dir.is_symlink():
132
+ errors.muted(
133
+ "Previous public/ was preserved; no partial output was published."
134
+ )
135
+ else:
136
+ errors.muted("No public/ output was published.")
137
+ errors.muted("Fix the issue above and run `byggleaf build` again.")
138
+ return 1
139
+
140
+ console.item("output", f"{_display(result.output_dir, root)}/")
141
+ console.item("posts", str(result.posts))
142
+ console.item("pages", str(result.pages))
143
+ console.item("assets", str(result.assets))
144
+ if result.favicon:
145
+ console.item("favicon", result.favicon)
146
+ console.success(f"Built in {result.duration_seconds * 1000:.0f} ms.")
147
+ return 0
148
+
149
+
150
+ def _cmd_serve(options: cli.ServeOptions) -> int:
151
+ console = cli.Console()
152
+ errors = cli.Console(stream=sys.stderr)
153
+ root = options.root.resolve()
154
+ public_dir = root / "public"
155
+
156
+ console.heading("serve")
157
+
158
+ if not public_dir.is_dir():
159
+ errors.error("public/ does not exist; run `byggleaf build` first")
160
+ return 1
161
+
162
+ configured_baseurl = ""
163
+ try:
164
+ configured_baseurl = load_config(root / CONFIG_FILENAME).site.baseurl
165
+ except ConfigError:
166
+ # A preview of already-built files should still work without a config.
167
+ pass
168
+
169
+ def ready(url: str) -> None:
170
+ console.item("bind", f"{options.host}:{options.port}")
171
+ console.item("local", url)
172
+ console.item("root", f"{_display(public_dir, root)}/")
173
+ if options.host in {"0.0.0.0", "::"}:
174
+ console.muted(
175
+ "Bound to all interfaces; use this machine's LAN IP "
176
+ "from another device."
177
+ )
178
+ if configured_baseurl:
179
+ local_prefixes = (
180
+ f"http://127.0.0.1:{options.port}",
181
+ f"http://localhost:{options.port}",
182
+ f"http://{options.host}:{options.port}",
183
+ )
184
+ if not any(
185
+ configured_baseurl == prefix
186
+ or configured_baseurl.startswith(prefix + "/")
187
+ for prefix in local_prefixes
188
+ ):
189
+ console.muted(
190
+ f"note: pages use baseurl {configured_baseurl!r}; "
191
+ 'set [site] baseurl = "" for a purely local preview.'
192
+ )
193
+ console.muted("Press Ctrl+C to stop.")
194
+
195
+ try:
196
+ serve(
197
+ public_dir,
198
+ host=options.host,
199
+ port=options.port,
200
+ on_ready=ready,
201
+ )
202
+ except ServerError as exc:
203
+ errors.error(str(exc))
204
+ return 1
205
+ except OSError as exc:
206
+ errors.error(str(exc))
207
+ return 1
208
+ return 0
209
+
210
+
211
+ def handlers() -> cli.Handlers:
212
+ """Wire command names to the application services they are allowed to use."""
213
+
214
+ return cli.Handlers(
215
+ init=_cmd_init,
216
+ build=_cmd_build,
217
+ clean=_cmd_clean,
218
+ new=_cmd_new,
219
+ serve=_cmd_serve,
220
+ )
221
+
222
+
223
+ def main(argv: list[str] | None = None) -> int:
224
+ """Console-script entry point."""
225
+
226
+ return cli.main(argv, handlers=handlers(), version=VERSION)