use crate::phighlighter::PLocation;
use crate::plectureparser::{
PReferenceUrl,
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
plabeler::PLabelId,
};
#[derive(Debug, Clone, PartialEq)]
pub struct PContentFormula{
p_formula: String,
p_is_inlined: bool,
p_location: PLocation,
}
impl PContentFormula {
pub fn new(formula: &String, is_inlined: bool, location: &PLocation) -> Self{
PContentFormula {
p_formula: formula.clone(),
p_is_inlined: is_inlined,
p_location: location.clone(),
}
}
pub fn is_inlined(&self) -> bool{
self.p_is_inlined
}
}
impl PAbstractContent for PContentFormula{
fn has_embeded_label(&self) -> bool{
true
}
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
PReferenceUrl::from_text(current_file, id, &String::from(format!("Formula {}", id)))
}
fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
where TLectureBackend: PAbstractLectureBackend
{
if !self.p_is_inlined {
backend.write(&String::from(&format!("<table id=\"{}\" class=\"formula\"><tr><td>\n", id.get_id())));
}else{
backend.write(&String::from(&format!("<span id=\"{}\" class=\"inlineformula\">", id.get_id())));
}
backend.write_formula(&self.p_formula);
if !id.get_label().is_empty() {
if self.p_is_inlined{
backend.write(&String::from(" "));
}else{
backend.write(&String::from("</td><td>"));
}
}
id.to_html(backend, true);
if !self.p_is_inlined {
backend.write(&String::from("\n</td></tr>\n</table>\n"));
}else{
backend.write(&String::from("</span>"));
}
}
}
#[cfg(test)]
mod tests{
use super::*;
use std::path::PathBuf;
use crate::plectureparser::pstrbackend::PStrBackend;
#[test]
fn test_content_formula_inline(){
let formula: PContentFormula = PContentFormula::new(&String::from("E = m c^2"), true, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
let mut backend = PStrBackend::new();
formula.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<span id=\"42\" class=\"inlineformula\">E = m c^2</span>"));
}
#[test]
fn test_content_formula(){
let formula: PContentFormula = PContentFormula::new(&String::from("E = m c^2"), false, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
let mut backend = PStrBackend::new();
formula.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<table id=\"42\" class=\"formula\"><tr><td>\nE = m c^2\n</td></tr>\n</table>\n"));
}
}