| 1 | const MARKERS: [char; 2] = ['*', '_']; |
| 2 | const CODE_OPEN: &str = "<code>"; |
| 3 | const CODE_CLOSE: &str = "</code>"; |
| 4 | const LINE_BREAK: &str = "<br>"; |
| 5 | const LINK_TARGET_OPEN: &str = "]("; |
| 6 | const URL_SCHEMES: [&str; 2] = ["https://", "http://"]; |
| 7 | |
| 8 | pub(crate) fn italicize(escaped: &str) -> String { |
| 9 | let characters: Vec<char> = escaped.chars().collect(); |
| 10 | let mut output = String::with_capacity(escaped.len()); |
| 11 | let mut index = 0; |
| 12 | while index < characters.len() { |
| 13 | if let Some(end) = protected_end(&characters, index) { |
| 14 | output.extend(&characters[index..end]); |
| 15 | index = end; |
| 16 | continue; |
| 17 | } |
| 18 | let character = characters[index]; |
| 19 | if let Some(close) = italic_close(&characters, index) { |
| 20 | output.push_str("<i>"); |
| 21 | output.extend(&characters[index + 1..close]); |
| 22 | output.push_str("</i>"); |
| 23 | index = close + 1; |
| 24 | continue; |
| 25 | } |
| 26 | output.push(character); |
| 27 | index += 1; |
| 28 | } |
| 29 | output |
| 30 | } |
| 31 | |
| 32 | fn starts_with(characters: &[char], index: usize, text: &str) -> bool { |
| 33 | text.chars() |
| 34 | .enumerate() |
| 35 | .all(|(offset, expected)| characters.get(index + offset) == Some(&expected)) |
| 36 | } |
| 37 | |
| 38 | fn protected_end(characters: &[char], index: usize) -> Option<usize> { |
| 39 | if starts_with(characters, index, CODE_OPEN) { |
| 40 | let close = |
| 41 | (index..characters.len()).find(|&at| starts_with(characters, at, CODE_CLOSE))?; |
| 42 | return Some(close + CODE_CLOSE.chars().count()); |
| 43 | } |
| 44 | if characters[index] == ']' && starts_with(characters, index, LINK_TARGET_OPEN) { |
| 45 | let close = (index..characters.len()).find(|&at| characters[at] == ')')?; |
| 46 | return Some(close + 1); |
| 47 | } |
| 48 | let boundary = index == 0 || !characters[index - 1].is_alphanumeric(); |
| 49 | if boundary |
| 50 | && URL_SCHEMES |
| 51 | .iter() |
| 52 | .any(|scheme| starts_with(characters, index, scheme)) |
| 53 | { |
| 54 | let end = (index..characters.len()) |
| 55 | .find(|&at| characters[at].is_whitespace() || characters[at] == '<') |
| 56 | .unwrap_or(characters.len()); |
| 57 | return Some(end); |
| 58 | } |
| 59 | None |
| 60 | } |
| 61 | |
| 62 | fn italic_close(characters: &[char], open: usize) -> Option<usize> { |
| 63 | let marker = characters[open]; |
| 64 | if !MARKERS.contains(&marker) { |
| 65 | return None; |
| 66 | } |
| 67 | if open > 0 && characters[open - 1].is_alphanumeric() { |
| 68 | return None; |
| 69 | } |
| 70 | let first = *characters.get(open + 1)?; |
| 71 | if first.is_whitespace() || MARKERS.contains(&first) { |
| 72 | return None; |
| 73 | } |
| 74 | for close in open + 2..characters.len() { |
| 75 | let character = characters[close]; |
| 76 | if character == '\n' || starts_with(characters, close, LINE_BREAK) { |
| 77 | return None; |
| 78 | } |
| 79 | if character != marker || characters[close - 1].is_whitespace() { |
| 80 | continue; |
| 81 | } |
| 82 | let closes = characters |
| 83 | .get(close + 1) |
| 84 | .is_none_or(|next| !next.is_alphanumeric() && *next != marker); |
| 85 | if closes { |
| 86 | return Some(close); |
| 87 | } |
| 88 | } |
| 89 | None |
| 90 | } |
| 91 | |
| 92 | #[cfg(test)] |
| 93 | mod tests { |
| 94 | use super::italicize; |
| 95 | |
| 96 | #[test] |
| 97 | fn underscore_and_star_pairs_become_italic() { |
| 98 | assert_eq!(italicize("a _b_ c"), "a <i>b</i> c"); |
| 99 | assert_eq!(italicize("a *b c* d"), "a <i>b c</i> d"); |
| 100 | assert_eq!(italicize("_a_ _b_"), "<i>a</i> <i>b</i>"); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn snake_case_and_inner_underscores_stay() { |
| 105 | assert_eq!(italicize("snake_case_word"), "snake_case_word"); |
| 106 | assert_eq!(italicize("_x_y"), "_x_y"); |
| 107 | assert_eq!(italicize("2*3*4"), "2*3*4"); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn urls_code_and_link_targets_stay() { |
| 112 | assert_eq!( |
| 113 | italicize("see https://a.example/_x_/_y_ now"), |
| 114 | "see https://a.example/_x_/_y_ now" |
| 115 | ); |
| 116 | assert_eq!( |
| 117 | italicize("<code>_x_</code> _y_"), |
| 118 | "<code>_x_</code> <i>y</i>" |
| 119 | ); |
| 120 | assert_eq!( |
| 121 | italicize("<a>[t](https://a.example/_x_)</a>"), |
| 122 | "<a>[t](https://a.example/_x_)</a>" |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn unpaired_and_spaced_markers_stay() { |
| 128 | assert_eq!(italicize("_a"), "_a"); |
| 129 | assert_eq!(italicize("* item"), "* item"); |
| 130 | assert_eq!(italicize("a _ b _"), "a _ b _"); |
| 131 | assert_eq!(italicize("**"), "**"); |
| 132 | assert_eq!(italicize("_a<br>b_"), "_a<br>b_"); |
| 133 | } |
| 134 | } |