Fix closing of EOF open preformat context
This commit is contained in:
parent
7a09bf113a
commit
bb5dde6c2e
10 changed files with 47 additions and 32 deletions
|
|
@ -169,6 +169,19 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec<Token> {
|
||||||
tokens
|
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 {
|
enum BlockContext {
|
||||||
Paragraph,
|
Paragraph,
|
||||||
Header(u8),
|
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 {
|
fn parse(tokens: &[Token]) -> String {
|
||||||
tokens.iter().map(Token::render).collect::<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]
|
#[test]
|
||||||
#[should_panic(expected = "End of file with open header")]
|
#[should_panic(expected = "End of file with open header")]
|
||||||
fn end_with_open_header() {
|
fn end_with_open_header() {
|
||||||
|
|
@ -325,18 +345,13 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "End of file with open preformat")]
|
|
||||||
fn end_with_open_preformat() {
|
fn end_with_open_preformat() {
|
||||||
let default_state = State::new();
|
let mut state = State::new();
|
||||||
let state = State {
|
state.context.block = BlockContext::PreFormat;
|
||||||
context: Context {
|
|
||||||
block: BlockContext::PreFormat,
|
|
||||||
..default_state.context
|
|
||||||
},
|
|
||||||
..default_state
|
|
||||||
};
|
|
||||||
|
|
||||||
close(&state, &mut vec![]);
|
let mut vec: Vec<Token> = vec![];
|
||||||
|
close(&state, &mut vec);
|
||||||
|
assert_eq!(vec, vec![Token::PreFormat(PreFormat::new(false))]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ pub mod header;
|
||||||
pub mod preformat;
|
pub mod preformat;
|
||||||
pub mod code;
|
pub mod code;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
Anchor(anchor::Anchor),
|
Anchor(anchor::Anchor),
|
||||||
Code(code::Code),
|
Code(code::Code),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub struct Anchor {
|
pub struct Anchor {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub destination: Option<String>,
|
pub destination: Option<String>,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
syntax::content::{Parseable, Lexeme},
|
syntax::content::{Parseable, Lexeme},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Code {
|
pub struct Code {
|
||||||
open: bool,
|
open: bool,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use crate::{
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
open: Option<bool>,
|
open: Option<bool>,
|
||||||
level: Level,
|
level: Level,
|
||||||
|
|
@ -111,7 +111,7 @@ impl Parseable for Header {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum Level {
|
pub enum Level {
|
||||||
One,
|
One,
|
||||||
Two,
|
Two,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
syntax::content::{Parseable, parser::lexeme::Lexeme},
|
syntax::content::{Parseable, parser::lexeme::Lexeme},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct LineBreak {}
|
pub struct LineBreak {}
|
||||||
|
|
||||||
impl Parseable for LineBreak {
|
impl Parseable for LineBreak {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Literal {
|
pub struct Literal {
|
||||||
text: String,
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Paragraph {
|
pub struct Paragraph {
|
||||||
open: Option<bool>,
|
open: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
syntax::content::{Parseable, Lexeme},
|
syntax::content::{Parseable, Lexeme},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct PreFormat {
|
pub struct PreFormat {
|
||||||
open: Option<bool>,
|
open: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
@ -15,7 +15,7 @@ impl PreFormat {
|
||||||
|
|
||||||
impl Parseable for PreFormat {
|
impl Parseable for PreFormat {
|
||||||
fn probe(lexeme: &Lexeme) -> bool {
|
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 {
|
fn lex(_lexeme: &Lexeme) -> PreFormat {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
use crate::syntax::content::{Parseable, parser::lexeme::Lexeme};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Span {
|
pub struct Span {
|
||||||
open: Option<bool>,
|
open: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue