-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from veryl-lang/mermaid_support
Mermaid support for documentation comment
- Loading branch information
Showing
19 changed files
with
2,372 additions
and
202 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The files in this directory are based on: | ||
|
||
https://github.com/mermaid-js/mermaid | ||
Copyright (c) 2014-2022 Knut Sveidqvist | ||
Released under the MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
10.9.1 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod doc_builder; | ||
mod mermaid; | ||
mod utils; | ||
mod wavedrom; | ||
pub use doc_builder::*; | ||
pub use mermaid::*; | ||
pub use wavedrom::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// This file is based on: | ||
// | ||
// https://github.com/badboy/mdbook-mermaid | ||
// Released under the MPL-2.0 license | ||
|
||
use crate::doc::utils::escape_html; | ||
use mdbook::book::{Book, BookItem, Chapter}; | ||
use mdbook::errors::Result; | ||
use mdbook::preprocess::{Preprocessor, PreprocessorContext}; | ||
use pulldown_cmark::{CodeBlockKind::*, Event, Options, Parser, Tag, TagEnd}; | ||
|
||
pub struct Mermaid; | ||
|
||
impl Preprocessor for Mermaid { | ||
fn name(&self) -> &str { | ||
"mermaid" | ||
} | ||
|
||
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> { | ||
let mut res = None; | ||
book.for_each_mut(|item: &mut BookItem| { | ||
if let Some(Err(_)) = res { | ||
return; | ||
} | ||
|
||
if let BookItem::Chapter(ref mut chapter) = *item { | ||
res = Some(Mermaid::add_mermaid(chapter).map(|md| { | ||
chapter.content = md; | ||
})); | ||
} | ||
}); | ||
|
||
res.unwrap_or(Ok(())).map(|_| book) | ||
} | ||
|
||
fn supports_renderer(&self, renderer: &str) -> bool { | ||
renderer == "html" | ||
} | ||
} | ||
|
||
impl Mermaid { | ||
fn add_mermaid(chapter: &mut Chapter) -> Result<String> { | ||
let content = &chapter.content; | ||
let mut mermaid_content = String::new(); | ||
let mut in_mermaid_block = false; | ||
|
||
let mut opts = Options::empty(); | ||
opts.insert(Options::ENABLE_TABLES); | ||
opts.insert(Options::ENABLE_FOOTNOTES); | ||
opts.insert(Options::ENABLE_STRIKETHROUGH); | ||
opts.insert(Options::ENABLE_TASKLISTS); | ||
|
||
let mut code_span = 0..0; | ||
let mut start_new_code_span = true; | ||
|
||
let mut mermaid_blocks = vec![]; | ||
|
||
let events = Parser::new_ext(content, opts); | ||
for (e, span) in events.into_offset_iter() { | ||
if let Event::Start(Tag::CodeBlock(Fenced(code))) = e.clone() { | ||
if &*code == "mermaid" { | ||
in_mermaid_block = true; | ||
mermaid_content.clear(); | ||
} | ||
continue; | ||
} | ||
|
||
if !in_mermaid_block { | ||
continue; | ||
} | ||
|
||
// We're in the code block. The text is what we want. | ||
// Code blocks can come in multiple text events. | ||
|
||
if let Event::Text(_) = e { | ||
if start_new_code_span { | ||
code_span = span; | ||
start_new_code_span = false; | ||
} else { | ||
code_span = code_span.start..span.end; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if let Event::End(TagEnd::CodeBlock) = e { | ||
in_mermaid_block = false; | ||
|
||
let mermaid_content = &content[code_span.clone()]; | ||
let mermaid_content = escape_html(mermaid_content); | ||
let mermaid_content = mermaid_content.replace("\r\n", "\n"); | ||
let mermaid_code = format!("<pre class=\"mermaid\">{}</pre>\n\n", mermaid_content); | ||
mermaid_blocks.push((span, mermaid_code)); | ||
|
||
start_new_code_span = true; | ||
} | ||
} | ||
|
||
let mut content = content.to_string(); | ||
for (span, block) in mermaid_blocks.iter().rev() { | ||
let pre_content = &content[0..span.start]; | ||
let post_content = &content[span.end..]; | ||
|
||
content = format!("{}\n{}{}", pre_content, block, post_content); | ||
} | ||
Ok(content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub fn escape_html(s: &str) -> String { | ||
let mut output = String::new(); | ||
for c in s.chars() { | ||
match c { | ||
'<' => output.push_str("<"), | ||
'>' => output.push_str(">"), | ||
'"' => output.push_str("""), | ||
'&' => output.push_str("&"), | ||
_ => output.push(c), | ||
} | ||
} | ||
output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// This file is based on: | ||
// | ||
// https://github.com/JasMoH/mdbook-wavedrom | ||
// Released under the MPL-2.0 license | ||
|
||
use crate::doc::utils::escape_html; | ||
use mdbook::book::{Book, BookItem, Chapter}; | ||
use mdbook::errors::Result; | ||
use mdbook::preprocess::{Preprocessor, PreprocessorContext}; | ||
use pulldown_cmark::{CodeBlockKind::*, Event, Options, Parser, Tag, TagEnd}; | ||
|
||
pub struct Wavedrom; | ||
|
||
impl Preprocessor for Wavedrom { | ||
fn name(&self) -> &str { | ||
"wavedrom" | ||
} | ||
|
||
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> { | ||
let mut res = None; | ||
book.for_each_mut(|item: &mut BookItem| { | ||
if let Some(Err(_)) = res { | ||
return; | ||
} | ||
|
||
if let BookItem::Chapter(ref mut chapter) = *item { | ||
res = Some(Wavedrom::add_wavedrom(chapter).map(|md| { | ||
chapter.content = md; | ||
})); | ||
} | ||
}); | ||
|
||
res.unwrap_or(Ok(())).map(|_| book) | ||
} | ||
|
||
fn supports_renderer(&self, renderer: &str) -> bool { | ||
renderer == "html" | ||
} | ||
} | ||
|
||
impl Wavedrom { | ||
fn add_wavedrom(chapter: &mut Chapter) -> Result<String> { | ||
let content = &chapter.content; | ||
let mut wavedrom_content = String::new(); | ||
let mut in_wavedrom_block = false; | ||
|
||
let mut opts = Options::empty(); | ||
opts.insert(Options::ENABLE_TABLES); | ||
opts.insert(Options::ENABLE_FOOTNOTES); | ||
opts.insert(Options::ENABLE_STRIKETHROUGH); | ||
opts.insert(Options::ENABLE_TASKLISTS); | ||
|
||
let mut code_span = 0..0; | ||
let mut start_new_code_span = true; | ||
|
||
let mut wavedrom_blocks = vec![]; | ||
|
||
let events = Parser::new_ext(content, opts); | ||
for (e, span) in events.into_offset_iter() { | ||
if let Event::Start(Tag::CodeBlock(Fenced(code))) = e.clone() { | ||
if &*code == "wavedrom" { | ||
in_wavedrom_block = true; | ||
wavedrom_content.clear(); | ||
} | ||
continue; | ||
} | ||
|
||
if !in_wavedrom_block { | ||
continue; | ||
} | ||
|
||
// We're in the code block. The text is what we want. | ||
// Code blocks can come in multiple text events. | ||
|
||
if let Event::Text(_) = e { | ||
if start_new_code_span { | ||
code_span = span; | ||
start_new_code_span = false; | ||
} else { | ||
code_span = code_span.start..span.end; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if let Event::End(TagEnd::CodeBlock) = e { | ||
in_wavedrom_block = false; | ||
|
||
let wavedrom_content = &content[code_span.clone()]; | ||
let wavedrom_content = escape_html(wavedrom_content); | ||
let wavedrom_content = wavedrom_content.replace("\r\n", "\n"); | ||
let wavedrom_code = format!("<body onload=\"WaveDrom.ProcessAll()\">\n\n<script type=\"WaveDrom\">{}</script>\n\n", wavedrom_content); | ||
wavedrom_blocks.push((span, wavedrom_code)); | ||
|
||
start_new_code_span = true; | ||
} | ||
} | ||
|
||
let mut content = content.to_string(); | ||
for (span, block) in wavedrom_blocks.iter().rev() { | ||
let pre_content = &content[0..span.start]; | ||
let post_content = &content[span.end..]; | ||
|
||
content = format!("{}\n{}{}", pre_content, block, post_content); | ||
} | ||
Ok(content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters