bard 2.0.1

Creates PDF and HTML songbooks out of easy-to-write Markdown sources.
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use std::borrow::Cow;
use std::ffi::{OsStr, OsString};
use std::io::{BufRead, Write};
use std::ops::Deref;
use std::process::{Command, Stdio};
use std::str::FromStr;
use std::time::Duration;
use std::{env, fmt, fs, io, thread};

use parking_lot::{const_mutex, Mutex, MutexGuard};
use serde::de::Error as _;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString, EnumVariantNames, VariantNames as _};

use crate::app::{keeplevel, verbosity, App, InterruptFlag};
use crate::prelude::*;
use crate::util::{ExitStatusExt, ProcessLines, StrExt, TempPath};
use crate::util_cmd;

static TEX_TOOLS: Mutex<Option<TexTools>> = const_mutex(None);

#[derive(EnumString, EnumVariantNames, Display, Clone, Copy, PartialEq, Eq, Debug)]
#[strum(ascii_case_insensitive, serialize_all = "kebab-case")]
pub enum TexDistro {
    Xelatex,
    Tectonic,
    TectonicEmbedded,
    None,
}

impl TexDistro {
    fn default_program(&self, app: &App) -> Option<OsString> {
        match self {
            Self::Xelatex => Some("xelatex".to_string().into()),
            Self::Tectonic => Some("tectonic".to_string().into()),
            Self::TectonicEmbedded => Some(app.bard_exe().to_owned().into()),
            _ => None,
        }
    }

    fn is_none(&self) -> bool {
        matches!(self, Self::None)
    }
}

#[derive(Clone, Debug)]
pub struct TexConfig {
    distro: TexDistro,
    program: Option<OsString>,
}

impl TexConfig {
    fn try_from_env() -> Result<Option<Self>> {
        env::var_os("BARD_TEX")
            .map(|var| Self::try_from(var.as_ref()))
            .transpose()
    }

    fn with_distro(distro: TexDistro) -> Self {
        Self {
            distro,
            program: None,
        }
    }

    fn with_embedded_tectonic(app: &App) -> Self {
        Self {
            distro: TexDistro::TectonicEmbedded,
            program: TexDistro::TectonicEmbedded.default_program(app),
        }
    }

    fn probe(&mut self, app: &App) -> Result<()> {
        if self.distro.is_none() {
            return Ok(());
        }

        if self.program.is_none() {
            self.program = self.distro.default_program(app);
        }

        let interrupt = app.interrupt_flag();
        let version = match self.distro {
            TexDistro::Xelatex => {
                test_program(interrupt, self.program.as_ref().unwrap(), "-version")?
            }
            TexDistro::Tectonic => {
                test_program(interrupt, self.program.as_ref().unwrap(), "--version")?
            }
            #[cfg(not(feature = "tectonic"))]
            TexDistro::TectonicEmbedded => {
                bail!("This bard binary was not built with embedded Tectonic.")
            }
            #[cfg(feature = "tectonic")]
            TexDistro::TectonicEmbedded => {
                *self = Self::with_embedded_tectonic(app);
                "Tectonic (embedded)".to_string()
            }
            _ => unreachable!(),
        };

        app.indent(version);
        Ok(())
    }

    fn render_args(&self, job: &TexRenderJob) -> Vec<OsString> {
        let mut args = match self.distro {
            TexDistro::Xelatex => vec![
                "-interaction=nonstopmode".to_os_string(),
                "-output-directory".to_os_string(),
                job.tmp_dir.to_os_string(),
            ],
            TexDistro::Tectonic => vec![
                "-k".to_os_string(),
                "-r".to_os_string(),
                "0".to_os_string(),
                "-o".to_os_string(),
                job.tmp_dir.to_os_string(),
                // Also need to add the out dir to search path, because otherwise tectonic
                // doesn't pickup the .toc file when -r 0.
                // See https://github.com/tectonic-typesetting/tectonic/issues/981
                "-Z".to_os_string(),
                {
                    let mut search_path = "search-path=".to_os_string();
                    search_path.push(job.tmp_dir.as_os_str());
                    search_path
                },
            ],
            TexDistro::TectonicEmbedded => vec![
                // With embedded tectonic the search path ToC workaround is done in tectonic_embed.
                "tectonic".to_os_string(),
                "-o".to_os_string(),
                job.tmp_dir.to_os_string(),
            ],
            TexDistro::None => unreachable!(),
        };

        args.extend(["--".to_os_string(), job.tex_file.to_os_string()]);
        args
    }

