use crate::{
ellipsis::replace_ellipsis,
fences::{attach_orphan_specifiers, compress_fences},
footnotes::convert_footnotes,
html::convert_html_tables,
table::reflow_table,
wrap::{FenceTracker, classify_block, wrap_text},
};
pub(crate) const WRAP_COLS: usize = 80;
#[expect(
clippy::struct_excessive_bools,
reason = "Options map directly to CLI flags"
)]
#[derive(Clone, Copy, Default)]
pub struct Options {
pub wrap: bool,
pub ellipsis: bool,
pub fences: bool,
pub footnotes: bool,
pub code_emphasis: bool,
pub headings: bool,
}
fn flush_buffer(buf: &mut Vec<String>, in_table: &mut bool, out: &mut Vec<String>) {
if buf.is_empty() {
return;
}
if *in_table {
out.extend(reflow_table(buf));
buf.clear();
} else {
out.extend(std::mem::take(buf));
}
*in_table = false;
}
fn handle_fence_line(
line: &str,
buf: &mut Vec<String>,
in_table: &mut bool,
out: &mut Vec<String>,
fences: &mut FenceTracker,
) -> bool {
if !fences.observe(line) {
return false;
}
flush_buffer(buf, in_table, out);
out.push(line.to_string());
true
}
fn handle_table_line(
line: &str,
buf: &mut Vec<String>,
in_table: &mut bool,
out: &mut Vec<String>,
) -> bool {
let trimmed = line.trim_start();
if trimmed.starts_with('|') {
*in_table = true;
buf.push(line.to_string());
return true;
}
if line.trim().is_empty() {
if *in_table {
flush_buffer(buf, in_table, out);
}
return false;
}
if *in_table && (line.contains('|') || crate::table::SEP_RE.is_match(line.trim())) {
buf.push(line.to_string());
return true;
}
if *in_table {
if classify_block(line).is_some() {
flush_buffer(buf, in_table, out);
return false;
}
flush_buffer(buf, in_table, out);
return false;
}
false
}
#[must_use]
pub fn process_stream_inner(lines: &[String], opts: Options) -> Vec<String> {
let lines = if opts.fences {
let tmp = compress_fences(lines);
attach_orphan_specifiers(&tmp)
} else {
lines.to_vec()
};
let pre = convert_html_tables(&lines);
let mut out = Vec::new();
let mut buf = Vec::new();
let mut fence_tracker = FenceTracker::default();
let mut in_table = false;
for line in &pre {
if handle_fence_line(line, &mut buf, &mut in_table, &mut out, &mut fence_tracker) {
continue;
}
if fence_tracker.in_fence() {
out.push(line.to_string());
continue;
}
if handle_table_line(line, &mut buf, &mut in_table, &mut out) {
continue;
}
flush_buffer(&mut buf, &mut in_table, &mut out);
out.push(line.to_string());
}
flush_buffer(&mut buf, &mut in_table, &mut out);
if opts.headings {
out = crate::headings::convert_setext_headings(&out);
}
if opts.code_emphasis {
out = crate::code_emphasis::fix_code_emphasis(&out);
}
let mut out = if opts.wrap {
wrap_text(&out, WRAP_COLS)
} else {
out
};
if opts.ellipsis {
out = replace_ellipsis(&out);
}
if opts.footnotes {
out = convert_footnotes(&out);
}
out
}
#[must_use]
pub fn process_stream(lines: &[String]) -> Vec<String> {
process_stream_inner(
lines,
Options {
wrap: true,
..Default::default()
},
)
}
#[must_use]
#[inline]
pub fn process_stream_no_wrap(lines: &[String]) -> Vec<String> {
process_stream_inner(lines, Options::default())
}
#[must_use]
pub fn process_stream_opts(lines: &[String], opts: Options) -> Vec<String> {
process_stream_inner(lines, opts)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn processes_html_and_tables() {
let input = vec![
"<table><tr><td>A</td><td>B</td></tr></table>".to_string(),
"| X | Y |".to_string(),
"|---|---|".to_string(),
"| 1 | 2 |".to_string(),
];
let output = process_stream(&input);
assert!(output.iter().any(|l| l.contains("| A | B |")));
assert!(output.iter().any(|l| l.contains("| X | Y |")));
}
#[test]
fn no_wrap_option() {
let input = vec!["| a | b |".to_string(), "| 1 | 2 |".to_string()];
let out = process_stream_no_wrap(&input);
assert_eq!(out, vec!["| a | b |", "| 1 | 2 |"]);
}
#[test]
fn integrates_code_emphasis_flag() {
let input = vec!["`X`** Y (in **`Z`**)**".to_string()];
let out = process_stream_inner(
&input,
Options {
code_emphasis: true,
..Default::default()
},
);
assert_eq!(out, vec!["**`X` Y (in `Z`)**"]);
}
#[test]
fn converts_headings_when_enabled() {
let input = vec![
"Heading".to_string(),
"====".to_string(),
"Paragraph".to_string(),
];
let disabled = process_stream_inner(
&input,
Options {
headings: false,
..Default::default()
},
);
assert_eq!(disabled, input);
let enabled = process_stream_inner(
&input,
Options {
headings: true,
..Default::default()
},
);
assert_eq!(
enabled,
vec!["# Heading".to_string(), "Paragraph".to_string()]
);
}
}