refactor script format

This commit is contained in:
LordMZTE 2022-01-22 18:50:19 +01:00
parent 97a41578a1
commit 2013b32f94
4 changed files with 116 additions and 102 deletions

View file

@ -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]

View file

@ -1,107 +1,110 @@
<!DOCTYPE html>
<html>
<head>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Plan</title>
<style type="text/css">
body {
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica,
Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
}
body {
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica,
Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
}
table td,
table th {
padding: 4px 10px;
border: 2px solid #dfe2e5;
}
table td,
table th {
padding: 4px 10px;
border: 2px solid #dfe2e5;
}
tr {
background-color: #fff;
border-top: 2px solid #c6cbd1;
}
tr {
background-color: #fff;
border-top: 2px solid #c6cbd1;
}
table {
display: block;
width: 100%;
overflow: auto;
border-spacing: 0;
border-collapse: collapse;
}
table {
display: block;
width: 100%;
overflow: auto;
border-spacing: 0;
border-collapse: collapse;
}
</style>
</head>
<body>
</head>
<body>
{% for i in range(end=repeat) %}
<table>
<thead>
<tr>
<th>&#35;</th>
{% if times %}
<th align="left">{{ locale.time }}</th>
{% endif %}
<th align="left">{{ locale.mo }}</th>
<th align="left">{{ locale.tu }}</th>
<th align="left">{{ locale.we }}</th>
<th align="left">{{ locale.th }}</th>
<th align="left">{{ locale.fr }}</th>
</tr>
</thead>
<tbody>
{% for r in rows %}
<tr>
<td>{{ r.idx }}</td>
<thead>
<tr>
<th>&#35;</th>
{% if times %}
<th align="left">{{ locale.time }}</th>
{% endif %}
<th align="left">{{ locale.mo }}</th>
<th align="left">{{ locale.tu }}</th>
<th align="left">{{ locale.we }}</th>
<th align="left">{{ locale.th }}</th>
<th align="left">{{ locale.fr }}</th>
</tr>
</thead>
<tbody>
{% for r in rows %}
<tr>
<td>{{ r.idx }}</td>
{% if times %}
<td>{{ r.time }}</td>
{% endif %}
{% if times %}
<td>{{ r.time }}</td>
{% endif %}
<td>
{% if r.mo %} {% if r.mo.room %}
<b>{{ r.mo.room }}</b>
{% endif %}
<td>
{% if r.mo %} {% if r.mo.room %}
<b>{{ r.mo.room }}</b>
{% endif %}
{{ r.mo.name }}
{% endif %}
</td>
<td>
{% if r.tu %} {% if r.tu.room %}
<b>{{ r.tu.room }}</b>
{% endif %}
{{ r.mo.name }}
{% endif %}
</td>
<td>
{% if r.tu %} {% if r.tu.room %}
<b>{{ r.tu.room }}</b>
{% endif %}
{{ r.tu.name }}
{% endif %}
</td>
<td>
{% if r.we %} {% if r.we.room %}
<b>{{ r.we.room }}</b>
{% endif %}
{{ r.tu.name }}
{% endif %}
</td>
<td>
{% if r.we %} {% if r.we.room %}
<b>{{ r.we.room }}</b>
{% endif %}
{{ r.we.name }}
{% endif %}
</td>
<td>
{% if r.th %} {% if r.th.room %}
<b>{{ r.th.room }}</b>
{% endif %}
{{ r.we.name }}
{% endif %}
</td>
<td>
{% if r.th %} {% if r.th.room %}
<b>{{ r.th.room }}</b>
{% endif %}
{{ r.th.name }}
{% endif %}
</td>
<td>
{% if r.fr %} {% if r.fr.room %}
<b>{{ r.fr.room }}</b>
{% endif %}
{{ r.th.name }}
{% endif %}
</td>
<td>
{% if r.fr %} {% if r.fr.room %}
<b>{{ r.fr.room }}</b>
{% endif %}
{{ r.fr.name }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
{{ r.fr.name }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if i != repeat - 1 %}
<hr />
{% endif %}{% endfor %}
</body>
</body>
</html>

View file

@ -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(())
}

View file

@ -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<ScriptData> {
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::<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)]
#[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<String>,
}
fn default_repeat_count() -> usize {
1
}
#[derive(Debug, Deserialize)]
pub struct Days {
pub mo: Vec<Class>,