    /// Returns what should be the stderr status prefix when logging lines in scrolled mode,
    /// see `App::subprocess_output()`.
    fn program_status(&self) -> Cow<str> {
        match self.distro {
            TexDistro::Xelatex | TexDistro::Tectonic => {
                self.program.as_ref().unwrap().to_string_lossy()
            }
            TexDistro::TectonicEmbedded => "tectonic".into(),
            TexDistro::None => unreachable!(),
        }
    }
}

#[cfg(unix)]
impl<'a> TryFrom<&'a OsStr> for TexConfig {
    type Error = Error;

    fn try_from(input: &'a OsStr) -> Result<Self, Self::Error> {
        use std::os::unix::ffi::{OsStrExt, OsStringExt};

        let input = input.as_bytes();
        let mut split = input.splitn(2, |&c| c == b':');
        let distro = OsStr::from_bytes(split.next().unwrap()).to_string_lossy();
        let program = split.next().map(|p| OsString::from_vec(p.to_owned()));
        let distro: TexDistro = distro.parse().map_err(|_| {
            anyhow!(
                "Unexpected TeX distro type: '{}', possible choices are: {:?}.",
                distro,
                TexDistro::VARIANTS,
            )
        })?;

        Ok(Self { distro, program })
    }
}
#[cfg(windows)]
impl<'a> TryFrom<&'a OsStr> for TexConfig {
    type Error = Error;

    fn try_from(input: &'a OsStr) -> Result<Self, Self::Error> {
        use std::os::windows::ffi::{OsStrExt, OsStringExt};

        const COLON: u16 = u16::from_le_bytes([b':', 0]);

        let input: Vec<_> = input.encode_wide().collect();
        let mut split = input.splitn(2, |&c| c == COLON);
        let distro = OsString::from_wide(split.next().unwrap());
        let distro = distro.to_string_lossy();
        let program = split.next().map(|p| OsString::from_wide(p));
        let distro: TexDistro = distro.parse().map_err(|_| {
            anyhow!(
                "Unexpected TeX distro type: '{}', possible choices are: {:?}.",
                distro,
                TexDistro::VARIANTS,
            )
        })?;

        Ok(Self { distro, program })
    }
}

impl FromStr for TexConfig {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let os: &OsStr = s.as_ref();
        Self::try_from(os)
    }
}

impl<'de> Deserialize<'de> for TexConfig {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let input: Cow<'de, str> = Deserialize::deserialize(deserializer)?;
        OsStr::new(input.as_ref())
            .try_into()
            .map_err(D::Error::custom)
    }
}

impl fmt::Display for TexConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.distro)?;

        if let Some(program) = self.program.as_ref() {
            write!(f, ":{}", program.to_string_lossy())?;
        }

        Ok(())
    }
}

impl Serialize for TexConfig {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = self.to_string();
        s.serialize(serializer)
    }
}

