Skip to content

Commit 21b943c

Browse files
Move plugins.
1 parent 06bdf2c commit 21b943c

File tree

3 files changed

+91
-79
lines changed

3 files changed

+91
-79
lines changed

src/lib.rs

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55
use std::io::prelude::*;
66
use std::path::Path;
7-
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
7+
8+
mod plugins;
89

910
/// Describes the ways in which this library can fail.
1011
#[derive(thiserror::Error, Debug)]
@@ -211,83 +212,6 @@ pub fn load_book(summary_src: &str) -> Result<Vec<IndexEntry>, Error> {
211212
Ok(index_entries)
212213
}
213214

214-
/// Adds `<section>` around <h1> and <h2> slides.
215-
struct SlideAdapter {
216-
first: AtomicBool,
217-
no_heading_slide: AtomicBool,
218-
in_speaker_notes: AtomicBool,
219-
}
220-
221-
impl SlideAdapter {
222-
fn new() -> SlideAdapter {
223-
SlideAdapter {
224-
first: AtomicBool::new(true),
225-
no_heading_slide: AtomicBool::new(false),
226-
in_speaker_notes: AtomicBool::new(false),
227-
}
228-
}
229-
}
230-
231-
impl comrak::adapters::HeadingAdapter for SlideAdapter {
232-
fn enter(
233-
&self,
234-
output: &mut dyn Write,
235-
heading: &comrak::adapters::HeadingMeta,
236-
_sourcepos: Option<comrak::nodes::Sourcepos>,
237-
) -> std::io::Result<()> {
238-
if heading.level == 3 && heading.content == "Notes" {
239-
// the start of speaker notes.
240-
writeln!(output, r#"<aside class="notes">"#)?;
241-
self.in_speaker_notes.store(true, Relaxed);
242-
} else if heading.level <= 2 {
243-
// We break slides on # and ##
244-
if !self.first.load(Relaxed) {
245-
// we have a previous slide open. Close it
246-
247-
// but first close any speaker notes
248-
if self.in_speaker_notes.load(Relaxed) {
249-
self.in_speaker_notes.store(false, Relaxed);
250-
writeln!(output, "</aside>")?;
251-
}
252-
// now close the slide
253-
writeln!(output, "</section>")?;
254-
} else {
255-
self.first.store(false, Relaxed);
256-
}
257-
// open a new slide
258-
writeln!(output, "<section>")?;
259-
}
260-
261-
if heading.content == "NO_HEADING" {
262-
// this is a slide with no heading - comment out the fake heading
263-
self.no_heading_slide.store(true, Relaxed);
264-
writeln!(output, "<!-- ")?;
265-
} else {
266-
// write out the heading
267-
self.no_heading_slide.store(false, Relaxed);
268-
writeln!(output, "<h{}>", heading.level)?;
269-
}
270-
271-
Ok(())
272-
}
273-
274-
fn exit(
275-
&self,
276-
output: &mut dyn Write,
277-
heading: &comrak::adapters::HeadingMeta,
278-
) -> std::io::Result<()> {
279-
if self.no_heading_slide.load(Relaxed) {
280-
// stop hiding the fake heading
281-
writeln!(output, " -->")?;
282-
self.no_heading_slide.store(false, Relaxed);
283-
} else {
284-
// close the heading
285-
writeln!(output, "</h{}>", heading.level)?;
286-
}
287-
Ok(())
288-
}
289-
}
290-
291215
/// Processes a markdown file into an HTML document, using the given template.
292216
///
293217
/// The template should contain the string `$TITLE`, which is the title of the
@@ -312,7 +236,7 @@ pub fn generate_deck(
312236
let md_content = std::fs::read_to_string(in_path)?;
313237

314238
let md_content = md_content.replace("---", "## NO_HEADING");
315-
let slide_adapter = SlideAdapter::new();
239+
let slide_adapter = plugins::SlideAdapter::new();
316240
let options = comrak::Options::default();
317241
let mut plugins = comrak::Plugins::default();
318242
plugins.render.heading_adapter = Some(&slide_adapter);

src/plugins/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! comrak plugins we need
2+
3+
mod slides;
4+
pub use slides::SlideAdapter;

src/plugins/slides.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//! Our Slide Adapter
2+
//!
3+
//! A comrak plugin for splitting up plain markdown text into reveal.js slides
4+
//! by adding `<section>` tags around each slide (as delimited by `h1` or `h3`
5+
//! heading).
6+
7+
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
8+
9+
/// Adds `<section>` around <h1> and <h2> slides.
10+
pub struct SlideAdapter {
11+
first: AtomicBool,
12+
no_heading_slide: AtomicBool,
13+
in_speaker_notes: AtomicBool,
14+
}
15+
16+
impl SlideAdapter {
17+
pub fn new() -> SlideAdapter {
18+
SlideAdapter {
19+
first: AtomicBool::new(true),
20+
no_heading_slide: AtomicBool::new(false),
21+
in_speaker_notes: AtomicBool::new(false),
22+
}
23+
}
24+
}
25+
26+
impl comrak::adapters::HeadingAdapter for SlideAdapter {
27+
fn enter(
28+
&self,
29+
output: &mut dyn std::io::Write,
30+
heading: &comrak::adapters::HeadingMeta,
31+
_sourcepos: Option<comrak::nodes::Sourcepos>,
32+
) -> std::io::Result<()> {
33+
if heading.level == 3 && heading.content == "Notes" {
34+
// the start of speaker notes.
35+
writeln!(output, r#"<aside class="notes">"#)?;
36+
self.in_speaker_notes.store(true, Relaxed);
37+
} else if heading.level <= 2 {
38+
// We break slides on # and ##
39+
if !self.first.load(Relaxed) {
40+
// we have a previous slide open. Close it
41+
42+
// but first close any speaker notes
43+
if self.in_speaker_notes.load(Relaxed) {
44+
self.in_speaker_notes.store(false, Relaxed);
45+
writeln!(output, "</aside>")?;
46+
}
47+
// now close the slide
48+
writeln!(output, "</section>")?;
49+
} else {
50+
self.first.store(false, Relaxed);
51+
}
52+
// open a new slide
53+
writeln!(output, "<section>")?;
54+
}
55+
56+
if heading.content == "NO_HEADING" {
57+
// this is a slide with no heading - comment out the fake heading
58+
self.no_heading_slide.store(true, Relaxed);
59+
writeln!(output, "<!-- ")?;
60+
} else {
61+
// write out the heading
62+
self.no_heading_slide.store(false, Relaxed);
63+
writeln!(output, "<h{}>", heading.level)?;
64+
}
65+
66+
Ok(())
67+
}
68+
69+
fn exit(
70+
&self,
71+
output: &mut dyn std::io::Write,
72+
heading: &comrak::adapters::HeadingMeta,
73+
) -> std::io::Result<()> {
74+
if self.no_heading_slide.load(Relaxed) {
75+
// stop hiding the fake heading
76+
writeln!(output, " -->")?;
77+
self.no_heading_slide.store(false, Relaxed);
78+
} else {
79+
// close the heading
80+
writeln!(output, "</h{}>", heading.level)?;
81+
}
82+
Ok(())
83+
}
84+
}

0 commit comments

Comments
 (0)