-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rs
119 lines (112 loc) · 4.52 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
mod writer;
use ayaka_runtime::{anyhow::Result, *};
use clap::Parser;
use flexi_logger::{LogSpecification, Logger};
use std::ffi::OsString;
use writer::LaTeXWriter;
#[derive(Debug, Parser)]
#[clap(about, version, author)]
pub struct Options {
#[clap(required = true)]
input: Vec<OsString>,
#[clap(short, long)]
output: OsString,
#[clap(short, long)]
locale: Option<Locale>,
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let opts = Options::parse();
let spec = LogSpecification::parse("warn")?;
let _log_handle = Logger::with(spec)
.log_to_stdout()
.set_palette("b1;3;2;4;6".to_string())
.use_utc()
.start()?;
let context = Context::open(&opts.input, FrontendType::Latex);
let mut ctx = context.await?;
let output = tokio::fs::File::create(&opts.output).await?;
let mut output = LaTeXWriter::new(output);
output.command("documentclass", ["ctexart"]).await?;
output.command("usepackage", ["graphicx"]).await?;
output.command("usepackage", ["lua-ul"]).await?;
output.command("usepackage", ["luatexja-ruby"]).await?;
output.command("usepackage", ["verbatim"]).await?;
output.command("title", [&ctx.game.config.title]).await?;
output.command("author", [&ctx.game.config.author]).await?;
output
.environment("document", |output| async move {
output.command0("maketitle").await?;
output.command0("tableofcontents").await?;
ctx.init_new();
let loc = opts.locale.unwrap_or_else(Locale::current);
let mut current_para = None;
let mut current_bg = None;
while let Some(raw_ctx) = ctx.next_run() {
let action = ctx.get_action(&loc, &raw_ctx)?;
let para_title = ctx.current_paragraph_title(&loc);
if para_title != current_para.as_ref() {
output
.command(
"section",
[para_title.map(|s| s.as_str()).unwrap_or_default()],
)
.await?;
current_para = para_title.cloned();
}
let bg = raw_ctx
.locals
.get("bg")
.map(|value| value.get_str().into_owned());
if current_bg != bg {
current_bg = bg;
if let Some(bg) = ¤t_bg {
output
.environment_attr("figure", "!htbp", |output| async move {
output.command0("centering").await?;
output
.command_attr(
"includegraphics",
"width=1\\linewidth",
[bg.replace('\\', "/")],
)
.await?;
Ok(output)
})
.await?;
}
}
match action {
Action::Empty | Action::Custom(_) => {}
Action::Text(action) => {
if let Some(name) = &action.character {
output.command("paragraph", [name]).await?;
}
for s in action.text {
output.write(s.as_str()).await?;
}
output.write("\n\n").await?;
}
Action::Switches(switches) => {
output
.environment("itemize", |output| async move {
for s in switches.iter() {
output.command0("item").await?;
if s.enabled {
output.write(&s.text).await?;
} else {
output.command("strikeThrough", [&s.text]).await?;
}
output.write("\n").await?;
}
Ok(output)
})
.await?;
}
}
}
Ok(output)
})
.await?;
Ok(())
}