Mostly port to new format

Titles are not handled properly yet.

See discussion at https://github.com/tldr-pages/tldr/pull/958
This commit is contained in:
Danilo Bargen 2016-11-12 13:34:13 +01:00
commit 00512fdc7b
2 changed files with 8 additions and 13 deletions

View file

@ -29,9 +29,8 @@ pub fn print_lines<R>(tokenizer: &mut Tokenizer<R>) 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!("");

View file

@ -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()));
}
}