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

View file

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

View file

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

View file

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

View file

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

View file

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