rtex 0.1.11

Native TeX to PDF converter written in Rust with zero external LaTeX dependencies
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Streaming and progress support for large LaTeX documents.
//!
//! Provides chunked file reading, conversion-stage progress callbacks,
//! and memory-efficiency helpers for documents that may exceed available
//! RAM when loaded as a single `String`.

use std::fs;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

use crate::ConversionOptions;
use crate::incremental::IncrementalCompiler;
use crate::output::{OutputFormat, render_elements_in_dir};
use crate::packages::PackageFetcher;

/// Callback trait for reporting conversion progress.
pub trait ProgressReporter: Send {
    /// Called when a stage begins.  `pct` is an estimated overall
    /// completion percentage (0.0–100.0).
    fn stage_started(&mut self, name: &str, pct: f64);
    /// Called when a stage finishes.
    fn stage_finished(&mut self, name: &str);
}

/// No-op reporter — useful when progress is not required.
pub struct NoOpReporter;

impl ProgressReporter for NoOpReporter {
    fn stage_started(&mut self, _name: &str, _pct: f64) {}
    fn stage_finished(&mut self, _name: &str) {}
}

/// Simple console reporter that prints stage names to stderr.
pub struct ConsoleReporter;

impl ProgressReporter for ConsoleReporter {
    fn stage_started(&mut self, name: &str, pct: f64) {
        eprintln!("[{:>5.1}%] {}", pct, name);
    }
    fn stage_finished(&mut self, name: &str) {
        eprintln!("[ done ] {}", name);
    }
}

/// Converts a LaTeX file to the configured output format with progress reporting and
/// memory-conscious chunked reading.
pub struct StreamingConverter<R: ProgressReporter> {
    reporter: R,
    chunk_size: usize,
    incremental: IncrementalCompiler,
    force_rebuild: bool,
    keep_intermediate: bool,
    options: ConversionOptions,
}

impl StreamingConverter<NoOpReporter> {
    pub fn new() -> Self {
        Self {
            reporter: NoOpReporter,
            chunk_size: 1024 * 1024, // 1 MiB
            incremental: IncrementalCompiler::new(),
            force_rebuild: false,
            keep_intermediate: false,
            options: ConversionOptions::default(),
        }
    }
}

fn package_cache_dir(options: &ConversionOptions) -> std::path::PathBuf {
    options
        .package_cache
        .clone()
        .unwrap_or_else(|| std::path::PathBuf::from(".rtex/cache"))
}

impl<R: ProgressReporter> StreamingConverter<R> {
    pub fn with_reporter(reporter: R) -> Self {
        Self {
            reporter,
            chunk_size: 1024 * 1024,
            incremental: IncrementalCompiler::new(),
            force_rebuild: false,
            keep_intermediate: false,
            options: ConversionOptions::default(),
        }
    }

    /// Configure output format, package fetching, and intermediate artifacts.
    pub fn with_options(mut self, options: ConversionOptions) -> Self {
        self.options = options;
        self
    }

    pub fn with_chunk_size(mut self, size: usize) -> Self {
        self.chunk_size = size;
        self
    }

    /// Enable or disable incremental compilation (enabled by default).
    pub fn with_incremental(mut self, enabled: bool) -> Self {
        self.incremental = if enabled {
            IncrementalCompiler::new()
        } else {
            IncrementalCompiler::disabled()
        };
        self
    }

    /// Force a full rebuild even when the source and dependencies are unchanged.
    pub fn with_force_rebuild(mut self, force: bool) -> Self {
        self.force_rebuild = force;
        self
    }

    /// Keep intermediate artifacts (`.expanded.tex`, `.ast.json`, `.meta.json`).
    pub fn with_keep_intermediate(mut self, keep: bool) -> Self {
        self.keep_intermediate = keep;
        self
    }

    /// Snapshot of incremental build statistics: `(skips, builds)`.
    pub fn incremental_stats(&self) -> (u64, u64) {
        self.incremental.stats()
    }

