Skip to content

Commit

Permalink
Start working on fixing more issues :( (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
xd009642 committed Aug 11, 2023
1 parent 2524878 commit 48ddf12
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 26 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 = "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"
Expand Down
2 changes: 2 additions & 0 deletions demo-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Summary

- [Chapter 1](./chapter_1.md)
- [No Chess](./no_chess.md)
- [Mixed](./mixed.md)
22 changes: 22 additions & 0 deletions demo-book/src/mixed.md
Original file line number Diff line number Diff line change
@@ -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"]
```
15 changes: 15 additions & 0 deletions demo-book/src/no_chess.md
Original file line number Diff line number Diff line change
@@ -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.
64 changes: 40 additions & 24 deletions src/chess_preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,30 +177,38 @@ fn process_code_blocks(chapter: &mut Chapter) -> Result<String, fmt::Error> {
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
Expand Down Expand Up @@ -262,6 +269,7 @@ fn process_chess_block(input: &str, boards: &mut HashMap<String, Board>) -> Stri
#[cfg(test)]
mod tests {
use super::*;
use std::fs;

#[test]
fn ensure_svg_in_output() {
Expand All @@ -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);
}
}

0 comments on commit 48ddf12

Please sign in to comment.