From 4dd7c9c7bebe022d7d2aea65e262b3559b54e983 Mon Sep 17 00:00:00 2001 From: eiiko6 Date: Mon, 12 Jan 2026 18:26:48 +0100 Subject: [PATCH] fixed responsiveness, and added cli options to disable navigation or toggle localhost --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++------- templates/_base.html | 5 ++++- templates/style.css | 4 +++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index a51071e..f1dc713 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,14 +63,24 @@ enum Commands { Serve { /// Path to the directory containing SUMMARY.md path: PathBuf, + + /// Whether the home page and navbar should be removed + #[arg(short, long)] + no_navigation: bool, + /// Port to listen on #[arg(short, long, default_value = "3456")] port: u16, + + /// Whether to serve on 0.0.0.0 (local network) + #[arg(short = 'H', long)] + host: bool, }, } struct AppState { docs_dir: PathBuf, + no_navigation: bool, } #[tokio::main] @@ -84,10 +94,18 @@ async fn main() -> anyhow::Result<()> { let cli = Cli::parse(); match cli.command { - Commands::Serve { path, port } => { + Commands::Serve { + path, + port, + host, + no_navigation, + } => { let abs_path = std::fs::canonicalize(&path)?; - let shared_state = Arc::new(AppState { docs_dir: abs_path }); + let shared_state = Arc::new(AppState { + docs_dir: abs_path, + no_navigation, + }); let app = Router::new() .route("/", get(render_summary)) @@ -95,8 +113,14 @@ async fn main() -> anyhow::Result<()> { .route("/style.css", get(serve_css)) .with_state(shared_state); - let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await?; - tracing::info!("Listening on http://localhost:{}", port); + let addr = if host { + format!("0.0.0.0:{}", port) + } else { + format!("127.0.0.1:{}", port) + }; + + let listener = tokio::net::TcpListener::bind(&addr).await?; + tracing::info!("Listening on {}", addr); axum::serve(listener, app).await?; } } @@ -105,6 +129,10 @@ async fn main() -> anyhow::Result<()> { } async fn render_summary(State(state): State>) -> impl IntoResponse { + if state.no_navigation { + return (StatusCode::NOT_FOUND, "Navigation is disabled").into_response(); + } + let mut context = Context::new(); context.insert("title", "Pages"); @@ -150,8 +178,8 @@ async fn render_summary(State(state): State>) -> impl IntoResponse context.insert("files", &pages); match TEMPLATES.render("home.html", &context) { - Ok(rendered) => Html(rendered), - Err(e) => Html(format!("

Template Error

{}
", e)), + Ok(rendered) => Html(rendered).into_response(), + Err(e) => Html(format!("

Template Error

{}
", e)).into_response(), } } @@ -198,13 +226,18 @@ async fn render_md_file(content: &String, filename: &str, state: Arc) let mut html_output = String::new(); html::push_html(&mut html_output, renderer); - let (prev_page, next_page) = get_nav_links(&state.docs_dir, filename); + let (prev_page, next_page) = if state.no_navigation { + (None, None) + } else { + get_nav_links(&state.docs_dir, filename) + }; let mut context = Context::new(); context.insert("title", filename); context.insert("content", &html_output); context.insert("prev_page", &prev_page); context.insert("next_page", &next_page); + context.insert("no_navigation", &state.no_navigation); match TEMPLATES.render("page.html", &context) { Ok(rendered) => Html(rendered), diff --git a/templates/_base.html b/templates/_base.html index abce6b4..e049f31 100644 --- a/templates/_base.html +++ b/templates/_base.html @@ -3,16 +3,19 @@ {% block head %} + {{ title }} {% endblock head %} - + + {% if not no_navigation %} + {% endif %}
{% block content %}{% endblock content %}
diff --git a/templates/style.css b/templates/style.css index f15214b..7513578 100644 --- a/templates/style.css +++ b/templates/style.css @@ -35,6 +35,7 @@ body { display: flex; justify-content: center; min-height: 100vh; + font-size: 14px; } article { @@ -212,7 +213,7 @@ input[type="checkbox"]:checked::before { } body { - padding-top: 100px; + padding-top: 75px; } nav { @@ -234,6 +235,7 @@ nav { nav a { color: var(--text-main); text-decoration: none; + font-size: 1.2rem; font-weight: 600; margin-left: 20px; transition: color 0.2s ease;