refactor script format
This commit is contained in:
parent
97a41578a1
commit
2013b32f94
4 changed files with 116 additions and 102 deletions
|
@ -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,107 +1,110 @@
|
||||||
<!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>
|
||||||
|
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
body {
|
body {
|
||||||
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica,
|
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica,
|
||||||
Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
|
Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
|
||||||
}
|
}
|
||||||
|
|
||||||
table td,
|
table td,
|
||||||
table th {
|
table th {
|
||||||
padding: 4px 10px;
|
padding: 4px 10px;
|
||||||
border: 2px solid #dfe2e5;
|
border: 2px solid #dfe2e5;
|
||||||
}
|
}
|
||||||
|
|
||||||
tr {
|
tr {
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
border-top: 2px solid #c6cbd1;
|
border-top: 2px solid #c6cbd1;
|
||||||
}
|
}
|
||||||
|
|
||||||
table {
|
table {
|
||||||
display: block;
|
display: block;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
border-spacing: 0;
|
border-spacing: 0;
|
||||||
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>
|
||||||
<tr>
|
<tr>
|
||||||
<th>#</th>
|
<th>#</th>
|
||||||
{% if times %}
|
{% if times %}
|
||||||
<th align="left">{{ locale.time }}</th>
|
<th align="left">{{ locale.time }}</th>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<th align="left">{{ locale.mo }}</th>
|
<th align="left">{{ locale.mo }}</th>
|
||||||
<th align="left">{{ locale.tu }}</th>
|
<th align="left">{{ locale.tu }}</th>
|
||||||
<th align="left">{{ locale.we }}</th>
|
<th align="left">{{ locale.we }}</th>
|
||||||
<th align="left">{{ locale.th }}</th>
|
<th align="left">{{ locale.th }}</th>
|
||||||
<th align="left">{{ locale.fr }}</th>
|
<th align="left">{{ locale.fr }}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for r in rows %}
|
{% for r in rows %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ r.idx }}</td>
|
<td>{{ r.idx }}</td>
|
||||||
|
|
||||||
{% if times %}
|
{% if times %}
|
||||||
<td>{{ r.time }}</td>
|
<td>{{ r.time }}</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<td>
|
<td>
|
||||||
{% if r.mo %} {% if r.mo.room %}
|
{% if r.mo %} {% if r.mo.room %}
|
||||||
<b>{{ r.mo.room }}</b>
|
<b>{{ r.mo.room }}</b>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{{ r.mo.name }}
|
{{ r.mo.name }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if r.tu %} {% if r.tu.room %}
|
{% if r.tu %} {% if r.tu.room %}
|
||||||
<b>{{ r.tu.room }}</b>
|
<b>{{ r.tu.room }}</b>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{{ r.tu.name }}
|
{{ r.tu.name }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if r.we %} {% if r.we.room %}
|
{% if r.we %} {% if r.we.room %}
|
||||||
<b>{{ r.we.room }}</b>
|
<b>{{ r.we.room }}</b>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{{ r.we.name }}
|
{{ r.we.name }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if r.th %} {% if r.th.room %}
|
{% if r.th %} {% if r.th.room %}
|
||||||
<b>{{ r.th.room }}</b>
|
<b>{{ r.th.room }}</b>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{{ r.th.name }}
|
{{ r.th.name }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if r.fr %} {% if r.fr.room %}
|
{% if r.fr %} {% if r.fr.room %}
|
||||||
<b>{{ r.fr.room }}</b>
|
<b>{{ r.fr.room }}</b>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{{ r.fr.name }}
|
{{ r.fr.name }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
{% 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