Skip to content

Commit

Permalink
Merge pull request #16 from Lurk/cloudianry_gallery
Browse files Browse the repository at this point in the history
cloudinary image gallery
  • Loading branch information
Lurk authored Jul 10, 2023
2 parents 90a333d + 704c4ea commit fe4794d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.3.0"
version = "0.4.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Yet Another Markdown (flavor)"
Expand Down
90 changes: 90 additions & 0 deletions src/nodes/cloudinary_image_gallery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use crate::toolkit::{
context::Context, deserializer::Deserializer, matcher::Matcher, node::Node,
pattern::Quantifiers::*,
};

#[derive(Debug, PartialEq, Clone)]
pub struct CloudinaryImageGallery {
username: String,
tag: String,
consumed_all_input: bool,
}

impl CloudinaryImageGallery {
pub fn new<S: Into<String>>(username: S, tag: S, consumed_all_input: bool) -> Self {
Self {
username: username.into(),
tag: tag.into(),
consumed_all_input,
}
}
}

impl Node for CloudinaryImageGallery {
fn serialize(&self) -> String {
format!(
"!!!!\n! {username}\n! {tag}\n!!!!{end}",
username = self.username,
tag = self.tag,
end = if self.consumed_all_input { "" } else { "\n\n" }
)
}

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

impl Deserializer for CloudinaryImageGallery {
fn deserialize_with_context(input: &str, _: Option<Context>) -> Option<Self> {
let mut matcher = Matcher::new(input);
if let Some(cloudinary_image_gallery) = matcher.get_match(
&[RepeatTimes(4, '!'), Once('\n')],
&[Once('\n'), RepeatTimes(4, '!')],
false,
) {
let mut inner_matcher = Matcher::new(cloudinary_image_gallery.body);
if let Some(username) =
inner_matcher.get_match(&[Once('!'), Once(' ')], &[Once('\n')], false)
{
if let Some(tag) =
inner_matcher.get_match(&[Once('!'), Once(' ')], &[Once('\n')], true)
{
let consumed_all_input = matcher
.get_match(&[RepeatTimes(2, '\n')], &[], false)
.is_none();
return Some(Self::new(username.body, tag.body, consumed_all_input));
}
}
}
None
}
}

#[cfg(test)]
mod test {
use crate::{
nodes::cloudinary_image_gallery::CloudinaryImageGallery,
toolkit::{deserializer::Deserializer, node::Node},
};

#[test]
fn test_cloudinary_image_gallery() {
let input = "!!!!\n! username\n! tag\n!!!!\n\n";
let expected = CloudinaryImageGallery::new("username", "tag", false);
assert_eq!(
CloudinaryImageGallery::deserialize(input),
Some(expected.clone()),
);
assert_eq!(expected.serialize(), input);
}

#[test]
fn cloudinary_image_gallery_len() {
let input = "!!!!\n! username\n! tag\n!!!!\n\n";
assert_eq!(
CloudinaryImageGallery::deserialize(input).unwrap().len(),
input.len(),
);
}
}
1 change: 1 addition & 0 deletions src/nodes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod anchor;
pub mod bold;
pub mod cloudinary_image_gallery;
pub mod code;
pub mod divider;
pub mod embed;
Expand Down
22 changes: 20 additions & 2 deletions src/nodes/yamd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
};

use super::{
code::Code, divider::Divider, embed::Embed, highlight::Highlight, image::Image,
image_gallery::ImageGallery, list::List, message::Message,
cloudinary_image_gallery::CloudinaryImageGallery, code::Code, divider::Divider, embed::Embed,
highlight::Highlight, image::Image, image_gallery::ImageGallery, list::List, message::Message,
};

#[derive(Debug, PartialEq)]
Expand All @@ -22,6 +22,7 @@ pub enum YamdNodes {
Divider(Divider),
Embed(Embed),
Message(Message),
CloudinaryImageGallery(CloudinaryImageGallery),
}

impl From<Paragraph> for YamdNodes {
Expand Down Expand Up @@ -84,6 +85,12 @@ impl From<Message> for YamdNodes {
}
}

impl From<CloudinaryImageGallery> for YamdNodes {
fn from(value: CloudinaryImageGallery) -> Self {
YamdNodes::CloudinaryImageGallery(value)
}
}

impl Node for YamdNodes {
fn serialize(&self) -> String {
match self {
Expand All @@ -97,6 +104,7 @@ impl Node for YamdNodes {
YamdNodes::Divider(node) => node.serialize(),
YamdNodes::Embed(node) => node.serialize(),
YamdNodes::Message(node) => node.serialize(),
YamdNodes::CloudinaryImageGallery(node) => node.serialize(),
}
}
fn len(&self) -> usize {
Expand All @@ -111,6 +119,7 @@ impl Node for YamdNodes {
YamdNodes::Divider(node) => node.len(),
YamdNodes::Embed(node) => node.len(),
YamdNodes::Message(node) => node.len(),
YamdNodes::CloudinaryImageGallery(node) => node.len(),
}
}
}
Expand Down Expand Up @@ -146,6 +155,7 @@ impl Branch<YamdNodes> for Yamd {
Divider::maybe_node(),
Embed::maybe_node(),
Message::maybe_node(),
CloudinaryImageGallery::maybe_node(),
]
}

Expand Down Expand Up @@ -191,6 +201,7 @@ mod tests {
nodes::paragraph::Paragraph,
nodes::{
bold::Bold,
cloudinary_image_gallery::CloudinaryImageGallery,
code::Code,
divider::Divider,
embed::Embed,
Expand Down Expand Up @@ -245,6 +256,11 @@ content **bold**
content _italic_
%%%%
!!!!
! username
! tag
!!!!
end"#;

#[test]
Expand Down Expand Up @@ -358,6 +374,7 @@ end"#;
false
)
.into(),
CloudinaryImageGallery::new("username", "tag", false).into(),
Paragraph::new_with_nodes(true, vec![Text::new("end").into()]).into()
]))
);
Expand Down Expand Up @@ -450,6 +467,7 @@ end"#;
false
)
.into(),
CloudinaryImageGallery::new("username", "tag", false).into(),
Paragraph::new_with_nodes(true, vec![Text::new("end").into()]).into()
])
.serialize(),
Expand Down

0 comments on commit fe4794d

Please sign in to comment.