Make Oblique no longer a context, add a point module

This commit is contained in:
Juno Takano 2026-01-05 12:52:48 -03:00
commit 315956e20d
4 changed files with 58 additions and 20 deletions

View file

@ -2,7 +2,7 @@ use crate::{prelude::*, types::Config};
use super::{Parseable as _, Token, LexMap};
use token::{
anchor::Anchor, linebreak::LineBreak, paragraph::Paragraph, header::Header,
preformat::PreFormat, literal::Literal, code::Code, oblique::Oblique,
preformat::PreFormat, literal::Literal, code::Code,
};
use lexeme::Lexeme;
use context::{Block, Inline};
@ -11,6 +11,7 @@ pub mod token;
pub mod lexeme;
pub mod segment;
pub mod context;
pub mod point;
pub mod state;
const LEXMAP: LexMap = &[
@ -24,7 +25,7 @@ const LEXMAP: LexMap = &[
fn lex(text: &str, map: LexMap, config: &Config) -> Vec<Token> {
let mut tokens: Vec<Token> = Vec::new();
let mut state = state::State::new();
let mut state = state::State::default();
let segments = segment::segment(text);
let lexemes = Lexeme::collect(&segments);
@ -79,17 +80,16 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec<Token> {
},
}
if point::puncture(lexeme, &mut state, &mut tokens) {
continue;
}
match state.context.inline {
Inline::None => {
if Code::probe(lexeme) {
state.context.inline = Inline::Code;
tokens.push(Token::Code(Code::new(true)));
continue;
} else if Oblique::probe(lexeme) {
log!("Inline Context: None -> Oblique on {lexeme}");
state.context.inline = Inline::Oblique;
tokens.push(Token::Oblique(Oblique::new(true)));
continue;
} else if Anchor::probe(lexeme) {
state.context.inline = Inline::Anchor;
state.buffers.anchor.clear();
@ -112,14 +112,6 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec<Token> {
continue;
}
},
Inline::Oblique => {
if Oblique::probe(lexeme) {
log!("Inline Context: Oblique -> None on {lexeme}");
state.context.inline = Inline::None;
tokens.push(Token::Oblique(Oblique::new(false)));
continue;
}
},
Inline::Anchor => {
if context::anchor::parse(lexeme, &mut state, &mut tokens) {
continue;
@ -390,7 +382,7 @@ mod tests {
#[test]
#[should_panic(expected = "End of input with open header")]
fn end_with_open_header() {
let mut state = State::new();
let mut state = State::default();
state.context.block = Block::Header(1);
context::close(&state, &mut vec![]);
@ -398,7 +390,7 @@ mod tests {
#[test]
fn end_with_open_preformat() {
let mut state = State::new();
let mut state = State::default();
state.context.block = Block::PreFormat;
let mut vec: Vec<Token> = vec![];

View file

@ -23,7 +23,6 @@ pub enum Block {
pub enum Inline {
Anchor,
Code,
Oblique,
None,
}

View file

@ -0,0 +1,40 @@
use crate::syntax::content::{
Parseable as _,
parser::{
lexeme::Lexeme,
token::{Token, oblique::Oblique},
state::State,
},
};
pub fn puncture(
lexeme: &Lexeme,
state: &mut State,
tokens: &mut Vec<Token>,
) -> bool {
if Oblique::probe(lexeme) {
tokens.push(Token::Oblique(Oblique::new(!state.switches.oblique)));
state.switches.oblique = !state.switches.oblique;
return true;
}
false
}
#[cfg(test)]
mod tests {
use crate::{syntax::content::parser, types::Graph};
fn read_noconfig(input: &str) -> String {
parser::read(input, &Graph::new(None).meta.config)
}
#[test]
fn oblique() {
assert_eq!(
read_noconfig(
"_|this anchor is oblique|o as are these literals_ but not these _just these_, not this _and these with an |anchor| again_"
),
r#"<p><em><a href="/node/o">this anchor is oblique</a> as are these literals</em> but not these <em>just these</em>, not this <em>and these with an <a href="/node/anchor">anchor</a> again</em></p>"#
);
}
}

View file

@ -9,9 +9,15 @@ use crate::syntax::content::parser::{
pub struct State {
pub context: Context,
pub dom_ids: HashMap<String, Vec<String>>,
pub switches: Switches,
pub buffers: Buffers,
}
#[derive(Clone, Debug)]
pub struct Switches {
pub oblique: bool,
}
#[derive(Clone, Debug)]
pub struct Buffers {
pub anchor: AnchorBuffer,
@ -56,14 +62,15 @@ impl std::fmt::Display for AnchorBuffer {
}
}
impl State {
pub fn new() -> State {
impl Default for State {
fn default() -> State {
State {
context: Context {
inline: Inline::None,
block: Block::None,
},
dom_ids: HashMap::new(),
switches: Switches { oblique: false },
buffers: Buffers {
anchor: AnchorBuffer {
candidate: Anchor::default(),