use crate::phighlighter::PLocation;
use crate::pcontent::{
PStrBackend,
plabeler::PLabelId,
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
pveccontent::PVecContent,
preferenceurl::PReferenceUrl,
};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentTitle{
p_title: PVecContent,
p_level: usize,
p_is_enumerated: bool,
p_vec_title_number: Vec<usize>,
p_output_filename: String,
p_previous_filename: String,
p_next_filename: String,
p_location: PLocation,
}
impl PContentTitle {
pub fn new(level: usize, is_enumerated: bool, location: &PLocation) -> Self {
PContentTitle{
p_title: Default::default(),
p_level: level,
p_is_enumerated: is_enumerated,
p_vec_title_number: vec![],
p_output_filename: String::from(""),
p_previous_filename: String::from(""),
p_next_filename: String::from(""),
p_location: location.clone(),
}
}
pub fn set_title_number(&mut self, vec_title_number: &Vec<usize>){
self.p_vec_title_number = vec_title_number.clone();
}
pub fn make_filename(&mut self){
let mut str_title_number = String::from("");
let mut comma = String::from("");
for number in self.p_vec_title_number.iter(){
str_title_number += &format!("{}{}", comma, number);
comma = String::from("-");
}
if str_title_number.is_empty() {
self.p_output_filename = String::from("index.html");
}else{
self.p_output_filename = String::from(format!("web_{}.html", str_title_number));
}
}
pub fn set_previous_filename(&mut self, previous_filename: &String){
self.p_previous_filename = previous_filename.clone();
}
pub fn set_next_filename(&mut self, next_filename: &String){
self.p_next_filename = next_filename.clone();
}
pub fn get_title(&self) -> &PVecContent{
&self.p_title
}
pub fn get_level(&self) -> usize{
self.p_level
}
pub fn get_title_mut(&mut self) -> &mut PVecContent{
&mut self.p_title
}
pub fn is_enumerated(&self) -> bool{
self.p_is_enumerated
}
pub fn get_output_filename(&self) -> &String{
&self.p_output_filename
}
pub fn get_str_title_number(&self) -> String{
let mut str_title_number = String::from("");
if self.p_is_enumerated {
for number in self.p_vec_title_number.iter(){
str_title_number += &format!("{}.", number);
}
}
return str_title_number;
}
}
impl PAbstractContent for PContentTitle{
fn has_embeded_label(&self) -> bool{
true
}
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
if self.p_is_enumerated {
PReferenceUrl::from_text(current_file, id, &String::from(self.get_str_title_number().trim_end_matches(".")))
}else{
PReferenceUrl::from_content(current_file, id, self.get_title())
}
}
fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
where TLectureBackend: PAbstractLectureBackend
{
let mut str_title_number = self.get_str_title_number();
if !str_title_number.is_empty() {
str_title_number += &String::from(" ");
}
let mut backend_str = PStrBackend::new();
self.p_title.to_html(&mut backend_str, id);
if self.p_is_enumerated {
backend.create_file(&self.p_output_filename, backend_str.get_body(), &self.p_location, &self.p_previous_filename, &self.p_next_filename);
}
backend.write(&format!("<h{} id=\"{}\">{}", self.p_level, id.get_id(), str_title_number));
self.p_title.to_html(backend, id);
id.to_html(backend, false);
backend.write(&format!("</h{}>\n", self.p_level));
}
}
#[cfg(test)]
mod tests{
use super::*;
use std::path::PathBuf;
use crate::pcontent::{
pstrbackend::PStrBackend,
PContentType,
plabeler::PLabeler,
pcontenttext::PContentText
};
#[test]
fn test_content_title_part(){
let title: PContentTitle = PContentTitle::new(1, false, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
let mut backend = PStrBackend::new();
title.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<h1 id=\"42\"></h1>\n"));
}
#[test]
fn test_content_title_part_number(){
let mut title: PContentTitle = PContentTitle::new(1, true, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
title.set_title_number(&vec![1]);
title.get_title_mut().add_child(&PContentType::Text(PLabeler::new(42, &PContentText::new(&String::from("Some interesting title")))));
let mut backend = PStrBackend::new();
title.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<h1 id=\"42\">1. Some interesting title</h1>\n"));
}
#[test]
fn test_content_title_chapter(){
let title: PContentTitle = PContentTitle::new(2, false, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
let mut backend = PStrBackend::new();
title.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<h2 id=\"42\"></h2>\n"));
}
#[test]
fn test_content_title_chapter_number(){
let mut title: PContentTitle = PContentTitle::new(2, true, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
title.set_title_number(&vec![1, 2]);
let mut backend = PStrBackend::new();
title.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<h2 id=\"42\">1.2. </h2>\n"));
}
}