add preview server

This commit is contained in:
Daniel Fichtinger 2025-06-21 20:06:52 -04:00
parent 296b3d2385
commit d8b491bc33
5 changed files with 97 additions and 1 deletions

View file

@ -1,6 +1,6 @@
import typer
from pathlib import Path
from zona import builder
from zona import builder, server
app = typer.Typer()
@ -15,6 +15,14 @@ def build(in_dir: str | None = None, out_dir: str | None = None):
builder.build(Path(in_dir), items)
@app.command()
def serve(dir: Path = Path("public")):
if not dir.exists():
raise typer.BadParameter(f"Directory {dir} does not exist.")
typer.echo(f"Serving files from {dir}")
server.serve(dir)
@app.command()
def init():
# init will populate the current directory with

View file

@ -9,6 +9,7 @@ class ZonaRenderer(HTMLRenderer):
@override
def render_link(self, element: Link):
href = element.dest
assert isinstance(href, str)
if href.endswith(".md") and not href.startswith("http"):
href = href[:-3] + ".html"
body: Any = self.render_children(element)

13
src/zona/server.py Normal file
View file

@ -0,0 +1,13 @@
from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
import typer
import uvicorn
from pathlib import Path
def serve(dir: Path, host: str = "localhost", port: int = 8000):
app = Starlette()
app.mount("/", StaticFiles(directory=dir, html=True), name="zona")
typer.echo(f"Preview server running at http://{host}:{port}")
uvicorn.run(app, host=host, port=port, log_level="warning")