    /// Convert `input` → `output`, calling back on each stage.
    pub fn convert(&mut self, input: &Path, output: &Path) -> Result<(), crate::error::LatexError> {
        if !input.exists() {
            return Err(crate::error::LatexError::InvalidPath);
        }

        if !self.force_rebuild && output.exists() {
            let deps = crate::watch::discover_inputs(input);
            if !self.incremental.needs_rebuild(input)
                || IncrementalCompiler::outputs_up_to_date(input, output, &deps)
            {
                self.reporter.stage_started("skipped (up to date)", 100.0);
                self.reporter.stage_finished("skipped (up to date)");
                self.incremental
                    .mark_built(input, vec![output.to_path_buf()], deps);
                return Ok(());
            }
        }

        // Stage 1: read source (0–30%)
        self.reporter.stage_started("reading source", 0.0);
        let content = self.read_file(input)?;
        self.reporter.stage_finished("reading source");

        let expanded = crate::macros::expand_document(&content);

        let mut search_paths = Vec::new();
        if self.options.fetch_packages {
            let fetcher = PackageFetcher::new(package_cache_dir(&self.options));
            let _ = fetcher.prepare_source(&content);
            search_paths = fetcher.search_paths();
        }

        // Stage 2: parse (30–60%)
        self.reporter.stage_started("parsing", 30.0);
        let mut parser = crate::parser::TexParser::new(content);
        if let Some(parent) = input.parent() {
            parser = parser.with_base_dir(parent);
        }
        if !search_paths.is_empty() {
            parser = parser.with_search_paths(search_paths);
        }
        let elements = parser.parse();
        self.reporter.stage_finished("parsing");

        // Stage 3: build output (60–100%)
        let build_stage = match self.options.format {
            OutputFormat::Pdf => "building PDF",
            OutputFormat::Html => "building HTML",
            OutputFormat::Docx => "building DOCX",
            OutputFormat::Epub => "building EPUB",
        };
        self.reporter.stage_started(build_stage, 60.0);
        match self.options.format {
            OutputFormat::Pdf | OutputFormat::Html | OutputFormat::Docx | OutputFormat::Epub => {
                let bytes =
                    render_elements_in_dir(elements.clone(), self.options.format, input.parent())?;
                fs::write(output, bytes).map_err(|source| crate::error::LatexError::IoError {
                    path: output.to_path_buf(),
                    source,
                })?;
            }
        }
        self.reporter.stage_finished(build_stage);

        if self.keep_intermediate {
            crate::intermediate::write_intermediates(
                output,
                &expanded,
                &elements,
                self.options.format,
            )?;
        }

        let deps = crate::watch::discover_inputs(input);
        self.incremental
            .mark_built(input, vec![output.to_path_buf()], deps);

        Ok(())
    }

    /// Read a potentially large file, allocating only what is needed.
    fn read_file(&mut self, path: &Path) -> Result<String, crate::error::LatexError> {
        let file = File::open(path)?;
        let meta = file.metadata()?;
        let len = meta.len() as usize;

        // For small files just read everything at once.
        if len <= self.chunk_size {
            let mut buf = String::with_capacity(len);
            let mut reader = BufReader::new(file);
            reader.read_to_string(&mut buf)?;
            return Ok(buf);
        }

        let mut reader = BufReader::with_capacity(self.chunk_size, file);
        let mut bytes = Vec::with_capacity(len);
        let mut chunk = vec![0u8; self.chunk_size];
        let mut total_read = 0usize;

        loop {
            let n = reader.read(&mut chunk)?;
            if n == 0 {
                break;
            }
            bytes.extend_from_slice(&chunk[..n]);
            total_read += n;

            if len > 0 {
                let pct = 30.0 * (total_read as f64 / len as f64).min(1.0);
                self.reporter.stage_started("reading source", pct);
            }
        }

        String::from_utf8(bytes).map_err(|error| crate::error::LatexError::ParseError {
            message: format!("source is not valid UTF-8: {error}"),
            line: None,
            column: None,
            context: Some(path.display().to_string()),
        })
    }
}

