| 1 |
use mdbook_core::static_regex; |
| 2 |
use std::ops::Bound::{Excluded, Included, Unbounded}; |
| 3 |
use std::ops::RangeBounds; |
| 4 |
|
| 5 |
/// Take a range of lines from a string. |
| 6 |
pub(super) fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String { |
| 7 |
let start = match range.start_bound() { |
| 8 |
Excluded(&n) => n + 1, |
| 9 |
Included(&n) => n, |
| 10 |
Unbounded => 0, |
| 11 |
}; |
| 12 |
let lines = s.lines().skip(start); |
| 13 |
match range.end_bound() { |
| 14 |
Excluded(end) => lines |
| 15 |
.take(end.saturating_sub(start)) |
| 16 |
.collect::<Vec<_>>() |
| 17 |
.join("\n"), |
| 18 |
Included(end) => lines |
| 19 |
.take((end + 1).saturating_sub(start)) |
| 20 |
.collect::<Vec<_>>() |
| 21 |
.join("\n"), |
| 22 |
Unbounded => lines.collect::<Vec<_>>().join("\n"), |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
static_regex!(ANCHOR_START, r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)"); |
| 27 |
static_regex!(ANCHOR_END, r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)"); |
| 28 |
|
| 29 |
/// Take anchored lines from a string. |
| 30 |
/// Lines containing anchor are ignored. |
| 31 |
pub(super) fn take_anchored_lines(s: &str, anchor: &str) -> String { |
| 32 |
let mut retained = Vec::<&str>::new(); |
| 33 |
let mut anchor_found = false; |
| 34 |
|
| 35 |
for l in s.lines() { |
| 36 |
if anchor_found { |
| 37 |
match ANCHOR_END.captures(l) { |
| 38 |
Some(cap) => { |
| 39 |
if &cap["anchor_name"] == anchor { |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
None => { |
| 44 |
if !ANCHOR_START.is_match(l) { |
| 45 |
retained.push(l); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} else if let Some(cap) = ANCHOR_START.captures(l) { |
| 50 |
if &cap["anchor_name"] == anchor { |
| 51 |
anchor_found = true; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
retained.join("\n") |
| 57 |
} |
| 58 |
|
| 59 |
/// Keep lines contained within the range specified as-is. |
| 60 |
/// For any lines not in the range, include them but use `#` at the beginning. This will hide the |
| 61 |
/// lines from initial display but include them when expanding the code snippet or testing with |
| 62 |
/// rustdoc. |
| 63 |
pub(super) fn take_rustdoc_include_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String { |
| 64 |
let mut output = String::with_capacity(s.len()); |
| 65 |
|
| 66 |
for (index, line) in s.lines().enumerate() { |
| 67 |
if !range.contains(&index) { |
| 68 |
output.push_str("# "); |
| 69 |
} |
| 70 |
output.push_str(line); |
| 71 |
output.push('\n'); |
| 72 |
} |
| 73 |
output.pop(); |
| 74 |
output |
| 75 |
} |
| 76 |
|
| 77 |
/// Keep lines between the anchor comments specified as-is. |
| 78 |
/// For any lines not between the anchors, include them but use `#` at the beginning. This will |
| 79 |
/// hide the lines from initial display but include them when expanding the code snippet or testing |
| 80 |
/// with rustdoc. |
| 81 |
pub(super) fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { |
| 82 |
let mut output = String::with_capacity(s.len()); |
| 83 |
let mut within_anchored_section = false; |
| 84 |
|
| 85 |
for l in s.lines() { |
| 86 |
if within_anchored_section { |
| 87 |
match ANCHOR_END.captures(l) { |
| 88 |
Some(cap) => { |
| 89 |
if &cap["anchor_name"] == anchor { |
| 90 |
within_anchored_section = false; |
| 91 |
} |
| 92 |
} |
| 93 |
None => { |
| 94 |
if !ANCHOR_START.is_match(l) { |
| 95 |
output.push_str(l); |
| 96 |
output.push('\n'); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} else if let Some(cap) = ANCHOR_START.captures(l) { |
| 101 |
if &cap["anchor_name"] == anchor { |
| 102 |
within_anchored_section = true; |
| 103 |
} |
| 104 |
} else if !ANCHOR_END.is_match(l) { |
| 105 |
output.push_str("# "); |
| 106 |
output.push_str(l); |
| 107 |
output.push('\n'); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
output.pop(); |
| 112 |
output |
| 113 |
} |
| 114 |
|
| 115 |
#[cfg(test)] |
| 116 |
mod tests { |
| 117 |
use super::{ |
| 118 |
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines, |
| 119 |
take_rustdoc_include_lines, |
| 120 |
}; |
| 121 |
|
| 122 |
#[test] |
| 123 |
#[allow(clippy::reversed_empty_ranges)] // Intentionally checking that those are correctly handled |
| 124 |
fn take_lines_test() { |
| 125 |
let s = "Lorem\nipsum\ndolor\nsit\namet"; |
| 126 |
assert_eq!(take_lines(s, 1..3), "ipsum\ndolor"); |
| 127 |
assert_eq!(take_lines(s, 3..), "sit\namet"); |
| 128 |
assert_eq!(take_lines(s, ..3), "Lorem\nipsum\ndolor"); |
| 129 |
assert_eq!(take_lines(s, ..), s); |
| 130 |
// corner cases |
| 131 |
assert_eq!(take_lines(s, 4..3), ""); |
| 132 |
assert_eq!(take_lines(s, ..100), s); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn take_anchored_lines_test() { |
| 137 |
let s = "Lorem\nipsum\ndolor\nsit\namet"; |
| 138 |
assert_eq!(take_anchored_lines(s, "test"), ""); |
| 139 |
|
| 140 |
let s = "Lorem\nipsum\ndolor\nANCHOR_END: test\nsit\namet"; |
| 141 |
assert_eq!(take_anchored_lines(s, "test"), ""); |
| 142 |
|
| 143 |
let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet"; |
| 144 |
assert_eq!(take_anchored_lines(s, "test"), "dolor\nsit\namet"); |
| 145 |
assert_eq!(take_anchored_lines(s, "something"), ""); |
| 146 |
|
| 147 |
let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum"; |
| 148 |
assert_eq!(take_anchored_lines(s, "test"), "dolor\nsit\namet"); |
| 149 |
assert_eq!(take_anchored_lines(s, "something"), ""); |
| 150 |
|
| 151 |
let s = "Lorem\nANCHOR: test\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum"; |
| 152 |
assert_eq!(take_anchored_lines(s, "test"), "ipsum\ndolor\nsit\namet"); |
| 153 |
assert_eq!(take_anchored_lines(s, "something"), ""); |
| 154 |
|
| 155 |
let s = "Lorem\nANCHOR: test2\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nANCHOR_END:test2\nipsum"; |
| 156 |
assert_eq!( |
| 157 |
take_anchored_lines(s, "test2"), |
| 158 |
"ipsum\ndolor\nsit\namet\nlorem" |
| 159 |
); |
| 160 |
assert_eq!(take_anchored_lines(s, "test"), "dolor\nsit\namet"); |
| 161 |
assert_eq!(take_anchored_lines(s, "something"), ""); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
#[allow(clippy::reversed_empty_ranges)] // Intentionally checking that those are correctly handled |
| 166 |
fn take_rustdoc_include_lines_test() { |
| 167 |
let s = "Lorem\nipsum\ndolor\nsit\namet"; |
| 168 |
assert_eq!( |
| 169 |
take_rustdoc_include_lines(s, 1..3), |
| 170 |
"# Lorem\nipsum\ndolor\n# sit\n# amet" |
| 171 |
); |
| 172 |
assert_eq!( |
| 173 |
take_rustdoc_include_lines(s, 3..), |
| 174 |
"# Lorem\n# ipsum\n# dolor\nsit\namet" |
| 175 |
); |
| 176 |
assert_eq!( |
| 177 |
take_rustdoc_include_lines(s, ..3), |
| 178 |
"Lorem\nipsum\ndolor\n# sit\n# amet" |
| 179 |
); |
| 180 |
assert_eq!(take_rustdoc_include_lines(s, ..), s); |
| 181 |
// corner cases |
| 182 |
assert_eq!( |
| 183 |
take_rustdoc_include_lines(s, 4..3), |
| 184 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet" |
| 185 |
); |
| 186 |
assert_eq!(take_rustdoc_include_lines(s, ..100), s); |
| 187 |
} |
| 188 |
|
| 189 |
#[test] |
| 190 |
fn take_rustdoc_include_anchored_lines_test() { |
| 191 |
let s = "Lorem\nipsum\ndolor\nsit\namet"; |
| 192 |
assert_eq!( |
| 193 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 194 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet" |
| 195 |
); |
| 196 |
|
| 197 |
let s = "Lorem\nipsum\ndolor\nANCHOR_END: test\nsit\namet"; |
| 198 |
assert_eq!( |
| 199 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 200 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet" |
| 201 |
); |
| 202 |
|
| 203 |
let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet"; |
| 204 |
assert_eq!( |
| 205 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 206 |
"# Lorem\n# ipsum\ndolor\nsit\namet" |
| 207 |
); |
| 208 |
assert_eq!( |
| 209 |
take_rustdoc_include_anchored_lines(s, "something"), |
| 210 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet" |
| 211 |
); |
| 212 |
|
| 213 |
let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum"; |
| 214 |
assert_eq!( |
| 215 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 216 |
"# Lorem\n# ipsum\ndolor\nsit\namet\n# lorem\n# ipsum" |
| 217 |
); |
| 218 |
assert_eq!( |
| 219 |
take_rustdoc_include_anchored_lines(s, "something"), |
| 220 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet\n# lorem\n# ipsum" |
| 221 |
); |
| 222 |
|
| 223 |
let s = "Lorem\nANCHOR: test\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum"; |
| 224 |
assert_eq!( |
| 225 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 226 |
"# Lorem\nipsum\ndolor\nsit\namet\n# lorem\n# ipsum" |
| 227 |
); |
| 228 |
assert_eq!( |
| 229 |
take_rustdoc_include_anchored_lines(s, "something"), |
| 230 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet\n# lorem\n# ipsum" |
| 231 |
); |
| 232 |
|
| 233 |
let s = "Lorem\nANCHOR: test2\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nANCHOR_END:test2\nipsum"; |
| 234 |
assert_eq!( |
| 235 |
take_rustdoc_include_anchored_lines(s, "test2"), |
| 236 |
"# Lorem\nipsum\ndolor\nsit\namet\nlorem\n# ipsum" |
| 237 |
); |
| 238 |
assert_eq!( |
| 239 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 240 |
"# Lorem\n# ipsum\ndolor\nsit\namet\n# lorem\n# ipsum" |
| 241 |
); |
| 242 |
assert_eq!( |
| 243 |
take_rustdoc_include_anchored_lines(s, "something"), |
| 244 |
"# Lorem\n# ipsum\n# dolor\n# sit\n# amet\n# lorem\n# ipsum" |
| 245 |
); |
| 246 |
|
| 247 |
let s = "Lorem\nANCHOR: test\nipsum\nANCHOR_END: test\ndolor\nANCHOR: test\nsit\nANCHOR_END: test\namet"; |
| 248 |
assert_eq!( |
| 249 |
take_rustdoc_include_anchored_lines(s, "test"), |
| 250 |
"# Lorem\nipsum\n# dolor\nsit\n# amet" |
| 251 |
); |
| 252 |
} |
| 253 |
} |