Skip to content

Commit

Permalink
Working on line breaking for environments
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 2, 2024
1 parent dbbe4f6 commit ff14461
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 deletions.
3 changes: 3 additions & 0 deletions notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Tests
** Look for problem cases in other latex documents
* Features
** TODO Environments should start and end on new lines
*** New line before begin/end with regex replace
*** Care with comments
** Fold long lines to 80 characters
*** Care with trailing comments
*** No folding in verbatim environments, just warn
Expand Down
3 changes: 1 addition & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::TAB;
pub fn format_file(file: String, debug: bool) -> String {
// preformat
let mut new_file = remove_extra_newlines(&file);
new_file = begin_environments_new_line(&new_file);
new_file = end_environments_new_line(&new_file);
//new_file = begin_end_environments_new_line(&new_file);
new_file = remove_tabs(&new_file);
new_file = remove_trailing_spaces(&new_file);
let lines: Vec<&str> = new_file.lines().collect();
Expand Down
35 changes: 25 additions & 10 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,32 @@ pub fn remove_trailing_spaces(file: &str) -> String {
RE_TRAIL.replace_all(file, "\n").to_string()
}

pub fn begin_environments_new_line(file: &str) -> String {
RE_ENV_BEGIN_SHARED_LINE
.replace_all(file, "$prev\n$env")
.to_string()
}
// TODO
//pub fn begin_end_environments_new_line(file: &str) -> String {
//file
//.lines()
//.map(|l| remove_comment(l))
//.map(|l|
//RE_ENV_BEGIN_SHARED_LINE
//.replace_all(&l, "$prev\n$env")
//.to_string())
//.map(|l|
//RE_ENV_END_SHARED_LINE
//.replace_all(&l, "$prev\n$env")
//.to_string())
//.fold(String::new(), |a, b| a + &b + "\n")

pub fn end_environments_new_line(file: &str) -> String {
RE_ENV_END_SHARED_LINE
.replace_all(file, "$prev\n$env")
.to_string()
}
//let lines: Vec<&str> = new_file.lines().collect();
//let n_lines = lines.len();
//let mut new_lines = vec![];
//for i in 0..n_lines {
//let line = lines[i];
//if RE_ENV_BEGIN_SHARED_LINE
//}
//RE_ENV_BEGIN_SHARED_LINE
//.replace_all(file, "$prev\n$env")
//.to_string()
//}

pub fn remove_comment(line: &str) -> String {
let new_line = RE_PERCENT.replace_all(line, "").to_string();
Expand Down
3 changes: 0 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ mod tests {

#[test]
fn test_files() {
test_file("environment_lines");
/*
let filenames: Vec<String> = fs::read_dir("tests/")
.unwrap()
.map(|f| f.unwrap().file_name().into_string().unwrap())
Expand All @@ -48,6 +46,5 @@ mod tests {
for filename in filenames {
test_file(&filename);
}
*/
}
}
20 changes: 11 additions & 9 deletions tests/environment_lines_in.tex
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
TODO

% environments on separate lines
\begin{env1}
\begin{env2}
\end{env2}
\end{env1}
%\begin{env1}
%\begin{env2}
%\end{env2}
%\end{env1}

% environments on shared lines
\begin{env1}\begin{env2}
\end{env2}\end{env1}
%\begin{env1}\begin{env2}
%\end{env2}\end{env1}

% environments on shared lines with spaces
\begin{env1} \begin{env2}
\end{env2} \end{env1}
%\begin{env1} \begin{env2}
%\end{env2} \end{env1}

% environments all on same line
\begin{env1}\begin{env2}\end{env2}\end{env1}
%\begin{env1}\begin{env2}\end{env2}\end{env1}
27 changes: 11 additions & 16 deletions tests/environment_lines_out.tex
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
TODO

% environments on separate lines
\begin{env1}
\begin{env2}
\end{env2}
\end{env1}
%\begin{env1}
%\begin{env2}
%\end{env2}
%\end{env1}

% environments on shared lines
\begin{env1}
\begin{env2}
\end{env2}
\end{env1}
%\begin{env1}\begin{env2}
%\end{env2}\end{env1}

% environments on shared lines with spaces
\begin{env1}
\begin{env2}
\end{env2}
\end{env1}
%\begin{env1} \begin{env2}
%\end{env2} \end{env1}

% environments all on same line
\begin{env1}
\begin{env2}
\end{env2}
\end{env1}
%\begin{env1}\begin{env2}\end{env2}\end{env1}

0 comments on commit ff14461

Please sign in to comment.