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
use std::borrow::Cow;

use nom::{
    bytes::complete::{tag, take_while1},
    character::complete::space0,
    sequence::delimited,
    IResult,
};

use crate::parse::combinators::{blank_lines_count, eol, lines_till};

/// Drawer Element
#[derive(Debug, Default, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "ser", derive(serde::Serialize))]
pub struct Drawer<'a> {
    /// Drawer name
    pub name: Cow<'a, str>,
    /// Numbers of blank lines between first drawer's line and next non-blank
    /// line
    pub pre_blank: usize,
    /// Numbers of blank lines between last drawer's line and next non-blank
    /// line or buffer's end
    pub post_blank: usize,
}

impl Drawer<'_> {
    pub(crate) fn parse(input: &str) -> Option<(&str, (Drawer, &str))> {
        parse_drawer(input).ok()
    }

    pub fn into_owned(self) -> Drawer<'static> {
        Drawer {
            name: self.name.into_owned().into(),
            pre_blank: self.pre_blank,
            post_blank: self.post_blank,
        }
    }
}

#[inline]
pub fn parse_drawer(input: &str) -> IResult<&str, (Drawer, &str), ()> {
    let (input, (mut drawer, content)) = parse_drawer_without_blank(input)?;

    let (content, blank) = blank_lines_count(content)?;
    drawer.pre_blank = blank;

    let (input, blank) = blank_lines_count(input)?;
    drawer.post_blank = blank;

    Ok((input, (drawer, content)))
}

pub fn parse_drawer_without_blank(input: &str) -> IResult<&str, (Drawer, &str), ()> {
    let (input, _) = space0(input)?;
    let (input, name) = delimited(
        tag(":"),
        take_while1(|c: char| c.is_ascii_alphabetic() || c == '-' || c == '_'),
        tag(":"),
    )(input)?;
    let (input, _) = eol(input)?;
    let (input, contents) = lines_till(|line| line.trim().eq_ignore_ascii_case(":END:"))(input)?;

    Ok((
        input,
        (
            Drawer {
                name: name.into(),
                pre_blank: 0,
                post_blank: 0,
            },
            contents,
        ),
    ))
}

#[test]
fn parse() {
    assert_eq!(
        parse_drawer(
            r#":PROPERTIES:
  :CUSTOM_ID: id
  :END:"#
        ),
        Ok((
            "",
            (
                Drawer {
                    name: "PROPERTIES".into(),
                    pre_blank: 0,
                    post_blank: 0
                },
                "  :CUSTOM_ID: id\n"
            )
        ))
    );
    assert_eq!(
        parse_drawer(
            r#":PROPERTIES:


  :END:

"#
        ),
        Ok((
            "",
            (
                Drawer {
                    name: "PROPERTIES".into(),
                    pre_blank: 2,
                    post_blank: 1,
                },
                ""
            )
        ))
    );

    // https://github.com/PoiScript/orgize/issues/9
    assert!(parse_drawer(":SPAGHETTI:\n").is_err());
}