(&self, content: &str, mut old_nodes: BTreeMap<String, Node>)
| 212 | } |
| 213 | |
| 214 | pub fn process(&self, content: &str, mut old_nodes: BTreeMap<String, Node>) -> Result<(BTreeMap<String, Node>, BTreeMap<usize, FoldInner>, Vec<usize>, bool)> { |
| 215 | // put new lines into a btree map for later |
| 216 | let (_, mut new_lines) = self.newlines.find_iter(content) |
| 217 | .map(|x| x.start()) |
| 218 | .fold((1, BTreeMap::new()), |(mut nr, mut map): (usize, BTreeMap<usize, usize>), idx| { |
| 219 | nr += 1; |
| 220 | map.insert(idx, nr); |
| 221 | |
| 222 | (nr, map) |
| 223 | }); |
| 224 | new_lines.insert(1, 1); |
| 225 | |
| 226 | let folds = self.header_regex.find_iter(content) |
| 227 | .filter_map(|x| new_lines.get(&x.start())) |
| 228 | .copied() |
| 229 | .collect::<Vec<_>>(); |
| 230 | |
| 231 | let mut nodes = BTreeMap::new(); |
| 232 | let mut any_changed = false; |
| 233 | |
| 234 | let maths = self.fences_regex.captures_iter(content) |
| 235 | .map(|x| { |
| 236 | let kind = x.name("name").unwrap().as_str(); |
| 237 | let content = x.name("inner").map_or("", |x| x.as_str()).to_string(); |
| 238 | let height = x.name("height") |
| 239 | .and_then(|x| x.as_str().parse::<usize>().ok()) |
| 240 | .unwrap_or_else(|| content.matches('\n').count() + 1); |
| 241 | let line = new_lines.get(&(x.get(0).unwrap().start() - 1)).unwrap(); |
| 242 | let id = utils::hash(&content); |
| 243 | |
| 244 | ContentType::from_fence(kind).map(|c| |
| 245 | (height, *line, content, id, c) |
| 246 | ) |
| 247 | }); |
| 248 | |
| 249 | let files = self.file_regex.captures_iter(content) |
| 250 | .map(|x| { |
| 251 | let file_name = x.name("file_name").unwrap().as_str().to_string(); |
| 252 | let height = x.name("new_lines").unwrap().as_str().len() - 1; |
| 253 | let line = new_lines.get(&x.get(0).unwrap().start()).unwrap() + 1; |
| 254 | let id = utils::hash(&file_name); |
| 255 | |
| 256 | Ok((height, line, file_name, id, ContentType::File)) |
| 257 | }); |
| 258 | |
| 259 | |
| 260 | let strcts_gen = maths.chain(files) |
| 261 | .map(|x| x.map(|(height, line, content, id, kind)| { |
| 262 | let new_range = (line, line + height); |
| 263 | |
| 264 | // try to load from existing structures |
| 265 | if let Some(mut node) = old_nodes.remove(&id) { |
| 266 | if new_range != node.range { |
| 267 | any_changed = true; |
| 268 | } |
| 269 | node.range = new_range; |
| 270 | |
| 271 | nodes.insert(id.clone(), node); |
no test coverage detected