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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! CLI argument parsing and command dispatch.
use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::commands;
/// Self-contained LaTeX to PDF compiler
#[derive(Parser)]
#[command(name = "texforge", version, about)]
pub struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Remove build artifacts
Clean,
/// Initialize a texforge project in the current directory
Init,
/// Create a new project from a template
New {
/// Project name
name: String,
/// Template name (default: basic)
#[arg(short, long)]
template: Option<String>,
},
/// Compile project to PDF
Build {
/// Watch for file changes and rebuild automatically
#[arg(long)]
watch: bool,
/// Debounce delay in seconds before rebuilding (default: 10)
#[arg(long, default_value = "2")]
delay: u64,
/// Print every engine warning instead of a per-file summary
#[arg(long)]
verbose: bool,
/// Build reproducibly: pin `SOURCE_DATE_EPOCH` so identical source plus
/// the same Tectonic version yields an identical PDF. An optional epoch
/// (seconds since the Unix epoch) overrides the fixed default.
#[arg(long, num_args = 0..=1)]
reproducible: Option<Option<u64>>,
/// After each successful rebuild, write a PNG of one page to a fixed
/// path so any file-watching image viewer becomes a live preview
/// (requires `--watch`)
#[arg(long, requires = "watch")]
preview: bool,
/// Page to rasterize for `--preview` (1-based; default: 1)
#[arg(long, default_value_t = 1, requires = "preview")]
preview_page: usize,
/// Fixed path for the `--preview` PNG (default: preview.png)
#[arg(long, value_name = "PATH", requires = "preview")]
preview_out: Option<PathBuf>,
},
/// Format .tex files
Fmt {
/// Check formatting without modifying files
#[arg(long)]
check: bool,
},
/// Lint project without compiling
Check {
/// Treat warnings as errors (exit non-zero if any warning is present)
#[arg(long)]
deny_warnings: bool,
},
/// Count words per section or file
Stats {
/// Output JSON instead of a human-readable breakdown
#[arg(long)]
json: bool,
/// Break down by .tex file instead of by section
#[arg(long, value_enum, default_value = "section")]
by: crate::commands::stats::ByMode,
},
/// Print the document's section tree
Outline {
/// Output JSON instead of a human-readable tree
#[arg(long)]
json: bool,
},
/// Rasterize the compiled PDF to PNG pages
Preview {
/// Page number to rasterize (1-based; default: all pages)
#[arg(long, value_name = "N")]
page: Option<usize>,
/// Rasterization scale in pixels per PDF point (default: 1.0)
#[arg(long, default_value_t = 1.0)]
scale: f32,
/// Directory to write PNGs to (default: <project>/preview)
#[arg(long, value_name = "DIR")]
out: Option<PathBuf>,
},
/// Inspect the compiled PDF (text, info, pages, fidelity)
Pdf {
#[command(subcommand)]
action: PdfAction,
},
/// Manage templates
Template {
#[command(subcommand)]
action: TemplateAction,
},
/// Manage the personal spell-check whitelist (dictionary)
Spell {
#[command(subcommand)]
action: SpellAction,
},
/// Diagnose the managed environment (Tectonic, cache, fonts, dictionaries, project)
Doctor,
/// Manage global configuration
Config {
/// Key to get/set (name, email, institution, language)
key: Option<String>,
/// Value to set (optional - if omitted, shows current value)
value: Option<String>,
},
}
#[derive(Subcommand)]
enum PdfAction {
/// Print text as a reader or ATS would see it
Text {
/// Keep ligature codepoints unnormalized
#[arg(long)]
raw: bool,
},
/// Report pages, fonts (embedded?), and metadata
Info,
/// Report which section opens each page (diff-friendly)
Pages,
/// Check significant source words appear in the PDF text
Check,
}
#[derive(Subcommand)]
enum SpellAction {
/// Add one or more words to the whitelist (default: global personal dictionary)
Add {
/// Word(s) to add
#[arg(required = true)]
words: Vec<String>,
/// Target the project's whitelist instead of the global personal dictionary
#[arg(long, conflicts_with = "global")]
local: bool,
/// Target the global personal dictionary (~/.texforge/spell-words); this is the default
#[arg(long, conflicts_with = "local")]
global: bool,
},
/// List the effective whitelist words for the scope (default: global personal dictionary)
List {
/// List the project's whitelist instead of the global personal dictionary
#[arg(long, conflicts_with = "global")]
local: bool,
/// List the global personal dictionary (~/.texforge/spell-words); this is the default
#[arg(long, conflicts_with = "local")]
global: bool,
},
/// Remove one or more words from the whitelist (default: global personal dictionary)
Remove {
/// Word(s) to remove
#[arg(required = true)]
words: Vec<String>,
/// Target the project's whitelist instead of the global personal dictionary
#[arg(long, conflicts_with = "global")]
local: bool,
/// Target the global personal dictionary (~/.texforge/spell-words); this is the default
#[arg(long, conflicts_with = "local")]
global: bool,
},
}
#[derive(Subcommand)]
enum TemplateAction {
/// List available templates (installed + remote registry by default)
List {
/// Only show locally installed templates (skip the remote registry)
#[arg(long)]
local: bool,
},
/// Add a template from URL or registry
Add { source: String },
/// Remove a template
Remove { name: String },
/// Validate template compatibility
Validate { name: String },
}
impl Cli {
pub fn execute(self) -> Result<()> {
match self.command {
Commands::Clean => commands::clean::execute(),
Commands::Init => commands::init::execute(),
Commands::New { name, template } => commands::new::execute(&name, template.as_deref()),
Commands::Build {
watch,
delay,
verbose,
reproducible,
preview,
preview_page,
preview_out,
} => {
if watch {
let live_preview = preview.then_some(commands::build::LivePreview {
page: preview_page,
out: preview_out,
});
commands::build::watch(delay, verbose, reproducible, live_preview.as_ref())
} else {
commands::build::execute(verbose, reproducible)
}
}
Commands::Fmt { check } => commands::fmt::execute(check),
Commands::Check { deny_warnings } => commands::check::execute(deny_warnings),
Commands::Stats { json, by } => commands::stats::execute(json, by),
Commands::Outline { json } => commands::outline::execute(json),
Commands::Preview { page, scale, out } => commands::preview::execute(page, scale, out),
Commands::Pdf { action } => {
let action = match action {
PdfAction::Text { raw } => commands::pdf::PdfAction::Text { raw },
PdfAction::Info => commands::pdf::PdfAction::Info,
PdfAction::Pages => commands::pdf::PdfAction::Pages,
PdfAction::Check => commands::pdf::PdfAction::Check,
};
commands::pdf::execute(action)
}
Commands::Template { action } => match action {
TemplateAction::List { local } => commands::template::list(!local),
TemplateAction::Add { source } => commands::template::add(&source),
TemplateAction::Remove { name } => commands::template::remove(&name),
TemplateAction::Validate { name } => commands::template::validate(&name),
},
Commands::Spell { action } => {
let action = match action {
SpellAction::Add {
words,
local,
global,
} => commands::spell::SpellAction::Add {
words,
scope: commands::spell::Scope::from_flags(local, global),
},
SpellAction::List { local, global } => commands::spell::SpellAction::List {
scope: commands::spell::Scope::from_flags(local, global),
},
SpellAction::Remove {
words,
local,
global,
} => commands::spell::SpellAction::Remove {
words,
scope: commands::spell::Scope::from_flags(local, global),
},
};
commands::spell::execute(action)
}
Commands::Doctor => commands::doctor::execute(),
Commands::Config { key, value } => match (key, value) {
(None, None) => commands::config::wizard(),
(Some(k), None) if k == "list" => commands::config::list(),
(Some(k), None) => commands::config::get(&k),
(Some(k), Some(v)) => commands::config::set(&k, &v),
(None, Some(_)) => anyhow::bail!("Cannot set value without a key"),
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// TE14 requirement 6: `--local --global` together must be rejected by
/// clap, not silently resolved to one or the other.
#[test]
fn spell_add_rejects_local_and_global_together() {
let result =
Cli::try_parse_from(["texforge", "spell", "add", "docker", "--local", "--global"]);
assert!(
result.is_err(),
"`--local --global` together must be a usage error"
);
}
#[test]
fn spell_list_rejects_local_and_global_together() {
let result = Cli::try_parse_from(["texforge", "spell", "list", "--local", "--global"]);
assert!(
result.is_err(),
"`--local --global` together must be a usage error"
);
}
#[test]
fn spell_remove_rejects_local_and_global_together() {
let result = Cli::try_parse_from([
"texforge", "spell", "remove", "docker", "--local", "--global",
]);
assert!(
result.is_err(),
"`--local --global` together must be a usage error"
);
}
#[test]
fn spell_add_accepts_local_alone() {
let result = Cli::try_parse_from(["texforge", "spell", "add", "docker", "--local"]);
assert!(result.is_ok());
}
}