Skip to content

Commit 1c50206

Browse files
committed
fix: Preserve whitespace in Google Docs RTF exports (#17)
1 parent 93486db commit 1c50206

3 files changed

Lines changed: 11 additions & 12 deletions

File tree

src/lexer.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ impl Lexer {
119119
let mut ret = vec![Token::ControlSymbol(control_word)];
120120
recursive_tokenize!(tail, ret);
121121

122-
// \u1234 \u1234 is ok, but \u1234 \u1234 is lost a space, \u1234 \u1234 lost two spaces, and so on
123-
// \u1234 1 -> No need to walk in here, it will enter plain text
124-
if control_word.0 == ControlWord::Unicode && tail.len() > 0 && tail.trim() == "" {
122+
// The first whitespace delimits the control word, the remaining ones are plain text
123+
if tail.len() > 0 && tail.is_only_whitespace() {
125124
ret.push(Token::PlainText(tail));
126125
}
127126
return Ok(ret);
@@ -338,11 +337,4 @@ if (a == b) \{\
338337
assert_eq!(tokens, [OpeningBracket, ControlSymbol((ColorNumber, Value(1))), PlainText(" "), ClosingBracket]);
339338
}
340339

341-
#[test]
342-
fn should_handle_google_docs_files() {
343-
use crate::include_test_file;
344-
let rtf = include_test_file!("google-docs.rtf");
345-
let tokens = Lexer::scan(rtf).unwrap();
346-
assert_eq!(tokens, []);
347-
}
348340
}

src/parser.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ pub mod tests {
625625
);
626626
}
627627

628+
#[test]
629+
fn parse_google_docs_whitespaces() {
630+
let rtf_content = include_test_file!("google-docs.rtf");
631+
let tokens = Lexer::scan(rtf_content).unwrap();
632+
let document = Parser::new(tokens).parse().unwrap();
633+
assert_eq!(document.get_text(), "Lorem ipsum odor amet");
634+
}
635+
628636
#[test]
629637
fn parse_image_data() {
630638
// Try to parse without error

src/utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ impl StrUtils for str {
1717
}
1818

1919
fn is_only_whitespace(&self) -> bool {
20-
// TODO
21-
false
20+
self.chars().all(|c| c.is_ascii_whitespace())
2221
}
2322
}
2423

0 commit comments

Comments
 (0)