DocumentEnd cleanup

This commit is contained in:
ef3d0c3e 2024-08-27 10:39:30 +02:00
parent 822c3e3b66
commit da6349e2f5
6 changed files with 21 additions and 16 deletions

View file

@ -120,6 +120,7 @@ pub trait Document<'a>: core::fmt::Debug {
/// Pushes a new element into the document's content /// Pushes a new element into the document's content
fn push(&self, elem: Box<dyn Element>) { fn push(&self, elem: Box<dyn Element>) {
// Add reference
if let Some(refname) = elem if let Some(refname) = elem
.as_referenceable() .as_referenceable()
.and_then(|reference| reference.reference_name()) .and_then(|reference| reference.reference_name())
@ -128,7 +129,10 @@ pub trait Document<'a>: core::fmt::Debug {
refname.clone(), refname.clone(),
ElemReference::Direct(self.content().borrow().len()), ElemReference::Direct(self.content().borrow().len()),
); );
} else if let Some(container) = self }
// Add contained references
else if let Some(container) =
self
.content() .content()
.borrow() .borrow()
.last() .last()

View file

@ -7,7 +7,6 @@ use crate::parser::source::VirtualSource;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
// TODO enforce to_string(from_string(to_string())) == to_string()
pub trait Variable { pub trait Variable {
fn location(&self) -> &Token; fn location(&self) -> &Token;

View file

@ -13,7 +13,6 @@ use blockquote_style::BlockquoteStyle;
use regex::Match; use regex::Match;
use regex::Regex; use regex::Regex;
use runtime_format::FormatArgs; use runtime_format::FormatArgs;
use runtime_format::FormatError;
use runtime_format::FormatKey; use runtime_format::FormatKey;
use runtime_format::FormatKeyError; use runtime_format::FormatKeyError;
@ -22,7 +21,6 @@ use crate::compiler::compiler::Target;
use crate::compiler::compiler::Target::HTML; use crate::compiler::compiler::Target::HTML;
use crate::document::document::Document; use crate::document::document::Document;
use crate::document::element::ContainerElement; use crate::document::element::ContainerElement;
use crate::document::element::DocumentEnd;
use crate::document::element::ElemKind; use crate::document::element::ElemKind;
use crate::document::element::Element; use crate::document::element::Element;
use crate::elements::paragraph::Paragraph; use crate::elements::paragraph::Paragraph;
@ -127,8 +125,7 @@ impl Element for Blockquote {
let mut in_paragraph = false; let mut in_paragraph = false;
for elem in &self.content { for elem in &self.content {
if elem.downcast_ref::<DocumentEnd>().is_some() { if elem.downcast_ref::<Blockquote>().is_some() {
} else if elem.downcast_ref::<Blockquote>().is_some() {
if in_paragraph { if in_paragraph {
result += "</p>"; result += "</p>";
in_paragraph = false; in_paragraph = false;

View file

@ -396,11 +396,13 @@ impl Rule for ListRule {
) )
.finish(), .finish(),
); );
break; // Return an empty paragraph
vec![]
} }
Ok(mut paragraph) => std::mem::take(&mut paragraph.content), Ok(mut paragraph) => std::mem::take(&mut paragraph.content),
}; };
if let Some(previous_depth) = document if let Some(previous_depth) = document
.last_element::<ListEntry>() .last_element::<ListEntry>()
.map(|ent| ent.numbering.clone()) .map(|ent| ent.numbering.clone())

View file

@ -306,7 +306,7 @@ mod tests {
"".to_string(), "".to_string(),
r#" r#"
Simple evals: Simple evals:
* %< 1+1>% * 1+1: %< 1+1>%
* %<" 1+1>% = 2 * %<" 1+1>% = 2
* %<! "**bold**">% * %<! "**bold**">%
@ -327,7 +327,7 @@ Evaluation: %<! make_ref("hello", "id")>%
validate_document!(doc.content().borrow(), 0, validate_document!(doc.content().borrow(), 0,
Paragraph; Paragraph;
ListMarker; ListMarker;
ListEntry {}; ListEntry;
ListEntry { ListEntry {
Text { content == "2" }; Text { content == "2" };
Text { content == " = 2" }; Text { content == " = 2" };

View file

@ -112,6 +112,8 @@ impl Parser for LangParser {
super::state::Scope::DOCUMENT, super::state::Scope::DOCUMENT,
)); ));
if parent.is_none()
{
state.push( state.push(
&doc, &doc,
Box::new(DocumentEnd(Token::new( Box::new(DocumentEnd(Token::new(
@ -119,6 +121,7 @@ impl Parser for LangParser {
doc.source(), doc.source(),
))), ))),
); );
}
(Box::new(doc), state) (Box::new(doc), state)
} }