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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
pub mod preamble;
pub mod token;
use crate::args::Args;
use pulldown_cmark::{
Alignment, CodeBlockKind, Event, HeadingLevel, LinkType, Options, Parser, Tag,
};
use regex::Regex;
use std::borrow::Cow;
use self::token::{ContextualToken, ContexualLink};
/// Use Pulldown-Cmark to perform markdown parsing and manipulation.
///
/// # Arguments
/// - `content` - Raw markdown content
/// - `args` - Parsed command line arguments
#[allow(unused_assignments)]
pub fn markdown_pipeline(content: String, args: &Args) -> String {
let parser = Parser::new_ext(&content, Options::all());
let mut content = String::new();
let mut temp_event_content = String::new();
let mut event_queue: Vec<Event> = vec![];
let mut context_event: Option<Event> = None;
let mut temp_last_token: Option<char> = None;
let mut temp_links_in_paragraph: Vec<ContextualToken> = vec![];
let mut global_links: Vec<ContextualToken> = vec![];
let mut list_index_stack: Vec<usize> = vec![];
let mut list_type_stack: Vec<Option<u64>> = vec![];
let mut table_alignment_list: Option<Vec<Alignment>> = None;
let mut table_context_stack: Vec<Event> = vec![];
let mut table_row_contents: Vec<String> = vec![];
let mut enclosed_event_stack: Vec<Event> = vec![];
parser.for_each(|ev| {
let mut should_submit = false;
match &ev {
Event::Start(tag) => {
enclosed_event_stack.push(ev.clone());
match tag {
Tag::Heading(heading_level, _, _) => {
temp_event_content = String::from("\n\n");
temp_event_content.push_str(
"#".repeat(match heading_level {
HeadingLevel::H1 => 1,
HeadingLevel::H2 => 2,
HeadingLevel::H3 => 3,
HeadingLevel::H4 => 4,
HeadingLevel::H5 => 5,
HeadingLevel::H6 => 6,
})
.as_str(),
);
temp_event_content.push_str(" ");
}
Tag::Paragraph => {
if let Some(Event::Html(_)) = event_queue.last() {
temp_event_content.push('\n');
}
if let Some(Event::Start(Tag::BlockQuote)) = context_event {
temp_event_content.push_str("\n> ");
if let Some(Event::End(Tag::Paragraph)) = event_queue.last() {
temp_event_content.push_str("\n> ");
}
} else {
if let Some(Event::Start(Tag::List(_))) = context_event.clone() {
if let Some(Event::End(Tag::Paragraph)) = event_queue.last() {
temp_event_content.push_str("\n\n");
temp_event_content.push_str(
" ".repeat(
if args.flag_google_list_style {
4
} else {
match list_type_stack.last().clone() {
Some(_) => 3,
None => 2,
}
} * list_index_stack.len(),
)
.as_str(),
);
}
} else {
match event_queue.last() {
Some(Event::End(Tag::Paragraph))
| Some(Event::End(Tag::List(..)))
| Some(Event::End(Tag::Table(..))) => {
temp_event_content.push_str("\n\n");
}
_ => (),
};
}
};
temp_links_in_paragraph = vec![];
}
Tag::List(ordered_list_first_item_number) => {
list_index_stack.push(1);
list_type_stack.push(*ordered_list_first_item_number);
temp_event_content =
if let Some(Event::End(Tag::Paragraph)) = event_queue.last() {
String::from("\n\n")
} else {
if let Some(Event::Start(Tag::List(_))) = context_event.clone() {
String::from("\n")
} else {
String::from("\n\n")
}
};
context_event = Some(ev.clone());
should_submit = true;
}
Tag::Item => {
temp_links_in_paragraph = vec![];
let num_indicator = match list_type_stack.last() {
Some(last_type) => match *last_type {
Some(_) => format!("{}.", list_index_stack.last().unwrap()),
None => {
if args.flag_google_list_style {
String::from("- ")
} else {
String::from("-")
}
}
},
None => String::from("-"),
};
if args.flag_google_list_style {
temp_event_content.push_str(
format!(
"{}{} ",
" ".repeat(list_index_stack.len() - 1),
num_indicator
)
.as_str(),
);
} else {
temp_event_content.push_str(
format!(
"{}{} ",
" ".repeat(list_index_stack.len() - 1),
num_indicator
)
.as_str(),
);
}
}
Tag::Emphasis => {
if content.chars().last().clone() != Some(' ')
&& temp_last_token.is_some()
&& content.chars().last() != Some('\n')
&& event_queue.last() != Some(&Event::Start(Tag::Paragraph))
{
temp_event_content.push_str(if let Some(' ') = temp_last_token {
""
} else {
" "
});
}
temp_event_content.push_str(if args.flag_italic_to_bold_font {
"**"
} else {
"_"
});
}
Tag::Strong => {
if content.chars().last().clone() != Some(' ')
&& temp_last_token.is_some()
&& content.chars().last() != Some('\n')
&& event_queue.last() != Some(&Event::Start(Tag::Paragraph))
{
temp_event_content.push_str(if let Some(' ') = temp_last_token {
""
} else {
" "
});
}
temp_event_content.push_str("**");
}
Tag::Strikethrough => {
if content.chars().last().clone() != Some(' ')
&& temp_last_token.is_some()
&& content.chars().last() != Some('\n')
&& event_queue.last() != Some(&Event::Start(Tag::Paragraph))
{
temp_event_content.push_str(if let Some(' ') = temp_last_token {
""
} else {
" "
});
}
temp_event_content.push_str(String::from("~~").as_str())
}
Tag::BlockQuote => {
temp_event_content = String::from("\n");
context_event = Some(ev.clone());
}
Tag::CodeBlock(code_block_kind) => {
temp_event_content = String::from("\n\n");
temp_event_content.push_str(
match code_block_kind {
CodeBlockKind::Fenced(lang) => format!("```{}\n", lang),
CodeBlockKind::Indented => format!("```\n"),
}
.as_str(),
);
context_event = Some(ev.clone());
}
Tag::Link(..) => {
// Force submit contents before a link to capture its link text.
should_submit = true;
context_event = Some(ev.clone());
}
Tag::Image(..) => {
// Force submit contents before a link to capture its link text.
should_submit = true;
context_event = Some(ev.clone());
}
Tag::Table(column_text_alignment_list) => {
context_event = Some(ev.clone());
table_alignment_list = Some(column_text_alignment_list.clone());
}
Tag::TableHead => {
table_context_stack.push(ev.clone());
}
Tag::TableRow => {
table_context_stack.push(ev.clone());
}
Tag::TableCell => {
table_context_stack.push(ev.clone());
}
Tag::FootnoteDefinition(label) => {
temp_event_content = format!("[^{}]: ", label);
context_event = Some(ev.clone());
}
};
}
Event::End(tag) => {
// enclosed_event_stack.push(ev.clone());
if let Some(Event::Start(_last_tag)) = enclosed_event_stack.last() {
if matches!(&tag, _last_tag) {
enclosed_event_stack.pop();
}
}
match tag {
Tag::Heading(_, _, _) => {
should_submit = true;
temp_event_content.push_str("\n\n");
}
Tag::Paragraph => {
should_submit = true;
if let Some(Event::End(Tag::Paragraph)) = event_queue.last() {
if let Some(Event::Start(Tag::BlockQuote)) = context_event {
temp_event_content = temp_event_content.trim().to_string();
}
}
// Auto extract links
if args.flag_extract_links {
if let Some(Event::Start(Tag::List(_))) = enclosed_event_stack.last() {
// Do not extract link from paragraph when in list scopes.
} else {
let mut should_append_links = false;
if let None = context_event.clone() {
should_append_links = true;
}
if should_append_links {
let mut temp_link_contents = vec![];
let prefix = format!(
"{}-{}",
" ".repeat(
if args.flag_google_list_style { 4 } else { 2 }
* if list_index_stack.len() > 0 {
list_index_stack.len() - 1
} else {
0
}
),
" ".repeat(if args.flag_google_list_style { 3 } else { 1 })
);
for link in temp_links_in_paragraph.iter() {
match link {
ContextualToken::Link(ContexualLink::Bare(url)) => {
if url.len() > 0 {
temp_link_contents
.push(format!("<url>{}</url>", url));
}
}
ContextualToken::Link(ContexualLink::Titled(
url,
title,
)) => {
if url.len() > 0 {
temp_link_contents.push(
if temp_links_in_paragraph.len() > 1 {
format!(
"{}{}: <url>{}</url>",
prefix, title, url
)
} else {
format!(" <url>{}</url>", url)
},
);
}
}
}
}
if temp_link_contents.len() > 1 {
temp_event_content.push_str("\n");
}
temp_event_content.push_str(
format!("{}\n", temp_link_contents.join("\n")).as_str(),
);
temp_link_contents.clear();
}
}
}
}
Tag::List(_) => {
list_index_stack.pop();
list_type_stack.pop();
temp_event_content.push_str("\n");
}
Tag::Item => {
if let Some(last) = list_index_stack.last_mut() {
*last += 1;
}
// Auto extract links
if args.flag_extract_links {
let mut temp_link_contents = vec![];
for link in temp_links_in_paragraph.iter() {
let url = match link {
ContextualToken::Link(ContexualLink::Bare(url)) => url,
ContextualToken::Link(ContexualLink::Titled(url, _)) => url,
};
if url.len() > 0 {
temp_link_contents.push(format!("<url>{}</url>", url));
}
}
if temp_link_contents.len() > 0 {
if temp_link_contents.len() > 1 {
// Multiple URL, should append line break first.
temp_event_content.push_str("\n");
}
temp_event_content
.push_str(format!("{}", temp_link_contents.join("")).as_str());
temp_link_contents.clear();
temp_links_in_paragraph.clear();
}
}
temp_event_content.push_str("\n");
}
Tag::Emphasis => {
should_submit = true;
if args.flag_italic_to_bold_font {
temp_event_content.push_str(String::from("** ").as_str());
} else {
temp_event_content.push_str("_ ");
}
}
Tag::Strong => {
should_submit = true;
temp_event_content.push_str(String::from("** ").as_str());
}
Tag::Strikethrough => {
should_submit = true;
temp_event_content.push_str(String::from("~~ ").as_str());
}
Tag::BlockQuote => {
should_submit = true;
temp_event_content = format!("\n\n");
context_event = None;
}
Tag::CodeBlock(_) => {
should_submit = true;
temp_event_content.push_str("```\n\n");
context_event = None;
}
Tag::Link(link_type, url, title) => {
let mut link = url.to_string();
if title.as_bytes() != b"" {
link.push_str(format!(" {}", title).as_str());
}
let mut contextual_link: Option<ContexualLink> = None;
temp_event_content = match *link_type {
LinkType::Autolink => {
let _link = if args.flag_extract_links_without_anchor {
Regex::new(r"#.*$")
.unwrap()
.replace(link.as_str(), "")
.to_string()
} else {
link.clone()
};
contextual_link = Some(ContexualLink::Bare(Cow::Owned(_link)));
format!("<{}>", link)
}
LinkType::Inline | LinkType::Email => {
let mut _link = if args.flag_extract_links_without_anchor {
Regex::new(r"#.*$")
.unwrap()
.replace(link.as_str(), "")
.to_string()
} else {
link.clone()
};
_link = Regex::new(r"^mailto:")
.unwrap()
.replace(_link.as_str(), "")
.to_string();
contextual_link = Some(ContexualLink::Titled(
Cow::Owned(_link),
Cow::Owned(temp_event_content.clone()),
));
format!("[{}]({})", &temp_event_content, link)
}
LinkType::Reference => {
format!("[{}][{}]", &temp_event_content, link)
}
LinkType::ReferenceUnknown => {
format!("[{}][{}]", &temp_event_content, link)
}
LinkType::Collapsed => {
format!("[{}][]", &temp_event_content)
}
LinkType::CollapsedUnknown => {
format!("[{}][]", &temp_event_content)
}
LinkType::Shortcut => format!("[{}]", &temp_event_content),
LinkType::ShortcutUnknown => format!("[{}]", &temp_event_content),
};
if let Some(l) = contextual_link {
temp_links_in_paragraph.push(ContextualToken::Link(l.clone()));
global_links.push(ContextualToken::Link(l));
}
if let Some(Event::Start(Tag::TableCell)) = enclosed_event_stack.last() {
should_submit = false;
// 表格中的链接不生成段后引用,但可以计入全局
temp_links_in_paragraph.clear();
} else {
should_submit = true;
}
context_event = None;
}
Tag::Image(link_type, url, title) => {
let mut link = url.to_string();
if title.as_bytes() != b"" {
link.push_str(format!(" {}", title).as_str());
}
temp_event_content = match *link_type {
LinkType::Inline => {
format!("\n\n\n", &temp_event_content, link)
}
_ => panic!("Unsupported format"),
};
should_submit = true;
context_event = None;
}
Tag::Table(_) => {
context_event = None;
table_context_stack.clear();
should_submit = true;
temp_event_content = "\n".to_string();
}
Tag::TableHead => {
table_context_stack.pop();
let mut temp_line = vec![];
for col in table_row_contents.clone() {
temp_line.push(col);
}
let temp_line_joined = temp_line.join(" | ");
temp_event_content = if args.flag_enclose_table {
format!("\n\n| {} |\n", temp_line_joined)
} else {
format!("\n\n{}\n", temp_line_joined)
};
// 去除因为空单元格产生的两个连续空格
temp_event_content = temp_event_content.replace(" ", " ");
temp_line.clear();
for align in table_alignment_list.clone().unwrap() {
let symbol = String::from(match align {
Alignment::Left => " :-- ",
Alignment::Right => " --: ",
Alignment::Center => " :-: ",
Alignment::None => " --- ",
});
temp_line.push(symbol);
}
let temp_line_joined = temp_line.join("|");
if args.flag_enclose_table {
temp_event_content.push_str(format!("|{}|", temp_line_joined).as_str());
} else {
temp_event_content.push_str(&temp_line_joined);
};
should_submit = true;
table_row_contents.clear();
}
Tag::TableRow => {
table_context_stack.pop();
let mut temp_line = vec![];
for col in table_row_contents.clone() {
temp_line.push(col);
}
let temp_line_joined = temp_line.join(" | ");
temp_event_content = if args.flag_enclose_table {
format!("\n| {} |", temp_line_joined)
} else {
format!("\n{}", temp_line_joined)
};
// 去除因为空单元格产生的两个连续空格
temp_event_content = temp_event_content.replace(" ", " ");
should_submit = true;
table_row_contents.clear();
}
Tag::TableCell => {
// 每当单元格扫描完成,需要提交处理,然后清理其中的内容。
table_context_stack.pop();
table_row_contents.push(temp_event_content.clone());
temp_event_content.clear();
}
Tag::FootnoteDefinition(_) => {
should_submit = true;
context_event = None;
}
}
}
Event::Html(s) => {
should_submit = true;
if let Some(e) = enclosed_event_stack.last() {
// 行内 HTML,不适用其他标记分隔
temp_event_content = match e {
Event::End(Tag::Paragraph) | Event::End(Tag::List(_)) => String::from("\n"),
_ => String::new(),
};
} else {
if let Some(Event::End(Tag::Paragraph)) = event_queue.last() {
// 处理一段之后单换行 (\n) 接着立即开始 HTML 的形式
temp_event_content = String::from("\n")
}
}
temp_event_content.push_str(s);
}
Event::Text(s) => {
should_submit = true;
if check_event_environment(&enclosed_event_stack, &Tag::BlockQuote) {
if let Some(Event::SoftBreak) = event_queue.last() {
temp_event_content.push_str("> ");
}
}
// 使用 Enclosed stack 来判断更简洁准确。
if let Some(Event::Start(tag)) = enclosed_event_stack.last() {
match tag {
Tag::Link(..)
| Tag::Image(..)
| Tag::Table(..)
| Tag::TableRow
| Tag::TableCell => {
should_submit = false;
}
_ => (),
}
}
if !Regex::new(r"[.,(),。!!??“;;::\]\[]]")
.unwrap()
.is_match(s)
{
match event_queue.last() {
Some(pev) => match pev {
Event::Code(_)
| Event::End(Tag::Emphasis)
| Event::End(Tag::Strong) => temp_event_content.push_str(" "),
_ => (),
},
_ => (),
}
}
temp_event_content.push_str(s);
}
Event::Code(s) => {
should_submit = true;
if let Some(Event::Start(Tag::Link(_, _, _))) = enclosed_event_stack.last() {
should_submit = false;
}
if let Some(Event::Start(Tag::Paragraph)) = enclosed_event_stack.last() {
if content.chars().last().clone() != Some(' ')
&& temp_last_token.is_some()
&& content.chars().last() != Some('\n')
&& event_queue.last() != Some(&Event::Start(Tag::Paragraph))
{
if let Some(' ') = temp_last_token {
// Do nothing
} else {
temp_event_content.push(' ');
}
}
}
temp_event_content.push_str(format!("`{}`", s).as_str());
}
Event::FootnoteReference(s) => {
temp_event_content += format!("[^{}]", s).as_str();
}
Event::TaskListMarker(b) => {
should_submit = true;
temp_event_content = if event_queue.len() >= 3 {
if let Some(Event::Start(Tag::List(_))) = event_queue.get(event_queue.len() - 2)
{
if let Some(Event::End(Tag::Paragraph)) =
event_queue.get(event_queue.len() - 3)
{
String::from("\n\n")
} else {
String::from("\n")
}
} else {
String::from("\n")
}
} else {
String::new()
};
temp_event_content.push_str(
if args.flag_google_list_style {
" "
} else {
" "
}
.repeat(list_index_stack.len() - 1)
.as_str(),
);
if args.flag_google_list_style {
temp_event_content.push_str(if *b { "- [ ] " } else { "- [x] " })
} else {
temp_event_content.push_str(if *b { "- [ ] " } else { "- [x] " })
};
}
Event::SoftBreak => {
should_submit = true;
temp_event_content = String::from("\n");
}
Event::HardBreak => {
should_submit = true;
temp_event_content = String::from("\n\n");
}
Event::Rule => {
should_submit = true;
temp_event_content = String::from("\n\n---\n\n")
}
}
if event_queue.len() >= 5 {
event_queue.remove(0);
}
event_queue.push(ev.clone());
#[cfg(feature = "debug")]
if args.debug_markdown_tag_pair {
println!("Enclosed event stack: {:?}", enclosed_event_stack);
}
if args.debug {
println!("[DEBUG] Temp content: {:?}", temp_event_content);
println!(" Event queue: {:?}", event_queue);
println!(" Context event: {:?}", context_event);
if let Some(Event::Start(Tag::List(_))) = context_event.clone() {
println!(" List index stack: {:?}", list_index_stack);
println!(" List type stack: {:?}", list_type_stack);
}
}
if should_submit {
content.push_str(&temp_event_content);
should_submit = false;
temp_last_token = content.chars().last();
temp_event_content = String::new();
}
});
let list_regex = Regex::new(r"^\s{4}*((- )|(\d+\.)) {2}[^\s].*\n?$").unwrap();
let reduce_whitespace_regex = Regex::new(r"(?P<w1>[^\s]) +(?P<w2>[^\s])").unwrap();
if args.flag_google_list_style {
let mut temp_lines: Vec<String> = vec![];
for line in content.lines() {
if list_regex.is_match(line) {
temp_lines.push(line.to_string());
} else {
let new_line = reduce_whitespace_regex.replace_all(line, r"${w1} ${w2}");
temp_lines.push(new_line.to_string());
}
}
content = temp_lines.join("\n");
} else {
content = reduce_whitespace_regex
.replace_all(&content, r"${w1} ${w2}")
.to_string();
}
// Trim whitespace: asterisks and brackets after Chinese symbols
content = Regex::new(r"(?P<w1>[,。?:“”!¥()\n]) +(?P<w2>[\*\[]])")
.unwrap()
.replace_all(&content, r"${w1}${w2}")
.to_string();
// Trim whitespace: asterisks and brackets before English symbols
content = Regex::new(r#"(?P<w1>[*\]]) +(?P<w2>[,。?:“”!!?"'{:;,./¥()\n])"#)
.unwrap()
.replace_all(&content, r"${w1}${w2}")
.to_string();
content = Regex::new(r"(?P<w1>[\u4e00-\u9fff]) +\*")
.unwrap()
.replace_all(&content, r"${w1} *")
.to_string();
content = Regex::new(r"(?P<w1>[^\s]) +\n")
.unwrap()
.replace_all(&content, "${w1}\n")
.to_string();
content = Regex::new(r"(?P<w1>[^\s])\n{3,}(?P<w2>[^\s])")
.unwrap()
.replace_all(&content, "${w1}\n\n${w2}")
.to_string();
content = Regex::new(r"(?P<w1>[^\s]*) {2,}(?P<w2>[`_*])")
.unwrap()
.replace_all(&content, "${w1} ${w2}")
.to_string();
content = Regex::new(r": (?P<w2>[`_*]\s)")
.unwrap()
.replace_all(&content, ":${w2}")
.to_string();
content.trim().to_string()
}
fn check_event_environment(stack: &Vec<Event>, target: &Tag) -> bool {
let mut enclosed = false;
let mut iterator = stack.len() - 1;
while iterator > 0 {
if let Some(Event::Start(_tag)) = stack.get(iterator) {
if matches!(target, _tag) {
enclosed = true;
}
}
iterator -= 1;
}
enclosed
}