refactor script format
This commit is contained in:
parent
97a41578a1
commit
2013b32f94
|
@ -11,11 +11,11 @@ structopt = "0.3.23"
|
||||||
tera = "1.12.1"
|
tera = "1.12.1"
|
||||||
|
|
||||||
[dependencies.minify-html]
|
[dependencies.minify-html]
|
||||||
version = "0.6.8"
|
version = "0.8.0"
|
||||||
features = ["js-esbuild"]
|
features = ["js-esbuild"]
|
||||||
|
|
||||||
[dependencies.mlua]
|
[dependencies.mlua]
|
||||||
version = "0.6.3"
|
version = "0.7.3"
|
||||||
features = ["luajit", "macros", "serialize"]
|
features = ["luajit", "macros", "serialize"]
|
||||||
|
|
||||||
[dependencies.serde]
|
[dependencies.serde]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
|
||||||
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width" />
|
<meta name="viewport" content="width=device-width" />
|
||||||
<title>Plan</title>
|
<title>Plan</title>
|
||||||
|
@ -30,8 +31,9 @@
|
||||||
border-collapse: collapse;
|
border-collapse: collapse;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
|
||||||
|
<body>
|
||||||
{% for i in range(end=repeat) %}
|
{% for i in range(end=repeat) %}
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -103,5 +105,6 @@
|
||||||
{% if i != repeat - 1 %}
|
{% if i != repeat - 1 %}
|
||||||
<hr />
|
<hr />
|
||||||
{% endif %}{% endfor %}
|
{% endif %}{% endfor %}
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use anyhow::Context;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
@ -13,12 +12,7 @@ struct Opt {
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
|
|
||||||
let s_data = script::run_buildscript(opt.infile)?;
|
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")?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +1,51 @@
|
||||||
use mlua::{Lua, LuaSerdeExt};
|
use mlua::{DeserializeOptions, Lua, LuaSerdeExt, Table, Value};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::path::PathBuf;
|
use std::{fs, path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
pub fn run_buildscript(infile: PathBuf) -> anyhow::Result<ScriptData> {
|
use crate::renderer;
|
||||||
|
|
||||||
|
pub fn run_buildscript(infile: PathBuf) -> anyhow::Result<()> {
|
||||||
let script = std::fs::read(infile)?;
|
let script = std::fs::read(infile)?;
|
||||||
|
|
||||||
// required to allow C lua libs
|
// required to allow C lua libs
|
||||||
let lua = unsafe { Lua::unsafe_new() };
|
let lua = unsafe { Lua::unsafe_new() };
|
||||||
|
|
||||||
|
lua.globals()
|
||||||
|
.set("render", lua.create_function(lua_render)?)?;
|
||||||
|
|
||||||
lua.load(&script).exec()?;
|
lua.load(&script).exec()?;
|
||||||
|
|
||||||
let sdata = lua.from_value_with(
|
Ok(())
|
||||||
mlua::Value::Table(lua.globals()),
|
}
|
||||||
mlua::DeserializeOptions::default()
|
|
||||||
.deny_recursive_tables(false)
|
|
||||||
.deny_unsupported_types(false),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(sdata)
|
fn lua_render<'lua>(lua: &'lua Lua, args: Table<'lua>) -> Result<(), mlua::Error> {
|
||||||
|
let args =
|
||||||
|
lua.from_value_with::<ScriptData>(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)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(rename_all = "PascalCase")]
|
|
||||||
pub struct ScriptData {
|
pub struct ScriptData {
|
||||||
pub days: Days,
|
pub days: Days,
|
||||||
pub outfile: PathBuf,
|
pub outfile: PathBuf,
|
||||||
|
#[serde(default)]
|
||||||
pub locale: Locale,
|
pub locale: Locale,
|
||||||
|
#[serde(default = "default_repeat_count")]
|
||||||
pub repeat_count: usize,
|
pub repeat_count: usize,
|
||||||
pub times: Vec<String>,
|
pub times: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_repeat_count() -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Days {
|
pub struct Days {
|
||||||
pub mo: Vec<Class>,
|
pub mo: Vec<Class>,
|
||||||
|
|
Loading…
Reference in a new issue