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

///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentClipboardText{
	///Text to be displayed
	p_display_text: String,
	///Text to be copied
	p_copy_text: String,
	///Style of the copy button
	p_style: String,
	///Text on the copy button
	p_button_text: String,
}

impl PContentClipboardText {
	///Constructor of the PContentClipboardText
	/// # Parameters
	/// - `display_text` : text to be displayed
	/// - `copy_text` : text to be copied
	/// - `style` : style of the copy button
	/// - `button_text` : text on the copy button
	/// # Returns
	/// Initialised PContentClipboardText
	pub fn new(display_text: &String, copy_text: &String, style: &String, button_text: &String) -> Self{
		PContentClipboardText {
			p_display_text: display_text.clone(),
			p_copy_text: copy_text.clone(),
			p_style: style.clone(),
			p_button_text: button_text.clone(),
		}
	}
}

impl PAbstractContent for PContentClipboardText{
	///Say if the PContentClipboardText has an embeded label
	/// # Returns
	/// True if the PContentClipboardText has an embeded label, false otherwise
	fn has_embeded_label(&self) -> bool{
		false
	}
	///Get the reference url of the current PContentClipboardText
	/// # Parameters
	/// - `current_file` : current output file of the PContentClipboardText
	/// - `id` : id of the PContentClipboardText
	/// # Returns
	/// Corresponding PReferenceUrl
	fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
		PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentClipboardText {}", id)))
	}
	///Convert the current struct into html
	/// # Parameters
	/// - `backend` : backend which write a lecture in files
	/// - `id` : id of the PContentClipboardText
	fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
		where TLectureBackend: PAbstractLectureBackend
	{
		backend.write(&String::from(format!("<span id=\"{}\">", id.get_id())));
		backend.write(&self.p_display_text);
		if !self.p_display_text.is_empty(){
			backend.write(&String::from(" "));
		}
		backend.write(&String::from("<button type=\"button\" "));
		if !self.p_style.is_empty() {
			backend.write(&String::from(format!("class=\"{}\" ", self.p_style)));
		}
		backend.write(&String::from(format!("onclick=\"phoenix_writeClipboardText('{}')\">{}</button>", self.p_copy_text.replace("'", "\\'"), self.p_button_text)));
		backend.write(&String::from("</span>"));
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	use crate::pcontent::pstrbackend::PStrBackend;
	
	///Test the PContentClipboardText
	#[test]
	fn test_content_clipboard_text(){
		let text: PContentClipboardText = PContentClipboardText::new(&String::from("Display text"), &String::from("Copy text"), &String::from("Style"), &String::from("Button Text"));
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<span id=\"42\">Display text <button type=\"button\" class=\"Style\" onclick=\"phoenix_writeClipboardText('Copy text')\">Button Text</button></span>"));
	}
	///Test the PContentClipboardText
	#[test]
	fn test_content_clipboard_text_no_style(){
		let text: PContentClipboardText = PContentClipboardText::new(&String::from(""), &String::from("Copy text"), &String::from(""), &String::from("Button Text"));
		let mut backend = PStrBackend::new();
		text.to_html(&mut backend, &PLabelId::new(42));
		assert_eq!(backend.get_body(), &String::from("<span id=\"42\"><button type=\"button\" onclick=\"phoenix_writeClipboardText('Copy text')\">Button Text</button></span>"));
	}
}