Fixes & Add tests
This commit is contained in:
parent
e43cf4a8f3
commit
d91279c1f2
20 changed files with 87 additions and 90 deletions
|
@ -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("</div>");
|
||||
|
|
|
@ -99,6 +99,7 @@ pub fn create_navigation(docs: &Vec<CompiledDocument>) -> Result<NavEntry, Strin
|
|||
}
|
||||
};
|
||||
|
||||
// Get entry to insert into
|
||||
let pent = if let Some(subcat) = subcat {
|
||||
let cat = match cat {
|
||||
Some(cat) => cat,
|
||||
|
@ -111,7 +112,7 @@ pub fn create_navigation(docs: &Vec<CompiledDocument>) -> Result<NavEntry, Strin
|
|||
}
|
||||
};
|
||||
|
||||
let mut cat_ent = match nav.children.get_mut(cat.as_str()) {
|
||||
let cat_ent = match nav.children.get_mut(cat.as_str()) {
|
||||
Some(cat_ent) => cat_ent,
|
||||
None => {
|
||||
// Insert
|
||||
|
|
|
@ -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(),
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
@ -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<String, String>;
|
||||
}
|
||||
|
||||
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<dyn Element>) -> 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<String, String> {
|
||||
Ok(String::new())
|
||||
}
|
||||
|
|
|
@ -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<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => {
|
||||
|
|
|
@ -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<String, String> {
|
||||
Ok("".to_string())
|
||||
}
|
||||
|
|
|
@ -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<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => {
|
||||
|
|
|
@ -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<String, String> {
|
||||
self.layout
|
||||
.compile(self.token, self.id, &self.properties, compiler, document)
|
||||
|
|
|
@ -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<Box<dyn Element>>,
|
||||
pub display: Vec<Box<dyn Element>>,
|
||||
/// 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<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => {
|
||||
|
|
|
@ -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<String, String> {
|
||||
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => match (self.kind, self.numbered) {
|
||||
(MarkerKind::Close, true) => Ok("</ol>".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<String, String> {
|
||||
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::<usize>()) {
|
||||
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" };
|
||||
|
|
|
@ -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<String, String> {
|
||||
|
@ -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<String, String> {
|
||||
|
|
|
@ -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<String, String> {
|
||||
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::<Paragraph>().is_none()
|
||||
{
|
||||
result.push_str("<p>");
|
||||
}
|
||||
//else
|
||||
//{ result.push_str(" "); }
|
||||
result.push_str("<p>");
|
||||
|
||||
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::<Paragraph>().is_none()
|
||||
{
|
||||
result.push_str("</p>");
|
||||
for elems in &self.content {
|
||||
result += elems.compile(compiler, document)?.as_str();
|
||||
}
|
||||
|
||||
match err {
|
||||
Err(e) => Err(e),
|
||||
Ok(()) => Ok(result),
|
||||
}
|
||||
result.push_str("</p>");
|
||||
Ok(result)
|
||||
}
|
||||
Target::LATEX => todo!("Unimplemented compiler"),
|
||||
_ => todo!("Unimplemented compiler"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String, String> {
|
||||
Ok(self.content.clone())
|
||||
}
|
||||
|
|
|
@ -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<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => {
|
||||
|
|
|
@ -277,3 +277,61 @@ impl RegexRule for ScriptRule {
|
|||
// TODO
|
||||
fn lua_bindings<'lua>(&self, _lua: &'lua Lua) -> Option<Vec<(String, Function<'lua>)>> { 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
|
||||
* %<! "**bold**">%
|
||||
|
||||
Definition:
|
||||
@<
|
||||
function make_ref(name, ref)
|
||||
return "[" .. name .. "](#" .. ref .. ")"
|
||||
end
|
||||
>@
|
||||
Evaluation: %<! make_ref("hello", "id")>%
|
||||
"#
|
||||
.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" }; };
|
||||
};
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, String> {
|
||||
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())
|
||||
},
|
||||
);
|
||||
|
|
|
@ -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<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => {
|
||||
|
|
|
@ -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<String, String> {
|
||||
match compiler.target() {
|
||||
Target::HTML => {
|
||||
|
|
|
@ -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<String, String> {
|
||||
Ok(Compiler::sanitize(compiler.target(), self.content.as_str()))
|
||||
|
|
|
@ -60,7 +60,7 @@ fn parse(input: &str, debug_opts: &Vec<String>) -> Result<Box<dyn Document<'stat
|
|||
doc.content()
|
||||
.borrow()
|
||||
.iter()
|
||||
.for_each(|elem| println!("{}", (elem).to_string()));
|
||||
.for_each(|elem| println!("{elem:#?}"));
|
||||
println!("-- END AST DEBUGGING --");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue