diff options
| author | A Farzat <a@farzat.xyz> | 2026-06-19 04:14:39 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-06-19 04:14:39 +0300 |
| commit | b1c8df99e5bc3bb6639540ae47c58f1b304715a2 (patch) | |
| tree | 3944ebc91d14ec092c46f1880c0e37e96f1af893 | |
| parent | 64edb40a08a1e8de5fbae8730b021f2cc2f31edb (diff) | |
| download | repo2markdown-b1c8df99e5bc3bb6639540ae47c58f1b304715a2.tar.gz repo2markdown-b1c8df99e5bc3bb6639540ae47c58f1b304715a2.zip | |
Add a module that wraps input with fences
This will be used to implement the r2md-fence binary next.
| -rw-r--r-- | src/fence_wrapper.rs | 69 | ||||
| -rw-r--r-- | src/lib.rs | 1 |
2 files changed, 70 insertions, 0 deletions
diff --git a/src/fence_wrapper.rs b/src/fence_wrapper.rs new file mode 100644 index 0000000..a218088 --- /dev/null +++ b/src/fence_wrapper.rs @@ -0,0 +1,69 @@ +use std::io::{Read, Write}; + +use crate::util::fence::generate_outer_backticks; + +pub fn wrap_in_fence<R: Read, W: Write>( + mut input: R, + mut output: W, + lang: &str, +) -> std::io::Result<()> { + let mut contents = Vec::new(); + input.read_to_end(&mut contents)?; + let fence = generate_outer_backticks(&contents); + writeln!(output, "{}{}", fence, lang)?; + output.write_all(&contents)?; + writeln!(output, "\n{}", fence) +} + +#[cfg(test)] +mod tests { + use std::io::Cursor; + + use super::wrap_in_fence; + + #[test] + fn wraps_input_in_fence_with_language() { + let input = Cursor::new("fn main() {}\n"); + let mut output = Vec::new(); + + wrap_in_fence(input, &mut output, "rust").unwrap(); + + assert_eq!( + String::from_utf8(output).unwrap(), + "```rust\nfn main() {}\n\n```\n" + ); + } + + #[test] + fn puts_closing_fence_on_new_line_when_input_has_no_trailing_newline() { + let input = Cursor::new("hello"); + let mut output = Vec::new(); + + wrap_in_fence(input, &mut output, "text").unwrap(); + + assert_eq!(String::from_utf8(output).unwrap(), "```text\nhello\n```\n"); + } + + #[test] + fn uses_longer_fence_when_input_contains_backticks() { + let input = Cursor::new("contains ``` inside\n"); + let mut output = Vec::new(); + + wrap_in_fence(input, &mut output, "markdown").unwrap(); + + assert_eq!( + String::from_utf8(output).unwrap(), + "````markdown\ncontains ``` inside\n\n````\n" + ); + } + + #[test] + fn wraps_empty_input() { + let input = Cursor::new(""); + let mut output = Vec::new(); + + wrap_in_fence(input, &mut output, "text").unwrap(); + + assert_eq!(String::from_utf8(output).unwrap(), "```text\n\n```\n"); + } +} @@ -1,3 +1,4 @@ +pub mod fence_wrapper; pub mod fenced_md_generator; pub mod logger; pub mod md_generator; |
