Fix links

This commit is contained in:
ef3d0c3e 2024-08-02 10:34:56 +02:00
parent eaadfca9b9
commit e43cf4a8f3

View file

@ -21,13 +21,11 @@ use regex::Regex;
use std::ops::Range; use std::ops::Range;
use std::rc::Rc; use std::rc::Rc;
use super::paragraph::Paragraph;
#[derive(Debug)] #[derive(Debug)]
pub struct Link { pub struct Link {
pub(self) location: Token, pub(self) location: Token,
/// Display content of link /// Display content of link
pub(self) display: Paragraph, pub(self) display: Vec<Box<dyn Element>>,
/// Url of link /// Url of link
pub(self) url: String, pub(self) url: String,
} }
@ -45,11 +43,9 @@ impl Element for Link {
Compiler::sanitize(compiler.target(), self.url.as_str()) Compiler::sanitize(compiler.target(), self.url.as_str())
); );
result += self for elem in &self.display {
.display result += elem.compile(compiler, document)?.as_str();
.compile(compiler, document) }
.as_ref()
.map(|r| r.as_str())?;
result += "</a>"; result += "</a>";
Ok(result) Ok(result)
@ -62,13 +58,14 @@ impl Element for Link {
} }
impl ContainerElement for Link { impl ContainerElement for Link {
fn contained(&self) -> &Vec<Box<dyn Element>> { &self.display.content } fn contained(&self) -> &Vec<Box<dyn Element>> { &self.display }
fn push(&mut self, elem: Box<dyn Element>) -> Result<(), String> { fn push(&mut self, elem: Box<dyn Element>) -> Result<(), String> {
if elem.downcast_ref::<Link>().is_some() { if elem.downcast_ref::<Link>().is_some() {
return Err("Tried to push a link inside of a link".to_string()); return Err("Tried to push a link inside of a link".to_string());
} }
self.display.push(elem) self.display.push(elem);
Ok(())
} }
} }
@ -152,7 +149,7 @@ impl RegexRule for LinkRule {
); );
return reports; return reports;
} }
Ok(paragraph) => *paragraph, Ok(mut paragraph) => std::mem::replace(&mut paragraph.content, vec![]),
} }
} }
_ => panic!("Empty link name"), _ => panic!("Empty link name"),
@ -215,6 +212,7 @@ impl RegexRule for LinkRule {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::elements::paragraph::Paragraph;
use crate::elements::style::Style; use crate::elements::style::Style;
use crate::elements::text::Text; use crate::elements::text::Text;
use crate::parser::langparser::LangParser; use crate::parser::langparser::LangParser;