use std::path::PathBuf;
use crate::plectureparser::pabstractcontent::{PAbstractContent, PAbstractLectureBackend};
use crate::plectureparser::plabeler::PLabelId;
use crate::plectureparser::{
PReferenceUrl,
};
#[derive(Debug,Clone, PartialEq)]
pub enum PMediaType{
Image,
Video
}
#[derive(Debug, Clone, PartialEq)]
pub struct PContentMedia{
filename: PathBuf,
p_type: PMediaType,
p_is_figure: bool,
width: usize,
}
impl 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,
}
}
pub fn is_figure(&self) -> bool{
self.p_is_figure
}
}
impl PAbstractContent for PContentMedia{
fn has_embeded_label(&self) -> bool{
self.p_is_figure
}
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)))
}
}
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::plectureparser::pstrbackend::PStrBackend;
#[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]
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"));
}
}