/// Run a command and get first line from stdout, if any
fn test_program(
    interrupt: InterruptFlag,
    program: impl AsRef<OsStr>,
    arg1: &str,
) -> Result<String> {
    let program = program.as_ref();
    let mut child = Command::new(program)
        .arg(arg1)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()?;

    // Crude way to wait for the subprocess with a timeout.
    for _ in 0..30 {
        interrupt.check_interrupted()?;

        if let Some(status) = child.try_wait()? {
            status.into_result()?;
            break;
        }

        thread::sleep(Duration::from_millis(50));
    }
    let _ = child.kill();

    let stdout = child.stdout.take().map(io::BufReader::new).unwrap();
    let first_line = stdout
        .lines()
        .next()
        .ok_or_else(|| anyhow!("No output from program {:?}", program))??;
    if first_line.is_empty() || first_line.chars().all(|c| c.is_ascii_whitespace()) {
        bail!("No output from program {:?}", program);
    }
    Ok(first_line)
}

fn run_program(
    app: &App,
    program: impl AsRef<OsStr>,
    args: &[impl AsRef<OsStr>],
    cwd: &Path,
    status: &str,
) -> Result<()> {
    let program = program.as_ref();
    if app.verbosity() >= verbosity::VERBOSE {
        app.status_bare("Command", program.to_string_lossy());
        for arg in args.iter() {
            eprint!(" {}", arg.as_ref().to_string_lossy());
        }
        eprintln!();
    }

    let mut child = Command::new(program)
        .args(args)
        .current_dir(cwd)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .with_context(|| format!("Could not run program {:?}", program))?;

    let mut ps_lines =
        ProcessLines::new(child.stdout.take().unwrap(), child.stderr.take().unwrap());

    app.subprocess_output(&mut ps_lines, program, status)?;

    let status = app
        .child_wait(&mut child)
        .with_context(|| format!("Error running program {:?}", program))?;

    if !status.success() && app.verbosity() == verbosity::NORMAL {
        app.status_bare("Command", program.to_string_lossy());
        for arg in args.iter() {
            eprint!(" {}", arg.as_ref().to_string_lossy());
        }
        eprintln!();

        let stderr = io::stderr();
        let mut stderr = stderr.lock();
        for line in ps_lines.collected_lines() {
            let _ = stderr.write_all(line);
        }
    }

    status.into_result()
}

#[derive(Debug)]
pub struct TexRenderJob<'a> {
    pub tex_file: TempPath,
    tmp_dir: TempPath,
    pdf_file: &'a Path,
    toc_sort_key: Option<&'a str>,
    reruns: u32,
}

impl<'a> TexRenderJob<'a> {
    pub fn new(
        tex_file: PathBuf,
        pdf_path: &'a Path,
        keep: u8,
        toc_sort_key: Option<&'a str>,
        reruns: u32,
    ) -> Result<Self> {
        Ok(Self {
            tex_file: TempPath::new_file(tex_file, keep < keeplevel::TEX_ONLY),
            tmp_dir: TempPath::make_temp_dir(pdf_path, keep < keeplevel::ALL)?,
            pdf_file: pdf_path,
            toc_sort_key,
            reruns,
        })
    }
}

impl<'a> TexRenderJob<'a> {
    fn cwd(&self) -> &'a Path {
        self.pdf_file.parent().unwrap()
    }

    fn sort_toc(&self) -> Result<()> {
        let key = match self.toc_sort_key {
            Some(key) => key,
            None => return Ok(()),
        };

        let tex_stem = self.tex_file.file_stem().unwrap();
        let toc = self.tmp_dir.join_stem(tex_stem, ".toc");

        if toc.exists() {
            util_cmd::sort_lines(key, &toc)
                .with_context(|| format!("Could not sort TOC file {:?}", toc))?;
        }

        Ok(())
    }

    fn move_pdf(&self) -> Result<()> {
        let tex_stem = self.tex_file.file_stem().unwrap();
        let out_pdf = self.tmp_dir.join_stem(tex_stem, ".pdf");
        fs::rename(out_pdf, self.pdf_file)
            .with_context(|| format!("Could not move to output file {:?}", self.pdf_file))
    }
}

pub struct TexTools {
    config: TexConfig,
}