impl Default for StreamingConverter<NoOpReporter> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    #[derive(Debug, Default)]
    struct CollectingReporter {
        events: Arc<Mutex<Vec<(String, f64)>>>,
    }

    impl ProgressReporter for CollectingReporter {
        fn stage_started(&mut self, name: &str, pct: f64) {
            self.events.lock().unwrap().push((name.to_string(), pct));
        }
        fn stage_finished(&mut self, name: &str) {
            self.events
                .lock()
                .unwrap()
                .push((format!("done:{}", name), 0.0));
        }
    }

    #[test]
    fn test_converter_reports_stages() {
        let reporter = CollectingReporter::default();
        let events = reporter.events.clone();

        let mut converter = StreamingConverter::with_reporter(reporter);
        // Use a non-existent path — the method returns InvalidPath before
        // any stage callback, so the log should be empty.
        let input = Path::new("/nonexistent/streaming_test.tex");
        let output = Path::new("/nonexistent/streaming_test.pdf");
        let result = converter.convert(input, output);
        assert!(result.is_err());

        let log = events.lock().unwrap();
        assert!(log.is_empty());
    }

    #[test]
    fn test_noop_reporter_does_not_panic() {
        let mut r = NoOpReporter;
        r.stage_started("x", 50.0);
        r.stage_finished("x");
    }

    #[test]
    fn test_console_reporter_does_not_panic() {
        let mut r = ConsoleReporter;
        r.stage_started("x", 50.0);
        r.stage_finished("x");
    }

    #[test]
    fn incremental_skips_unchanged_conversion() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.pdf");
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}Hi\\end{document}",
        )
        .unwrap();

        let mut first = StreamingConverter::new();
        first.convert(&src, &dst).expect("first conversion");
        assert!(dst.exists());

        let reporter = CollectingReporter::default();
        let events = reporter.events.clone();
        let mut second = StreamingConverter::with_reporter(reporter);
        second
            .convert(&src, &dst)
            .expect("second conversion should skip");

        let log = events.lock().unwrap();
        assert!(log.iter().any(|(name, _)| name == "skipped (up to date)"));
    }

    #[test]
    fn force_rebuild_runs_even_when_unchanged() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.pdf");
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}Hi\\end{document}",
        )
        .unwrap();

        let mut converter = StreamingConverter::new();
        converter.convert(&src, &dst).expect("first conversion");

        let reporter = CollectingReporter::default();
        let events = reporter.events.clone();
        let mut converter = StreamingConverter::with_reporter(reporter).with_force_rebuild(true);
        converter.convert(&src, &dst).expect("forced conversion");

        let log = events.lock().unwrap();
        assert!(!log.iter().any(|(name, _)| name == "skipped (up to date)"));
        assert!(log.iter().any(|(name, _)| name == "reading source"));
    }

    #[test]
    fn streaming_converts_html() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.html");
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}Hello\\end{document}",
        )
        .unwrap();

        let mut converter = StreamingConverter::new()
            .with_options(ConversionOptions::default().with_format(OutputFormat::Html));
        converter.convert(&src, &dst).expect("html conversion");

        let html = std::fs::read_to_string(&dst).unwrap();
        assert!(html.contains("Hello"));
        assert!(html.contains("<html"));
    }

    #[test]
    fn streaming_writes_intermediate_artifacts() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.pdf");
        std::fs::write(
            &src,
            "\\documentclass{article}\\title{T}\\begin{document}Hi\\end{document}",
        )
        .unwrap();

        let mut converter = StreamingConverter::new().with_keep_intermediate(true);
        converter.convert(&src, &dst).expect("conversion");

        assert!(dst.exists());
        assert!(tmp.path().join("doc.expanded.tex").exists());
        assert!(tmp.path().join("doc.ast.json").exists());
        assert!(tmp.path().join("doc.meta.json").exists());
    }

    #[test]
    fn streaming_converts_epub() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.epub");
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}EPUB content\\end{document}",
        )
        .unwrap();

        let mut converter = StreamingConverter::new()
            .with_options(ConversionOptions::default().with_format(OutputFormat::Epub));
        converter.convert(&src, &dst).expect("epub conversion");

        assert!(dst.exists());
        let data = std::fs::read(&dst).unwrap();
        assert!(data.starts_with(b"PK"), "EPUB should be a ZIP");
    }

    #[test]
    fn streaming_converts_docx() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.docx");
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}DOCX content\\end{document}",
        )
        .unwrap();

        let mut converter = StreamingConverter::new()
            .with_options(ConversionOptions::default().with_format(OutputFormat::Docx));
        converter.convert(&src, &dst).expect("docx conversion");

        assert!(dst.exists());
        let data = std::fs::read(&dst).unwrap();
        assert!(data.starts_with(b"PK"), "DOCX should be a ZIP");
    }

    #[test]
    fn streaming_custom_chunk_size() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.pdf");
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}Chunked\\end{document}",
        )
        .unwrap();

        let mut converter = StreamingConverter::new().with_chunk_size(256);
        converter.convert(&src, &dst).expect("conversion with small chunk size");

        assert!(dst.exists());
        assert!(std::fs::read(&dst).unwrap().starts_with(b"%PDF"));
    }

    #[test]
    fn incremental_rebuilds_on_source_change() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("doc.tex");
        let dst = tmp.path().join("doc.pdf");

        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}Version 1\\end{document}",
        )
        .unwrap();

        let mut first = StreamingConverter::new();
        first.convert(&src, &dst).expect("first conversion");
        assert!(dst.exists());

        // Modify source
        std::fs::write(
            &src,
            "\\documentclass{article}\\begin{document}Version 2\\end{document}",
        )
        .unwrap();

        let reporter = CollectingReporter::default();
        let events = reporter.events.clone();
        let mut second = StreamingConverter::with_reporter(reporter);
        second.convert(&src, &dst).expect("second conversion after change");

        let log = events.lock().unwrap();
        assert!(log.iter().any(|(name, _)| name == "reading source"),
            "Should rebuild when source changes");
    }

    #[test]
    fn streaming_nonexistent_file_returns_error() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("nonexistent.tex");
        let dst = tmp.path().join("out.pdf");

        let mut converter = StreamingConverter::new();
        assert!(converter.convert(&src, &dst).is_err());
    }
}