Skip to content

Commit

Permalink
cloudinary image galery refactoring (#33)
Browse files Browse the repository at this point in the history
* Cloudinary image gallery refactoring
  • Loading branch information
Lurk authored Oct 21, 2023
1 parent 8b486ab commit 87ca5b0
Show file tree
Hide file tree
Showing 24 changed files with 65 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yamd"
version = "0.8.0"
version = "0.9.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Yet Another Markdown Document (flavor)"
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/accordion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::toolkit::{

use super::accordion_tab::AccordionTab;

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type")]
pub enum AccordionNodes {
AccordionTab(AccordionTab),
Expand Down Expand Up @@ -39,7 +39,7 @@ impl From<AccordionTab> for AccordionNodes {
}
}

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Accordion {
#[serde(skip_serializing)]
consumed_all_input: bool,
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/accordion_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
list::List, paragraph::Paragraph,
};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type")]
pub enum AccordionTabNodes {
Pargaraph(Paragraph),
Expand Down Expand Up @@ -124,7 +124,7 @@ impl From<Code> for AccordionTabNodes {
}
}

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct AccordionTab {
pub header: Option<String>,
pub nodes: Vec<AccordionTabNodes>,
Expand Down Expand Up @@ -366,11 +366,11 @@ t**b**
.into(),
Image::new(false, 'a', 'u').into(),
ImageGallery::new_with_nodes(
false,
vec![
Image::new(true, "a", "u").into(),
Image::new(true, "a2", "u2").into(),
],
false,
)
.into(),
Divider::new(false).into(),
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

/// Representation of an anchor
#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Anchor {
pub text: String,
pub url: String,
Expand Down
10 changes: 2 additions & 8 deletions src/nodes/bold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
},
};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type")]
pub enum BoldNodes {
Text(Text),
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Node for BoldNodes {
}
}

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone, Default)]
pub struct Bold {
pub nodes: Vec<BoldNodes>,
}
Expand Down Expand Up @@ -92,12 +92,6 @@ impl Branch<BoldNodes> for Bold {
}
}

impl Default for Bold {
fn default() -> Self {
Self::new()
}
}

impl Display for Bold {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
Expand Down
12 changes: 8 additions & 4 deletions src/nodes/cloudinary_image_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matc

#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct CloudinaryImageGallery {
username: String,
pub cloud_name: String,
pub tag: String,
#[serde(skip_serializing)]
pub consumed_all_input: bool,
Expand All @@ -15,7 +15,7 @@ pub struct CloudinaryImageGallery {
impl CloudinaryImageGallery {
pub fn new<S: Into<String>>(username: S, tag: S, consumed_all_input: bool) -> Self {
Self {
username: username.into(),
cloud_name: username.into(),
tag: tag.into(),
consumed_all_input,
}
Expand All @@ -25,13 +25,17 @@ impl CloudinaryImageGallery {
impl Display for CloudinaryImageGallery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let end = if self.consumed_all_input { "" } else { "\n\n" };
write!(f, "!!!!\n! {}\n! {}\n!!!!{}", self.username, self.tag, end)
write!(
f,
"!!!!\n! {}\n! {}\n!!!!{}",
self.cloud_name, self.tag, end
)
}
}

impl Node for CloudinaryImageGallery {
fn len(&self) -> usize {
self.username.len() + self.tag.len() + 15 + if self.consumed_all_input { 0 } else { 2 }
self.cloud_name.len() + self.tag.len() + 15 + if self.consumed_all_input { 0 } else { 2 }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;

use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Code {
pub lang: String,
pub code: String,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/divider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;

use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Divider {
#[serde(skip_serializing)]
consumed_all_input: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;

use crate::toolkit::{deserializer::Deserializer, matcher::Matcher, node::Node};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Embed {
pub url: String,
pub kind: String,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;

use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Heading {
pub level: u8,
pub text: String,
Expand Down
28 changes: 14 additions & 14 deletions src/nodes/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::toolkit::{

use super::paragraph::Paragraph;

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type")]
pub enum HighlightNodes {
Paragraph(Paragraph),
Expand Down Expand Up @@ -39,7 +39,7 @@ impl From<Paragraph> for HighlightNodes {
}
}

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Highlight {
pub header: Option<String>,
pub icon: Option<String>,
Expand All @@ -50,17 +50,17 @@ pub struct Highlight {

impl Highlight {
pub fn new<H: Into<String>, I: Into<String>>(
consumed_all_input: bool,
header: Option<H>,
icon: Option<I>,
consumed_all_input: bool,
) -> Self {
Self::new_with_nodes(header, icon, consumed_all_input, vec![])
Self::new_with_nodes(consumed_all_input, header, icon, vec![])
}

pub fn new_with_nodes<H: Into<String>, I: Into<String>>(
consumed_all_input: bool,
header: Option<H>,
icon: Option<I>,
consumed_all_input: bool,
nodes: Vec<HighlightNodes>,
) -> Self {
Self {
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Deserializer for Highlight {

return Self::parse_branch(
matcher.get_rest(),
Self::new(header, icon, consumed_all_input),
Self::new(consumed_all_input, header, icon),
);
}

Expand All @@ -166,9 +166,9 @@ mod tests {
fn len() {
assert_eq!(
Highlight::new_with_nodes(
true,
Some("h"),
Some("i"),
true,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -179,9 +179,9 @@ mod tests {
);
assert_eq!(
Highlight::new_with_nodes(
false,
Some("h"),
Some("i"),
false,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -192,9 +192,9 @@ mod tests {
);
assert_eq!(
Highlight::new_with_nodes::<String, String>(
false,
None,
None,
false,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -208,9 +208,9 @@ mod tests {
fn serialize() {
assert_eq!(
Highlight::new_with_nodes(
true,
Some("h"),
Some("i"),
true,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -221,9 +221,9 @@ mod tests {
);
assert_eq!(
Highlight::new_with_nodes(
false,
Some("h"),
Some("i"),
false,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -234,9 +234,9 @@ mod tests {
);
assert_eq!(
Highlight::new_with_nodes::<String, String>(
false,
None,
None,
false,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -252,9 +252,9 @@ mod tests {
assert_eq!(
Highlight::deserialize(">>>\n>> h\n> i\nt\n\nt\n>>>"),
Some(Highlight::new_with_nodes(
true,
Some("h"),
Some("i"),
true,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into()
Expand All @@ -265,9 +265,9 @@ mod tests {
assert_eq!(
Highlight::deserialize(">>>\n>> h\n> i\nt\n\nt2\n>>>\n\n"),
Some(Highlight::new_with_nodes(
false,
Some("h"),
Some("i"),
false,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("t2").into()]).into()
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;

use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node};

#[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Image {
pub alt: String,
pub src: String,
Expand Down
Loading

0 comments on commit 87ca5b0

Please sign in to comment.