-
Notifications
You must be signed in to change notification settings - Fork 27
/
lib.rs
1278 lines (1169 loc) · 36.8 KB
/
lib.rs
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Helpers for translating `mdbook` projects.
//!
//! The functions here are used to implement a robust
//! internationalization (i18n) workflow for `mdbook`. This allows you
//! to translate your books into other languages while also making it
//! easy to keep the translations up to date as you edit the original
//! source text.
//!
//! See <https://github.com/google/mdbook-i18n-helpers> for details on
//! how to use the supplied `mdbook` plugins.
use polib::catalog::Catalog;
use pulldown_cmark::{Event, LinkType, Tag};
use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options, State};
use regex::Regex;
use std::sync::OnceLock;
pub mod normalize;
/// Like `mdbook::utils::new_cmark_parser`, but also passes a
/// `BrokenLinkCallback`.
pub fn new_cmark_parser<'input, 'callback>(
text: &'input str,
broken_link_callback: pulldown_cmark::BrokenLinkCallback<'input, 'callback>,
) -> pulldown_cmark::Parser<'input, 'callback> {
let mut options = pulldown_cmark::Options::empty();
options.insert(pulldown_cmark::Options::ENABLE_TABLES);
options.insert(pulldown_cmark::Options::ENABLE_FOOTNOTES);
options.insert(pulldown_cmark::Options::ENABLE_STRIKETHROUGH);
options.insert(pulldown_cmark::Options::ENABLE_TASKLISTS);
options.insert(pulldown_cmark::Options::ENABLE_HEADING_ATTRIBUTES);
pulldown_cmark::Parser::new_with_broken_link_callback(text, options, broken_link_callback)
}
/// Extract Markdown events from `text`.
///
/// The `state` can be used to give the parsing context. In
/// particular, if a code block has started, the text should be parsed
/// without interpreting special Markdown characters.
///
/// The events are labeled with the line number where they start in
/// the document.
///
/// # Examples
///
/// ```
/// use mdbook_i18n_helpers::extract_events;
/// use pulldown_cmark::{Event, Tag};
///
/// assert_eq!(
/// extract_events("Hello,\nworld!", None),
/// vec![
/// (1, Event::Start(Tag::Paragraph)),
/// (1, Event::Text("Hello,".into())),
/// (1, Event::Text(" ".into())),
/// (2, Event::Text("world!".into())),
/// (1, Event::End(Tag::Paragraph)),
/// ]
/// );
/// ```
pub fn extract_events<'a>(text: &'a str, state: Option<State<'static>>) -> Vec<(usize, Event<'a>)> {
// Offsets of each newline in the input, used to calculate line
// numbers from byte offsets.
let offsets = text
.match_indices('\n')
.map(|(offset, _)| offset)
.collect::<Vec<_>>();
fn expand_shortcut_link(tag: Tag) -> Tag {
match tag {
Tag::Link(LinkType::Shortcut, reference, title) => {
Tag::Link(LinkType::Reference, reference, title)
}
Tag::Image(LinkType::Shortcut, reference, title) => {
Tag::Image(LinkType::Reference, reference, title)
}
_ => tag,
}
}
match state {
// If we're in a code block, we disable the normal parsing and
// return lines of text. This matches the behavior of the
// parser in this case.
Some(state) if state.is_in_code_block => text
.split_inclusive('\n')
.enumerate()
.map(|(idx, line)| (idx + 1, Event::Text(line.into())))
.collect(),
// Otherwise, we parse the text line normally.
_ => new_cmark_parser(text, None)
.into_offset_iter()
.map(|(event, range)| {
let lineno = offsets.partition_point(|&o| o < range.start) + 1;
let event = match event {
Event::SoftBreak => Event::Text(" ".into()),
// Shortcut links like "[foo]" end up as "[foo]"
// in output. By changing them to a reference
// link, the link is expanded on the fly and the
// output becomes self-contained.
Event::Start(tag @ Tag::Link(..) | tag @ Tag::Image(..)) => {
Event::Start(expand_shortcut_link(tag))
}
Event::End(tag @ Tag::Link(..) | tag @ Tag::Image(..)) => {
Event::End(expand_shortcut_link(tag))
}
_ => event,
};
(lineno, event)
})
.collect(),
}
}
/// Markdown events grouped by type.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Group<'a> {
/// Markdown events which should be translated.
///
/// This includes `[Text("foo")]` as well as sequences with text
/// such as `[Start(Emphasis), Text("foo") End(Emphasis)]`.
Translate(&'a [(usize, Event<'a>)]),
/// Markdown events which should be skipped when translating.
///
/// This includes structural events such as `Start(Heading(H1,
/// None, vec![]))`.
Skip(&'a [(usize, Event<'a>)]),
}
/// Group Markdown events into translatable and skipped events.
///
/// This function will partition the input events into groups of
/// events which should be translated or skipped. Concatenating the
/// events in each group will give you back the original events.
///
/// # Examples
///
/// ```
/// use mdbook_i18n_helpers::{extract_events, group_events, Group};
/// use pulldown_cmark::{Event, Tag};
///
/// let events = extract_events("- A list item.", None);
/// assert_eq!(
/// events,
/// vec![
/// (1, Event::Start(Tag::List(None))),
/// (1, Event::Start(Tag::Item)),
/// (1, Event::Text("A list item.".into())),
/// (1, Event::End(Tag::Item)),
/// (1, Event::End(Tag::List(None))),
/// ],
/// );
///
/// let groups = group_events(&events);
/// assert_eq!(
/// groups,
/// vec![
/// Group::Skip(&[
/// (1, Event::Start(Tag::List(None))),
/// (1, Event::Start(Tag::Item)),
/// ]),
/// Group::Translate(&[
/// (1, Event::Text("A list item.".into())),
/// ]),
/// Group::Skip(&[
/// (1, Event::End(Tag::Item)),
/// (1, Event::End(Tag::List(None))),
/// ]),
/// ]
/// );
/// ```
pub fn group_events<'a>(events: &'a [(usize, Event<'a>)]) -> Vec<Group<'a>> {
let mut groups = Vec::new();
#[derive(Debug)]
struct GroupingContext {
skip_next_group: bool,
// TODO: this struct is planned to expand with translator
// comments and message contexts.
}
impl GroupingContext {
fn clear_skip_next_group(self) -> Self {
Self {
skip_next_group: false,
}
}
}
#[derive(Debug)]
enum State {
Translate(usize),
Skip(usize),
}
impl State {
/// Creates a group based on the capturing state and context.
fn into_group<'a>(
self,
idx: usize,
events: &'a [(usize, Event<'a>)],
ctx: GroupingContext,
) -> (Group<'a>, GroupingContext) {
match self {
State::Translate(start) => {
if ctx.skip_next_group {
(
Group::Skip(&events[start..idx]),
ctx.clear_skip_next_group(),
)
} else if is_nontranslatable_codeblock_group(&events[start..idx]) {
(Group::Skip(&events[start..idx]), ctx)
} else {
(Group::Translate(&events[start..idx]), ctx)
}
}
State::Skip(start) => (Group::Skip(&events[start..idx]), ctx),
}
}
}
let mut state = State::Skip(0);
let mut ctx = GroupingContext {
skip_next_group: false,
};
for (idx, (_, event)) in events.iter().enumerate() {
match event {
// These block-level events force new groups. We do this
// because we want to include these events in the group to
// make the group self-contained.
Event::Start(Tag::Paragraph | Tag::CodeBlock(..)) => {
// A translatable group starts here.
let next_group;
(next_group, ctx) = state.into_group(idx, events, ctx);
groups.push(next_group);
state = State::Translate(idx);
}
Event::End(Tag::Paragraph | Tag::CodeBlock(..)) => {
// A translatable group ends after `idx`.
let idx = idx + 1;
let next_group;
(next_group, ctx) = state.into_group(idx, events, ctx);
groups.push(next_group);
state = State::Skip(idx);
}
// Inline events start or continue a translating group.
Event::Start(
Tag::Emphasis | Tag::Strong | Tag::Strikethrough | Tag::Link(..) | Tag::Image(..),
)
| Event::End(
Tag::Emphasis | Tag::Strong | Tag::Strikethrough | Tag::Link(..) | Tag::Image(..),
)
| Event::Text(_)
| Event::Code(_)
| Event::FootnoteReference(_)
| Event::SoftBreak
| Event::HardBreak => {
// If we're currently skipping, then a new
// translatable group starts here.
if let State::Skip(_) = state {
let next_group;
(next_group, ctx) = state.into_group(idx, events, ctx);
groups.push(next_group);
state = State::Translate(idx);
}
}
// An HTML comment directive to skip the next translation
// group.
Event::Html(s) if is_comment_skip_directive(s) => {
// If in the middle of translation, finish it.
if let State::Translate(_) = state {
let next_group;
(next_group, ctx) = state.into_group(idx, events, ctx);
groups.push(next_group);
// Restart translation: subtle but should be
// needed to handle the skipping of the rest of
// the inlined content.
state = State::Translate(idx);
}
ctx.skip_next_group = true;
}
// All other block-level events start or continue a
// skipping group.
_ => {
if let State::Translate(_) = state {
let next_group;
(next_group, ctx) = state.into_group(idx, events, ctx);
groups.push(next_group);
state = State::Skip(idx);
}
}
}
}
match state {
State::Translate(start) => groups.push(Group::Translate(&events[start..])),
State::Skip(start) => groups.push(Group::Skip(&events[start..])),
}
groups
}
/// Check whether the HTML is a directive to skip the next translation group.
fn is_comment_skip_directive(html: &str) -> bool {
static RE: OnceLock<Regex> = OnceLock::new();
let re =
RE.get_or_init(|| Regex::new(r"<!-{2,}\s*mdbook-xgettext\s*:\s*skip\s*-{2,}>").unwrap());
re.is_match(html.trim())
}
/// Returns true if the events appear to be a codeblock without translatable text.
fn is_nontranslatable_codeblock_group(events: &[(usize, Event)]) -> bool {
match events {
[(_, Event::Start(Tag::CodeBlock(_))), .., (_, Event::End(Tag::CodeBlock(_)))] => {
let (codeblock_text, _) = reconstruct_markdown(events, None);
// Heuristic to check whether the codeblock nether has a
// literal string nor a line comment. We may actually
// want to use a lexer here to make this more robust.
!codeblock_text.contains('"') && !codeblock_text.contains("//")
}
_ => false,
}
}
/// Render a slice of Markdown events back to Markdown.
///
/// # Examples
///
/// ```
/// use mdbook_i18n_helpers::{extract_events, reconstruct_markdown};
/// use pulldown_cmark::{Event, Tag};
///
/// let group = extract_events("Hello *world!*", None);
/// let (reconstructed, _) = reconstruct_markdown(&group, None);
/// assert_eq!(reconstructed, "Hello _world!_");
/// ```
///
/// Notice how this will normalize the Markdown to use `_` for
/// emphasis and `**` for strong emphasis. The style is chosen to
/// match the [Google developer documentation style
/// guide](https://developers.google.com/style/text-formatting).
pub fn reconstruct_markdown(
group: &[(usize, Event)],
state: Option<State<'static>>,
) -> (String, State<'static>) {
let events = group.iter().map(|(_, event)| event);
let mut markdown = String::new();
let options = Options {
code_block_token_count: 3,
list_token: '-',
emphasis_token: '_',
strong_token: "**",
..Options::default()
};
// Advance the true state, but throw away the rendered Markdown
// since it can contain unwanted padding.
let new_state = cmark_resume_with_options(
events.clone(),
String::new(),
state.clone(),
options.clone(),
)
.unwrap();
// Block quotes and lists add padding to the state, which is
// reflected in the rendered Markdown. We want to capture the
// Markdown without the padding to remove the effect of these
// structural elements. Similarly, we don't want extra newlines at
// the start.
let simplified_state = state.map(|state| State {
newlines_before_start: 0,
padding: Vec::new(),
..state
});
cmark_resume_with_options(events, &mut markdown, simplified_state, options).unwrap();
// Even with `newlines_before_start` set to zero, we get a leading
// `\n` for code blocks (since they must start on a new line). We
// can safely trim this here since we know that we always
// reconstruct Markdown for a self-contained group of events.
(String::from(markdown.trim_matches('\n')), new_state)
}
/// Extract translatable strings from `document`.
///
/// # Examples
///
/// Structural markup like headings and lists are removed from the
/// messages:
///
/// ```
/// use mdbook_i18n_helpers::extract_messages;
///
/// assert_eq!(
/// extract_messages("# A heading"),
/// vec![(1, "A heading".into())],
/// );
/// assert_eq!(
/// extract_messages(
/// "1. First item\n\
/// 2. Second item\n"
/// ),
/// vec![
/// (1, "First item".into()),
/// (2, "Second item".into()),
/// ],
/// );
/// ```
///
/// Indentation due to structural elements like block quotes and lists
/// is ignored:
///
/// ```
/// use mdbook_i18n_helpers::extract_messages;
///
/// let messages = extract_messages(
/// "> * Hello, this is a\n\
/// > list in a quote.\n\
/// >\n\
/// > This is the second\n\
/// > paragraph.\n"
/// );
/// assert_eq!(
/// messages,
/// vec![
/// (1, "Hello, this is a list in a quote.".into()),
/// (4, "This is the second paragraph.".into()),
/// ],
/// );
/// ```
pub fn extract_messages(document: &str) -> Vec<(usize, String)> {
let events = extract_events(document, None);
let mut messages = Vec::new();
let mut state = None;
for group in group_events(&events) {
match group {
Group::Translate(events) => {
if let Some((lineno, _)) = events.first() {
let (text, new_state) = reconstruct_markdown(events, state);
messages.push((*lineno, text));
state = Some(new_state);
}
}
Group::Skip(events) => {
let (_, new_state) = reconstruct_markdown(events, state);
state = Some(new_state);
}
}
}
messages
}
/// Trim `new_events` if they're wrapped in an unwanted paragraph.
///
/// If `new_events` is wrapped in a paragraph and `old_events` isn't,
/// then the paragraph is removed. This is useful when a text event
/// has been wrapped in a paragraph:
///
/// ```
/// use pulldown_cmark::{Event, Tag};
/// use mdbook_i18n_helpers::{extract_events, reconstruct_markdown, trim_paragraph};
///
/// let old_events = vec![(1, Event::Text("A line of text".into()))];
/// let (markdown, _) = reconstruct_markdown(&old_events, None);
/// let new_events = extract_events(&markdown, None);
/// // The stand-alone text has been wrapped in an extra paragraph:
/// assert_eq!(
/// new_events,
/// &[
/// (1, Event::Start(Tag::Paragraph)),
/// (1, Event::Text("A line of text".into())),
/// (1, Event::End(Tag::Paragraph)),
/// ],
/// );
///
/// assert_eq!(
/// trim_paragraph(&new_events, &old_events),
/// &[(1, Event::Text("A line of text".into()))],
/// );
/// ```
pub fn trim_paragraph<'a, 'event>(
new_events: &'a [(usize, Event<'event>)],
old_events: &'a [(usize, Event<'event>)],
) -> &'a [(usize, Event<'event>)] {
use pulldown_cmark::Event::{End, Start};
use pulldown_cmark::Tag::Paragraph;
match new_events {
[(_, Start(Paragraph)), inner @ .., (_, End(Paragraph))] => match old_events {
[(_, Start(Paragraph)), .., (_, End(Paragraph))] => new_events,
[..] => inner,
},
[..] => new_events,
}
}
/// Translate `events` using `catalog`.
pub fn translate_events<'a>(
events: &'a [(usize, Event<'a>)],
catalog: &'a Catalog,
) -> Vec<(usize, Event<'a>)> {
let mut translated_events = Vec::new();
let mut state = None;
for group in group_events(events) {
match group {
Group::Translate(events) => {
// Reconstruct the message.
let (msgid, new_state) = reconstruct_markdown(events, state.clone());
let translated = catalog
.find_message(None, &msgid, None)
.filter(|msg| !msg.flags().is_fuzzy())
.and_then(|msg| msg.msgstr().ok())
.filter(|msgstr| !msgstr.is_empty());
match translated {
Some(msgstr) => {
// Generate new events for `msgstr`, taking
// care to trim away unwanted paragraphs.
translated_events.extend_from_slice(trim_paragraph(
&extract_events(msgstr, state),
events,
));
}
None => translated_events.extend_from_slice(events),
}
// Advance the state.
state = Some(new_state);
}
Group::Skip(events) => {
// Copy the events unchanged to the output.
translated_events.extend_from_slice(events);
// Advance the state.
let (_, new_state) = reconstruct_markdown(events, state);
state = Some(new_state);
}
}
}
translated_events
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use pulldown_cmark::CodeBlockKind;
use pulldown_cmark::Event::*;
use pulldown_cmark::HeadingLevel::*;
use pulldown_cmark::Tag::*;
/// Extract messages in `document`, assert they match `expected`.
#[track_caller]
fn assert_extract_messages(document: &str, expected: Vec<(usize, &str)>) {
assert_eq!(
extract_messages(document)
.iter()
.map(|(lineno, msg)| (*lineno, &msg[..]))
.collect::<Vec<_>>(),
expected,
)
}
#[test]
fn extract_events_empty() {
assert_eq!(extract_events("", None), vec![]);
}
#[test]
fn extract_events_paragraph() {
assert_eq!(
extract_events("foo bar", None),
vec![
(1, Start(Paragraph)),
(1, Text("foo bar".into())),
(1, End(Paragraph)),
]
);
}
#[test]
fn extract_events_softbreak() {
assert_eq!(
extract_events("foo\nbar", None),
vec![
(1, Start(Paragraph)),
(1, Text("foo".into())),
(1, Text(" ".into())),
(2, Text("bar".into())),
(1, End(Paragraph)),
]
);
}
#[test]
fn extract_events_heading() {
assert_eq!(
extract_events("# Foo Bar", None),
vec![
(1, Start(Heading(H1, None, vec![]))),
(1, Text("Foo Bar".into())),
(1, End(Heading(H1, None, vec![]))),
]
);
}
#[test]
fn extract_events_list_item() {
assert_eq!(
extract_events("* foo bar", None),
vec![
(1, Start(List(None))),
(1, Start(Item)),
(1, Text("foo bar".into())),
(1, End(Item)),
(1, End(List(None))),
]
);
}
#[test]
fn extract_events_code_block() {
let (_, state) =
reconstruct_markdown(&[(1, Start(CodeBlock(CodeBlockKind::Indented)))], None);
assert_eq!(
extract_events("foo\nbar\nbaz", Some(state)),
vec![
(1, Text("foo\n".into())),
(2, Text("bar\n".into())),
(3, Text("baz".into())),
]
);
// Compare with extraction without state:
assert_eq!(
extract_events("foo\nbar\nbaz", None),
vec![
(1, Start(Paragraph)),
(1, Text("foo".into())),
(1, Text(" ".into())),
(2, Text("bar".into())),
(2, Text(" ".into())),
(3, Text("baz".into())),
(1, End(Paragraph)),
]
);
}
#[test]
fn extract_events_comments() {
assert_eq!(
extract_events("<!-- mdbook-xgettext:skip -->\nHello", None),
vec![
(1, Html("<!-- mdbook-xgettext:skip -->\n".into())),
(2, Start(Paragraph)),
(2, Text("Hello".into())),
(2, End(Paragraph)),
]
);
}
#[test]
fn extract_messages_empty() {
assert_extract_messages("", vec![]);
}
#[test]
fn extract_messages_single_line() {
assert_extract_messages("This is a paragraph.", vec![(1, "This is a paragraph.")]);
}
#[test]
fn extract_messages_simple() {
assert_extract_messages(
"This is\n\
the first\n\
paragraph.🦀\n\
\n\
Second paragraph.",
vec![
(1, "This is the first paragraph.🦀"),
(5, "Second paragraph."),
],
);
}
#[test]
fn extract_messages_leading_newlines() {
assert_extract_messages(
"\n\
\n\
\n\
This is the\n\
first paragraph.",
vec![(4, "This is the first paragraph.")],
);
}
#[test]
fn extract_messages_trailing_newlines() {
assert_extract_messages(
"This is\n\
a paragraph.\n\
\n\
\n",
vec![(1, "This is a paragraph.")],
);
}
#[test]
fn extract_messages_styled_text() {
// The parser normalizes "*emphasis*" to "_emphasis_" and
// "__strong emphasis__" to "**strong emphasis**".
assert_extract_messages(
"**This** __~~message~~__ _has_ `code` *style*\n",
vec![(1, "**This** **~~message~~** _has_ `code` _style_")],
);
}
#[test]
fn extract_messages_inline_html() {
// HTML tags are skipped, but text inside is extracted:
assert_extract_messages(
"Hi <script>alert('there');</script>",
vec![
(1, "Hi "), //
(1, "alert('there');"),
],
);
}
#[test]
fn extract_messages_inline_link() {
assert_extract_messages(
"See [this page](https://example.com) for more info.",
vec![(1, "See [this page](https://example.com) for more info.")],
);
}
#[test]
fn extract_messages_reference_link() {
assert_extract_messages(
"See [this page][1] for more info.\n\n\
[1]: https://example.com",
// The parser expands reference links on the fly.
vec![(1, "See [this page](https://example.com) for more info.")],
);
}
#[test]
fn extract_messages_collapsed_link() {
// We make the parser expand collapsed links on the fly.
assert_extract_messages(
"Click [here][]!\n\n\
[here]: http://example.net/",
vec![(1, "Click [here](http://example.net/)!")],
);
}
#[test]
fn extract_messages_shortcut_link() {
assert_extract_messages(
"Click [here]!\n\n\
[here]: http://example.net/",
vec![(1, "Click [here](http://example.net/)!")],
);
}
#[test]
fn extract_messages_autolink() {
assert_extract_messages(
"Visit <http://example.net>!",
vec![(1, "Visit <http://example.net>!")],
);
}
#[test]
fn extract_messages_email() {
assert_extract_messages(
"Contact <info@example.net>!",
vec![(1, "Contact <info@example.net>!")],
);
}
#[test]
fn extract_messages_broken_reference_link() {
// A reference link without the corresponding link definition
// results in an escaped link.
//
// See `SourceMap::extract_messages` for a more complex
// approach which can work around this in some cases.
assert_extract_messages("[foo][unknown]", vec![(1, r"\[foo\]\[unknown\]")]);
}
#[test]
fn extract_messages_footnotes() {
assert_extract_messages(
"
The document[^1] text.
[^1]: The footnote text.
",
vec![
(2, "The document[^1] text."), //
(4, "The footnote text."),
],
);
}
#[test]
fn extract_messages_block_quote() {
assert_extract_messages(
r#"One of my favorite quotes is:
> Don't believe everything you read on the Internet.
>
> I didn't say this second part, but I needed a paragraph for testing.
--Abraham Lincoln
"#,
vec![
(1, "One of my favorite quotes is:"),
(3, "Don't believe everything you read on the Internet."),
(
5,
"I didn't say this second part, but I needed a paragraph for testing.",
),
(7, "\\--Abraham Lincoln"),
],
);
}
#[test]
fn extract_messages_table() {
let input = "\
| Module Type | Description\n\
|-------------------|-------------------------\n\
| `rust_binary` | Produces a Rust binary.\n\
| `rust_library` | Produces a Rust library.\n\
";
assert_extract_messages(
input,
vec![
(1, "Module Type"),
(1, "Description"),
(3, "`rust_binary`"),
(3, "Produces a Rust binary."),
(4, "`rust_library`"),
(4, "Produces a Rust library."),
],
);
}
#[test]
fn extract_messages_code_block() {
assert_extract_messages(
"Preamble\n```rust\n// Example:\nfn hello() {\n some_code()\n\n todo!()\n}\n```\nPostamble",
vec![
(1, "Preamble"),
(
2,
"```rust\n// Example:\nfn hello() {\n some_code()\n\n todo!()\n}\n```",
),
(10, "Postamble"),
],
);
}
#[test]
fn extract_messages_two_code_blocks() {
assert_extract_messages(
"```\n\
\"First\" block\n\
```\n\
```\n\
\"Second\" block\n\
```\n\
",
vec![
(1, "```\n\"First\" block\n```"), //
(4, "```\n\"Second\" block\n```"),
],
);
}
#[test]
fn extract_messages_quoted_code_block() {
assert_extract_messages(
"\
> Preamble\n\
> ```rust\n\
> fn hello() {\n\
> some_code()\n\
>\n\
> // FIXME: do something here!\n\
> todo!()\n\
> }\n\
> ```\n\
> Postamble",
vec![
(1, "Preamble"),
(
2,
"```rust\nfn hello() {\n some_code()\n\n // FIXME: do something here!\n todo!()\n}\n```",
),
(10, "Postamble"),
],
);
}
#[test]
fn extract_messages_details() {
// This isn't great: we lose text following a HTML tag:
assert_extract_messages(
"Preamble\n\
<details>\n\
Some Details\n\
</details>\n\
\n\
Postamble",
vec![
(1, "Preamble"), //
// Missing "Some Details"
(6, "Postamble"),
],
);
// It works well enough when `<details>` has blank lines
// before and after.
assert_extract_messages(
"Preamble\n\
\n\
<details>\n\
\n\
Some Details\n\
\n\
</details>\n\
\n\
Postamble",
vec![
(1, "Preamble"), //
(5, "Some Details"),
(9, "Postamble"),
],
);
}
#[test]
fn extract_messages_list() {
assert_extract_messages(
"Some text\n * List item 1🦀\n * List item 2\n\nMore text",
vec![
(1, "Some text"), //
(2, "List item 1🦀"),
(3, "List item 2"),
(5, "More text"),
],
);
}
#[test]
fn extract_messages_multilevel_list() {
assert_extract_messages(
"Some text\n * List item 1\n * List item 2\n * Sublist 1\n * Sublist 2\n\nMore text",
vec![
(1, "Some text"), //
(2, "List item 1"),
(3, "List item 2"),
(4, "Sublist 1"),
(5, "Sublist 2"),
(7, "More text"),
],
);
}
#[test]
fn extract_messages_list_with_paragraphs() {