use crate::nodes::*;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct CodeNode {
lang: String,
inline: bool,
highlight: bool,
code: String,
file: Option<String>,
high_line: Vec<usize>,
}
impl Default for CodeNode {
fn default() -> Self {
Self { lang: String::from("text"), code: String::new(), inline: false, highlight: false, file: None, high_line: vec![] }
}
}
impl Display for CodeNode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.inline {
write!(f, "{mark}{lang}\n{body}\n{mark}", mark = "`", lang = "", body = self.code)
}
else {
write!(f, "{mark}{lang}\n{body}\n{mark}", mark = "`".repeat(3), lang = self.lang, body = self.code)
}
}
}
impl CodeNode {
#[inline]
pub fn into_node(self, range: Option<(u32, u32)>) -> ASTNode {
ASTNode { value: ASTKind::CodeNode(Box::new(self)), range }
}
#[inline]
pub fn set_language(mut self, lang: String) -> Self {
self.lang = lang;
return self;
}
#[inline]
pub fn set_highlight_line(mut self, lines: Vec<usize>) -> Self {
self.high_line = lines;
return self;
}
#[inline]
pub fn set_file_name(mut self, name: String) -> Self {
self.file = Some(name);
return self;
}
}
impl CodeNode {
#[inline]
pub fn code_inline(code: String) -> Self {
Self { lang: String::from("text"), inline: true, highlight: false, code, file: None, high_line: vec![] }
}
#[inline]
pub fn code_block(lang: String, code: String) -> Self {
Self { lang, inline: false, highlight: true, code, file: None, high_line: vec![] }
}
}