added graph and todo commands, and reorganized project structure
This commit is contained in:
132
src/analysis.rs
Normal file
132
src/analysis.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
use anyhow::{Context, Result};
|
||||
use pulldown_cmark::{Event, Parser, Tag};
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::WikiConfig;
|
||||
|
||||
pub struct WikiGraph {
|
||||
pub nodes: HashMap<String, WikiConfig>,
|
||||
pub edges: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
impl WikiGraph {
|
||||
pub async fn new(docs_dir: PathBuf) -> Result<Self> {
|
||||
let mut nodes = HashMap::new();
|
||||
let mut raw_files = HashMap::new();
|
||||
|
||||
// Scan for all TOML files
|
||||
let mut entries = tokio::fs::read_dir(&docs_dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|s| s.to_str()) == Some("toml") {
|
||||
let content = tokio::fs::read_to_string(&path).await?;
|
||||
let config: WikiConfig = toml::from_str(&content)
|
||||
.with_context(|| format!("Failed to parse {:?}", path))?;
|
||||
|
||||
let slug = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
nodes.insert(slug.clone(), config.clone());
|
||||
raw_files.insert(slug, config);
|
||||
}
|
||||
}
|
||||
|
||||
// Scan content for links
|
||||
let mut edges = HashMap::new();
|
||||
for (slug, config) in &raw_files {
|
||||
let mut page_links = Vec::new();
|
||||
|
||||
if let Some(content_file) = &config.content_file {
|
||||
let md_path = docs_dir.join(content_file);
|
||||
if md_path.exists() {
|
||||
let md_content = tokio::fs::read_to_string(&md_path).await?;
|
||||
let links = extract_markdown_links(&md_content);
|
||||
|
||||
for link in links {
|
||||
// Normalize link
|
||||
let target_slug = normalize_link(&link);
|
||||
// Only add if not a self-link
|
||||
if target_slug != *slug {
|
||||
page_links.push(target_slug);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
edges.insert(slug.clone(), page_links);
|
||||
}
|
||||
|
||||
Ok(Self { nodes, edges })
|
||||
}
|
||||
|
||||
pub fn print_dot(&self) {
|
||||
println!("digraph Wiki {{");
|
||||
println!(" graph [layout=neato, overlap=false, splines=true];");
|
||||
println!(" node [shape=box, style=\"filled,rounded\", fontname=\"Helvetica\"];");
|
||||
|
||||
// Nodes
|
||||
for (slug, config) in &self.nodes {
|
||||
println!(
|
||||
" \"{}\" [label=\"{}\", href=\"{}.html\"];",
|
||||
slug, config.title, slug
|
||||
);
|
||||
}
|
||||
|
||||
println!("");
|
||||
|
||||
// Edges
|
||||
for (source, targets) in &self.edges {
|
||||
for target in targets {
|
||||
if self.nodes.contains_key(target) {
|
||||
println!(" \"{}\" -> \"{}\";", source, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("}}");
|
||||
}
|
||||
|
||||
pub fn check_dead_links(&self) {
|
||||
let mut found_issues = false;
|
||||
|
||||
for (source, targets) in &self.edges {
|
||||
for target in targets {
|
||||
if !self.nodes.contains_key(target) {
|
||||
println!("\x1b[1m{}\x1b[0m -> ❌ \x1b[31m{}\x1b[0m", source, target);
|
||||
found_issues = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found_issues {
|
||||
println!("✅ No broken links found!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_markdown_links(content: &str) -> Vec<String> {
|
||||
let parser = Parser::new(content);
|
||||
let mut links = Vec::new();
|
||||
|
||||
for event in parser {
|
||||
if let Event::Start(Tag::Link { dest_url, .. }) = event {
|
||||
let url = dest_url.to_string();
|
||||
// Filter out external links
|
||||
if !url.starts_with("http") && !url.starts_with("mailto:") && !url.starts_with('#') {
|
||||
links.push(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
links
|
||||
}
|
||||
|
||||
fn normalize_link(link: &str) -> String {
|
||||
let path = Path::new(link);
|
||||
// Remove extension
|
||||
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(link);
|
||||
// Remove leading ./
|
||||
stem.trim_start_matches("./").to_string()
|
||||
}
|
||||
44
src/cli.rs
Normal file
44
src/cli.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "A simple wiki server/builder")]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Serve the wiki locally
|
||||
Serve {
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
#[arg(short, long)]
|
||||
no_navigation: bool,
|
||||
#[arg(short = 'P', long, default_value = "8090")]
|
||||
port: u16,
|
||||
#[arg(short = 'H', long)]
|
||||
host: bool,
|
||||
},
|
||||
/// Build the static site
|
||||
Build {
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
#[arg(short, long)]
|
||||
no_navigation: bool,
|
||||
#[arg(short, long)]
|
||||
out_dir: Option<PathBuf>,
|
||||
},
|
||||
/// Output a DOT graph of the wiki connections
|
||||
Graph {
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
},
|
||||
/// List broken links
|
||||
Todo {
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
},
|
||||
}
|
||||
219
src/main.rs
219
src/main.rs
@@ -1,21 +1,27 @@
|
||||
use ax_models::{Page, WikiConfig};
|
||||
use axum::{
|
||||
Router,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
};
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap::Parser;
|
||||
use lazy_static::lazy_static;
|
||||
use pulldown_cmark::{Options, Parser as MarkdownParser, html};
|
||||
use std::sync::Arc;
|
||||
use std::{io::Cursor, path::PathBuf};
|
||||
use syntect::{highlighting::ThemeSet, parsing::SyntaxSet};
|
||||
use tera::{Context, Tera};
|
||||
|
||||
mod analysis;
|
||||
mod cli;
|
||||
mod codeblocks;
|
||||
use codeblocks::*;
|
||||
mod rendering;
|
||||
|
||||
use crate::{
|
||||
cli::{Cli, Commands},
|
||||
rendering::{render_page_handler, render_summary_handler, render_wiki_page},
|
||||
};
|
||||
use indexmap::IndexMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TEMPLATES: Tera = {
|
||||
@@ -46,33 +52,19 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "A simple wiki server/builder")]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
pub struct Page {
|
||||
pub filename: String,
|
||||
pub title: String,
|
||||
pub datetime: String,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Serve {
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
#[arg(short, long)]
|
||||
no_navigation: bool,
|
||||
#[arg(short = 'P', long, default_value = "8090")]
|
||||
port: u16,
|
||||
#[arg(short = 'H', long)]
|
||||
host: bool,
|
||||
},
|
||||
Build {
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
#[arg(short, long)]
|
||||
no_navigation: bool,
|
||||
#[arg(short, long)]
|
||||
out_dir: Option<PathBuf>,
|
||||
},
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
pub struct WikiConfig {
|
||||
pub title: String,
|
||||
pub image: Option<String>,
|
||||
pub infobox: Option<IndexMap<String, String>>,
|
||||
pub content_file: Option<String>,
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
@@ -105,7 +97,6 @@ async fn main() -> anyhow::Result<()> {
|
||||
.route("/", get(render_summary_handler))
|
||||
.route("/{page}", get(render_page_handler))
|
||||
.route("/style.css", get(serve_css))
|
||||
// Serve images relative to the docs directory
|
||||
.nest_service(
|
||||
"/assets",
|
||||
tower_http::services::ServeDir::new(&shared_state.docs_dir),
|
||||
@@ -131,6 +122,16 @@ async fn main() -> anyhow::Result<()> {
|
||||
tokio::fs::create_dir_all(&output_path).await?;
|
||||
run_build(abs_path, output_path, no_navigation).await?;
|
||||
}
|
||||
Commands::Graph { path } => {
|
||||
let abs_path = std::fs::canonicalize(&path)?;
|
||||
let graph = analysis::WikiGraph::new(abs_path).await?;
|
||||
graph.print_dot();
|
||||
}
|
||||
Commands::Todo { path } => {
|
||||
let abs_path = std::fs::canonicalize(&path)?;
|
||||
let graph = analysis::WikiGraph::new(abs_path).await?;
|
||||
graph.check_dead_links();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -140,7 +141,6 @@ async fn get_summary_data(docs_dir: &PathBuf) -> Vec<Page> {
|
||||
if let Ok(mut entries) = tokio::fs::read_dir(docs_dir).await {
|
||||
while let Ok(Some(entry)) = entries.next_entry().await {
|
||||
let path = entry.path();
|
||||
// We now look for TOML files as the entry points
|
||||
if path.extension().and_then(|s| s.to_str()) != Some("toml") {
|
||||
continue;
|
||||
}
|
||||
@@ -148,7 +148,6 @@ async fn get_summary_data(docs_dir: &PathBuf) -> Vec<Page> {
|
||||
let filename = entry.file_name();
|
||||
let filename_str = filename.to_str().unwrap_or("");
|
||||
|
||||
// Read TOML to get the title
|
||||
let title = if let Ok(content) = tokio::fs::read_to_string(&path).await {
|
||||
if let Ok(config) = toml::from_str::<WikiConfig>(&content) {
|
||||
config.title
|
||||
@@ -159,13 +158,10 @@ async fn get_summary_data(docs_dir: &PathBuf) -> Vec<Page> {
|
||||
filename_str.to_string()
|
||||
};
|
||||
|
||||
// TODO:
|
||||
let datetime = "".to_string();
|
||||
|
||||
pages.push(Page {
|
||||
filename: filename_str.to_string(), // Keep .toml extension here for now
|
||||
filename: filename_str.to_string(),
|
||||
title,
|
||||
datetime,
|
||||
datetime: "".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -173,89 +169,9 @@ async fn get_summary_data(docs_dir: &PathBuf) -> Vec<Page> {
|
||||
pages
|
||||
}
|
||||
|
||||
async fn render_wiki_page(
|
||||
filename: &str,
|
||||
docs_dir: &PathBuf,
|
||||
no_navigation: bool,
|
||||
is_static: bool,
|
||||
) -> Result<String, String> {
|
||||
let toml_path = docs_dir.join(filename);
|
||||
let toml_content = tokio::fs::read_to_string(&toml_path)
|
||||
.await
|
||||
.map_err(|_| "Page configuration not found".to_string())?;
|
||||
|
||||
let config: WikiConfig =
|
||||
toml::from_str(&toml_content).map_err(|e| format!("Invalid TOML configuration: {}", e))?;
|
||||
|
||||
let markdown_content = if let Some(md_file) = &config.content_file {
|
||||
let md_path = docs_dir.join(md_file);
|
||||
tokio::fs::read_to_string(&md_path)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
"# Content missing\nThe linked markdown file could not be found.".to_string()
|
||||
})
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
// Render Markdown
|
||||
let mut options = Options::empty();
|
||||
options.insert(
|
||||
Options::ENABLE_TABLES
|
||||
| Options::ENABLE_FOOTNOTES
|
||||
| Options::ENABLE_STRIKETHROUGH
|
||||
| Options::ENABLE_TASKLISTS,
|
||||
);
|
||||
|
||||
let parser = MarkdownParser::new_ext(&markdown_content, options);
|
||||
let renderer = CodeblockRenderer::new(parser);
|
||||
let mut html_output = String::new();
|
||||
html::push_html(&mut html_output, renderer);
|
||||
|
||||
let (mut prev, mut next) = if no_navigation {
|
||||
(None, None)
|
||||
} else {
|
||||
get_nav_links(docs_dir, filename)
|
||||
};
|
||||
|
||||
if is_static {
|
||||
prev = prev.map(|s| {
|
||||
if s == "." {
|
||||
"index.html".to_string()
|
||||
} else {
|
||||
s.replace(".toml", ".html")
|
||||
}
|
||||
});
|
||||
next = next.map(|s| s.replace(".toml", ".html"));
|
||||
}
|
||||
|
||||
let infobox_list: Vec<InfoboxItem> = match config.infobox {
|
||||
Some(map) => map
|
||||
.into_iter()
|
||||
.map(|(k, v)| InfoboxItem { key: k, value: v })
|
||||
.collect(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
|
||||
let mut context = Context::new();
|
||||
context.insert("title", &config.title);
|
||||
context.insert("content", &html_output);
|
||||
context.insert("infobox", &infobox_list); // Pass the ordered list, not the map
|
||||
context.insert("main_image", &config.image);
|
||||
context.insert("prev_page", &prev);
|
||||
context.insert("next_page", &next);
|
||||
context.insert("no_navigation", &no_navigation);
|
||||
context.insert("is_static", &is_static);
|
||||
|
||||
TEMPLATES
|
||||
.render("page.html", &context)
|
||||
.map_err(|e| format!("Template Error: {}", e))
|
||||
}
|
||||
|
||||
async fn run_build(docs_dir: PathBuf, out_dir: PathBuf, no_navigation: bool) -> anyhow::Result<()> {
|
||||
tracing::info!("Building static site to: {:?}", out_dir);
|
||||
|
||||
// Build summary
|
||||
if !no_navigation {
|
||||
let pages = get_summary_data(&docs_dir).await;
|
||||
let static_pages: Vec<Page> = pages
|
||||
@@ -275,13 +191,9 @@ async fn run_build(docs_dir: PathBuf, out_dir: PathBuf, no_navigation: bool) ->
|
||||
tokio::fs::write(out_dir.join("index.html"), rendered).await?;
|
||||
}
|
||||
|
||||
// Build css
|
||||
let css = TEMPLATES.render("style.css", &Context::new())?;
|
||||
tokio::fs::write(out_dir.join("style.css"), css).await?;
|
||||
|
||||
// Copy assets (images, etc)
|
||||
// NOTE: In a real app you'd recursively copy everything not .md/.toml
|
||||
// For now we just copy files that look like images if they are in root
|
||||
let mut entries = tokio::fs::read_dir(&docs_dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
@@ -293,7 +205,6 @@ async fn run_build(docs_dir: PathBuf, out_dir: PathBuf, no_navigation: bool) ->
|
||||
}
|
||||
}
|
||||
|
||||
// Build pages
|
||||
let mut entries = tokio::fs::read_dir(&docs_dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
@@ -315,38 +226,6 @@ async fn run_build(docs_dir: PathBuf, out_dir: PathBuf, no_navigation: bool) ->
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn render_summary_handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
if state.no_navigation {
|
||||
return (StatusCode::NOT_FOUND, "Disabled").into_response();
|
||||
}
|
||||
let pages = get_summary_data(&state.docs_dir).await;
|
||||
let mut context = Context::new();
|
||||
context.insert("title", "Wiki Index");
|
||||
context.insert("files", &pages);
|
||||
context.insert("is_static", &false);
|
||||
|
||||
match TEMPLATES.render("home.html", &context) {
|
||||
Ok(rendered) => Html(rendered).into_response(),
|
||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn render_page_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(page): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
let filename = if page.ends_with(".toml") {
|
||||
page
|
||||
} else {
|
||||
format!("{}.toml", page)
|
||||
};
|
||||
|
||||
match render_wiki_page(&filename, &state.docs_dir, state.no_navigation, false).await {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(e) => (StatusCode::NOT_FOUND, format!("<h1>404</h1><p>{}</p>", e)).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_css() -> impl IntoResponse {
|
||||
match TEMPLATES.render("style.css", &Context::new()) {
|
||||
Ok(css) => Response::builder()
|
||||
@@ -357,34 +236,6 @@ async fn serve_css() -> impl IntoResponse {
|
||||
}
|
||||
}
|
||||
|
||||
mod ax_models {
|
||||
use indexmap::IndexMap;
|
||||
use serde::{Deserialize, Serialize}; // Use IndexMap instead of BTreeMap
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
pub struct Page {
|
||||
pub filename: String,
|
||||
pub title: String,
|
||||
pub datetime: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
pub struct WikiConfig {
|
||||
pub title: String,
|
||||
pub image: Option<String>,
|
||||
// IndexMap preserves the order from the file
|
||||
pub infobox: Option<IndexMap<String, String>>,
|
||||
pub content_file: Option<String>,
|
||||
}
|
||||
}
|
||||
|
||||
// Helper struct for the template
|
||||
#[derive(serde::Serialize)]
|
||||
struct InfoboxItem {
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
fn get_nav_links(dir: &PathBuf, current_file: &str) -> (Option<String>, Option<String>) {
|
||||
let mut files: Vec<String> = std::fs::read_dir(dir)
|
||||
.unwrap()
|
||||
|
||||
129
src/rendering.rs
Normal file
129
src/rendering.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
use pulldown_cmark::{Options, Parser as MarkdownParser, html};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tera::Context;
|
||||
|
||||
use crate::{
|
||||
AppState, TEMPLATES, WikiConfig, codeblocks::CodeblockRenderer, get_nav_links, get_summary_data,
|
||||
};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct InfoboxItem {
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
pub async fn render_wiki_page(
|
||||
filename: &str,
|
||||
docs_dir: &PathBuf,
|
||||
no_navigation: bool,
|
||||
is_static: bool,
|
||||
) -> Result<String, String> {
|
||||
let toml_path = docs_dir.join(filename);
|
||||
let toml_content = tokio::fs::read_to_string(&toml_path)
|
||||
.await
|
||||
.map_err(|_| "Page configuration not found".to_string())?;
|
||||
|
||||
let config: WikiConfig =
|
||||
toml::from_str(&toml_content).map_err(|e| format!("Invalid TOML configuration: {}", e))?;
|
||||
|
||||
let markdown_content = if let Some(md_file) = &config.content_file {
|
||||
let md_path = docs_dir.join(md_file);
|
||||
tokio::fs::read_to_string(&md_path)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
"# Content missing\nThe linked markdown file could not be found.".to_string()
|
||||
})
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let mut options = Options::empty();
|
||||
options.insert(
|
||||
Options::ENABLE_TABLES
|
||||
| Options::ENABLE_FOOTNOTES
|
||||
| Options::ENABLE_STRIKETHROUGH
|
||||
| Options::ENABLE_TASKLISTS,
|
||||
);
|
||||
|
||||
let parser = MarkdownParser::new_ext(&markdown_content, options);
|
||||
let renderer = CodeblockRenderer::new(parser);
|
||||
let mut html_output = String::new();
|
||||
html::push_html(&mut html_output, renderer);
|
||||
|
||||
let (mut prev, mut next) = if no_navigation {
|
||||
(None, None)
|
||||
} else {
|
||||
get_nav_links(docs_dir, filename)
|
||||
};
|
||||
|
||||
if is_static {
|
||||
prev = prev.map(|s| {
|
||||
if s == "." {
|
||||
"index.html".to_string()
|
||||
} else {
|
||||
s.replace(".toml", ".html")
|
||||
}
|
||||
});
|
||||
next = next.map(|s| s.replace(".toml", ".html"));
|
||||
}
|
||||
|
||||
let infobox_list: Vec<InfoboxItem> = match config.infobox {
|
||||
Some(map) => map
|
||||
.into_iter()
|
||||
.map(|(k, v)| InfoboxItem { key: k, value: v })
|
||||
.collect(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
|
||||
let mut context = Context::new();
|
||||
context.insert("title", &config.title);
|
||||
context.insert("content", &html_output);
|
||||
context.insert("infobox", &infobox_list);
|
||||
context.insert("main_image", &config.image);
|
||||
context.insert("prev_page", &prev);
|
||||
context.insert("next_page", &next);
|
||||
context.insert("no_navigation", &no_navigation);
|
||||
context.insert("is_static", &is_static);
|
||||
|
||||
TEMPLATES
|
||||
.render("page.html", &context)
|
||||
.map_err(|e| format!("Template Error: {}", e))
|
||||
}
|
||||
|
||||
pub async fn render_summary_handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
if state.no_navigation {
|
||||
return (StatusCode::NOT_FOUND, "Disabled").into_response();
|
||||
}
|
||||
let pages = get_summary_data(&state.docs_dir).await;
|
||||
let mut context = Context::new();
|
||||
context.insert("title", "Wiki Index");
|
||||
context.insert("files", &pages);
|
||||
context.insert("is_static", &false);
|
||||
|
||||
match TEMPLATES.render("home.html", &context) {
|
||||
Ok(rendered) => Html(rendered).into_response(),
|
||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn render_page_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(page): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
let filename = if page.ends_with(".toml") {
|
||||
page
|
||||
} else {
|
||||
format!("{}.toml", page)
|
||||
};
|
||||
|
||||
match render_wiki_page(&filename, &state.docs_dir, state.no_navigation, false).await {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(e) => (StatusCode::NOT_FOUND, format!("<h1>404</h1><p>{}</p>", e)).into_response(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user