fix clippy lints

This commit is contained in:
Maciej Jur 2024-04-22 21:45:53 +02:00
parent 764c6f44f0
commit b09a32733c
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
10 changed files with 166 additions and 143 deletions

View file

@ -49,7 +49,6 @@ fn to_source(path: Utf8PathBuf, exts: &HashSet<&'static str>) -> Source {
pub fn gather(pattern: &str, exts: &HashSet<&'static str>) -> Vec<Source> {
glob(pattern)
.unwrap()
.into_iter()
.filter_map(|path| {
let path = path.unwrap();
let path = Utf8PathBuf::from_path_buf(path).expect("Filename is not valid UTF8");

View file

@ -1,5 +1,32 @@
mod load;
mod render;
mod sack;
use camino::Utf8PathBuf;
use hypertext::Renderable;
pub use load::{gather, Source, SourceKind};
pub use render::{render, Asset, AssetKind, Virtual, Item};
pub use sack::{TreePage, Sack};
use crate::{html::Linkable, text::md::Outline};
/// Represents a piece of content that can be rendered into a page.
pub trait Content {
fn transform<'f, 'm, 's, 'html, T>(
&'f self,
content: T,
outline: Outline,
sack: &'s Sack,
) -> impl Renderable + 'html
where
'f: 'html,
'm: 'html,
's: 'html,
T: Renderable + 'm;
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable>;
fn render(data: &str) -> (Outline, String);
}

View file

@ -18,10 +18,10 @@ pub enum AssetKind {
impl Debug for AssetKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Html(ptr) => f.debug_tuple("Html").field(&"ptr").finish(),
Self::Html(ptr) => f.debug_tuple("Html").field(&format!("{:p}", *ptr)).finish(),
Self::Image => write!(f, "Image"),
Self::Unknown => write!(f, "Unknown"),
Self::Bib(arg0) => f.debug_tuple("Bib").field(arg0).finish(),
Self::Bib(bib) => f.debug_tuple("Bib").field(bib).finish(),
}
}
}
@ -66,62 +66,62 @@ impl From<Virtual> for Item {
pub fn render(items: &[Item]) {
let assets: Vec<&Asset> = items
.into_iter()
.iter()
.filter_map(|item| match item {
Item::Real(a) => Some(a),
Item::Fake(_) => None,
})
.collect();
let everything = Sack { assets: &assets };
let sack = Sack::new(&assets);
for item in items {
match item {
Item::Real(real) => render_real(real, &everything),
Item::Fake(fake) => render_fake(fake, &everything),
Item::Real(real) => render_real(real, &sack),
Item::Fake(fake) => render_fake(fake, &sack),
}
}
}
fn render_real(item: &Asset, assets: &Sack) {
fn render_real(item: &Asset, sack: &Sack) {
match &item.kind {
AssetKind::Html(render) => {
let i = &item.meta.path;
let o = Utf8Path::new("dist").join(&item.out);
fs::create_dir_all(&o.parent().unwrap()).unwrap();
fs::create_dir_all(o.parent().unwrap()).unwrap();
let mut file = File::create(&o).unwrap();
file.write_all(render(assets).as_bytes()).unwrap();
file.write_all(render(sack).as_bytes()).unwrap();
println!("HTML: {} -> {}", i, o);
},
AssetKind::Image => {
let i = &item.meta.path;
let o = Utf8Path::new("dist").join(&item.out);
fs::create_dir_all(&o.parent().unwrap()).unwrap();
fs::copy(&i, &o).unwrap();
fs::create_dir_all(o.parent().unwrap()).unwrap();
fs::copy(i, &o).unwrap();
println!("Image: {} -> {}", i, o);
},
AssetKind::Bib(_) => (),
AssetKind::Unknown => {
let i = &item.meta.path;
let o = Utf8Path::new("dist").join(&item.out);
fs::create_dir_all(&o.parent().unwrap()).unwrap();
fs::copy(&i, &o).unwrap();
fs::create_dir_all(o.parent().unwrap()).unwrap();
fs::copy(i, &o).unwrap();
println!("Unknown: {} -> {}", i, o);
},
}
}
fn render_fake(item: &Virtual, assets: &Sack) {
fn render_fake(item: &Virtual, sack: &Sack) {
let Virtual(out, render) = item;
let o = Utf8Path::new("dist").join(&out);
fs::create_dir_all(&o.parent().unwrap()).unwrap();
let o = Utf8Path::new("dist").join(out);
fs::create_dir_all(o.parent().unwrap()).unwrap();
let mut file = File::create(&o).unwrap();
file.write_all(render(assets).as_bytes()).unwrap();
file.write_all(render(sack).as_bytes()).unwrap();
println!("Virtual: -> {}", o);
}

72
src/gen/sack.rs Normal file
View file

@ -0,0 +1,72 @@
use std::collections::HashMap;
use crate::html::{Link, LinkDate, Linkable};
use super::Asset;
#[derive(Debug)]
pub struct TreePage {
pub link: Option<Link>,
pub subs: HashMap<String, TreePage>,
}
impl TreePage {
fn new() -> Self {
TreePage {
link: None,
subs: HashMap::new(),
}
}
fn add_link(&mut self, link: &Link) {
let mut ptr = self;
for part in link.path.iter().skip(1) {
ptr = ptr.subs
.entry(part.to_string())
.or_insert(TreePage::new());
}
ptr.link = Some(link.clone());
}
}
/// This struct allows for querying the website hierarchy.
#[derive(Debug)]
pub struct Sack<'a> {
assets: &'a [&'a Asset],
}
impl<'a> Sack<'a> {
pub fn new(assets: &'a [&'a Asset]) -> Self {
Self { assets }
}
pub fn get_links(&self, path: &str) -> Vec<LinkDate> {
let pattern = glob::Pattern::new(path).unwrap();
self.assets.iter()
.filter(|f| pattern.matches_path(f.out.as_ref()))
.filter_map(|f| match &f.link {
Some(Linkable::Date(link)) => Some(link.clone()),
_ => None,
})
.collect()
}
pub fn get_tree(&self, path: &str) -> TreePage {
let glob = glob::Pattern::new(path).unwrap();
let list = self.assets.iter()
.filter(|f| glob.matches_path(f.out.as_ref()))
.filter_map(|f| match &f.link {
Some(Linkable::Link(link)) => Some(link.clone()),
_ => None,
});
let mut tree = TreePage::new();
for link in list {
tree.add_link(&link);
};
tree
}
}

View file

@ -1,4 +1,3 @@
use chrono::{self, Datelike};
use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable};
use crate::REPO;

View file

@ -1,3 +1,4 @@
use camino::Utf8PathBuf;
use chrono::{DateTime, Utc};
use hypertext::{html_elements, maud_move, GlobalAttributes, Renderable};
use crate::html::page;
@ -5,7 +6,7 @@ use crate::html::page;
#[derive(Debug, Clone)]
pub struct Link {
pub path: String,
pub path: Utf8PathBuf,
pub name: String,
pub desc: Option<String>,
}
@ -63,7 +64,7 @@ fn section(year: i32, group: &[LinkDate]) -> impl Renderable + '_ {
fn link(data: &LinkDate) -> impl Renderable + '_ {
let time = data.date.format("%m/%d");
maud_move!(
a .page-item href=(&data.link.path) {
a .page-item href=(data.link.path.as_str()) {
div .page-item__header {
h3 {
(&data.link.name)

View file

@ -1,45 +1,16 @@
use std::collections::HashMap;
use hypertext::{html_elements, maud_move, GlobalAttributes, Renderable};
use crate::md::Wiki;
use crate::html::page;
use crate::text::md::Outline;
use crate::Sack;
use super::Link;
use crate::{
gen::{Sack, TreePage},
html::page,
md::Wiki,
text::md::Outline
};
#[derive(Debug)]
struct TreeNode {
pub link: Option<Link>,
pub subs: HashMap<String, TreeNode>,
}
impl TreeNode {
fn new() -> Self {
TreeNode {
link: None,
subs: HashMap::new(),
}
}
fn add_link(&mut self, link: &Link) {
let mut ptr = self;
for part in link.path.split('/').filter(|s| !s.is_empty()) {
ptr = ptr.subs
.entry(part.to_string())
.or_insert(TreeNode::new());
}
ptr.link = Some(link.clone());
}
}
fn tree(sack: &Sack) -> impl Renderable {
let mut tree = TreeNode::new();
for link in sack.get_links_2("wiki/**/*.html") {
tree.add_link(&link);
};
let tree = sack.get_tree("wiki/**/*.html");
maud_move!(
h2 .link-tree__heading {
@ -56,7 +27,7 @@ fn tree(sack: &Sack) -> impl Renderable {
)
}
fn list(tree: &TreeNode) -> impl Renderable + '_ {
fn list(tree: &TreePage) -> impl Renderable + '_ {
let subs = {
let mut subs: Vec<_> = tree.subs.iter().collect();
subs.sort_by(|a, b| a.0.cmp(b.0));
@ -69,7 +40,7 @@ fn list(tree: &TreeNode) -> impl Renderable + '_ {
li .link-tree__nav-list-item {
span .link-tree__nav-list-text {
@if let Some(ref link) = next.link {
a .link-tree__nav-list-text.link href=(&link.path) {
a .link-tree__nav-list-text.link href=(link.path.as_str()) {
(&link.name)
}
} @else {
@ -78,7 +49,7 @@ fn list(tree: &TreeNode) -> impl Renderable + '_ {
}
}
}
@if next.subs.len() > 0 {
@if !next.subs.is_empty() {
(list(next))
}
}
@ -91,7 +62,7 @@ fn list(tree: &TreeNode) -> impl Renderable + '_ {
pub fn wiki<'data, 'html, 'sack, T>(
fm: &'data Wiki,
content: T,
outline: Outline,
_: Outline,
sack: &'sack Sack,
) -> impl Renderable + 'html
where

View file

@ -1,8 +1,10 @@
use std::process::Command;
use std::{collections::HashMap, path::Path};
use std::collections::HashMap;
use std::fs;
use camino::{Utf8Path, Utf8PathBuf};
use chrono::Datelike;
use grass;
use gen::{Asset, Sack, Content};
use html::{Link, LinkDate, Linkable};
use hypertext::{Raw, Renderable};
use once_cell::sync::Lazy;
@ -45,74 +47,26 @@ static REPO: Lazy<BuildInfo> = Lazy::new(|| {
}
});
/// This struct allows for querying the website hierarchy
#[derive(Debug)]
struct Sack<'a> {
assets: &'a [&'a gen::Asset],
}
impl Sack<'_> {
fn get_links(&self, path: &str) -> Vec<LinkDate> {
let pattern = glob::Pattern::new(path).unwrap();
self.assets.iter()
.filter(|f| pattern.matches_path(f.out.as_ref()))
.filter_map(|f| match &f.link {
Some(Linkable::Date(link)) => Some(link.clone()),
_ => None,
})
.collect()
}
fn get_links_2(&self, path: &str) -> Vec<Link> {
let pattern = glob::Pattern::new(path).unwrap();
self.assets.iter()
.filter(|f| pattern.matches_path(f.out.as_ref()))
.filter_map(|f| match &f.link {
Some(Linkable::Link(link)) => Some(link.clone()),
_ => None,
})
.collect()
}
}
trait Transformable {
fn transform<'f, 'm, 'html, 'sack, T>(
impl Content for md::Post {
fn transform<'f, 'm, 's, 'html, T>(
&'f self,
content: T,
outline: Outline,
sack: &'sack Sack,
_: &'s Sack,
) -> impl Renderable + 'html
where
'f: 'html,
'm: 'html,
'sack: 'html,
T: Renderable + 'm;
fn as_link(&self, path: String) -> Option<Linkable>;
fn render(data: &str) -> (Outline, String);
}
impl Transformable for md::Post {
fn transform<'f, 'm, 'html, 'sack, T>(
&'f self,
content: T,
outline: Outline,
sack: &'sack Sack,
) -> impl Renderable + 'html
where
'f: 'html,
'm: 'html,
'sack: 'html,
's: 'html,
T: Renderable + 'm {
html::post(self, content, outline)
}
fn as_link(&self, path: String) -> Option<Linkable> {
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
Some(Linkable::Date(LinkDate {
link: Link {
path: path.strip_suffix("index.html").unwrap().to_owned(),
path,
name: self.title.to_owned(),
desc: self.desc.to_owned(),
},
@ -125,25 +79,25 @@ impl Transformable for md::Post {
}
}
impl Transformable for md::Slide {
fn transform<'f, 'm, 'html, 'sack, T>(
impl Content for md::Slide {
fn transform<'f, 'm, 's, 'html, T>(
&'f self,
content: T,
_: Outline,
sack: &'sack Sack,
_: &'s Sack,
) -> impl Renderable + 'html
where
'f: 'html,
'm: 'html,
'sack: 'html,
's: 'html,
T: Renderable + 'm {
html::show(self, content)
}
fn as_link(&self, path: String) -> Option<Linkable> {
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
Some(Linkable::Date(LinkDate {
link: Link {
path: path.strip_suffix("index.html").unwrap().to_owned(),
path,
name: self.title.to_owned(),
desc: self.desc.to_owned(),
},
@ -164,24 +118,24 @@ impl Transformable for md::Slide {
}
}
impl Transformable for md::Wiki {
fn transform<'f, 'm, 'html, 'sack, T>(
impl Content for md::Wiki {
fn transform<'f, 'm, 's, 'html, T>(
&'f self,
content: T,
outline: Outline,
sack: &'sack Sack,
sack: &'s Sack,
) -> impl Renderable + 'html
where
'f: 'html,
'm: 'html,
'sack: 'html,
's: 'html,
T: Renderable + 'm {
html::wiki(self, content, outline, sack)
}
fn as_link(&self, path: String) -> Option<Linkable> {
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
Some(Linkable::Link(Link {
path: path.strip_suffix("index.html").unwrap().to_owned(),
path,
name: self.title.to_owned(),
desc: None,
}))
@ -214,21 +168,21 @@ fn to_list(list: Vec<LinkDate>) -> String {
}
fn transform<T>(meta: gen::Source) -> gen::Asset
fn transform<T>(meta: gen::Source) -> Asset
where
T: for<'de> serde::Deserialize<'de>,
T: Transformable + 'static,
T: Content + 'static,
{
let loc = meta.dirs.strip_prefix("content").unwrap();
let dir = meta.dirs.strip_prefix("content").unwrap();
match meta.kind {
gen::SourceKind::Index => match meta.ext.as_str() {
"md" | "mdx" | "lhs" => {
let loc = loc.join("index.html");
let path = dir.join("index.html");
let data = fs::read_to_string(&meta.path).unwrap();
let (fm, md) = md::preflight::<T>(&data);
let link = T::as_link(&fm, Path::new("/").join(&loc).to_str().unwrap().to_owned());
let link = T::as_link(&fm, Utf8Path::new("/").join(dir));
let call = move |everything: &Sack| {
let (outline, html) = T::render(&md);
@ -237,20 +191,20 @@ fn transform<T>(meta: gen::Source) -> gen::Asset
gen::Asset {
kind: gen::AssetKind::Html(Box::new(call)),
out: loc,
out: path,
link,
meta,
}
},
_ => gen::Asset {
kind: gen::AssetKind::Unknown,
out: loc.join(meta.path.file_name().unwrap()).to_owned(),
out: dir.join(meta.path.file_name().unwrap()).to_owned(),
link: None,
meta,
}
},
gen::SourceKind::Asset => {
let loc = loc.join(meta.path.file_name().unwrap()).to_owned();
let loc = dir.join(meta.path.file_name().unwrap()).to_owned();
match meta.ext.as_str() {
"jpg" | "png" | "gif" => gen::Asset {
kind: gen::AssetKind::Image,
@ -353,7 +307,7 @@ fn main() {
fs::write("dist/styles.css", css).unwrap();
let res = Command::new("pagefind")
.args(&["--site", "dist"])
.args(["--site", "dist"])
.output()
.unwrap();

View file

@ -165,7 +165,7 @@ fn annotate(input: &str) -> Vec<Annotated> {
fn make_ruby(event: Event) -> Vec<Event> {
match event {
Event::Text(ref text) => annotate(&text)
Event::Text(ref text) => annotate(text)
.into_iter()
.map(|el| match el {
Annotated::Text(text) => Event::Text(text.to_owned().into()),

View file

@ -1,7 +1,7 @@
use std::borrow::Cow;
use hypertext::{html_elements, maud_move, Raw, Renderable, GlobalAttributes};
use tree_sitter_highlight::Highlighter;
use tree_sitter_highlight::HighlightEvent;
use tree_sitter_highlight::{Highlighter, HighlightEvent};
mod captures;
mod configs;
@ -33,7 +33,7 @@ pub fn highlight<'data, 'html>(
}
fn to_html(lang: &str, code: &str) -> String {
return get_events(lang, code)
get_events(lang, code)
.into_iter()
.map(|event| match event {
Event::Write(text) => Cow::from(
@ -56,7 +56,7 @@ fn get_events(lang: &str, src: &str) -> Vec<Event> {
let mut hl = Highlighter::new();
let highlights = hl.highlight(
&config,
config,
src.as_bytes(),
None,
|name| configs::get_config(name)