diff --git a/Cargo.lock b/Cargo.lock index 74e2dbd..e288a8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1029,7 +1029,7 @@ dependencies = [ [[package]] name = "mdbook-chess" -version = "0.2.1" +version = "0.2.2" dependencies = [ "chess", "clap 3.2.25", diff --git a/Cargo.toml b/Cargo.toml index 6e35e9f..e516b55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mdbook-chess" -version = "0.2.1" +version = "0.2.2" description = "An mdbook preprocessing plugin to generate chess boards" readme = "README.md" license = "MIT/Apache-2.0" diff --git a/demo-book/src/SUMMARY.md b/demo-book/src/SUMMARY.md index 7390c82..d5666b9 100644 --- a/demo-book/src/SUMMARY.md +++ b/demo-book/src/SUMMARY.md @@ -1,3 +1,5 @@ # Summary - [Chapter 1](./chapter_1.md) +- [No Chess](./no_chess.md) +- [Mixed](./mixed.md) diff --git a/demo-book/src/mixed.md b/demo-book/src/mixed.md new file mode 100644 index 0000000..467a4a0 --- /dev/null +++ b/demo-book/src/mixed.md @@ -0,0 +1,22 @@ +# Oh boy! + +Lets look at the Caro-Kann defense. First lets create a new board + +```chess +save: caro-kann +board: start +``` + +- [x] Get a chess board +- [ ] Play Caro Kann + +| Syntax | Description | Misc 1 | Misc 2 | +| ----------- | ----------- | --- | ---- | +| Header | Title | **Hello** | | +| Paragraph | Text | | _what?_ | + +```chess +load: caro-kann +save: caro-kann-show-moves +moves: ["d4", "d5"] +``` diff --git a/demo-book/src/no_chess.md b/demo-book/src/no_chess.md new file mode 100644 index 0000000..f6130df --- /dev/null +++ b/demo-book/src/no_chess.md @@ -0,0 +1,15 @@ +# No Chess + +Just to make sure we don't break things here's a markdown file with a bunch of +non-chess elements. + +| Syntax | Description | Misc 1 | Misc 2 | +| ----------- | ----------- | --- | ---- | +| Header | Title | **Hello** | | +| Paragraph | Text | | _what?_ | + +- [x] Check other markdown things +- [ ] Make it work +- [ ] Get better at chess. [^1] + +[^1]: Longer time formats, not bullet you degenerate. diff --git a/src/chess_preproc.rs b/src/chess_preproc.rs index 993dca7..b7275b3 100644 --- a/src/chess_preproc.rs +++ b/src/chess_preproc.rs @@ -7,10 +7,9 @@ use pulldown_cmark::{CodeBlockKind, CowStr, Event, Parser, Tag}; use pulldown_cmark_to_cmark::cmark; use serde::Deserialize; use std::collections::HashMap; - use std::fmt; use std::str::FromStr; -use tracing::{error, info}; +use tracing::{debug, error, info}; /// A constant X axis offset to apply to all pieces (and pawns). const X_OFFSET: f32 = 0.6; @@ -178,30 +177,38 @@ fn process_code_blocks(chapter: &mut Chapter) -> Result { use Event::*; use Tag::{CodeBlock, Paragraph}; - let mut boards = HashMap::new(); + if chapter.content.contains("```chess") { + // TODO Something in this doesn't work with tables! + let mut boards = HashMap::new(); - let mut logged_found = false; - let mut output = String::with_capacity(chapter.content.len()); - let mut inside_block = false; - let events = Parser::new(&chapter.content).map(|e| match (&e, inside_block) { - (Start(CodeBlock(Fenced(Borrowed("chess")))), false) => { - inside_block = true; - if !logged_found { - info!("Found chess block(s) in {}", chapter.name); - logged_found = true; + let mut logged_found = false; + let mut output = String::with_capacity(chapter.content.len()); + let mut inside_block = false; + let events = Parser::new(&chapter.content).map(|e| match (&e, inside_block) { + (Start(CodeBlock(Fenced(Borrowed("chess")))), false) => { + inside_block = true; + if !logged_found { + info!("Found chess block(s) in {}", chapter.name); + logged_found = true; + } + Start(Paragraph) } - Start(Paragraph) - } - (Text(Borrowed(text)), true) => { - inside_block = false; - Html(process_chess_block(text, &mut boards).into()) - } - (End(CodeBlock(Fenced(Borrowed("chess")))), false) => End(Paragraph), - (Text(text), false) => Html(text.clone()), - _ => e, - }); + (Text(Borrowed(text)), true) => { + inside_block = false; + Html(process_chess_block(text, &mut boards).into()) + } + (End(CodeBlock(Fenced(Borrowed("chess")))), false) => End(Paragraph), + (Text(text), _) => Html(text.clone()), + _ => { + debug!("Ignoring event: {:?}", e); + e + } + }); - cmark(events, &mut output).map(|_| output) + cmark(events, &mut output).map(|_| output) + } else { + Ok(chapter.content.clone()) + } } /// Given our c @@ -262,6 +269,7 @@ fn process_chess_block(input: &str, boards: &mut HashMap) -> Stri #[cfg(test)] mod tests { use super::*; + use std::fs; #[test] fn ensure_svg_in_output() { @@ -284,6 +292,14 @@ mod tests { vec![], ); let s = process_code_blocks(&mut chapter).unwrap(); - assert!(!s.contains(r#"\|foo|bar|"#), "{}", s); + assert!(!s.contains(r#"|foo|bar|\n|---|---|\n|a|b|"#), "{}", s); + } + + #[test] + fn dont_break_emphasis_in_tables() { + let content = fs::read_to_string("demo-book/src/no_chess.md").unwrap(); + let mut chapter = Chapter::new("test", content.clone(), ".", vec![]); + let s = process_code_blocks(&mut chapter).unwrap(); + assert_eq!(content, s); } }