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
//! Module providing iterators over the hyperlinks found in the input text. //! Only HTML no other markup languages are parsed here. use crate::parser::parse::take_img_link; use crate::parser::parse::take_link; use parse_hyperlinks::parser::Link; use std::borrow::Cow; #[derive(Debug, PartialEq)] /// Iterator over the inline hyperlinks in the HTML formatted `input` text. /// This struct holds the iterator's state as an advancing pointer into the `input` text. /// The iterator's `next()` method returns a tuple with 2 tuples inside: /// `Some(((input_split)(html_hyperlink_element)))`. /// /// Each tuple has the following parts: /// * `input_split = (skipped_characters, consumed_characters, remaining_characters)` /// * `html_hyperlink_element = (text_text, link_destination, link_title)` /// /// # Input split /// /// ``` /// use parse_hyperlinks_html::iterator::Hyperlink; /// use std::borrow::Cow; /// /// let i = "abc<a href=\"dest1\" title=\"title1\">text1</a>abc\n\ /// abc<a href=\"dest2\" title=\"title2\">text2</a>xyz"; /// /// let mut iter = Hyperlink::new(i); /// assert_eq!(iter.next().unwrap().0, /// ("abc", /// "<a href=\"dest1\" title=\"title1\">text1</a>", /// "abc\nabc<a href=\"dest2\" title=\"title2\">text2</a>xyz") /// ); /// assert_eq!(iter.next().unwrap().0, /// ("abc\nabc", /// "<a href=\"dest2\" title=\"title2\">text2</a>", /// "xyz") /// ); /// assert_eq!(iter.next(), None); /// ``` /// # Link content /// ## HTML /// /// ``` /// use parse_hyperlinks_html::iterator::Hyperlink; /// use std::borrow::Cow; /// /// let i = "abc<a href=\"dest1\" title=\"title1\">text1</a>abc\ /// abc<a href=\"dest2\" title=\"title2\">text2</a>abc"; /// /// /// let mut iter = Hyperlink::new(i); /// assert_eq!(iter.next().unwrap().1, (Cow::from("text1"), Cow::from("dest1"), Cow::from("title1"))); /// assert_eq!(iter.next().unwrap().1, (Cow::from("text2"), Cow::from("dest2"), Cow::from("title2"))); /// assert_eq!(iter.next(), None); /// ``` pub struct Hyperlink<'a> { /// The remaining text input. input: &'a str, } /// Constructor for the `Hyperlink` struct. impl<'a> Hyperlink<'a> { /// Constructor for the iterator. `input` is the text with inline images to be /// extracted. #[inline] pub fn new(input: &'a str) -> Self { Self { input } } } /// Iterator over the HTML inline images in the `input`-text. /// The iterator's `next()` method returns a tuple with 2 tuples inside: /// * `Some(((input_split)(link_content)))` /// /// Each tuple has the following parts: /// * `input_split = (skipped_characters, consumed_characters, remaining_characters)` /// * `link_content = (link_text, link_destination, link_title)` /// impl<'a> Iterator for Hyperlink<'a> { type Item = ( (&'a str, &'a str, &'a str), (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>), ); fn next(&mut self) -> Option<Self::Item> { let mut output = None; if let Ok((remaining_input, (skipped, Link::Text2Dest(link_text, link_dest, link_title)))) = take_link(self.input) { let consumed = &self.input[skipped.len()..self.input.len() - remaining_input.len()]; // Assigning output. output = Some(( (skipped, consumed, remaining_input), (link_text, link_dest, link_title), )); debug_assert_eq!(self.input, { let mut s = "".to_string(); s.push_str(skipped); s.push_str(consumed); s.push_str(remaining_input); s }); self.input = remaining_input; }; output } } #[derive(Debug, PartialEq)] /// Iterator over the inline images in the HTML formatted `input` text. /// This struct holds the iterator's state, as an advancing pointer into the `input` text. The /// iterator's `next()` method returns a tuple with 2 tuples inside: /// `Some(((input_split)(html_image_element)))`. /// /// Each tuple has the following parts: /// * `input_split = (skipped_characters, consumed_characters, remaining_characters)` /// * `html_image_element = (img_src, img_alt)` /// /// # Input split /// /// ``` /// use parse_hyperlinks_html::iterator::InlineImage; /// use std::borrow::Cow; /// /// let i = r#"abc<img src="dest1" alt="text1">efg<img src="dest2" alt="text2">hij"#; /// /// let mut iter = InlineImage::new(i); /// assert_eq!(iter.next().unwrap().0, ("abc", r#"<img src="dest1" alt="text1">"#, /// r#"efg<img src="dest2" alt="text2">hij"#)); /// assert_eq!(iter.next().unwrap().0, ("efg", r#"<img src="dest2" alt="text2">"#, /// "hij")); /// assert_eq!(iter.next(), None); /// ``` /// # Link content /// ## HTML /// /// ``` /// use parse_hyperlinks_html::iterator::InlineImage; /// use std::borrow::Cow; /// /// let i = r#"abc<img src="dest1" alt="text1">abc /// abc<img src="dest2" alt="text2">abc /// "#; /// /// let mut iter = InlineImage::new(i); /// assert_eq!(iter.next().unwrap().1, (Cow::from("text1"), Cow::from("dest1"))); /// assert_eq!(iter.next().unwrap().1, (Cow::from("text2"), Cow::from("dest2"))); /// assert_eq!(iter.next(), None); /// ``` pub struct InlineImage<'a> { /// The remaining text input. input: &'a str, } /// Constructor for the `Hyperlink` struct. impl<'a> InlineImage<'a> { /// Constructor for the iterator. `input` is the text with inline images to be /// extracted. #[inline] pub fn new(input: &'a str) -> Self { Self { input } } } /// Iterator over the HTML inline images in the `input`-text. /// The iterator's `next()` method returns a tuple with 2 tuples inside: /// * `Some(((input_split)(link_content)))` /// /// Each tuple has the following parts: /// * `input_split = (skipped_characters, consumed_characters, remaining_characters)` /// * `link_content = (image_alt, image_src)` /// impl<'a> Iterator for InlineImage<'a> { type Item = ((&'a str, &'a str, &'a str), (Cow<'a, str>, Cow<'a, str>)); fn next(&mut self) -> Option<Self::Item> { let mut output = None; if let Ok((remaining_input, (skipped, Link::Image(alt, src)))) = take_img_link(self.input) { let consumed = &self.input[skipped.len()..self.input.len() - remaining_input.len()]; // Assigning output. output = Some(((skipped, consumed, remaining_input), (alt, src))); debug_assert_eq!(self.input, { let mut s = "".to_string(); s.push_str(skipped); s.push_str(consumed); s.push_str(remaining_input); s }); self.input = remaining_input; }; output } }