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
#![allow(dead_code)]
#![allow(clippy::type_complexity)]
use crate::parser::Link;
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
use percent_encoding::percent_decode_str;
use std::borrow::Cow;
pub fn wikitext_text2dest_link(i: &str) -> nom::IResult<&str, Link> {
let (i, (te, de, ti)) = wikitext_text2dest(i)?;
Ok((i, Link::Text2Dest(te, de, ti)))
}
pub fn wikitext_text2dest(i: &str) -> nom::IResult<&str, (Cow<str>, Cow<str>, Cow<str>)> {
let (i, (link_text, link_destination)) = nom::sequence::delimited(
tag("["),
nom::combinator::map_parser(is_not("]\n\r"), parse_inner),
tag("]"),
)(i)?;
Ok((i, (link_text, link_destination, Cow::from(""))))
}
fn parse_inner(i: &str) -> nom::IResult<&str, (Cow<str>, Cow<str>)> {
let (i, link_destination) = nom::sequence::terminated(
nom::combinator::map_parser(
nom::bytes::complete::take_till(|c| c == ' ' || c == '\t'),
parse_url,
),
nom::character::complete::space0,
)(i)?;
let link_text = i;
Ok((i, (Cow::from(link_text), link_destination)))
}
fn parse_url(i: &str) -> nom::IResult<&str, Cow<str>> {
nom::combinator::peek(alt((tag("http:"), tag("https:"), tag("mailto:"))))(i)?;
let url = percent_decode_str(i).decode_utf8().unwrap();
Ok(("", url))
}
#[test]
fn test_wikitext_text2dest() {
let expected = (
"abc",
(
Cow::from("W3Schools"),
Cow::from("https://www.w3schools.com/"),
Cow::from(""),
),
);
assert_eq!(
wikitext_text2dest(r#"[https://www.w3schools.com/ W3Schools]abc"#).unwrap(),
expected
);
assert_eq!(
wikitext_text2dest(r#"[https://www.w3schools.com/ W3Schools]abc"#).unwrap(),
expected
);
let expected = (
"abc",
(
Cow::from("W3Schools"),
Cow::from("http://www.w3schools.com/"),
Cow::from(""),
),
);
assert_eq!(
wikitext_text2dest(r#"[http://www.w3schools.com/ W3Schools]abc"#).unwrap(),
expected
);
let expected = (
"abc",
(
Cow::from("W3Schools website"),
Cow::from("http://www.w3schools.com/"),
Cow::from(""),
),
);
assert_eq!(
wikitext_text2dest(r#"[http://www.w3schools.com/ W3Schools website]abc"#).unwrap(),
expected
);
assert_eq!(
wikitext_text2dest("[http://www.w3schools.com/\tW3Schools website]abc").unwrap(),
expected
);
let expected = (
"abc",
(
Cow::from(""),
Cow::from("http://www.w3schools.com/"),
Cow::from(""),
),
);
assert_eq!(
wikitext_text2dest(r#"[http://www.w3schools.com/]abc"#).unwrap(),
expected
);
assert_eq!(
wikitext_text2dest(r#"[http://www.w3schools.com/ ]abc"#).unwrap(),
expected
);
assert_eq!(
wikitext_text2dest("[http://www.w3schools.com/\t ]abc").unwrap(),
expected
);
let expected = (
"abc",
(
Cow::from("John Don"),
Cow::from("mailto:john.don@somemail.com"),
Cow::from(""),
),
);
assert_eq!(
wikitext_text2dest(r#"[mailto:john.don@somemail.com John Don]abc"#).unwrap(),
expected
);
assert_eq!(
wikitext_text2dest(r#"[httpx://www.w3schools.com/ W3Schools]abc"#).unwrap_err(),
nom::Err::Error(nom::error::Error::new(
"httpx://www.w3schools.com/",
nom::error::ErrorKind::Tag
))
);
}