Fix closing of EOF open preformat context

This commit is contained in:
Juno Takano 2026-01-01 03:05:44 -03:00
commit bb5dde6c2e
10 changed files with 47 additions and 32 deletions

View file

@ -169,6 +169,19 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec<Token> {
tokens
}
fn close(state: &State, tokens: &mut Vec<Token>) {
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<Token>) {
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::<String>()
}
@ -309,6 +311,24 @@ mod tests {
);
}
#[test]
fn pre() {
let payload = "D0qdJ184f3q1okbYu3Xm1d93jj6jy615";
assert_eq!(
read_noconfig(&format!("`\n{payload}\n`\n")),
format!("<pre>\n{payload}\n</pre>\n"),
);
}
#[test]
fn eof_pre() {
let payload = "Jp8INpWzsQmk20jpIhBFCfMUXOztxv0w";
assert_eq!(
read_noconfig(&format!("`\n{payload}\n`")),
format!("<pre>\n{payload}\n</pre>"),
);
}
#[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<Token> = vec![];
close(&state, &mut vec);
assert_eq!(vec, vec![Token::PreFormat(PreFormat::new(false))]);
}
#[test]

View file

@ -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),

View file

@ -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<String>,

View file

@ -2,7 +2,7 @@ use crate::{
syntax::content::{Parseable, Lexeme},
};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Code {
open: bool,
}

View file

@ -10,7 +10,7 @@ use crate::{
use std::fmt::Display;
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Header {
open: Option<bool>,
level: Level,
@ -111,7 +111,7 @@ impl Parseable for Header {
}
}
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub enum Level {
One,
Two,

View file

@ -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 {

View file

@ -1,6 +1,6 @@
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Literal {
text: String,
}

View file

@ -1,6 +1,6 @@
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Paragraph {
open: Option<bool>,
}

View file

@ -2,7 +2,7 @@ use crate::{
syntax::content::{Parseable, Lexeme},
};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct PreFormat {
open: Option<bool>,
}
@ -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 {

View file

@ -1,6 +1,6 @@
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Span {
open: Option<bool>,
}