diff --git a/Cargo.toml b/Cargo.toml index 1d6ea86..6bc70d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,11 @@ structopt = "0.3.23" tera = "1.12.1" [dependencies.minify-html] -version = "0.6.8" +version = "0.8.0" features = ["js-esbuild"] [dependencies.mlua] -version = "0.6.3" +version = "0.7.3" features = ["luajit", "macros", "serialize"] [dependencies.serde] diff --git a/assets/template.html.tera b/assets/template.html.tera index 3b5ccb9..67a8959 100644 --- a/assets/template.html.tera +++ b/assets/template.html.tera @@ -1,107 +1,110 @@ - + + Plan - - + + + {% for i in range(end=repeat) %} - - - - {% if times %} - - {% endif %} - - - - - - - - - {% for r in rows %} - - + + + + {% if times %} + + {% endif %} + + + + + + + + + {% for r in rows %} + + - {% if times %} - - {% endif %} + {% if times %} + + {% endif %} - - + - + - + - + - - {% endfor %} - + {{ r.fr.name }} + {% endif %} + + + {% endfor %} +
#{{ locale.time }}{{ locale.mo }}{{ locale.tu }}{{ locale.we }}{{ locale.th }}{{ locale.fr }}
{{ r.idx }}
#{{ locale.time }}{{ locale.mo }}{{ locale.tu }}{{ locale.we }}{{ locale.th }}{{ locale.fr }}
{{ r.idx }}{{ r.time }}{{ r.time }} - {% if r.mo %} {% if r.mo.room %} - {{ r.mo.room }} - {% endif %} + + {% if r.mo %} {% if r.mo.room %} + {{ r.mo.room }} + {% endif %} - {{ r.mo.name }} - {% endif %} - - {% if r.tu %} {% if r.tu.room %} - {{ r.tu.room }} - {% endif %} + {{ r.mo.name }} + {% endif %} + + {% if r.tu %} {% if r.tu.room %} + {{ r.tu.room }} + {% endif %} - {{ r.tu.name }} - {% endif %} - - {% if r.we %} {% if r.we.room %} - {{ r.we.room }} - {% endif %} + {{ r.tu.name }} + {% endif %} + + {% if r.we %} {% if r.we.room %} + {{ r.we.room }} + {% endif %} - {{ r.we.name }} - {% endif %} - - {% if r.th %} {% if r.th.room %} - {{ r.th.room }} - {% endif %} + {{ r.we.name }} + {% endif %} + + {% if r.th %} {% if r.th.room %} + {{ r.th.room }} + {% endif %} - {{ r.th.name }} - {% endif %} - - {% if r.fr %} {% if r.fr.room %} - {{ r.fr.room }} - {% endif %} + {{ r.th.name }} + {% endif %} + + {% if r.fr %} {% if r.fr.room %} + {{ r.fr.room }} + {% endif %} - {{ r.fr.name }} - {% endif %} -
{% if i != repeat - 1 %}
{% endif %}{% endfor %} - + + diff --git a/src/main.rs b/src/main.rs index fbea62a..c3971e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use anyhow::Context; use std::path::PathBuf; use structopt::StructOpt; @@ -13,12 +12,7 @@ struct Opt { fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); - let s_data = script::run_buildscript(opt.infile)?; - let path = s_data.outfile.clone(); - let rendered = renderer::render(s_data)?; - - std::fs::write(path, rendered) - .context("failed to write outfile")?; + script::run_buildscript(opt.infile)?; Ok(()) } diff --git a/src/script.rs b/src/script.rs index 876cf6e..ac32fd7 100644 --- a/src/script.rs +++ b/src/script.rs @@ -1,34 +1,51 @@ -use mlua::{Lua, LuaSerdeExt}; +use mlua::{DeserializeOptions, Lua, LuaSerdeExt, Table, Value}; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::{fs, path::PathBuf, sync::Arc}; -pub fn run_buildscript(infile: PathBuf) -> anyhow::Result { +use crate::renderer; + +pub fn run_buildscript(infile: PathBuf) -> anyhow::Result<()> { let script = std::fs::read(infile)?; // required to allow C lua libs let lua = unsafe { Lua::unsafe_new() }; + + lua.globals() + .set("render", lua.create_function(lua_render)?)?; + lua.load(&script).exec()?; - let sdata = lua.from_value_with( - mlua::Value::Table(lua.globals()), - mlua::DeserializeOptions::default() - .deny_recursive_tables(false) - .deny_unsupported_types(false), - )?; + Ok(()) +} - Ok(sdata) +fn lua_render<'lua>(lua: &'lua Lua, args: Table<'lua>) -> Result<(), mlua::Error> { + let args = + lua.from_value_with::(Value::Table(args), DeserializeOptions::default())?; + let path = args.outfile.clone(); + + let rendered = + renderer::render(args).map_err(|e| mlua::Error::ExternalError(Arc::from(Box::from(e))))?; + + fs::write(path, rendered)?; + + Ok(()) } #[derive(Debug, Deserialize)] -#[serde(rename_all = "PascalCase")] pub struct ScriptData { pub days: Days, pub outfile: PathBuf, + #[serde(default)] pub locale: Locale, + #[serde(default = "default_repeat_count")] pub repeat_count: usize, pub times: Vec, } +fn default_repeat_count() -> usize { + 1 +} + #[derive(Debug, Deserialize)] pub struct Days { pub mo: Vec,