Skip to main content

snapper_fmt/parser/
mod.rs

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