livetex/
lib.rs

1#![feature(decl_macro)]
2#![feature(try_blocks)]
3#![feature(let_chains)]
4
5use clap::Parser;
6use log::debug;
7use once_cell::sync::Lazy;
8use std::ffi::OsString;
9use std::{fs, mem};
10use std::path::PathBuf;
11use std::sync::Mutex;
12
13pub mod tex_monitor;
14pub mod server;
15
16pub static ARGS: Lazy<Mutex<Args>> = Lazy::new(|| Mutex::new(Default::default()));
17pub static ARGS_SHARED: Lazy<Args> = Lazy::new(|| mutex_lock!(ARGS).clone());
18pub static COMPILED_PATH: Lazy<PathBuf> = Lazy::new(|| {
19    let compiled_path = ARGS_SHARED.root.join("live-compiled");
20    if !compiled_path.exists() {
21        fs::create_dir(&compiled_path).unwrap();
22        debug!("Created directory: {}", compiled_path.display());
23    }
24    compiled_path
25});
26pub static INTERMEDIATES_PATH: Lazy<PathBuf> = Lazy::new(|| {
27    let dir = temp_dir::TempDir::new().unwrap();
28    let path = dir.path().to_path_buf();
29    mem::forget(dir);
30    path
31});
32
33#[derive(Parser, Debug, Default, Clone)]
34/// A live integrated server that compiles TeX and serve its PDF automatically on source changes.
35pub struct Args {
36    /// Server address
37    #[arg(short, long, default_value = "0.0.0.0:8080", required = false)]
38    pub addr: String,
39    /// Root directory to serve
40    #[arg(short, long)]
41    pub root: PathBuf,
42    /// Command to build a TeX file. This argument should be present last.
43    #[arg(short = 'c', long, num_args = 1.., allow_hyphen_values = true)]
44    pub build_command: Vec<OsString>,
45}
46
47pub macro mutex_lock($m:expr) {
48    $m.lock().unwrap()
49}