From 8376f40054002a2dbba7f718aef82c6a8c5f7bc6 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Wed, 1 May 2024 22:03:32 -0400 Subject: [PATCH] Simplified comment matching regex --- src/regexes.rs | 3 +-- src/subs.rs | 3 +-- tests/comments_in.tex | 4 ++++ tests/comments_out.tex | 4 ++++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/regexes.rs b/src/regexes.rs index 5621f8b..b73350b 100644 --- a/src/regexes.rs +++ b/src/regexes.rs @@ -7,8 +7,7 @@ lazy_static! { pub static ref RE_NEWLINES: Regex = Regex::new(r"\n\n\n+").unwrap(); pub static ref RE_TABS: Regex = Regex::new(r"\t").unwrap(); pub static ref RE_TRAIL: Regex = Regex::new(r" +\n").unwrap(); - pub static ref RE_PERCENT: Regex = Regex::new(r"\\\%").unwrap(); - pub static ref RE_COMMENT: Regex = Regex::new(r"\%.*").unwrap(); + pub static ref RE_COMMENT: Regex = Regex::new(r"(?P.*^|[^\\])(?P\%.*$)").unwrap(); pub static ref RE_ITEM: Regex = Regex::new(r".*\\item.*").unwrap(); pub static ref RE_DOCUMENT_BEGIN: Regex = Regex::new(r".*\\begin\{document\}.*").unwrap(); diff --git a/src/subs.rs b/src/subs.rs index a847460..dd2d8d4 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -42,6 +42,5 @@ pub fn remove_trailing_spaces(file: &str) -> String { //} pub fn remove_comment(line: &str) -> String { - let new_line = RE_PERCENT.replace_all(line, "").to_string(); - RE_COMMENT.replace_all(&new_line, "").to_string() + RE_COMMENT.replace_all(line, "$text").to_string() } diff --git a/tests/comments_in.tex b/tests/comments_in.tex index 0fba314..c6ffac9 100644 --- a/tests/comments_in.tex +++ b/tests/comments_in.tex @@ -15,4 +15,8 @@ % Similarly they might contain \begin{align} % unmatched environment tags. +This is a percent sign \% and not a comment + +Some lines might have both \% percents % and comments \end{align} + \end{document} diff --git a/tests/comments_out.tex b/tests/comments_out.tex index 853727a..bc25171 100644 --- a/tests/comments_out.tex +++ b/tests/comments_out.tex @@ -15,4 +15,8 @@ % Similarly they might contain \begin{align} % unmatched environment tags. +This is a percent sign \% and not a comment + +Some lines might have both \% percents % and comments \end{align} + \end{document}