rustyphoenixlecture 1.4.0

This project aims to provide a simple a powerfull lecture compilation to generate html web sites
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

use std::path::PathBuf;

use crate::plectureparser::lecture_book_theme::{
	lecture_book_theme_header,
	lecture_book_theme_body_begin,
	lecture_book_theme_body_top_menu,
	lecture_book_theme_body_end
};
use crate::phighlighter::PLocation;
use crate::pcontent::{
	PAbstractLectureBackend,
	PVecContent,
};
use crate::plectureparser::{
	plecturebuffer::PLectureBuffer,
	plecturemenu::PLectureMenu,
	pformulamanager::PFormulaManager
};

///Backend of the lecture to write html files
pub struct PLecureBackend{
	///Output directory of the full website
	p_output_directory: PathBuf,
	///Url of the current project
	p_project_url: String,
	///Email of the author
	p_author_email: String,
	///Name of the current branch
	p_current_branch: String,
	///Master project url this lecture is a part of
	p_master_project_url: String,
	///Previous pave to visit
	p_prev_page: String,
	///Next pave to visit
	p_next_page: String,
	///Current file to be written
	p_current_filename: String,
	///Buffer to be used to write html files
	p_buffer: PLectureBuffer,
	///Menu, table of content of the generated website
	p_menu: PLectureMenu,
	///Manager of the generated formulae of the website
	p_formula_manager: PFormulaManager,
}

impl Drop for PLecureBackend {
	///Destructor of the PLecureBackend
	fn drop(&mut self) {
		//Let's close properly the very last file
		self.close();
	}
}

impl PLecureBackend {
	///Constructor of the PLectureBackend
	/// # Parameters
	/// - `absolute_output_directory` : absolute output directory of the full website
	/// - `project_url` : url of the current project
	/// - `author_email` : email of the author
	/// - `current_branch` : name of the current branch
	/// - `master_project_url` : master project the current lecture is related to
	/// - `vec_content` : vector of content of the whole website
	/// # Returns
	/// Initialised PLectureBackend
	pub fn new(absolute_output_directory: &PathBuf, project_url: &String, author_email: &String, current_branch: &String, master_project_url: &String, vec_content: &PVecContent) -> Self{
		PLecureBackend {
			p_output_directory: absolute_output_directory.clone(),
			p_project_url: project_url.clone(),
			p_author_email: author_email.clone(),
			p_current_branch: current_branch.clone(),
			p_master_project_url: master_project_url.clone(),
			p_prev_page: String::from("index.html"),
			p_next_page: String::from("index.html"),
			p_current_filename: String::from("index.html"),
			p_buffer: Default::default(),
			p_menu: PLectureMenu::from(vec_content),
			p_formula_manager: PFormulaManager::new(absolute_output_directory)
		}
	}
}

impl PAbstractLectureBackend for PLecureBackend {
	///Create a new file
	/// # Parameters
	/// - `output_filename` : name of the file to write
	/// - `title` : title of the section
	/// - `location` : location of the current source file where this section was defined
	/// - `prev_page` : previous page to visit
	/// - `next_page` : next page to visit
	fn create_file(&mut self, output_filename: &String, title: &String,
		location: &PLocation,
		prev_page: &String, next_page: &String)
	{
		let base_filename = PathBuf::from(output_filename);
		let absolute_output_filename: PathBuf = if base_filename.is_absolute() {
			base_filename
		}else{
			self.p_output_directory.join(base_filename)
		};
		//Let's close the previous file is there was one
		self.close();
		self.p_buffer = PLectureBuffer::new(&absolute_output_filename);
		self.write(&lecture_book_theme_header(title));
		self.write(&lecture_book_theme_body_begin());
		self.p_menu.to_html(&mut self.p_buffer, output_filename);
		
		self.write(&lecture_book_theme_body_top_menu(&self.p_project_url, &self.p_author_email, &self.p_current_branch, &String::from(location.get_filename().to_str().unwrap()), location.get_current_line(), &self.p_master_project_url));
		
		self.p_current_filename = output_filename.clone();
		self.p_prev_page = prev_page.clone();
		self.p_next_page = next_page.clone();
	}
	///Write text in the current file
	/// # Parameters
	/// - `text` : text to write in the current file
	fn write(&mut self, text: &String){
		self.p_buffer.write(text);
	}
	///Write a formula in html
	/// # Parameters
	/// - `formula` : formula to write in html
	/// # Errors
	/// This function will panic if there is a problem during the formula generation
	fn write_formula(&mut self, formula: &String){
		let html: String = self.p_formula_manager.formula_to_html(formula);
		self.write(&html);
	}
	///Close the current file
	fn close(&mut self){
		//Let's finish to write the current file before closing it
		if self.p_buffer.is_writable() {	//Just to avoid the multiple close call when drop
			self.p_buffer.write(&lecture_book_theme_body_end(&self.p_prev_page, &self.p_next_page));
		}
		//We finish to close the buffer, so we cannot use is any more and wait for the next one
		self.p_buffer.close();
	}
}