-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
55 lines (51 loc) · 1.53 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
// SPDX-FileCopyrightText: 2023 Wiktor Kwapisiewicz <[email protected]>
// SPDX-License-Identifier: Apache-2.0 OR MIT
#![doc = include_str!("../README.md")]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
use std::io::{BufRead, BufReader, Read, Result, Write};
/// Extracts expected code-blocks from Markdown source and writes them into sink.
///
/// Multiple expected code-block infos can be passed. In this case it's
/// sufficient for any one of them to match for the code-block to be printed.
///
/// This function will not lint the source and assumes that the input is a valid
/// Markdown document.
///
/// # Examples
///
/// ```
/// use std::io::Write;
///
/// # fn main() -> testresult::TestResult {
/// let source = r#"```sh
/// echo x
/// ```"#;
/// let mut sink = Vec::new();
///
/// tangler::extract(source.as_bytes(), &mut sink, &["sh"])?;
///
/// assert_eq!(&b"echo x\n\n"[..], &sink[..]);
/// # Ok(()) }
/// ```
pub fn extract(
source: impl Read,
mut sink: impl Write,
expected: &[impl AsRef<str>],
) -> Result<()> {
let mut outputting = false;
for line in BufReader::new(source).lines() {
let line = line?;
if line == "```" && outputting {
sink.write_all(b"\n")?;
outputting = false;
} else if line.starts_with("```") && expected.iter().any(|info| info.as_ref() == &line[3..])
{
outputting = true;
} else if outputting {
sink.write_all(line.as_bytes())?;
sink.write_all(b"\n")?;
}
}
Ok(())
}