Skip to content

Commit

Permalink
Better handle trailing header closer thingys
Browse files Browse the repository at this point in the history
  • Loading branch information
darakian committed Sep 15, 2022
1 parent 749108f commit ec416f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,20 @@ pub(crate) fn lex_heading<'a>(char_iter: &mut MiniIter<'a>) -> Result<Token, Par
.strip_prefix("{#").unwrap()
.strip_suffix("}").unwrap();
}
let parsed_line = crate::render_ignore(line.trim_end_matches(&['#', ' ', '\t']), &['#'])
let line_without_optional_trailing_hash_sequence = match line.rsplit_once(' ') {
Some((left, right)) => {
match right.chars().all(|c| c == '#') {
true => left,
false => line,
}
},
None => line,
};
let parsed_line = crate::render_ignore(line_without_optional_trailing_hash_sequence.trim_end_matches(&[' ', '\t']), &['#'])
.strip_prefix("<p>").unwrap_or("")
.strip_suffix("</p>\n").unwrap_or("").trim().to_string();
println!("line: {:?}", line);
println!("parsed_line: {:?}", parsed_line);
if heading != "" {
return Ok(Token::Header(hashes.len(), heading.trim().to_string(), Some(parsed_line)));
}
Expand Down Expand Up @@ -192,9 +203,6 @@ pub(crate) fn lex_tabs_spaces<'a>(char_iter: &mut MiniIter<'a>, tokens: &Vec<Tok
}
let whitespace = whitespace.unwrap_or("");
let line = char_iter.consume_until_tail_is("\n").unwrap_or("");
println!("??> {:?}", tokens.last());
println!(">>? {:?}", line);
println!(">>? {:?}", whitespace);
match whitespace {
" " if (matches!(tokens.last(), Some(Token::Plaintext(_))) && line.contains('#')) => return Err(ParseError{content: line}),
" " if (matches!(tokens.last(), Some(Token::Newline)) && line.contains('#')) => return Err(ParseError{content: line}),
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ pub fn lex(source: &str, ignore: &[char]) -> Vec<Token>{

/// Parse tokens to produce safe html output
pub fn parse(tokens: &[Token]) -> String {
println!("??? {:?}", tokens);
let mut html = String::with_capacity(tokens.len()*100);
let mut in_task_list = false;
let mut in_ordered_list = false;
Expand Down Expand Up @@ -172,7 +171,6 @@ pub fn parse(tokens: &[Token]) -> String {
// Add content
match token {
Token::Plaintext(t) => {
println!("?????? {:?}", t);
if t.trim().is_empty() {continue}

// Handle references
Expand Down

0 comments on commit ec416f6

Please sign in to comment.