added rendering katex formulas

master
Kenton Hamaluik 5 years ago
parent 43dbbecd11
commit 5b0cc03ac8

2
Cargo.lock generated

@ -411,7 +411,7 @@ dependencies = [
[[package]]
name = "mkbook"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"askama 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",

@ -1,6 +1,6 @@
[package]
name = "mkbook"
version = "0.2.0"
version = "0.3.0"
authors = ["Kenton Hamaluik <kenton@hamaluik.ca>"]
edition = "2018"
build = "build.rs"

@ -0,0 +1,21 @@
---
title = "KaTeX (Math) Formulas"
---
If you have [KaTeX](https://github.com/KaTeX/KaTeX) installed and available on your path, _mkbook_ will try to render any code blocks with a language tag of `katex` as inline math blocks.
For example:
~~~
```katex
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
```
~~~
is rendered as:
```katex
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
```
This feature is still experimental, but I find it handy for my books.

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//01-introduction/index.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//02-markdown/01-commonmark.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//02-markdown/02-syntax-highlighting.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//02-markdown/03-plantuml-diagrams.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html" class = "current">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>
@ -91,7 +94,7 @@
<span class="placeholder"></span>
<a href="../03-frontmatter&#x2f;index.html" alt="Front Matter">
<a href="../02-markdown&#x2f;04-katex-formulas.html" alt="KaTeX (Math) Formulas">
<span class="icon">
<svg class="icon-arrow-right">
<use xlink:href="../icons.svg#icon-arrow-right">
@ -122,8 +125,8 @@
<div class="next-chapter">
<a href="../03-frontmatter&#x2f;index.html">
<span>Next chapter: “Front Matter</span>
<a href="../02-markdown&#x2f;04-katex-formulas.html">
<span>Next chapter: “KaTeX (Math) Formulas</span>
<span class="icon">
<svg class="icon-arrow-right">
<use xlink:href="../icons.svg#icon-arrow-right">

@ -0,0 +1,149 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The mkbook Book | KaTeX (Math) Formulas</title>
<link rel="stylesheet" href="../style.css" type="text/css" media="all" />
<link rel="shortcut icon" href="../favicon.ico" />
<meta property="og:title" content="The mkbook Book" />
<meta property="og:site_name" content="The mkbook Book" />
<meta property="og:type" content="book" />
<meta property="og:url" content="https://hamaluik.github.io/mkbook//02-markdown/04-katex-formulas.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">
</head>
<body>
<nav class="big">
<header>
<h1><a href="../index.html">The mkbook Book</a></h1>
<h2>by Kenton Hamaluik</h2>
</header>
<ol>
<li>
<a href="../01-introduction/index.html">Introduction</a>
</li>
<li>
<a href="../02-markdown/index.html">Markdown</a>
<ol>
<li><a href="../02-markdown/01-commonmark.html">CommonMark</a></li>
<li><a href="../02-markdown/02-syntax-highlighting.html">Syntax Highlighting</a></li>
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html" class = "current">KaTeX (Math) Formulas</a></li>
</ol>
</li>
<li>
<a href="../03-frontmatter/index.html">Front Matter</a>
</li>
<li>
<a href="../04-structure/index.html">Structure</a>
</li>
<li>
<a href="../05-customization/index.html">Customization</a>
</li>
<li>
<a href="../06-how-it-works/index.html">How it Works</a>
</li>
</ol>
</nav>
<nav class="small">
<span>
<a href="../index.html" alt="Table of Contents">
<span class="icon">
<svg class="icon-list-ol">
<use xlink:href="../icons.svg#icon-list-ol">
</svg>
</span>
</a>
<a href="../02-markdown&#x2f;03-plantuml-diagrams.html" alt="PlantUML Diagrams">
<span class="icon">
<svg class="icon-arrow-left">
<use xlink:href="../icons.svg#icon-arrow-left">
</svg>
</span>
</a>
</span>
<span class="title">KaTeX (Math) Formulas</span>
<span>
<span class="placeholder"></span>
<a href="../03-frontmatter&#x2f;index.html" alt="Front Matter">
<span class="icon">
<svg class="icon-arrow-right">
<use xlink:href="../icons.svg#icon-arrow-right">
</svg>
</span>
</a>
</span>
</nav>
<article>
<p>If you have <a href="https://github.com/KaTeX/KaTeX">KaTeX</a> installed and available on your path, <em>mkbook</em> will try to render any code blocks with a language tag of <code>katex</code> as inline math blocks.</p>
<p>For example:</p>
<pre style="background-color:#2d2d2d;">
<span style="color:#d3d0c8;">```katex
</span><span style="color:#d3d0c8;">x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
</span><span style="color:#d3d0c8;">```
</span></pre>
<p>is rendered as:</p>
<figure class="math"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo></mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo></mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex">x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:2.276389em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.590389em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">2</span><span class="mord mathdefault">a</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mord mathdefault">b</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">±</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mord sqrt"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.913389em;"><span class="svg-align" style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord" style="padding-left:0.833em;"><span class="mord"><span class="mord mathdefault">b</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.740108em;"><span style="top:-2.9890000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mord">4</span><span class="mord mathdefault">a</span><span class="mord mathdefault">c</span></span></span><span style="top:-2.873389em;"><span class="pstrut" style="height:3em;"></span><span class="hide-tail" style="min-width:0.853em;height:1.08em;"><svg width='400em' height='1.08em' viewBox='0 0 400000 1080' preserveAspectRatio='xMinYMin slice'><path d='M95,702
c-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14
c0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54
c44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10
s173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429
c69,-144,104.5,-217.7,106.5,-221
l0 -0
c5.3,-9.3,12,-14,20,-14
H400000v40H845.2724
s-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7
c-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z
M834 80h400000v40h-400000z'/></svg></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.12661100000000003em;"><span></span></span></span></span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span>
</figure>
<p>This feature is still experimental, but I find it handy for my books.</p>
<div class="next-chapter">
<a href="../03-frontmatter&#x2f;index.html">
<span>Next chapter: “Front Matter”</span>
<span class="icon">
<svg class="icon-arrow-right">
<use xlink:href="../icons.svg#icon-arrow-right">
</svg>
</span>
</a>
</div>
<footer><p>&copy; 2019 Kenton Hamaluik</p></footer>
</article>
</body>
</html>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//02-markdown/index.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//03-frontmatter/index.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>
@ -75,7 +78,7 @@
</a>
<a href="../02-markdown&#x2f;03-plantuml-diagrams.html" alt="PlantUML Diagrams">
<a href="../02-markdown&#x2f;04-katex-formulas.html" alt="KaTeX (Math) Formulas">
<span class="icon">
<svg class="icon-arrow-left">

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//04-structure/index.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//05-customization/index.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -12,7 +12,8 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//06-how-it-works/index.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body>
<nav class="big">
@ -38,6 +39,8 @@
<li><a href="../02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="../02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -14,13 +14,14 @@
&lt;p&gt;This tool aims to work somewhat similarly to &lt;em&gt;mdbook&lt;&#x2f;em&gt;, but is generally intended to be a more minimal alternative that is customized more towards my needs and desires than anything else.&lt;&#x2f;p&gt;
" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-11-29T23:22:44.521902918+00:00" />
<meta property="book:release_date" content="2019-12-01T18:55:05.215513238+00:00" />
</head>
<body class="toc">
<header>
<h1>The mkbook Book</h1>
<h2>by Kenton Hamaluik</h2>
<time datetime="2019-11-29T23:22:44.521902918+00:00">Nov 29, 2019</time>
<time datetime="2019-12-01T18:55:05.215513238+00:00">Dec 1, 2019</time>
</header>
<article>
<p><em>mkbook</em> is my simpler alternative to <a href="https://crates.io/crates/mdbook">mdbook</a> which is a great tool, but for which I really dislike some of the decisions they took, such as relying on javascript for highlighting and navigation, and including a lot of bells and whistles such as javascript-based search.</p>
@ -47,6 +48,8 @@
<li><a href="02-markdown/03-plantuml-diagrams.html">PlantUML Diagrams</a></li>
<li><a href="02-markdown/04-katex-formulas.html">KaTeX (Math) Formulas</a></li>
</ol>
</li>

@ -72,18 +72,60 @@ mod filters;
use models::frontmatter::{ParsedFrontMatter, FrontMatter};
use models::chapter::{Chapter};
fn create_katex_inline(src: &str) -> Result<String, Box<dyn std::error::Error>> {
use std::process::{Command, Stdio};
use io::Write;
let mut child = match Command::new("katex")
.arg("-d")
.arg("-t")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn() {
Ok(c) => c,
Err(e) => {
eprintln!("[WARNING] failed to launch katex, not rendering math block: {:?}", e);
return Ok(format_code("", src)?.output);
}
};
let stdin = child.stdin.as_mut().expect("valid katex stdin");
stdin.write_all(src.as_ref())?;
let output = child.wait_with_output()?;
if !output.status.success() {
eprintln!("failed to generate katex, exit code: {:?}", output.status.code());
eprintln!("katex STDOUT:");
eprintln!("{}", String::from_utf8_lossy(output.stdout.as_ref()));
eprintln!("katex STDERR:");
eprintln!("{}", String::from_utf8_lossy(output.stdout.as_ref()));
eprintln!("/katex output");
return Ok(format_code("", src)?.output);
}
let rendered: String = String::from_utf8(output.stdout)?;
Ok(format!(r#"<figure class="math">{}</figure>"#, rendered))
}
fn create_plantuml_svg(src: &str) -> Result<String, Box<dyn std::error::Error>> {
use std::process::{Command, Stdio};
use io::Write;
let mut child = Command::new("plantuml")
let mut child = match Command::new("plantuml")
.arg("-tsvg")
.arg("-nometadata")
.arg("-pipe")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
.spawn() {
Ok(c) => c,
Err(e) => {
eprintln!("[WARNING] failed to launch plantuml, not rendering plantuml block: {:?}", e);
return Ok(format_code("", src)?.output);
}
};
let stdin = child.stdin.as_mut().expect("valid plantuml stdin");
stdin.write_all(src.as_ref())?;
@ -96,7 +138,7 @@ fn create_plantuml_svg(src: &str) -> Result<String, Box<dyn std::error::Error>>
eprintln!("plantuml STDERR:");
eprintln!("{}", String::from_utf8_lossy(output.stdout.as_ref()));
eprintln!("/plantuml output");
return format_code("", src);
return Ok(format_code("", src)?.output);
}
let svg: String = String::from_utf8(output.stdout)?;
let svg = svg.replace(r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#, "");
@ -104,13 +146,28 @@ fn create_plantuml_svg(src: &str) -> Result<String, Box<dyn std::error::Error>>
Ok(format!("<figure>{}</figure>", svg))
}
fn format_code(lang: &str, src: &str) -> Result<String, Box<dyn std::error::Error>> {
struct FormatResponse {
output: String,
include_katex_css: bool,
}
fn format_code(lang: &str, src: &str) -> Result<FormatResponse, Box<dyn std::error::Error>> {
use syntect::parsing::SyntaxReference;
use syntect::html::highlighted_html_for_string;
// render plantuml code blocks into an inline svg
if lang == "plantuml" {
return create_plantuml_svg(src);
return Ok(FormatResponse {
output: create_plantuml_svg(src)?,
include_katex_css: false,
});
}
// render katex code blocks into an inline math
if lang == "katex" {
return Ok(FormatResponse {
output: create_katex_inline(src)?,
include_katex_css: true,
});
}
let syntax: Option<&SyntaxReference> = if lang.len() > 0 {
@ -127,7 +184,10 @@ fn format_code(lang: &str, src: &str) -> Result<String, Box<dyn std::error::Erro
let html = highlighted_html_for_string(src, &HIGHLIGHT_SYNTAX_SETS, &syntax, &HIGHLIGHT_THEME);
Ok(html)
Ok(FormatResponse {
output: html,
include_katex_css: false,
})
}
fn extract_frontmatter(src: &str) -> Result<(Option<ParsedFrontMatter>, String), Box<dyn std::error::Error>> {
@ -162,7 +222,7 @@ fn extract_frontmatter(src: &str) -> Result<(Option<ParsedFrontMatter>, String),
}
}
fn format_markdown(src: &str) -> Result<String, Box<dyn std::error::Error>> {
fn format_markdown(src: &str) -> Result<FormatResponse, Box<dyn std::error::Error>> {
use comrak::{Arena, parse_document, format_html};
use comrak::nodes::{AstNode, NodeValue};
@ -173,8 +233,8 @@ fn format_markdown(src: &str) -> Result<String, Box<dyn std::error::Error>> {
src,
&COMRAK_OPTIONS);
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) -> Result<(), Box<dyn std::error::Error>>
where F : Fn(&'a AstNode<'a>) -> Result<(), Box<dyn std::error::Error>> {
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &mut F) -> Result<(), Box<dyn std::error::Error>>
where F : FnMut(&'a AstNode<'a>) -> Result<(), Box<dyn std::error::Error>> {
f(node)?;
for c in node.children() {
iter_nodes(c, f)?;
@ -182,13 +242,17 @@ fn format_markdown(src: &str) -> Result<String, Box<dyn std::error::Error>> {
Ok(())
}
iter_nodes(root, &|node| {
let mut use_katex_css = false;
iter_nodes(root, &mut |node| {
let value = &mut node.data.borrow_mut().value;
if let NodeValue::CodeBlock(ref block) = value {
let lang = String::from_utf8(block.info.clone()).expect("code lang is utf-8");
let source = String::from_utf8(block.literal.clone()).expect("source code is utf-8");
let highlighted: String = format_code(&lang, &source)?;
let highlighted: Vec<u8> = Vec::from(highlighted.into_bytes());
let FormatResponse { output, include_katex_css } = format_code(&lang, &source)?;
if include_katex_css {
use_katex_css = true;
}
let highlighted: Vec<u8> = Vec::from(output.into_bytes());
*value = NodeValue::HtmlBlock(comrak::nodes::NodeHtmlBlock {
literal: highlighted,
@ -201,7 +265,10 @@ fn format_markdown(src: &str) -> Result<String, Box<dyn std::error::Error>> {
let mut output: Vec<u8> = Vec::with_capacity((src.len() as f64 * 1.2) as usize);
format_html(root, &COMRAK_OPTIONS, &mut output).expect("can format HTML");
let output = String::from_utf8(output).expect("valid utf-8 generated HTML");
Ok(output)
Ok(FormatResponse {
output,
include_katex_css: use_katex_css,
})
}
#[derive(Template)]
@ -210,14 +277,16 @@ struct IndexTemplate<'a, 'b, 'c> {
book: &'a FrontMatter,
chapters: &'b Vec<Chapter>,
book_description: &'c str,
include_katex_css: bool,
}
fn generate_index<W: io::Write>(book: &FrontMatter, content: String, chapters: &Vec<Chapter>, mut output: W) -> Result<(), Box<dyn std::error::Error>> {
fn generate_index<W: io::Write>(book: &FrontMatter, content: String, include_katex_css: bool, chapters: &Vec<Chapter>, mut output: W) -> Result<(), Box<dyn std::error::Error>> {
// fill out our template
let template = IndexTemplate {
book,
chapters,
book_description: &content,
include_katex_css,
};
// and render!
@ -236,9 +305,10 @@ struct PageTemplate<'a, 'b, 'c, 'd, 'e, 'g> {
prev_chapter: Option<&'d Chapter>,
next_chapter: Option<&'e Chapter>,
book: &'g FrontMatter,
include_katex_css: bool,
}
fn format_page<W: io::Write>(book: &FrontMatter, chapter: &Chapter, chapters: &Vec<Chapter>, prev_chapter: Option<&Chapter>, next_chapter: Option<&Chapter>, content: &str, mut output: W) -> Result<(), Box<dyn std::error::Error>> {
fn format_page<W: io::Write>(book: &FrontMatter, chapter: &Chapter, chapters: &Vec<Chapter>, prev_chapter: Option<&Chapter>, next_chapter: Option<&Chapter>, content: &str, include_katex_css: bool, mut output: W) -> Result<(), Box<dyn std::error::Error>> {
// fill out our template
let template = PageTemplate {
chapter,
@ -247,6 +317,7 @@ fn format_page<W: io::Write>(book: &FrontMatter, chapter: &Chapter, chapters: &V
prev_chapter,
next_chapter,
book,
include_katex_css,
};
// and render!
@ -301,7 +372,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
(None, content)
};
let book_front = FrontMatter::from_root(book_front.unwrap_or_default());
let book_description = format_markdown(&book_description)?;
let FormatResponse { output, include_katex_css } = format_markdown(&book_description)?;
let book_description = output;
// load all our chapters
let mut chapters: Vec<Chapter> = Vec::default();
@ -385,7 +457,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let index_out_path = dest.join("index.html");
let index_out = fs::File::create(&index_out_path)?;
let index_out = io::BufWriter::new(index_out);
generate_index(&book_front, book_description, &chapters, index_out)?;
generate_index(&book_front, book_description, include_katex_css, &chapters, index_out)?;
println!("Rendered index into `{}`", index_out_path.display());
// compile markdown and write the actual pages
@ -400,7 +472,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let outfile = fs::File::create(&out)?;
let outfile = io::BufWriter::new(outfile);
let contents = format_markdown(&chapter.contents)?;
let FormatResponse { output, include_katex_css } = format_markdown(&chapter.contents)?;
let next_chapter =
if chapter.sections.len() > 0 {
@ -413,7 +485,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
None
};
format_page(&book_front, &chapter, &chapters, prev_chapter, next_chapter, &contents, outfile)?;
format_page(&book_front, &chapter, &chapters, prev_chapter, next_chapter, &output, include_katex_css, outfile)?;
prev_chapter = Some(chapter);
println!(" done!");
@ -427,7 +499,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let outfile = fs::File::create(&out)?;
let outfile = io::BufWriter::new(outfile);
let contents = format_markdown(&section.contents)?;
let FormatResponse { output, include_katex_css } = format_markdown(&section.contents)?;
let next_chapter = if section_index < chapter.sections.len() - 1 {
Some(chapter.sections.iter().nth(section_index + 1).expect("chapter n+1 exists"))
@ -439,7 +511,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
None
};
format_page(&book_front, &section, &chapters, prev_chapter, next_chapter, &contents, outfile)?;
format_page(&book_front, &section, &chapters, prev_chapter, next_chapter, &output, include_katex_css, outfile)?;
prev_chapter = Some(section);
println!(" done!");

@ -13,6 +13,7 @@
<meta property="og:description" content="{{ book_description }}" />
<meta property="book:author" content="{{ book.author }}" />
<meta property="book:release_date" content="{{ book.pubdate|rfc3339_utc }}" />
{% if include_katex_css %}<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">{% endif %}
</head>
<body class="toc">
<header>

@ -13,6 +13,7 @@
{#<meta property="og:description" content="{{ book.description }}" />#}
<meta property="book:author" content="{{ book.author }}" />
<meta property="book:release_date" content="{{ book.pubdate|rfc3339_utc }}" />
{% if include_katex_css %}<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">{% endif %}
</head>
<body>
<nav class="big">

Loading…
Cancel
Save