Skip to content

Commit

Permalink
Mostly port to new format
Browse files Browse the repository at this point in the history
Titles are not handled properly yet.

See discussion at tldr-pages/tldr#958
  • Loading branch information
dbrgn committed Nov 12, 2016
1 parent 699d221 commit 00512fd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!("");
Expand Down
16 changes: 6 additions & 10 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
}
Expand Down Expand Up @@ -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()));
}
}

0 comments on commit 00512fd

Please sign in to comment.