Skip to content

Commit

Permalink
Merge pull request #51 from darakian/lex-tabs
Browse files Browse the repository at this point in the history
Add tab lexing
  • Loading branch information
darakian committed Jun 16, 2022
2 parents 0bc0a09 + bdc28f0 commit 40f3e3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ pub(crate) fn lex_newlines<'a>(char_iter: &mut MiniIter<'a>) -> Result<Token<'a>
}
}

pub(crate) fn lex_tabs<'a>(char_iter: &mut MiniIter<'a>) -> Result<Token<'a>, ParseError<'a>> {
match char_iter.consume_while_case_holds(&|c| c == "\t") {
Some(s) if s.len() > 1 => return Ok(Token::DoubleTab),
Some(s) if s.len() == 1 => return Ok(Token::Tab),
_ => return Err(ParseError{content: ""}),
}
}

pub(crate) fn lex_blockquotes<'a>(char_iter: &mut MiniIter<'a>) -> Result<Token<'a>, ParseError<'a>> {
let right_arrows = char_iter.consume_while_case_holds(&|c| c == ">").unwrap_or("");
match char_iter.next_if_eq(" ") {
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ pub fn lex(source: &str) -> Vec<Token>{
Err(e) => push_str(&mut tokens, e.content),
}
},
"\t" => {
match lex_tabs(&mut char_iter) {
Ok(t) => tokens.push(t),
Err(e) => push_str(&mut tokens, e.content),
}
},
">" => {
match lex_blockquotes(&mut char_iter) {
Ok(t) => {
Expand Down Expand Up @@ -227,6 +233,8 @@ pub fn parse(tokens: &[Token]) -> String {
html.push_str(format!("<li>{}</li>", sanitize_display_text(t)).as_str())
},
Token::Newline => {html.push('\n')},
Token::Tab => {html.push('\t')},
Token::DoubleTab => {html.push_str("\t\t")},
Token::Italic(t) => {html.push_str(format!("<em>{}</em>", sanitize_display_text(t)).as_str())},
Token::Bold(t) => {html.push_str(format!("<strong>{}</strong>", sanitize_display_text(t)).as_str())},
Token::BoldItalic(t) => {html.push_str(format!("<strong><em>{}</em></strong>", sanitize_display_text(t)).as_str())},
Expand Down

0 comments on commit 40f3e3e

Please sign in to comment.