Stuff!
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
use crate::{ColorMode, Opt};
|
||||
use crate::{ColorMode, Config};
|
||||
use async_recursion::async_recursion;
|
||||
use futures::future::join_all;
|
||||
use handlebars::{Context, Handlebars};
|
||||
use serde::Serialize;
|
||||
use std::ffi::OsStr;
|
||||
use std::io::{self, ErrorKind};
|
||||
use std::path::PathBuf;
|
||||
use templar::{Context, InnerData, StandardContext, Templar};
|
||||
use tokio::fs::{copy, create_dir, read_dir, read_to_string, write};
|
||||
use tokio::try_join;
|
||||
|
||||
@ -16,27 +16,36 @@ struct TemplateContext<'a> {
|
||||
darkmode: bool,
|
||||
}
|
||||
|
||||
pub async fn build_tree(opt: &Opt) -> io::Result<()> {
|
||||
const TEMPLATE_EXTENSION: &str = "tpl";
|
||||
|
||||
pub async fn build_tree(cfg: &Config) -> io::Result<()> {
|
||||
let tpl = Templar::global();
|
||||
|
||||
let hostname = read_to_string("/etc/hostname").await?;
|
||||
let darkmode = cfg.color == ColorMode::Dark;
|
||||
let ctx = StandardContext::new();
|
||||
ctx.set(
|
||||
InnerData::new(TemplateContext {
|
||||
hostname: hostname.trim(),
|
||||
darkmode,
|
||||
lightmode: !darkmode,
|
||||
})
|
||||
.expect("serialize template context"),
|
||||
)
|
||||
.expect("set template context");
|
||||
|
||||
let darkmode = opt.color.map(|m| m == ColorMode::Dark).unwrap_or(true);
|
||||
|
||||
let hbs = Handlebars::new();
|
||||
|
||||
let ctx = Context::wraps(TemplateContext {
|
||||
hostname: hostname.trim(),
|
||||
darkmode,
|
||||
lightmode: !darkmode,
|
||||
})
|
||||
.expect("template context");
|
||||
|
||||
dir(opt, &ctx, &hbs, PathBuf::new()).await
|
||||
dir(cfg, &ctx, &tpl, PathBuf::new()).await
|
||||
}
|
||||
|
||||
#[async_recursion]
|
||||
async fn dir(opt: &Opt, ctx: &Context, hbs: &Handlebars<'_>, relative: PathBuf) -> io::Result<()> {
|
||||
let template_path = opt.template_dir.join(&relative);
|
||||
let build_path = opt.build_dir.join(&relative);
|
||||
async fn dir(
|
||||
cfg: &Config,
|
||||
ctx: &StandardContext,
|
||||
tpl: &Templar,
|
||||
relative: PathBuf,
|
||||
) -> io::Result<()> {
|
||||
let template_path = cfg.template_dir.join(&relative);
|
||||
let build_path = cfg.build_dir.join(&relative);
|
||||
|
||||
info!("traversing {:?}", template_path);
|
||||
|
||||
@ -56,9 +65,9 @@ async fn dir(opt: &Opt, ctx: &Context, hbs: &Handlebars<'_>, relative: PathBuf)
|
||||
let new_relative = relative.join(entry.file_name());
|
||||
|
||||
if meta.is_dir() {
|
||||
dir_tasks.push(dir(opt, ctx, hbs, new_relative));
|
||||
dir_tasks.push(dir(cfg, ctx, tpl, new_relative));
|
||||
} else if meta.is_file() {
|
||||
file_tasks.push(file(opt, ctx, hbs, new_relative));
|
||||
file_tasks.push(file(cfg, ctx, tpl, new_relative));
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,22 +90,28 @@ async fn dir(opt: &Opt, ctx: &Context, hbs: &Handlebars<'_>, relative: PathBuf)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn file(opt: &Opt, ctx: &Context, hbs: &Handlebars<'_>, relative: PathBuf) -> io::Result<()> {
|
||||
let template_path = opt.template_dir.join(&relative);
|
||||
let mut new_path = opt.build_dir.join(&relative);
|
||||
async fn file(
|
||||
cfg: &Config,
|
||||
ctx: &StandardContext,
|
||||
tpl: &Templar,
|
||||
relative: PathBuf,
|
||||
) -> io::Result<()> {
|
||||
let template_path = cfg.template_dir.join(&relative);
|
||||
let mut new_path = cfg.build_dir.join(&relative);
|
||||
|
||||
// if it is a handlebars file
|
||||
if template_path.extension() == Some(OsStr::new("hbs")) {
|
||||
info!("rendering {:?}", template_path);
|
||||
let file_data = read_to_string(&template_path).await?;
|
||||
info!("rendering {:?}", template_path);
|
||||
let file_data = read_to_string(&template_path).await?;
|
||||
|
||||
if template_path.extension() == Some(OsStr::new(TEMPLATE_EXTENSION)) {
|
||||
// perform templating
|
||||
// TODO: error handling
|
||||
let rendered = hbs
|
||||
.render_template_with_context(&file_data, &ctx)
|
||||
.expect("template error");
|
||||
let rendered = tpl
|
||||
.parse_template(&file_data)
|
||||
.expect("failed to parse template")
|
||||
.render(ctx)
|
||||
.expect("failed to render template");
|
||||
|
||||
// remove .hbs
|
||||
// remove template file extension
|
||||
new_path.set_extension("");
|
||||
|
||||
// write the rendered file
|
||||
|
||||
Reference in New Issue
Block a user