use crate::shapes::action::Hyperlink;
use crate::text::font::Font;
use crate::xml_util::xml_escape;
#[derive(Debug, Clone, PartialEq)]
pub struct Run {
text: String,
font: Font,
pub hyperlink: Option<Hyperlink>,
pub is_line_break: bool,
}
impl Default for Run {
fn default() -> Self {
Self {
text: String::new(),
font: Font::new(),
hyperlink: None,
is_line_break: false,
}
}
}
impl Run {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
pub fn set_text(&mut self, text: &str) {
self.text = text.to_string();
}
#[must_use]
pub const fn font(&self) -> &Font {
&self.font
}
pub fn font_mut(&mut self) -> &mut Font {
&mut self.font
}
pub fn set_language(&mut self, lang: &str) {
self.font.language_id = Some(lang.to_string());
}
pub fn set_hyperlink(&mut self, hyperlink: Hyperlink) {
self.hyperlink = Some(hyperlink);
}
#[must_use]
pub fn to_xml_string(&self) -> String {
if self.is_line_break {
let mut xml = String::from("<a:br>");
xml.push_str(&self.font.to_xml_string());
xml.push_str("</a:br>");
return xml;
}
let mut xml = String::from("<a:r>");
if let Some(ref hlink) = self.hyperlink {
let font_xml = self.font.to_xml_string();
if font_xml.ends_with("/>") {
let prefix = &font_xml[..font_xml.len() - 2];
xml.push_str(prefix);
xml.push('>');
xml.push_str(&hyperlink_to_xml(hlink));
xml.push_str("</a:rPr>");
} else {
let close_tag = "</a:rPr>";
if let Some(pos) = font_xml.rfind(close_tag) {
xml.push_str(&font_xml[..pos]);
xml.push_str(&hyperlink_to_xml(hlink));
xml.push_str(close_tag);
} else {
xml.push_str(&font_xml);
}
}
} else {
xml.push_str(&self.font.to_xml_string());
}
xml.push_str("<a:t>");
xml.push_str(&xml_escape(&self.text));
xml.push_str("</a:t>");
xml.push_str("</a:r>");
xml
}
}
fn hyperlink_to_xml(hlink: &Hyperlink) -> String {
let mut xml = String::from("<a:hlinkClick");
if let Some(ref rid) = hlink.r_id {
xml.push_str(&format!(r#" r:id="{}""#, xml_escape(rid.as_str())));
}
if let Some(ref tooltip) = hlink.tooltip {
xml.push_str(&format!(r#" tooltip="{}""#, xml_escape(tooltip)));
}
xml.push_str("/>");
xml
}
#[cfg(test)]
#[path = "run_tests.rs"]
mod tests;