Skip to content

Commit

Permalink
accordion: improve terminology (#42)
Browse files Browse the repository at this point in the history
* Accordion improve terminology

* Accordion nodes should have the same serialized type as in Yamd

* Rename highlight header - title
  • Loading branch information
Lurk authored Nov 4, 2023
1 parent 03747db commit 93156ee
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@
//!
//! ### Highlight
//!
//! Element that starts with ">>>\n", followed by optional header that starts with ">> " and ends with a new line,
//! Element that starts with ">>>\n", followed by optional title that starts with ">> " and ends with a new line,
//! followed by optional icon specifier that starts with "> " and ends with a new line, followed by body that can
//! contain any number of paragraph elements
//!
//! Example:
//! ```text
//! >>>
//! >> Header
//! >> Title
//! > icon
//! body
//!
Expand Down Expand Up @@ -175,11 +175,11 @@
//! ```text
//! ///
//! //
//! / header
//! / Title
//! some random text
//! \\
//! //
//! / header
//! / Title
//! some random text
//! \\
//! \\\
Expand Down
54 changes: 27 additions & 27 deletions src/nodes/accordion_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use super::{
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type")]
pub enum AccordionTabNodes {
Pargaraph(Paragraph),
Heading(Heading),
P(Paragraph),
H(Heading),
Image(Image),
ImageGallery(ImageGallery),
List(List),
Expand All @@ -31,8 +31,8 @@ pub enum AccordionTabNodes {
impl Display for AccordionTabNodes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AccordionTabNodes::Pargaraph(node) => write!(f, "{}", node),
AccordionTabNodes::Heading(node) => write!(f, "{}", node),
AccordionTabNodes::P(node) => write!(f, "{}", node),
AccordionTabNodes::H(node) => write!(f, "{}", node),
AccordionTabNodes::Image(node) => write!(f, "{}", node),
AccordionTabNodes::ImageGallery(node) => write!(f, "{}", node),
AccordionTabNodes::List(node) => write!(f, "{}", node),
Expand All @@ -47,8 +47,8 @@ impl Display for AccordionTabNodes {
impl Node for AccordionTabNodes {
fn len(&self) -> usize {
match self {
AccordionTabNodes::Pargaraph(node) => node.len(),
AccordionTabNodes::Heading(node) => node.len(),
AccordionTabNodes::P(node) => node.len(),
AccordionTabNodes::H(node) => node.len(),
AccordionTabNodes::Image(node) => node.len(),
AccordionTabNodes::ImageGallery(node) => node.len(),
AccordionTabNodes::List(node) => node.len(),
Expand All @@ -62,13 +62,13 @@ impl Node for AccordionTabNodes {

impl From<Paragraph> for AccordionTabNodes {
fn from(value: Paragraph) -> Self {
Self::Pargaraph(value)
Self::P(value)
}
}

impl From<Heading> for AccordionTabNodes {
fn from(value: Heading) -> Self {
Self::Heading(value)
Self::H(value)
}
}

Expand Down Expand Up @@ -116,15 +116,15 @@ impl From<Code> for AccordionTabNodes {

#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct AccordionTab {
pub header: Option<String>,
pub title: Option<String>,
pub nodes: Vec<AccordionTabNodes>,
}

impl AccordionTab {
pub fn new<S: Into<String>>(header: Option<S>, nodes: Vec<AccordionTabNodes>) -> Self {
pub fn new<S: Into<String>>(title: Option<S>, nodes: Vec<AccordionTabNodes>) -> Self {
Self {
nodes,
header: header.map(|s| s.into()),
title: title.map(|s| s.into()),
}
}
}
Expand All @@ -133,11 +133,11 @@ impl Display for AccordionTab {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"//\n{header}{nodes}\n\\\\",
header = self
.header
"//\n{title}{nodes}\n\\\\",
title = self
.title
.as_ref()
.map_or("".to_string(), |header| format!("/ {}\n", header)),
.map_or("".to_string(), |title| format!("/ {}\n", title)),
nodes = self
.nodes
.iter()
Expand Down Expand Up @@ -184,7 +184,7 @@ impl Branch<AccordionTabNodes> for AccordionTab {
}

fn get_outer_token_length(&self) -> usize {
6 + self.header.as_ref().map_or(0, |header| header.len() + 3)
6 + self.title.as_ref().map_or(0, |header| header.len() + 3)
}

fn is_empty(&self) -> bool {
Expand All @@ -197,11 +197,11 @@ impl Deserializer for AccordionTab {
let mut matcher = Matcher::new(input);
if let Some(tab) = matcher.get_match("//\n", "\n\\\\", false) {
let mut inner_matcher = Matcher::new(tab.body);
let header = inner_matcher
let title = inner_matcher
.get_match("/ ", "\n", false)
.map(|header| header.body);

return Self::parse_branch(inner_matcher.get_rest(), "\n\n", Self::new(header, vec![]));
return Self::parse_branch(inner_matcher.get_rest(), "\n\n", Self::new(title, vec![]));
}
None
}
Expand All @@ -227,9 +227,9 @@ mod cfg {
#[test]
fn test_accordion_tab_deserialize() {
assert_eq!(
AccordionTab::deserialize("//\n/ Header\n# Heading\n\\\\\n\n"),
AccordionTab::deserialize("//\n/ Title\n# Heading\n\\\\\n\n"),
Some(AccordionTab::new(
Some("Header"),
Some("Title"),
vec![Heading::new("Heading", 1).into()]
))
);
Expand Down Expand Up @@ -260,17 +260,17 @@ mod cfg {
#[test]
fn test_accordion_tab_len() {
assert_eq!(
AccordionTab::new(Some("Header"), vec![Heading::new("Heading", 1).into()]).len(),
24
AccordionTab::new(Some("Title"), vec![Heading::new("Heading", 1).into()]).len(),
23
);
assert_eq!(AccordionTab::new(Some("Header"), vec![]).len(), 15);
assert_eq!(AccordionTab::new(Some("Title"), vec![]).len(), 14);
}

#[test]
fn test_accordion_tab_serialize() {
assert_eq!(
AccordionTab::new(Some("Header"), vec![Heading::new("Heading", 1).into()]).to_string(),
"//\n/ Header\n# Heading\n\\\\"
AccordionTab::new(Some("Title"), vec![Heading::new("Heading", 1).into()]).to_string(),
"//\n/ Title\n# Heading\n\\\\"
);
}

Expand All @@ -282,7 +282,7 @@ mod cfg {
#[test]
fn with_all_nodes() {
let input = r#"//
/ Header
/ Title
# hello
```rust
Expand All @@ -308,7 +308,7 @@ t**b**
{{cloudinary_gallery|cloud_name&tag}}
\\"#;
let tab = AccordionTab::new(
Some("Header"),
Some("Title"),
vec![
Heading::new("hello", 1).into(),
Code::new("rust", "let a=1;").into(),
Expand Down
24 changes: 12 additions & 12 deletions src/nodes/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use super::paragraph::Paragraph;

#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Highlight {
pub header: Option<String>,
pub title: Option<String>,
pub icon: Option<String>,
pub nodes: Vec<Paragraph>,
}

impl Highlight {
pub fn new<H: Into<String>, I: Into<String>>(
header: Option<H>,
pub fn new<T: Into<String>, I: Into<String>>(
title: Option<T>,
icon: Option<I>,
nodes: Vec<Paragraph>,
) -> Self {
Self {
header: header.map(|header| header.into()),
title: title.map(|title| title.into()),
icon: icon.map(|icon| icon.into()),
nodes,
}
Expand All @@ -29,8 +29,8 @@ impl Highlight {

impl Display for Highlight {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let header = match &self.header {
Some(header) => format!(">> {header}\n"),
let title = match &self.title {
Some(title) => format!(">> {title}\n"),
None => String::new(),
};
let icon = match &self.icon {
Expand All @@ -39,13 +39,13 @@ impl Display for Highlight {
};
write!(
f,
">>>\n{header}{icon}{}\n>>>",
">>>\n{title}{icon}{}\n>>>",
self.nodes
.iter()
.map(|node| node.to_string())
.collect::<Vec<String>>()
.join("\n\n"),
header = header,
title = title,
icon = icon,
)
}
Expand All @@ -61,7 +61,7 @@ impl Node for Highlight {
self.nodes.iter().map(|node| node.len()).sum::<usize>()
+ delimiter_length
+ 8
+ self.header.as_ref().map_or(0, |header| header.len() + 4)
+ self.title.as_ref().map_or(0, |title| title.len() + 4)
+ self.icon.as_ref().map_or(0, |icon| icon.len() + 3)
}
}
Expand All @@ -71,13 +71,13 @@ impl Deserializer for Highlight {
let mut outer_matcher = Matcher::new(input);
if let Some(highlight) = outer_matcher.get_match(">>>\n", "\n>>>", false) {
let mut matcher = Matcher::new(highlight.body);
let header = matcher
let title = matcher
.get_match(">> ", "\n", false)
.map(|header| header.body);
.map(|title| title.body);

let icon = matcher.get_match("> ", "\n", false).map(|icon| icon.body);
return Some(Self::new(
header,
title,
icon,
matcher
.get_rest()
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/yamd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ _I_
///
//
/ accordeon tab
/ accordion tab
\\
//
/ one more accordeon tab
/ one more accordion tab
\\
\\\
Expand Down Expand Up @@ -383,8 +383,8 @@ end"#;
Embed::new("youtube", "123",).into(),
Embed::new("cloudinary_gallery", "cloud_name&tag",).into(),
Accordion::new(vec![
AccordionTab::new(Some("accordeon tab"), vec![]).into(),
AccordionTab::new(Some("one more accordeon tab"), vec![]).into()
AccordionTab::new(Some("accordion tab"), vec![]).into(),
AccordionTab::new(Some("one more accordion tab"), vec![]).into()
])
.into(),
Paragraph::new(vec![Text::new("end").into()]).into()
Expand Down Expand Up @@ -459,8 +459,8 @@ end"#;
Embed::new("youtube", "123",).into(),
Embed::new("cloudinary_gallery", "cloud_name&tag",).into(),
Accordion::new(vec![
AccordionTab::new(Some("accordeon tab"), vec![]).into(),
AccordionTab::new(Some("one more accordeon tab"), vec![]).into()
AccordionTab::new(Some("accordion tab"), vec![]).into(),
AccordionTab::new(Some("one more accordion tab"), vec![]).into()
])
.into(),
Paragraph::new(vec![Text::new("end").into()]).into()
Expand Down

0 comments on commit 93156ee

Please sign in to comment.