Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gallery code block #96

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions demo/issue-1/first.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# The first article of Issue 1

Hello Zine.
Expand All @@ -17,4 +16,9 @@ Text 2
fn main() {
println!("{}", "Hello, Zine!");
}
```
```

```gallery
/static/duck.png
/static/zine.png
```
32 changes: 32 additions & 0 deletions src/code_blocks/gallery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::fmt::Write;

use super::CodeBlock;

pub struct GalleryBlock<'a> {
images: Vec<&'a str>,
}

// enum GalleryMode {
// Grid,
// Slide,
// }

impl<'a> GalleryBlock<'a> {
pub fn new(block: &'a str) -> Self {
let images = block.lines().collect();
GalleryBlock { images }
}
}

impl<'a> CodeBlock for GalleryBlock<'a> {
fn render(&self) -> anyhow::Result<String> {
let mut html = String::new();

writeln!(&mut html, r#"<div class="gallery">"#)?;
for image in &self.images {
writeln!(&mut html, r#"<p><img src="{}" /></p>"#, image)?;
}
writeln!(&mut html, r#"</div>"#)?;
Ok(html)
}
}
9 changes: 8 additions & 1 deletion src/code_blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use anyhow::{bail, Result};

mod author;
mod callout;
mod gallery;
mod url_preview;

use crate::{data, engine::Vistor, helpers, html};
pub use author::AuthorCode;
use gallery::GalleryBlock;
use url_preview::{UrlPreviewBlock, UrlPreviewError};

use self::callout::CalloutBlock;
Expand All @@ -17,9 +19,10 @@ pub trait CodeBlock {
}

const CALLOUT: &str = "callout";
const GALLERY: &str = "gallery";
const URL_PREVIEW: &str = "urlpreview";

const ALL_CODE_BLOCKS: &[&str] = &[CALLOUT, URL_PREVIEW];
const ALL_CODE_BLOCKS: &[&str] = &[CALLOUT, GALLERY, URL_PREVIEW];

#[derive(Debug, Default, PartialEq, Eq)]
pub struct Fenced<'a> {
Expand Down Expand Up @@ -78,6 +81,10 @@ impl<'a> Fenced<'a> {
.unwrap();
Some(html)
}
GALLERY => {
let html = GalleryBlock::new(block).render().unwrap();
Some(html)
}
_ => None,
}
}
Expand Down