impl TexTools {
    pub fn initialize(app: &App, from_settings: Option<&TexConfig>) -> Result<()> {
        app.status("Locating", "TeX tools...");

        // 1. Priority: BARD_TEX env var
        if let Some(mut config) = TexConfig::try_from_env()? {
            config.probe(app).with_context(|| {
                format!(
                    "Error using TeX distribution '{}' configured from the BARD_TEX environment variable.", config)})?;
            return Self::set(config);
        }

        // 2. Config from bard.toml
        if let Some(mut config) = from_settings.cloned() {
            config.probe(app).with_context(|| {
                format!(
                    "Error using TeX distribution '{}' configured from the bard.toml project file.",
                    config
                )
            })?;
            return Self::set(config);
        }

        // 3. No explicit config
        if cfg!(feature = "tectonic") {
            // We have embedded tectonic...
            let config = TexConfig::with_embedded_tectonic(app);
            return Self::set(config);
        } else {
            // try to probe automatically...
            for kind in [TexDistro::Xelatex, TexDistro::Tectonic] {
                let mut config = TexConfig::with_distro(kind);
                if config.probe(app).is_ok() {
                    return Self::set(config);
                }
            }
        }

        bail!("No TeX distribution found. Please install a TeX distribution. For more information see https://bard.md/book/install.html.");
    }

    pub fn get() -> impl Deref<Target = Self> {
        struct Guard(MutexGuard<'static, Option<TexTools>>);

        impl Deref for Guard {
            type Target = TexTools;

            fn deref(&self) -> &Self::Target {
                self.0.as_ref().expect("TexTools not initialized")
            }
        }

        Guard(TEX_TOOLS.lock())
    }

    fn set(config: TexConfig) -> Result<()> {
        let this = Self { config };
        *TEX_TOOLS.lock() = Some(this);
        Ok(())
    }

    pub fn render_pdf(&self, app: &App, mut job: TexRenderJob) -> Result<()> {
        if self.config.distro.is_none() {
            // TODO: test this:
            job.tex_file.set_remove(false);
            return Ok(());
        }

        app.status("Running", "TeX...");

        let args = self.config.render_args(&job);
        let program = self.config.program.as_ref().unwrap();
        let status = self.config.program_status();

        run_program(app, program, &args, job.cwd(), &status)?;
        for _ in 0..job.reruns {
            job.sort_toc()?;
            run_program(app, program, &args, job.cwd(), &status)?;
        }

        job.move_pdf()?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn tex_config_parsing() {
        let config: TexConfig = ("xelatex").parse().unwrap();
        assert_eq!(config.distro, TexDistro::Xelatex);
        assert_eq!(config.program, None);

        let config: TexConfig = ("tectonic").parse().unwrap();
        assert_eq!(config.distro, TexDistro::Tectonic);
        assert_eq!(config.program, None);

        let config: TexConfig = ("xelatex:foo:bar").parse().unwrap();
        assert_eq!(config.distro, TexDistro::Xelatex);
        assert_eq!(config.program, Some("foo:bar".to_string().into()));

        let config: TexConfig = ("tectonic:foo:bar").parse().unwrap();
        assert_eq!(config.distro, TexDistro::Tectonic);
        assert_eq!(config.program, Some("foo:bar".to_string().into()));

        let config: TexConfig = ("tectonic-embedded").parse().unwrap();
        assert_eq!(config.distro, TexDistro::TectonicEmbedded);
        assert_eq!(config.program, None);

        TexConfig::from_str("xxx").unwrap_err();
    }

    #[cfg(unix)]
    #[test]
    fn test_test_program() {
        use std::sync::atomic::AtomicBool;

        static INTERRUPT: AtomicBool = AtomicBool::new(false);
        let interrupt = InterruptFlag(&INTERRUPT);

        assert_eq!(test_program(interrupt, "echo", "hello").unwrap(), "hello");
        test_program(interrupt, "xxx-surely-this-doesnt-exist", "").unwrap_err();
        test_program(interrupt, "false", "").unwrap_err();
        test_program(interrupt, "sleep", "9800").unwrap_err();
    }
}