mod remnants;
mod strip;
use std::borrow::Cow;
use std::io;
pub use remnants::is_remnant;
pub use strip::strip_line;
const BLANK_ENVIRONMENTS: &[&str] = &[
"tabular",
"tabular*",
"tabularx",
"algorithmic",
"algorithm",
"lstlisting",
"minted",
"verbatim",
"verbatim*",
"Verbatim",
"tikzpicture",
"displaymath",
"math",
"equation",
"equation*",
"eqnarray",
"eqnarray*",
"IEEEeqnarray",
"IEEEeqnarray*",
"align",
"align*",
"aligned",
"alignat",
"alignat*",
"flalign",
"flalign*",
"gather",
"gather*",
"gathered",
"multline",
"multline*",
"split",
"subequations",
"array",
"matrix",
"pmatrix",
"bmatrix",
"Bmatrix",
"vmatrix",
"Vmatrix",
"smallmatrix",
"cases",
"quote",
"IEEEkeywords",
"figure",
"figure*",
"table",
"table*",
"thebibliography",
];
#[must_use]
pub fn strip(source: &str) -> String {
let mut out: Vec<Cow<'_, str>> = Vec::new();
let mut blank_depth: u32 = 0;
let mut brace_blank_depth: u32 = 0;
let mut caption_depth: u32 = 0;
for line in source.lines() {
let rendered: Cow<'_, str> = 'line: {
if brace_blank_depth > 0 {
brace_blank_depth = close_brace_run(line.bytes(), brace_blank_depth);
break 'line Cow::Borrowed("");
}
if let Some(offset) = starts_multiline_drop(line) {
brace_blank_depth = close_brace_run(line.bytes().skip(offset + 1), 1);
break 'line Cow::Borrowed("");
}
if begins_blank_environment(line) {
if blank_depth == 0 {
append_trailing_period(&mut out);
}
blank_depth += 1;
}
if blank_depth > 0 {
if ends_blank_environment(line) {
blank_depth = blank_depth.saturating_sub(1);
break 'line Cow::Borrowed("");
}
if caption_depth > 0 {
caption_depth = update_brace_depth(line, caption_depth);
break 'line strip_prose(line);
}
if let Some((_, after_caption)) = line.split_once("\\caption") {
caption_depth = update_brace_depth(after_caption, 0);
break 'line strip_prose(line);
}
break 'line Cow::Borrowed("");
}
if ends_blank_environment(line) {
blank_depth = blank_depth.saturating_sub(1);
break 'line Cow::Borrowed("");
}
strip_prose(line)
};
out.push(rendered);
}
let mut result = String::with_capacity(source.len());
for line in &out {
result.push_str(line);
result.push('\n');
}
result
}
pub fn strip_reader(reader: impl io::Read) -> io::Result<String> {
Ok(strip(&io::read_to_string(reader)?))
}
fn strip_prose(line: &str) -> Cow<'_, str> {
let stripped = strip_line(line);
if is_remnant(&stripped) { Cow::Borrowed("") } else { stripped }
}
fn close_brace_run(bytes: impl Iterator<Item = u8>, mut depth: u32) -> u32 {
for byte in bytes {
match byte {
b'{' => depth += 1,
b'}' => {
depth = depth.saturating_sub(1);
if depth == 0 {
break;
}
}
_ => {}
}
}
depth
}
const MULTILINE_DROP_COMMANDS: &[&str] = &["\\author{", "\\title{"];
fn update_brace_depth(line: &str, mut depth: u32) -> u32 {
let mut bytes = line.bytes();
while let Some(byte) = bytes.next() {
match byte {
b'\\' => drop(bytes.next()),
b'{' => depth += 1,
b'}' => depth = depth.saturating_sub(1),
_ => {}
}
}
depth
}
fn starts_multiline_drop(line: &str) -> Option<usize> {
let trimmed = line.trim_start();
let leading = line.len() - trimmed.len();
MULTILINE_DROP_COMMANDS
.iter()
.find(|cmd| trimmed.starts_with(**cmd))
.map(|cmd| leading + cmd.len() - 1)
}
fn parse_env<'src>(line: &'src str, prefix: &str) -> Option<&'src str> {
let rest = line.trim().strip_prefix(prefix)?;
rest.get(..rest.find('}')?)
}
fn begins_blank_environment(line: &str) -> bool {
parse_env(line, r"\begin{").is_some_and(|env| BLANK_ENVIRONMENTS.contains(&env))
}
fn ends_blank_environment(line: &str) -> bool {
parse_env(line, r"\end{").is_some_and(|env| BLANK_ENVIRONMENTS.contains(&env))
}
fn append_trailing_period(lines: &mut [Cow<'_, str>]) {
let Some(last) = lines.iter_mut().rev().find(|l| !l.trim().is_empty()) else {
return;
};
let text = last.to_mut();
let end = text.trim_end().len();
text.truncate(end);
if !text.ends_with(['.', '!', '?', ':']) {
text.push('.');
}
}