Merge serial module into the graph module

This commit is contained in:
Juno Takano 2026-01-13 12:11:51 -03:00
commit 697dcc720d
17 changed files with 421 additions and 332 deletions

View file

@ -1,5 +1,5 @@
use crate::{prelude::*, graph::Graph};
use super::{Parseable as _, LexMap};
use super::{TokenOutput, Parseable as _, LexMap};
use token::{LineBreak, Literal};
use context::{Block, Inline};
pub use {lexeme::Lexeme, token::Token, state::State};
@ -20,7 +20,7 @@ const LEXMAP: LexMap = &[
}),
];
fn lex(text: &str, map: LexMap, graph: &Graph, blocking: bool) -> Vec<Token> {
fn lex(text: &str, map: LexMap, graph: &Graph, blocking: bool) -> TokenOutput {
let mut tokens: Vec<Token> = Vec::default();
let mut state = State::default();
@ -75,27 +75,37 @@ fn lex(text: &str, map: LexMap, graph: &Graph, blocking: bool) -> Vec<Token> {
}
context::close(&state, &mut tokens);
tokens
TokenOutput {
tokens,
format_tokens: state.format_tokens,
text: None,
}
}
pub(super) fn read(input: &str, graph: &Graph) -> String {
parse(&lex(input, LEXMAP, graph, true))
parse(&lex(input, LEXMAP, graph, true).tokens)
}
pub(super) fn rich_read(input: &str, graph: &Graph) -> (String, Vec<Token>) {
let tokens = lex(input, LEXMAP, graph, true);
let text = parse(&tokens);
(text, tokens)
pub(super) fn rich_read(input: &str, graph: &Graph) -> TokenOutput {
let lex_output = lex(input, LEXMAP, graph, true);
let text = parse(&lex_output.tokens);
TokenOutput {
text: Some(text),
tokens: lex_output.tokens,
format_tokens: lex_output.format_tokens,
}
}
/// Apply end-to-end point and inline parsing for nested formatting, such as
/// inside the display text of anchors and list items
pub fn format(input: &str, graph: &Graph) -> String {
parse(&lex(input, LEXMAP, graph, false))
pub fn format(input: &str, graph: &Graph) -> (String, Vec<Token>) {
let tokens = lex(input, LEXMAP, graph, false).tokens;
(parse(&tokens), tokens)
}
// Strip special syntax for display in noninteractive or plain-text display
pub fn flatten(input: &str, graph: &Graph) -> String {
let tokens = lex(input, LEXMAP, graph, true);
let tokens = lex(input, LEXMAP, graph, true).tokens;
let flat = tokens.iter().map(Token::flatten).collect::<String>();
log!("Flattened {tokens:?} to {flat}");
flat

View file

@ -67,6 +67,9 @@ pub fn parse(
} else if lexeme.match_char('|') && lexeme.is_next_delimiter() {
log!("End: Pipe followed by delimiter");
if buffer.destination.is_empty() {
if candidate.text().contains(':') {
candidate.set_external(true);
}
push(Some(&candidate.text().clone()), tokens, state, graph);
} else {
push(Some(&buffer.destination.clone()), tokens, state, graph);

View file

@ -48,7 +48,10 @@ pub fn parse(
}
if item_candidate.depth.is_some() {
// if the current item candidate has a known depth, push it
item_candidate.text = format(&item_candidate.text, graph);
let (text, format_tokens) =
format(&item_candidate.text, graph);
item_candidate.text = text;
state.format_tokens.extend_from_slice(&format_tokens);
candidate.items.push(item_candidate.clone());
}
// push list candidate, reset state and exit context
@ -60,7 +63,9 @@ pub fn parse(
} else if lexeme.match_char('\n') {
// found end of item, push it and reset state
log!("Accepting item candidate {item_candidate}");
item_candidate.text = format(&item_candidate.text, graph);
let (text, format_tokens) = format(&item_candidate.text, graph);
item_candidate.text = text;
state.format_tokens.extend_from_slice(&format_tokens);
candidate.items.push(item_candidate.clone());
*item_candidate = Item::default();
buffer.depth = 0;

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use crate::syntax::content::parser::{
Token,
context::{Block, Context, Inline},
token::{Anchor, Item, List},
};
@ -11,6 +12,7 @@ pub struct State {
pub dom_ids: HashMap<String, Vec<String>>,
pub switches: Switches,
pub buffers: Buffers,
pub format_tokens: Vec<Token>,
}
#[derive(Clone, Debug)]
@ -91,6 +93,7 @@ impl Default for State {
depth: 0,
},
},
format_tokens: vec![],
}
}
}