rustyphoenixlecture 1.7.1

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::pcontent::{
	pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
	plabeler::PLabelId,
	preferenceurl::PReferenceUrl,
};

///Type of a media
#[derive(Debug,Clone, PartialEq)]
pub enum PMediaType{
	Image,
	Video
}

///Media Content object of the lecture (image, video)
#[derive(Debug, Clone, PartialEq)]
pub struct PContentMedia{
	///Text content
	filename: PathBuf,
	///Type of the media
	p_type: PMediaType,
	///Say if the current media is a figure or not
	p_is_figure: bool,
	///Width of the video in the page
	width: usize,
	//TODO : at some point we could add resolution, quality, etc
}

impl PContentMedia {
	///Constructor of a PContentMedia
	/// # Parameters
	/// - `filename` : file name to the media
	/// - `width` : width of the video in the page
	/// - `media_type` : type of the media (Image, Video)
	/// - `is_figure` : true if the current PContentMedia is a figure
	/// # Returns
	/// Initialised PContentMedia
	pub fn new(filename: &PathBuf, width: usize, media_type: &PMediaType, is_figure: bool) -> Self{
		PContentMedia {
			filename: filename.clone(),
			width: width,
			p_type: media_type.clone(),
			p_is_figure: is_figure,
		}
	}
	///Say if the media is a figure or not
	/// # Returns
	/// True if the media is a figure, false if not
	pub fn is_figure(&self) -> bool{
		self.p_is_figure
	}
}

impl PAbstractContent for PContentMedia{
	///Say if the PContent has an embeded label
	/// # Returns
	/// True if the PContent has an embeded label, false otherwise
	fn has_embeded_label(&self) -> bool{
		self.p_is_figure
	}
	///Get the reference url of the current PContent
	/// # Parameters
	/// - `current_file` : current output file of the PContent
	/// - `id` : id of the PContent
	/// # Returns
	/// Corresponding PReferenceUrl
	fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
		match self.p_type {
			PMediaType::Image => {
				if self.p_is_figure {
					PReferenceUrl::from_text(current_file, id, &String::from(&format!("Figure {}", id)))
				}else{
					PReferenceUrl::from_text(current_file, id, &String::from(&format!("Image {}", id)))
				}
			},
			PMediaType::Video => PReferenceUrl::from_text(current_file, id, &String::from(&format!("Video {}", id)))
		}
	}
	///Convert the current struct into html
	/// # Parameters
	/// - `backend` : backend which write a lecture in files
	/// - `id` : id of the PContent
	fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
		where TLectureBackend: PAbstractLectureBackend
	{
		if self.p_is_figure {
			backend.write(&format!("<div id=\"{}\" class=\"figureStyle\">", id.get_id()));
		}
		match self.p_type {
			PMediaType::Image => backend.write(&format!("<img id=\"{}\" src=\"{}\" alt=\"nothing\" />", id.get_id(), self.filename.to_str().unwrap())),
			PMediaType::Video => backend.write(&format!("<video width=\"{}\" controls>\n\t<!-- Be careful, if you do not have sound, check if the video has MP3 sound and not AC3 -->\n\t<source src=\"{}\" type=\"video/mp4\">\n</video>\n", self.width, self.filename.to_str().unwrap()))
		};
		if self.p_is_figure {
			backend.write(&String::from("<p>"));
		}
		id.to_html(backend, false);
		if self.p_is_figure {
			backend.write(&format!("<b>Figure {}</b></p></div>\n", id.get_id()));
		}
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	use crate::pcontent::pstrbackend::PStrBackend;
	
	///Test the PContentMedia for Image
	#[test]
	fn test_content_media_image(){
		let image: PContentMedia = PContentMedia::new(&PathBuf::from("tweety.png"), 200, &PMediaType::Image, false);
		let mut backend = PStrBackend::new();
		image.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<img id=\"42\" src=\"tweety.png\" alt=\"nothing\" />"));
	}
	///Test the PContentMedia for Image
	#[test]
	fn test_content_media_video(){
		let video: PContentMedia = PContentMedia::new(&PathBuf::from("tweety.mp4"), 200, &PMediaType::Video, true);
		let mut backend = PStrBackend::new();
		video.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<div id=\"42\" class=\"figureStyle\"><video width=\"200\" controls>\n\t<!-- Be careful, if you do not have sound, check if the video has MP3 sound and not AC3 -->\n\t<source src=\"tweety.mp4\" type=\"video/mp4\">\n</video>\n<p><b>Figure 42</b></p></div>\n"));
	}
}