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
/// a composite is a group of compounds. It can be a whole line,
/// or a table cell
///
use crate::compound::Compound;
use crate::line_parser::LineParser;

/// The global style of a composite
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CompositeStyle {
    Paragraph,
    Header(u8), // never 0, and <= MAX_HEADER_DEPTH
    ListItem,
    Code,
    Quote,
}

/// a composite is a monoline sequence of compounds.
/// It's defined by
/// - the global style of the composite, if any
/// - a vector of styled parts
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Composite<'a> {
    pub style: CompositeStyle,
    pub compounds: Vec<Compound<'a>>,
}

impl<'a> From<Vec<Compound<'a>>> for Composite<'a> {
    fn from(compounds: Vec<Compound<'a>>) -> Composite<'a> {
        Composite {
            style: CompositeStyle::Paragraph,
            compounds,
        }
    }
}

impl<'a> Composite<'a> {
    pub fn new() -> Composite<'a> {
        Composite {
            style: CompositeStyle::Paragraph,
            compounds: Vec::new(),
        }
    }
    /// parse a monoline markdown snippet which isn't from a text.
    pub fn from_inline(md: &'a str) -> Composite<'a> {
        LineParser::from(md).inline()
    }
    #[inline(always)]
    pub fn is_code(&self) -> bool {
        match self.style {
            CompositeStyle::Code { .. } => true,
            _ => false,
        }
    }
    #[inline(always)]
    pub fn is_list_item(&self) -> bool {
        match self.style {
            CompositeStyle::ListItem { .. } => true,
            _ => false,
        }
    }
    #[inline(always)]
    pub fn is_quote(&self) -> bool {
        match self.style {
            CompositeStyle::Quote { .. } => true,
            _ => false,
        }
    }
    /// return the total number of characters in the composite
    ///
    /// Example
    /// ```rust
    /// assert_eq!(minimad::Line::from("τ:`2π`").char_length(), 4);
    /// ```
    ///
    /// This may not be the visible width: a renderer can
    ///  add some things (maybe some caracters) to wrap inline code,
    ///  or a bullet in front of a list item
    #[inline(always)]
    pub fn char_length(&self) -> usize {
        self.compounds
            .iter()
            .fold(0, |sum, compound| sum + compound.as_str().chars().count())
    }
    /// remove all white spaces at left, unless in inline code
    /// Empty compounds are cleaned out
    pub fn trim_left_spaces(&mut self) {
        loop {
            if self.compounds.len() == 0 {
                break;
            }
            if self.compounds[0].code {
                break;
            }
            self.compounds[0].trim_left_spaces();
            if self.compounds[0].is_empty() {
                self.compounds.remove(0);
            } else {
                break;
            }
        }
    }
    /// remove all white spaces at right, unless in inline code
    /// Empty compounds are cleaned out
    pub fn trim_right_spaces(&mut self) {
        loop {
            if self.compounds.len() == 0 {
                break;
            }
            let last = self.compounds.len() - 1;
            if self.compounds[last].code {
                break;
            }
            self.compounds[last].trim_right_spaces();
            if self.compounds[last].is_empty() {
                self.compounds.remove(last);
            } else {
                break;
            }
        }
    }
    pub fn trim_spaces(&mut self) {
        self.trim_left_spaces();
        self.trim_right_spaces();
    }
    pub fn is_empty(&self) -> bool {
        self.compounds.len() == 0
    }
}

// Tests trimming composite
#[cfg(test)]
mod tests {
    use crate::composite::*;
    use crate::compound::*;

    #[test]
    fn composite_trim() {
        let mut left = Composite::from_inline(" *some* text  ");
        left.trim_spaces();
        assert_eq!(
            left,
            Composite {
                style: CompositeStyle::Paragraph,
                compounds: vec![
                    Compound::raw_str("some").italic(),
                    Compound::raw_str(" text"),
                ]
            }
        );
    }

    #[test]
    fn composite_trim_keep_code() {
        let mut left = Composite::from_inline(" ` `  ");
        left.trim_spaces();
        assert_eq!(
            left,
            Composite {
                style: CompositeStyle::Paragraph,
                compounds: vec![
                    Compound::raw_str(" ").code(),
                ]
            }
        );
    }

    #[test]
    fn empty_composite_trim() {
        let mut left = Composite::from_inline(" * * ** `` **  ");
        left.trim_left_spaces();
        assert_eq!(left.compounds.len(), 0);
    }
}