Skip to content

Commit

Permalink
Fixes for updates to pulldown_cmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Cypher1 committed Apr 7, 2024
1 parent ccb102b commit 7d2ff1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
20 changes: 7 additions & 13 deletions src/preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use mdbook::errors::Result;
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::utils::new_cmark_parser;
use mdbook::BookItem;
use pulldown_cmark::{CodeBlockKind, Event, Tag};
use pulldown_cmark::{CodeBlockKind::Fenced, Event, Tag, TagEnd};
use pulldown_cmark_to_cmark::cmark;

use crate::renderer::{CLIGraphviz, CLIGraphvizToFile, GraphvizRenderer};
Expand Down Expand Up @@ -123,16 +123,10 @@ impl<R: GraphvizRenderer> Graphviz<R> {
if let Some(mut builder) = graphviz_block_builder.take() {
match e {
Event::Text(ref text) => {
builder.append_code(&**text);
builder.append_code(text.to_string());
graphviz_block_builder = Some(builder);
}
Event::End(Tag::CodeBlock(CodeBlockKind::Fenced(ref info_string))) => {
assert_eq!(
Some(0),
(**info_string).find(INFO_STRING_PREFIX),
"We must close our graphviz block"
);

Event::End(TagEnd::CodeBlock) => {
// finish our digraph
let block = builder.build(image_index);
image_index += 1;
Expand All @@ -145,12 +139,12 @@ impl<R: GraphvizRenderer> Graphviz<R> {
}
} else {
match e {
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(ref info_string)))
if (**info_string).find(INFO_STRING_PREFIX) == Some(0) =>
Event::Start(Tag::CodeBlock(Fenced(info_string)))
if info_string.find(INFO_STRING_PREFIX) == Some(0) =>
{
graphviz_block_builder = Some(GraphvizBlockBuilder::new(
&**info_string,
&chapter.name.clone(),
info_string.to_string(),
chapter.name.clone(),
chapter_path.clone(),
));
}
Expand Down
17 changes: 13 additions & 4 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio::process::{Child, Command};

use async_trait::async_trait;
use mdbook::errors::Result;
use pulldown_cmark::{Event, LinkType, Tag};
use pulldown_cmark::{Event, LinkType, Tag, TagEnd};
use regex::Regex;

use crate::preprocessor::GraphvizBlock;
Expand Down Expand Up @@ -60,11 +60,16 @@ impl GraphvizRenderer for CLIGraphvizToFile {
.await?
.success()
{
let image_tag = Tag::Image(LinkType::Inline, file_name.into(), graph_name.into());
let image_tag = Tag::Image {
link_type: LinkType::Inline,
dest_url: file_name.into(),
title: graph_name.into(),
id: "".into(),
};

Ok(vec![
Event::Start(image_tag.clone()),
Event::End(image_tag),
Event::Start(image_tag),
Event::End(TagEnd::Image),
Event::Text("\n\n".into()),
])
} else {
Expand Down Expand Up @@ -93,14 +98,18 @@ fn format_output(output: String) -> String {
static ref DOCTYPE_RE: Regex = Regex::new(r"<!DOCTYPE [^>]+>").unwrap();
static ref XML_TAG_RE: Regex = Regex::new(r"<\?xml [^>]+\?>").unwrap();
static ref NEW_LINE_TAGS_RE: Regex = Regex::new(r">\s+<").unwrap();
static ref NEWLINES_RE: Regex = Regex::new(r"\n").unwrap();
}

// yes yes: https://stackoverflow.com/a/1732454 ZA̡͊͠͝LGΌ and such
let output = DOCTYPE_RE.replace(&output, "");
let output = XML_TAG_RE.replace(&output, "");
// remove newlines between our tags to help commonmark determine the full set of HTML
let output = NEW_LINE_TAGS_RE.replace_all(&output, "><");
// remove explicit newlines as they won't be preserved and break commonmark parsing
let output = NEWLINES_RE.replace_all(&output, "");
let output = output.trim();
eprintln!("HTML\n{:?}", &output);

format!("<div>{output}</div>")
}
Expand Down

0 comments on commit 7d2ff1e

Please sign in to comment.