From bb5dde6c2eb24a2827b019009810760a016dad72 Mon Sep 17 00:00:00 2001 From: jutty Date: Thu, 1 Jan 2026 03:05:44 -0300 Subject: [PATCH] Fix closing of EOF open preformat context --- src/syntax/content/parser.rs | 57 ++++++++++++-------- src/syntax/content/parser/token.rs | 2 +- src/syntax/content/parser/token/anchor.rs | 2 +- src/syntax/content/parser/token/code.rs | 2 +- src/syntax/content/parser/token/header.rs | 4 +- src/syntax/content/parser/token/linebreak.rs | 2 +- src/syntax/content/parser/token/literal.rs | 2 +- src/syntax/content/parser/token/paragraph.rs | 2 +- src/syntax/content/parser/token/preformat.rs | 4 +- src/syntax/content/parser/token/span.rs | 2 +- 10 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/syntax/content/parser.rs b/src/syntax/content/parser.rs index 37a8abc..ae64b4c 100644 --- a/src/syntax/content/parser.rs +++ b/src/syntax/content/parser.rs @@ -169,6 +169,19 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec { tokens } +fn close(state: &State, tokens: &mut Vec) { + match state.context.block { + BlockContext::PreFormat => { + tokens.push(Token::PreFormat(PreFormat::new(false))); + }, + BlockContext::Paragraph => { + tokens.push(Token::Paragraph(Paragraph::new(false))); + }, + BlockContext::Header(_) => panic!("End of file with open header"), + BlockContext::None => (), + } +} + enum BlockContext { Paragraph, Header(u8), @@ -231,17 +244,6 @@ impl State { } } -fn close(state: &State, tokens: &mut Vec) { - match state.context.block { - BlockContext::Paragraph => { - tokens.push(Token::Paragraph(Paragraph::new(false))); - }, - BlockContext::Header(_) => panic!("End of file with open header"), - BlockContext::PreFormat => panic!("End of file with open preformat"), - BlockContext::None => (), - } -} - fn parse(tokens: &[Token]) -> String { tokens.iter().map(Token::render).collect::() } @@ -309,6 +311,24 @@ mod tests { ); } + #[test] + fn pre() { + let payload = "D0qdJ184f3q1okbYu3Xm1d93jj6jy615"; + assert_eq!( + read_noconfig(&format!("`\n{payload}\n`\n")), + format!("
\n{payload}\n
\n"), + ); + } + + #[test] + fn eof_pre() { + let payload = "Jp8INpWzsQmk20jpIhBFCfMUXOztxv0w"; + assert_eq!( + read_noconfig(&format!("`\n{payload}\n`")), + format!("
\n{payload}\n
"), + ); + } + #[test] #[should_panic(expected = "End of file with open header")] fn end_with_open_header() { @@ -325,18 +345,13 @@ mod tests { } #[test] - #[should_panic(expected = "End of file with open preformat")] fn end_with_open_preformat() { - let default_state = State::new(); - let state = State { - context: Context { - block: BlockContext::PreFormat, - ..default_state.context - }, - ..default_state - }; + let mut state = State::new(); + state.context.block = BlockContext::PreFormat; - close(&state, &mut vec![]); + let mut vec: Vec = vec![]; + close(&state, &mut vec); + assert_eq!(vec, vec![Token::PreFormat(PreFormat::new(false))]); } #[test] diff --git a/src/syntax/content/parser/token.rs b/src/syntax/content/parser/token.rs index 8484fff..8951a37 100644 --- a/src/syntax/content/parser/token.rs +++ b/src/syntax/content/parser/token.rs @@ -9,7 +9,7 @@ pub mod header; pub mod preformat; pub mod code; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum Token { Anchor(anchor::Anchor), Code(code::Code), diff --git a/src/syntax/content/parser/token/anchor.rs b/src/syntax/content/parser/token/anchor.rs index 0e58c57..aee6d6d 100644 --- a/src/syntax/content/parser/token/anchor.rs +++ b/src/syntax/content/parser/token/anchor.rs @@ -1,6 +1,6 @@ use crate::syntax::content::{Parseable, parser::lexeme::Lexeme}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct Anchor { pub text: String, pub destination: Option, diff --git a/src/syntax/content/parser/token/code.rs b/src/syntax/content/parser/token/code.rs index f3a5f70..c463873 100644 --- a/src/syntax/content/parser/token/code.rs +++ b/src/syntax/content/parser/token/code.rs @@ -2,7 +2,7 @@ use crate::{ syntax::content::{Parseable, Lexeme}, }; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Code { open: bool, } diff --git a/src/syntax/content/parser/token/header.rs b/src/syntax/content/parser/token/header.rs index 5c067c0..a38488e 100644 --- a/src/syntax/content/parser/token/header.rs +++ b/src/syntax/content/parser/token/header.rs @@ -10,7 +10,7 @@ use crate::{ use std::fmt::Display; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Header { open: Option, level: Level, @@ -111,7 +111,7 @@ impl Parseable for Header { } } -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum Level { One, Two, diff --git a/src/syntax/content/parser/token/linebreak.rs b/src/syntax/content/parser/token/linebreak.rs index 9105dcc..5c9b93e 100644 --- a/src/syntax/content/parser/token/linebreak.rs +++ b/src/syntax/content/parser/token/linebreak.rs @@ -2,7 +2,7 @@ use crate::{ syntax::content::{Parseable, parser::lexeme::Lexeme}, }; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct LineBreak {} impl Parseable for LineBreak { diff --git a/src/syntax/content/parser/token/literal.rs b/src/syntax/content/parser/token/literal.rs index 5c1c292..57954b1 100644 --- a/src/syntax/content/parser/token/literal.rs +++ b/src/syntax/content/parser/token/literal.rs @@ -1,6 +1,6 @@ use crate::syntax::content::{Parseable, parser::lexeme::Lexeme}; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Literal { text: String, } diff --git a/src/syntax/content/parser/token/paragraph.rs b/src/syntax/content/parser/token/paragraph.rs index c26a397..8beb82e 100644 --- a/src/syntax/content/parser/token/paragraph.rs +++ b/src/syntax/content/parser/token/paragraph.rs @@ -1,6 +1,6 @@ use crate::syntax::content::{Parseable, parser::lexeme::Lexeme}; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Paragraph { open: Option, } diff --git a/src/syntax/content/parser/token/preformat.rs b/src/syntax/content/parser/token/preformat.rs index 9bb88bb..e75e37a 100644 --- a/src/syntax/content/parser/token/preformat.rs +++ b/src/syntax/content/parser/token/preformat.rs @@ -2,7 +2,7 @@ use crate::{ syntax::content::{Parseable, Lexeme}, }; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct PreFormat { open: Option, } @@ -15,7 +15,7 @@ impl PreFormat { impl Parseable for PreFormat { fn probe(lexeme: &Lexeme) -> bool { - lexeme.match_first_char('`') && lexeme.next == "\n" + lexeme.match_first_char('`') && (lexeme.next() == "\n" || lexeme.last()) } fn lex(_lexeme: &Lexeme) -> PreFormat { diff --git a/src/syntax/content/parser/token/span.rs b/src/syntax/content/parser/token/span.rs index 252b83e..1eb95a5 100644 --- a/src/syntax/content/parser/token/span.rs +++ b/src/syntax/content/parser/token/span.rs @@ -1,6 +1,6 @@ use crate::syntax::content::{Parseable, parser::lexeme::Lexeme}; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub struct Span { open: Option, }