flav_md_engine/lexer/pattern/
inline.rs1pub mod inline {
2 use once_cell::sync::Lazy;
3 use regex::Regex;
4
5 pub struct Pattern {
6 pattern: Regex,
7 template: String,
8 }
9
10 impl Pattern {
11 pub fn new(pattern: Regex, template: String) -> Self {
12 Pattern { pattern, template }
13 }
14
15 pub fn parse(&self, input: &String) -> String {
16 self.pattern.replace_all(input, &self.template).to_string()
17 }
18 }
19
20 static IMAGE_PARSE: Lazy<Pattern> = Lazy::new(|| {
21 Pattern::new(
22 Regex::new(r"!\[(.*?)]\((.*?)\)").unwrap(),
23 r#"<img class="flav-md-img" src="$2" alt="$1">"#.to_string(),
24 )
25 });
26
27 static LINK_PARSE: Lazy<Pattern> = Lazy::new(|| {
28 Pattern::new(
29 Regex::new(r"\[(.*?)]\((.*?)\)").unwrap(),
30 r#"<a class="flav-md-a" href="$2" alt="$1">$1</a>"#.to_string(),
31 )
32 });
33
34 static CODE_PARSE: Lazy<Pattern> = Lazy::new(|| {
35 Pattern::new(
36 Regex::new(r"`(.*?)`").unwrap(),
37 r#"<code class="flav-md-code-inline">$1</code>"#.to_string(),
38 )
39 });
40
41 static STRONG_PARSE: Lazy<Pattern> = Lazy::new(|| {
42 Pattern::new(
43 Regex::new(r"\*{2}(.*?)\*{2}").unwrap(),
44 r#"<strong class="flav-md-strong">$1</strong>"#.to_string(),
45 )
46 });
47
48 static EMPHASIS_PARSE: Lazy<Pattern> = Lazy::new(|| {
49 Pattern::new(
50 Regex::new(r"\*(.*?)\*").unwrap(),
51 r#"<em class="flav-md-em">$1</em>"#.to_string(),
52 )
53 });
54
55 trait Parsable {
56 fn parse(&self, parser: &Pattern) -> Self;
57 }
58
59 impl Parsable for String {
60 fn parse(&self, parser: &Pattern) -> Self {
61 parser.parse(self)
62 }
63 }
64
65 pub fn inline_parse(input: &String) -> String {
66 input
67 .parse(&IMAGE_PARSE)
68 .parse(&LINK_PARSE)
69 .parse(&CODE_PARSE)
70 .parse(&STRONG_PARSE)
71 .parse(&EMPHASIS_PARSE)
72 }
73
74 #[cfg(test)]
75 mod test_inline {
76 use super::*;
77
78 #[test]
79 fn test_image_pattern() {
80 let output = inline_parse(&String::from(""));
81 assert_eq!(
82 output,
83 String::from(r#"<img class="flav-md-img" src="hoge2" alt="hoge1">"#)
84 );
85 }
86
87 #[test]
88 fn test_link_pattern() {
89 let output = inline_parse(&String::from("[hoge1](hoge2)"));
90 assert_eq!(
91 output,
92 String::from(r#"<a class="flav-md-a" href="hoge2" alt="hoge1">hoge1</a>"#)
93 );
94 }
95
96 #[test]
97 fn test_code_pattern() {
98 let output = inline_parse(&String::from("`hoge`"));
99 assert_eq!(
100 output,
101 String::from(r#"<code class="flav-md-code-inline">hoge</code>"#)
102 );
103 }
104
105 #[test]
106 fn test_strong_pattern() {
107 let output = inline_parse(&String::from("**hoge**"));
108 assert_eq!(
109 output,
110 String::from(r#"<strong class="flav-md-strong">hoge</strong>"#)
111 );
112 }
113
114 #[test]
115 fn test_emphasis_pattern() {
116 let output = inline_parse(&String::from("*hoge*"));
117 assert_eq!(output, String::from(r#"<em class="flav-md-em">hoge</em>"#));
118 }
119
120 #[test]
121 fn test_inline_parse() {
122 let output = inline_parse(&String::from("*hoge*"));
123 assert_eq!(output, String::from(r#"<em class="flav-md-em">hoge</em>"#));
124 }
125 }
126}