From 00512fdc7bc04eda29fe0e9c2c4bf922626a1f90 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sat, 12 Nov 2016 13:34:13 +0100 Subject: [PATCH] Mostly port to new format Titles are not handled properly yet. See discussion at https://github.com/tldr-pages/tldr/pull/958 --- src/formatter.rs | 5 ++--- src/types.rs | 16 ++++++---------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 56f4891..93656fd 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -29,9 +29,8 @@ pub fn print_lines(tokenizer: &mut Tokenizer) where R: BufRead { LineType::Empty => println!(""), LineType::Title(_) => debug!("Ignoring title"), LineType::Description(text) => println!(" {}", text), - LineType::ExampleText(text) => println!(" {}", Colour::Green.paint(format!("- {}", text))), - LineType::ExampleCode(text) => println!(" {}", &format_braces(&text)), - LineType::Other(text) => debug!("Unknown line type: {:?}", text), + LineType::ExampleText(text) => println!(" {}", Colour::Green.paint(text)), + LineType::ExampleCode(text) => println!(" {}", &format_braces(&text)), } } println!(""); diff --git a/src/types.rs b/src/types.rs index e3a9b60..1f7c5d9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -40,21 +40,19 @@ pub enum LineType { Description(String), ExampleText(String), ExampleCode(String), - Other(String), } impl<'a> From<&'a str> for LineType { - /// Convert a string slice to a LineType. Newlines and whitespace are trimmed. + /// Convert a string slice to a LineType. Newlines and trailing whitespace are trimmed. fn from(line: &'a str) -> LineType { - let trimmed = line.trim(); + let trimmed: &str = line.trim_right(); let mut chars = trimmed.chars(); match chars.next() { None => LineType::Empty, Some('#') => LineType::Title(trimmed.trim_left_matches(|chr: char| chr == '#' || chr.is_whitespace()).into()), Some('>') => LineType::Description(trimmed.trim_left_matches(|chr: char| chr == '>' || chr.is_whitespace()).into()), - Some('-') => LineType::ExampleText(trimmed.trim_left_matches(|chr: char| chr == '-' || chr.is_whitespace()).into()), - Some('`') if chars.last() == Some('`') => LineType::ExampleCode(trimmed.trim_matches(|chr: char| chr == '`' || chr.is_whitespace()).into()), - _ => LineType::Other(trimmed.into()), + Some(' ') => LineType::ExampleCode(trimmed.trim_left_matches(|chr: char| chr.is_whitespace()).into()), + _ => LineType::ExampleText(trimmed.into()), } } } @@ -93,9 +91,7 @@ mod test { assert_eq!(LineType::from(" \n \r"), LineType::Empty); assert_eq!(LineType::from("# Hello there"), LineType::Title("Hello there".into())); assert_eq!(LineType::from("> tis a description \n"), LineType::Description("tis a description".into())); - assert_eq!(LineType::from("- some command"), LineType::ExampleText("some command".into())); - assert_eq!(LineType::from("`$ cargo run`"), LineType::ExampleCode("$ cargo run".into())); - assert_eq!(LineType::from("`$ cargo run"), LineType::Other("`$ cargo run".into())); - assert_eq!(LineType::from("jkl\u{f6}"), LineType::Other("jkl\u{f6}".into())); + assert_eq!(LineType::from("some command "), LineType::ExampleText("some command".into())); + assert_eq!(LineType::from(" $ cargo run "), LineType::ExampleCode("$ cargo run".into())); } }