Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert unordered lists to use str #47

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum Token<'a> {
/// u8: Header level (1..=6). String: Header text. Option<String>: html label
Header(u8, &'a str, Option<&'a str>),
/// String: Text for list entry
UnorderedListEntry(String),
UnorderedListEntry(&'a str),
/// String: Text for list entry
OrderedListEntry(String),
/// String: Text to be italicized
Expand Down Expand Up @@ -132,7 +132,7 @@ pub(crate) fn lex_asterisk_underscore<'a>(char_iter: &mut MiniIter<'a>) -> Resul
if asterunds.len() == 1 && char_iter.next_if_eq(&" ").is_some(){
let s = char_iter.consume_while_case_holds(&|c| c != "\n").unwrap_or("");
char_iter.next();
return Ok(Token::UnorderedListEntry(s.to_string()))
return Ok(Token::UnorderedListEntry(s))
}
if asterunds.chars().all(|x| x == '*') && char_iter.peek() == Some(&"\n"){
return Ok(Token::HorizontalRule)
Expand Down Expand Up @@ -365,7 +365,7 @@ pub(crate) fn lex_plus_minus<'a>(char_iter: &mut MiniIter<'a>) -> Result<Token<'
} else if line.starts_with(" [X] ") {
return Ok(Token::TaskListItem(TaskBox::Checked,line.strip_prefix(" [X] ").unwrap_or("").to_string()))
} else if line.starts_with(" "){
return Ok(Token::UnorderedListEntry(line.strip_prefix(" ").unwrap_or("").to_string()))
return Ok(Token::UnorderedListEntry(line.strip_prefix(" ").unwrap_or("")))
} else {
return Err(ParseError{content: char_iter.get_substring_from(start_index).unwrap_or("")})
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn parse(tokens: &[Token]) -> String {
in_unordered_list = true;
html.push_str(format!("<ul>").as_str())
}
html.push_str(format!("<li>{}</li>", sanitize_display_text(t)).as_str())
html.push_str(format!("<li>{}</li>", sanitize_display_text(&t.to_string())).as_str())
},
Token::OrderedListEntry(t) => {
if in_ordered_list == false {
Expand Down
8 changes: 4 additions & 4 deletions tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fn test_lex() {
("I just love _*_bold italic text*_*.", vec![Token::Plaintext("I just love ".to_string()), Token::BoldItalic("bold italic text".to_string()), Token::Plaintext(".".to_string())]),
]);
tests.extend(vec![
("* unodered list\n", vec![Token::UnorderedListEntry("unodered list".to_string())]),
("* unodered list\n* with two\n", vec![Token::UnorderedListEntry("unodered list".to_string()), Token::UnorderedListEntry("with two".to_string())]),
("* unodered list\n* with two\n* with three\n", vec![Token::UnorderedListEntry("unodered list".to_string()), Token::UnorderedListEntry("with two".to_string()), Token::UnorderedListEntry("with three".to_string())]),
("* unodered list\n", vec![Token::UnorderedListEntry("unodered list")]),
("* unodered list\n* with two\n", vec![Token::UnorderedListEntry("unodered list"), Token::UnorderedListEntry("with two")]),
("* unodered list\n* with two\n* with three\n", vec![Token::UnorderedListEntry("unodered list"), Token::UnorderedListEntry("with two"), Token::UnorderedListEntry("with three")]),
]);
tests.extend(vec![
("Some text _with italics_ in the same paragraph", vec![Token::Plaintext("Some text ".to_string()), Token::Italic("with italics".to_string()), Token::Plaintext(" in the same paragraph".to_string())]),
Expand Down Expand Up @@ -75,7 +75,7 @@ fn test_lex() {
("+ [ ] Unchecked box", vec![Token::TaskListItem(TaskBox::Unchecked, "Unchecked box".to_string())]),
("- [x] Checked box", vec![Token::TaskListItem(TaskBox::Checked, "Checked box".to_string())]),
("- [X] Also a checked box", vec![Token::TaskListItem(TaskBox::Checked, "Also a checked box".to_string())]),
("- [X]Not a checked box", vec![Token::UnorderedListEntry("[X]Not a checked box".to_string())]),
("- [X]Not a checked box", vec![Token::UnorderedListEntry("[X]Not a checked box")]),
("- [X] A checked box\n- [X] Also a checked box", vec![Token::TaskListItem(TaskBox::Checked, "A checked box".to_string()), Token::Plaintext("\n".to_string()), Token::TaskListItem(TaskBox::Checked, "Also a checked box".to_string())]),
]);
for test in tests.iter(){
Expand Down