From 27b8e7f363ae68bb9a2d1990af9e7b149be0dc00 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sun, 20 Nov 2016 22:38:01 +0100 Subject: [PATCH] Detect and store format version --- src/tokenizer.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 67a4c4a..e87b533 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -4,6 +4,16 @@ use std::io::BufRead; use types::LineType; +#[derive(Debug, PartialEq, Eq)] +pub enum TldrFormat { + /// Not yet clear + Undecided, + /// The original format + V1, + /// The new format (see https://github.com/tldr-pages/tldr/pull/958) + V2, +} + /// A tokenizer is initialized with a BufReader instance that contains the /// entire Tldr page. It then returns tokens as `Option`. #[derive(Debug)] @@ -14,6 +24,8 @@ pub struct Tokenizer { first_line: bool, /// Buffer for the current line. Used internally. current_line: String, + /// The tldr page format. + format: TldrFormat, } impl Tokenizer where R: BufRead { @@ -22,6 +34,7 @@ impl Tokenizer where R: BufRead { reader: reader, first_line: true, current_line: String::new(), + format: TldrFormat::Undecided, } } @@ -45,11 +58,17 @@ impl Tokenizer where R: BufRead { return None; } self.first_line = false; + self.format = TldrFormat::V2; return Some(LineType::Title(self.current_line.trim_right().to_string())); } - // Clear `first_line` flag - self.first_line = false; + if self.first_line { + // Clear `first_line` flag + self.first_line = false; + + // It's the old format. + self.format = TldrFormat::V1; + } // Convert line to a `LineType` instance Some(LineType::from(&self.current_line[..]))