diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs
index 0d28d08..7714571 100644
--- a/src/compiler/compiler.rs
+++ b/src/compiler/compiler.rs
@@ -163,7 +163,7 @@ impl Compiler {
match elem.compile(self, document) {
Ok(result) => body.push_str(result.as_str()),
- Err(err) => println!("Unable to compile element: {err}\n{}", elem.to_string()),
+ Err(err) => println!("Unable to compile element: {err}\n{elem:#?}"),
}
}
body.push_str("");
diff --git a/src/compiler/navigation.rs b/src/compiler/navigation.rs
index bed071d..723597d 100644
--- a/src/compiler/navigation.rs
+++ b/src/compiler/navigation.rs
@@ -99,6 +99,7 @@ pub fn create_navigation(docs: &Vec) -> Result cat,
@@ -111,7 +112,7 @@ pub fn create_navigation(docs: &Vec) -> Result cat_ent,
None => {
// Insert
diff --git a/src/document/document.rs b/src/document/document.rs
index 44470e5..2e92c6a 100644
--- a/src/document/document.rs
+++ b/src/document/document.rs
@@ -169,7 +169,7 @@ pub trait Document<'a>: core::fmt::Debug {
Some(merge_as) => self.scope().borrow_mut().merge(
&mut *scope.borrow_mut(),
merge_as,
- self.content().borrow().len() + 1,
+ self.content().borrow().len(),
),
_ => {}
}
diff --git a/src/document/element.rs b/src/document/element.rs
index cde0615..24eec1c 100644
--- a/src/document/element.rs
+++ b/src/document/element.rs
@@ -34,7 +34,7 @@ impl FromStr for ElemKind {
}
}
-pub trait Element: Downcast {
+pub trait Element: Downcast + core::fmt::Debug {
/// Gets the element defined location i.e token without filename
fn location(&self) -> &Token;
@@ -43,9 +43,6 @@ pub trait Element: Downcast {
/// Get the element's name
fn element_name(&self) -> &'static str;
- /// Outputs element to string for debug purposes
- fn to_string(&self) -> String;
-
/// Gets the element as a referenceable i.e an element that can be referenced
fn as_referenceable(&self) -> Option<&dyn ReferenceableElement> { None }
@@ -57,12 +54,6 @@ pub trait Element: Downcast {
}
impl_downcast!(Element);
-impl core::fmt::Debug for dyn Element {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.to_string())
- }
-}
-
pub trait ReferenceableElement: Element {
/// Reference name
fn reference_name(&self) -> Option<&String>;
@@ -74,11 +65,6 @@ pub trait ReferenceableElement: Element {
fn compile_reference(&self, compiler: &Compiler, document: &dyn Document, reference: &Reference, refid: usize) -> Result;
}
-impl core::fmt::Debug for dyn ReferenceableElement {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.to_string())
- }
-}
pub trait ContainerElement: Element {
/// Gets the contained elements
@@ -88,12 +74,6 @@ pub trait ContainerElement: Element {
fn push(&mut self, elem: Box) -> Result<(), String>;
}
-impl core::fmt::Debug for dyn ContainerElement {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.to_string())
- }
-}
-
#[derive(Debug)]
pub struct DocumentEnd(pub Token);
@@ -104,8 +84,6 @@ impl Element for DocumentEnd {
fn element_name(&self) -> &'static str { "Document End" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, _compiler: &Compiler, _document: &dyn Document) -> Result {
Ok(String::new())
}
diff --git a/src/elements/code.rs b/src/elements/code.rs
index d1fcf83..447e610 100644
--- a/src/elements/code.rs
+++ b/src/elements/code.rs
@@ -263,8 +263,6 @@ impl Element for Code {
fn element_name(&self) -> &'static str { "Code Block" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
diff --git a/src/elements/comment.rs b/src/elements/comment.rs
index 1fbb65b..308ef10 100644
--- a/src/elements/comment.rs
+++ b/src/elements/comment.rs
@@ -35,7 +35,6 @@ impl Element for Comment {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Invisible }
fn element_name(&self) -> &'static str { "Comment" }
- fn to_string(&self) -> String { format!("{self:#?}") }
fn compile(&self, _compiler: &Compiler, _document: &dyn Document) -> Result {
Ok("".to_string())
}
diff --git a/src/elements/graphviz.rs b/src/elements/graphviz.rs
index b2d3586..1977117 100644
--- a/src/elements/graphviz.rs
+++ b/src/elements/graphviz.rs
@@ -113,8 +113,6 @@ impl Element for Graphviz {
fn element_name(&self) -> &'static str { "Graphviz" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
diff --git a/src/elements/layout.rs b/src/elements/layout.rs
index 7b3d310..cf5017a 100644
--- a/src/elements/layout.rs
+++ b/src/elements/layout.rs
@@ -237,7 +237,6 @@ impl Element for Layout {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Block }
fn element_name(&self) -> &'static str { "Layout" }
- fn to_string(&self) -> String { format!("{self:#?}") }
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
self.layout
.compile(self.token, self.id, &self.properties, compiler, document)
diff --git a/src/elements/link.rs b/src/elements/link.rs
index be927a5..109f044 100644
--- a/src/elements/link.rs
+++ b/src/elements/link.rs
@@ -23,18 +23,17 @@ use std::rc::Rc;
#[derive(Debug)]
pub struct Link {
- pub(self) location: Token,
+ pub location: Token,
/// Display content of link
- pub(self) display: Vec>,
+ pub display: Vec>,
/// Url of link
- pub(self) url: String,
+ pub url: String,
}
impl Element for Link {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Link" }
- fn to_string(&self) -> String { format!("{self:#?}") }
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
diff --git a/src/elements/list.rs b/src/elements/list.rs
index 894fa33..cb648f6 100644
--- a/src/elements/list.rs
+++ b/src/elements/list.rs
@@ -50,9 +50,7 @@ impl Element for ListMarker {
fn element_name(&self) -> &'static str { "List Marker" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
- fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
+ fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => match (self.kind, self.numbered) {
(MarkerKind::Close, true) => Ok("".to_string()),
@@ -80,8 +78,6 @@ impl Element for ListEntry {
fn element_name(&self) -> &'static str { "List Entry" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
@@ -184,12 +180,12 @@ impl ListRule {
let pm = self.properties.parse(processed.as_str())?;
let offset = match pm.get("offset", |_, s| s.parse::()) {
- Ok((prop, val)) => Some(val),
+ Ok((_, val)) => Some(val),
Err(err) => match err {
PropertyMapError::ParseError(err) => {
return Err(format!("Failed to parse `offset`: {err}"))
}
- PropertyMapError::NotFoundError(err) => None,
+ PropertyMapError::NotFoundError(_) => None,
},
};
@@ -471,7 +467,6 @@ mod tests {
None,
));
let parser = LangParser::default();
- let compiler = Compiler::new(Target::HTML, None);
let doc = parser.parse(source, None);
validate_document!(doc.content().borrow(), 0,
@@ -480,7 +475,7 @@ mod tests {
Text { content == "1" };
};
ListEntry { numbering == vec![(false, 7)] } {
- Text { /*content == "2 continued"*/ };
+ Text { content == "2 continued" };
};
ListEntry { numbering == vec![(false, 8)] } {
Text { content == "3" };
diff --git a/src/elements/media.rs b/src/elements/media.rs
index ee03396..105f0c8 100644
--- a/src/elements/media.rs
+++ b/src/elements/media.rs
@@ -72,8 +72,6 @@ impl Element for Media {
fn element_name(&self) -> &'static str { "Media" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn as_container(&self) -> Option<&dyn ContainerElement> { Some(self) }
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
@@ -137,8 +135,6 @@ impl Element for Medium {
fn element_name(&self) -> &'static str { "Medium" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn as_referenceable(&self) -> Option<&dyn ReferenceableElement> { Some(self) }
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
diff --git a/src/elements/paragraph.rs b/src/elements/paragraph.rs
index 583f7c0..c8d3313 100644
--- a/src/elements/paragraph.rs
+++ b/src/elements/paragraph.rs
@@ -50,8 +50,6 @@ impl Element for Paragraph {
fn element_name(&self) -> &'static str { "Paragraph" }
- fn to_string(&self) -> String { format!("{:#?}", self) }
-
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
if self.content.is_empty() {
return Ok(String::new());
@@ -59,34 +57,21 @@ impl Element for Paragraph {
match compiler.target() {
Target::HTML => {
+ if self.content.is_empty() {
+ return Ok(String::new());
+ }
+
let mut result = String::new();
- //if prev.is_none() || prev.unwrap().downcast_ref::().is_none()
- {
- result.push_str("");
- }
- //else
- //{ result.push_str(" "); }
+ result.push_str("
");
- let err = self.content.iter().try_for_each(|elem| {
- match elem.compile(compiler, document) {
- Err(e) => return Err(e),
- Ok(content) => {
- result.push_str(content.as_str());
- Ok(())
- }
- }
- });
- //if next.is_none() || next.unwrap().downcast_ref::().is_none()
- {
- result.push_str("
");
+ for elems in &self.content {
+ result += elems.compile(compiler, document)?.as_str();
}
- match err {
- Err(e) => Err(e),
- Ok(()) => Ok(result),
- }
+ result.push_str("
");
+ Ok(result)
}
- Target::LATEX => todo!("Unimplemented compiler"),
+ _ => todo!("Unimplemented compiler"),
}
}
diff --git a/src/elements/raw.rs b/src/elements/raw.rs
index 7a07903..b5cd6f3 100644
--- a/src/elements/raw.rs
+++ b/src/elements/raw.rs
@@ -39,8 +39,6 @@ impl Element for Raw {
fn element_name(&self) -> &'static str { "Raw" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, _compiler: &Compiler, _document: &dyn Document) -> Result {
Ok(self.content.clone())
}
diff --git a/src/elements/reference.rs b/src/elements/reference.rs
index e5cf754..d0288b5 100644
--- a/src/elements/reference.rs
+++ b/src/elements/reference.rs
@@ -46,8 +46,6 @@ impl Element for Reference {
fn element_name(&self) -> &'static str { "Reference" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
diff --git a/src/elements/script.rs b/src/elements/script.rs
index dab19da..325adc4 100644
--- a/src/elements/script.rs
+++ b/src/elements/script.rs
@@ -277,3 +277,61 @@ impl RegexRule for ScriptRule {
// TODO
fn lua_bindings<'lua>(&self, _lua: &'lua Lua) -> Option)>> { None }
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::elements::link::Link;
+use crate::elements::list::ListEntry;
+ use crate::elements::list::ListMarker;
+ use crate::elements::paragraph::Paragraph;
+ use crate::elements::style::Style;
+ use crate::parser::langparser::LangParser;
+ use crate::parser::source::SourceFile;
+ use crate::validate_document;
+
+ #[test]
+ fn parser() {
+ let source = Rc::new(SourceFile::with_content(
+ "".to_string(),
+ r#"
+Simple evals:
+ * %< 1+1>%
+ * %<" 1+1>% = 2
+ * %%
+
+Definition:
+@<
+function make_ref(name, ref)
+ return "[" .. name .. "](#" .. ref .. ")"
+end
+>@
+Evaluation: %%
+ "#
+ .to_string(),
+ None,
+ ));
+ let parser = LangParser::default();
+ let doc = parser.parse(source, None);
+
+ validate_document!(doc.content().borrow(), 0,
+ Paragraph;
+ ListMarker;
+ ListEntry {};
+ ListEntry {
+ Text { content == "2" };
+ Text { content == " = 2" };
+ };
+ ListEntry {
+ Style;
+ Text { content == "bold" };
+ Style;
+ };
+ ListMarker;
+ Paragraph {
+ Text; Text;
+ Link { url == "#id" } { Text { content == "hello" }; };
+ };
+ );
+ }
+}
diff --git a/src/elements/section.rs b/src/elements/section.rs
index 4625861..60e97a9 100644
--- a/src/elements/section.rs
+++ b/src/elements/section.rs
@@ -34,7 +34,6 @@ impl Element for Section {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Block }
fn element_name(&self) -> &'static str { "Section" }
- fn to_string(&self) -> String { format!("{self:#?}") }
fn as_referenceable(&self) -> Option<&dyn ReferenceableElement> { Some(self) }
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result {
match compiler.target() {
@@ -140,10 +139,11 @@ impl RegexRule for SectionRule {
let section_refname = matches.get(2).map_or_else(
|| None,
|refname| {
- /* TODO: Wait for reference rework
// Check for duplicate reference
- if let Some((ref_doc, reference)) = document.get_reference(refname.as_str())
+ if let Some(elem_reference) = document.get_reference(refname.as_str())
{
+ let elem = document.get_from_reference(&elem_reference).unwrap();
+
result.push(
Report::build(ReportKind::Warning, token.source(), refname.start())
.with_message("Duplicate reference name")
@@ -151,20 +151,19 @@ impl RegexRule for SectionRule {
Label::new((token.source(), refname.range()))
.with_message(format!("Reference with name `{}` is already defined in `{}`",
refname.as_str().fg(parser.colors().highlight),
- ref_doc.source().name().as_str().fg(parser.colors().highlight)))
+ elem.location().source().name().as_str().fg(parser.colors().highlight)))
.with_message(format!("`{}` conflicts with previously defined reference to {}",
refname.as_str().fg(parser.colors().highlight),
- reference.element_name().fg(parser.colors().highlight)))
+ elem.element_name().fg(parser.colors().highlight)))
.with_color(parser.colors().warning))
.with_label(
- Label::new((ref_doc.source(), reference.location().start()+1..reference.location().end() ))
+ Label::new((elem.location().source(), elem.location().start()..elem.location().end() ))
.with_message(format!("`{}` previously defined here",
refname.as_str().fg(parser.colors().highlight)))
.with_color(parser.colors().warning))
.with_note(format!("Previous reference was overwritten"))
.finish());
}
- */
Some(refname.as_str().to_string())
},
);
diff --git a/src/elements/style.rs b/src/elements/style.rs
index b841788..c398420 100644
--- a/src/elements/style.rs
+++ b/src/elements/style.rs
@@ -46,7 +46,6 @@ impl Element for Style {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Section" }
- fn to_string(&self) -> String { format!("{self:#?}") }
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
diff --git a/src/elements/tex.rs b/src/elements/tex.rs
index d2deea1..b84385d 100644
--- a/src/elements/tex.rs
+++ b/src/elements/tex.rs
@@ -151,8 +151,6 @@ impl Element for Tex {
fn element_name(&self) -> &'static str { "LaTeX" }
- fn to_string(&self) -> String { format!("{self:#?}") }
-
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result {
match compiler.target() {
Target::HTML => {
diff --git a/src/elements/text.rs b/src/elements/text.rs
index 8843bc4..0c768e8 100644
--- a/src/elements/text.rs
+++ b/src/elements/text.rs
@@ -36,7 +36,6 @@ impl Element for Text {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Text" }
- fn to_string(&self) -> String { format!("{self:#?}") }
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result {
Ok(Compiler::sanitize(compiler.target(), self.content.as_str()))
diff --git a/src/main.rs b/src/main.rs
index ff5fb64..d820a0d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -60,7 +60,7 @@ fn parse(input: &str, debug_opts: &Vec) -> Result