fixed and improved images, and generalized renderer
This commit is contained in:
@@ -26,8 +26,8 @@ pub enum Commands {
|
||||
Build {
|
||||
#[arg(short, long)]
|
||||
no_navigation: bool,
|
||||
#[arg(short, long)]
|
||||
out_dir: Option<PathBuf>,
|
||||
#[arg(short, long, default_value = "dist")]
|
||||
out_dir: PathBuf,
|
||||
},
|
||||
/// Output a DOT graph of the wiki connections
|
||||
Graph {},
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Parser as MarkdownParser, Tag, TagEnd};
|
||||
use pulldown_cmark_escape::escape_html;
|
||||
use syntect::html::highlighted_html_for_string;
|
||||
|
||||
use crate::{SYNTAX_SET, THEME_SET};
|
||||
|
||||
// I found this at <https://github.com/pulldown-cmark/pulldown-cmark/issues/167#issuecomment-3700787117>
|
||||
|
||||
pub struct CodeblockRenderer<'a> {
|
||||
inner: MarkdownParser<'a>,
|
||||
}
|
||||
|
||||
impl<'a> CodeblockRenderer<'a> {
|
||||
pub fn new(inner: MarkdownParser<'a>) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for CodeblockRenderer<'a> {
|
||||
type Item = Event<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let event = self.inner.next()?;
|
||||
|
||||
// Intercept CodeBlock starts
|
||||
let Event::Start(Tag::CodeBlock(kind)) = event else {
|
||||
return Some(event);
|
||||
};
|
||||
|
||||
let mut code_content = String::new();
|
||||
|
||||
while let Some(inner_event) = self.inner.next() {
|
||||
match inner_event {
|
||||
Event::End(TagEnd::CodeBlock) => break,
|
||||
Event::Text(code) => code_content.push_str(&code),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let lang = match kind {
|
||||
CodeBlockKind::Indented => "text",
|
||||
CodeBlockKind::Fenced(ref language) => language.as_ref(),
|
||||
};
|
||||
|
||||
let rendered_html = render_code_to_html(&code_content, lang);
|
||||
|
||||
let mut escaped_code = String::new();
|
||||
let _ = escape_html(&mut escaped_code, &code_content);
|
||||
|
||||
let rendered_html =
|
||||
rendered_html.replace("<pre", &format!("<pre data-code=\"{}\"", escaped_code));
|
||||
|
||||
Some(Event::Html(CowStr::Boxed(rendered_html.into_boxed_str())))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_code_to_html(code: &str, lang: &str) -> String {
|
||||
let syntax = SYNTAX_SET
|
||||
.find_syntax_by_token(lang)
|
||||
.unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
|
||||
|
||||
let theme = &THEME_SET.themes["Catppuccin Macchiato"];
|
||||
|
||||
highlighted_html_for_string(code, &SYNTAX_SET, syntax, theme)
|
||||
.unwrap_or_else(|_| format!("<pre><code>{}</code></pre>", code))
|
||||
}
|
||||
12
src/entry.rs
12
src/entry.rs
@@ -58,10 +58,11 @@ async fn list_entries(root: &PathBuf) -> Result<()> {
|
||||
fn check_file_status(root: &PathBuf, filename: Option<String>) -> String {
|
||||
match filename {
|
||||
Some(f) => {
|
||||
if root.join(&f).exists() {
|
||||
let path = root.join("images").join(&f);
|
||||
if path.exists() {
|
||||
f.green().to_string()
|
||||
} else {
|
||||
format!("{} (Missing)", f.red())
|
||||
format!("{} (Missing in images/)", f.red())
|
||||
}
|
||||
}
|
||||
None => "None".to_string(),
|
||||
@@ -88,6 +89,9 @@ async fn create_entry(root: &PathBuf, title: &str) -> Result<()> {
|
||||
let toml_path = root.join(&toml_filename);
|
||||
let md_path = root.join(&md_filename);
|
||||
|
||||
let images_dir = root.join("images");
|
||||
fs::create_dir_all(&images_dir).await?;
|
||||
|
||||
if toml_path.exists() {
|
||||
return Err(anyhow!("Entry '{}' already exists", slug));
|
||||
}
|
||||
@@ -119,7 +123,7 @@ content_file = "{}"
|
||||
println!("Created entry '{}'", title);
|
||||
println!(" - TOML: {:?}", toml_path);
|
||||
println!(" - Markdown: {:?}", md_path);
|
||||
println!(" - (Expected image: {:?})", img_filename);
|
||||
println!(" - (Expected image: {:?})", images_dir.join(img_filename));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -151,7 +155,7 @@ async fn remove_entry(root: &PathBuf, name: &str) -> Result<()> {
|
||||
let p = root.join(&img_file);
|
||||
if p.exists() {
|
||||
fs::remove_file(&p).await?;
|
||||
println!("Removed: {}", img_file);
|
||||
println!("Removed: images/{}", img_file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
23
src/main.rs
23
src/main.rs
@@ -13,7 +13,6 @@ use tera::{Context, Tera};
|
||||
|
||||
mod analysis;
|
||||
mod cli;
|
||||
mod codeblocks;
|
||||
mod entry;
|
||||
mod rendering;
|
||||
|
||||
@@ -99,6 +98,10 @@ async fn main() -> anyhow::Result<()> {
|
||||
.route("/", get(render_summary_handler))
|
||||
.route("/{page}", get(render_page_handler))
|
||||
.route("/style.css", get(serve_css))
|
||||
.nest_service(
|
||||
"/images",
|
||||
tower_http::services::ServeDir::new(&shared_state.docs_dir.join("images")),
|
||||
)
|
||||
.nest_service(
|
||||
"/assets",
|
||||
tower_http::services::ServeDir::new(&shared_state.docs_dir),
|
||||
@@ -118,7 +121,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
no_navigation,
|
||||
out_dir,
|
||||
} => {
|
||||
let output_path = out_dir.unwrap_or_else(|| abs_path.clone());
|
||||
let output_path = out_dir;
|
||||
tokio::fs::create_dir_all(&output_path).await?;
|
||||
run_build(abs_path, output_path, no_navigation).await?;
|
||||
}
|
||||
@@ -206,6 +209,22 @@ async fn run_build(docs_dir: PathBuf, out_dir: PathBuf, no_navigation: bool) ->
|
||||
}
|
||||
}
|
||||
|
||||
let out_images = out_dir.join("images");
|
||||
tokio::fs::create_dir_all(&out_images).await?;
|
||||
|
||||
let src_images = docs_dir.join("images");
|
||||
if src_images.exists() {
|
||||
let mut entries = tokio::fs::read_dir(&src_images).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
tracing::info!("Copied {}", entry.file_name().display(),);
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
let dest = out_images.join(path.file_name().unwrap());
|
||||
tokio::fs::copy(path, dest).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut entries = tokio::fs::read_dir(&docs_dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
|
||||
@@ -3,15 +3,78 @@ use axum::{
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
use pulldown_cmark::{Options, Parser as MarkdownParser, html};
|
||||
use pulldown_cmark::{
|
||||
CodeBlockKind, CowStr, Event, Options, Parser as MarkdownParser, Tag, TagEnd, html,
|
||||
};
|
||||
use pulldown_cmark_escape::escape_html;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use syntect::html::highlighted_html_for_string;
|
||||
use tera::Context;
|
||||
|
||||
use crate::{
|
||||
AppState, TEMPLATES, WikiConfig, codeblocks::CodeblockRenderer, get_nav_links, get_summary_data,
|
||||
AppState, SYNTAX_SET, TEMPLATES, THEME_SET, WikiConfig, get_nav_links, get_summary_data,
|
||||
};
|
||||
|
||||
pub struct Renderer<'a> {
|
||||
inner: MarkdownParser<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Renderer<'a> {
|
||||
pub fn new(inner: MarkdownParser<'a>) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Renderer<'a> {
|
||||
type Item = Event<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let event = self.inner.next()?;
|
||||
|
||||
// Intercept CodeBlock starts
|
||||
let Event::Start(Tag::CodeBlock(kind)) = event else {
|
||||
return Some(event);
|
||||
};
|
||||
|
||||
let mut code_content = String::new();
|
||||
|
||||
while let Some(inner_event) = self.inner.next() {
|
||||
match inner_event {
|
||||
Event::End(TagEnd::CodeBlock) => break,
|
||||
Event::Text(code) => code_content.push_str(&code),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let lang = match kind {
|
||||
CodeBlockKind::Indented => "text",
|
||||
CodeBlockKind::Fenced(ref language) => language.as_ref(),
|
||||
};
|
||||
|
||||
let rendered_html = render_code_to_html(&code_content, lang);
|
||||
|
||||
let mut escaped_code = String::new();
|
||||
let _ = escape_html(&mut escaped_code, &code_content);
|
||||
|
||||
let rendered_html =
|
||||
rendered_html.replace("<pre", &format!("<pre data-code=\"{}\"", escaped_code));
|
||||
|
||||
Some(Event::Html(CowStr::Boxed(rendered_html.into_boxed_str())))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_code_to_html(code: &str, lang: &str) -> String {
|
||||
let syntax = SYNTAX_SET
|
||||
.find_syntax_by_token(lang)
|
||||
.unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
|
||||
|
||||
let theme = &THEME_SET.themes["Catppuccin Macchiato"];
|
||||
|
||||
highlighted_html_for_string(code, &SYNTAX_SET, syntax, theme)
|
||||
.unwrap_or_else(|_| format!("<pre><code>{}</code></pre>", code))
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct InfoboxItem {
|
||||
key: String,
|
||||
@@ -64,7 +127,7 @@ pub async fn render_wiki_page(
|
||||
);
|
||||
|
||||
let parser = MarkdownParser::new_ext(&markdown_content, options);
|
||||
let renderer = CodeblockRenderer::new(parser);
|
||||
let renderer = Renderer::new(parser);
|
||||
let mut html_output = String::new();
|
||||
html::push_html(&mut html_output, renderer);
|
||||
|
||||
@@ -93,11 +156,13 @@ pub async fn render_wiki_page(
|
||||
None => Vec::new(),
|
||||
};
|
||||
|
||||
let main_image_path = config.image.as_ref().map(|img| format!("images/{}", img));
|
||||
|
||||
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("main_image", &main_image_path);
|
||||
context.insert("prev_page", &prev);
|
||||
context.insert("next_page", &next);
|
||||
context.insert("no_navigation", &no_navigation);
|
||||
|
||||
Reference in New Issue
Block a user