Skip to main content

snapper_fmt/parser/
mod.rs

1pub mod latex;
2pub mod markdown;
3pub mod org;
4pub mod pandoc;
5pub mod plaintext;
6pub mod rst;
7
8/// A region of text classified by a format parser.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Region {
11    /// Prose text that should be reflowed with semantic line breaks.
12    Prose(String),
13    /// Structural content that must pass through unchanged.
14    Structure(String),
15    /// Blank line(s) preserved as paragraph separators.
16    BlankLines(String),
17}
18
19/// Trait for format-specific parsers that classify text into regions.
20pub trait FormatParser {
21    fn parse(&self, input: &str) -> Vec<Region>;
22}
23
24/// Check if a line contains a snapper pragma.
25/// Returns Some(false) for "snapper:off", Some(true) for "snapper:on", None otherwise.
26pub fn check_pragma(line: &str) -> Option<bool> {
27    let trimmed = line.trim();
28    // Strip format-specific comment markers
29    let content = trimmed
30        .strip_prefix("# ") // Org comment
31        .or_else(|| trimmed.strip_prefix("% ")) // LaTeX comment
32        .or_else(|| {
33            // HTML/Markdown comment
34            trimmed
35                .strip_prefix("<!-- ")
36                .and_then(|s| s.strip_suffix(" -->"))
37        })
38        .unwrap_or(trimmed); // Plaintext: bare pragma
39    let content = content.trim();
40    if content == "snapper:off" {
41        Some(false)
42    } else if content == "snapper:on" {
43        Some(true)
44    } else {
45        None
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn pragma_org_comment() {
55        assert_eq!(check_pragma("# snapper:off"), Some(false));
56        assert_eq!(check_pragma("# snapper:on"), Some(true));
57    }
58
59    #[test]
60    fn pragma_latex_comment() {
61        assert_eq!(check_pragma("% snapper:off"), Some(false));
62        assert_eq!(check_pragma("% snapper:on"), Some(true));
63    }
64
65    #[test]
66    fn pragma_html_comment() {
67        assert_eq!(check_pragma("<!-- snapper:off -->"), Some(false));
68        assert_eq!(check_pragma("<!-- snapper:on -->"), Some(true));
69    }
70
71    #[test]
72    fn pragma_bare() {
73        assert_eq!(check_pragma("snapper:off"), Some(false));
74        assert_eq!(check_pragma("snapper:on"), Some(true));
75    }
76
77    #[test]
78    fn pragma_none() {
79        assert_eq!(check_pragma("regular text"), None);
80        assert_eq!(check_pragma("# a comment"), None);
81        assert_eq!(check_pragma(""), None);
82    }
83}