added assets copying, fixed figures some more

master v0.3.0
Kenton Hamaluik 5 years ago
parent d858a51a34
commit e1dafa7f6f

@ -16,7 +16,7 @@ _mkbook_ may be installed using _Cargo_ (`cargo install --force --path .` in the
```sh
$ mkbook
mkbook 0.2.0
mkbook 0.3.0
Kenton Hamaluik <kenton@hamaluik.ca>

@ -2,6 +2,8 @@
title = "PlantUML Diagrams"
---
# PlantUML Diagrams
If you have [PlantUML](http://plantuml.com/) installed and available on your path, _mkbook_ will try to render any code blocks with a language tag of `plantuml` as inline SVG images.
For example:

@ -2,6 +2,8 @@
title = "KaTeX (Math) Formulas"
---
# 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:

@ -2,10 +2,12 @@
title = "Images"
---
# Images
To include an image, use the standard markdown format:
```md
![title](url)
![alt](url "title")
```
This will wrap the image in a `figure` with an associated `figcaption` containing the title of the image, as so:

@ -61,7 +61,14 @@ desires than anything else.
## Assets
The current version of _mkbook_ doesn't copy any assets into your book—it only parses `.md` files and generates `.html` files. So if you want to include images or other assets, you're on your own. Support for assets is planned for the next minor release.
Any files in the `src` directory which are not included in `.gitignore` and do not end in the extension `.md` will be copied to the output folder. You can use this to include images, files, etc, for example the following image is an asset bundled with the book:
```md
![chapter-six](book-chapter-six-5834.jpg "Photo by Kaboompics.com from Pexels")
```
![chapter-six](../book-chapter-six-5834.jpg "Photo by Kaboompics.com from Pexels")
## Documents
@ -74,6 +81,7 @@ src/
├── mkbook.toml
├── 00-foreword.md
├── 01-introduction.md
├── my-picture.jpg
└── 02-my-first-chapter
├── index.md
├── 01-my-first-section.md

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>
@ -107,7 +107,7 @@
<p><em>mkbook</em> may be installed using <em>Cargo</em> (<code>cargo install --force --path .</code> in the <em>mkbook</em> repo directory), and after that it presents a command-line interface:</p>
<pre style="background-color:#2d2d2d;">
<span style="color:#6699cc;">$</span><span style="color:#d3d0c8;"> mkbook
</span><span style="color:#6699cc;">mkbook</span><span style="color:#d3d0c8;"> 0.2.0
</span><span style="color:#6699cc;">mkbook</span><span style="color:#d3d0c8;"> 0.3.0
</span><span style="color:#6699cc;">Kenton</span><span style="color:#d3d0c8;"> Hamaluik &lt;kenton@hamaluik.ca&gt;
</span><span style="color:#d3d0c8;">
</span><span style="color:#d3d0c8;">

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>
@ -107,7 +107,8 @@
</span>
</nav>
<article>
<p>If you have <a href="http://plantuml.com/">PlantUML</a> installed and available on your path, <em>mkbook</em> will try to render any code blocks with a language tag of <code>plantuml</code> as inline SVG images.</p>
<h1><a href="#plantuml-diagrams" aria-hidden="true" class="anchor" id="headerplantuml-diagrams"></a>PlantUML Diagrams</h1>
<p>If you have <a href="http://plantuml.com/">PlantUML</a> installed and available on your path, <em>mkbook</em> will try to render any code blocks with a language tag of <code>plantuml</code> as inline SVG images.</p>
<p>For example:</p>
<pre style="background-color:#2d2d2d;">
<span style="color:#d3d0c8;">```plantuml

@ -12,7 +12,7 @@
<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-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+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>
@ -107,7 +107,8 @@
</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>
<h1><a href="#katex-math-formulas" aria-hidden="true" class="anchor" id="headerkatex-math-formulas"></a>KaTeX (Math) Formulas</h1>
<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

@ -12,7 +12,7 @@
<meta property="og:url" content="https://hamaluik.github.io/mkbook//02-markdown/05-images.html" />
<meta property="book:author" content="Kenton Hamaluik" />
<meta property="book:release_date" content="2019-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>
@ -107,9 +107,10 @@
</span>
</nav>
<article>
<p>To include an image, use the standard markdown format:</p>
<h1><a href="#images" aria-hidden="true" class="anchor" id="headerimages"></a>Images</h1>
<p>To include an image, use the standard markdown format:</p>
<pre style="background-color:#2d2d2d;">
<span style="color:#f99157;">![title](url)
<span style="color:#f99157;">![alt](url </span><span style="color:#d3d0c8;">&quot;</span><span style="color:#f2777a;">title</span><span style="color:#d3d0c8;">&quot;</span><span style="color:#f99157;">)
</span></pre>
<p>This will wrap the image in a <code>figure</code> with an associated <code>figcaption</code> containing the title of the image, as so:</p>
<pre style="background-color:#2d2d2d;">

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>
@ -169,7 +169,11 @@
</dd>
</dl>
<h2><a href="#assets" aria-hidden="true" class="anchor" id="headerassets"></a>Assets</h2>
<p>The current version of <em>mkbook</em> doesnt copy any assets into your book—it only parses <code>.md</code> files and generates <code>.html</code> files. So if you want to include images or other assets, youre on your own. Support for assets is planned for the next minor release.</p>
<p>Any files in the <code>src</code> directory which are not included in <code>.gitignore</code> and do not end in the extension <code>.md</code> will be copied to the output folder. You can use this to include images, files, etc, for example the following image is an asset bundled with the book:</p>
<pre style="background-color:#2d2d2d;">
<span style="color:#f99157;">![chapter-six](book-chapter-six-5834.jpg </span><span style="color:#d3d0c8;">&quot;</span><span style="color:#f2777a;">Photo by Kaboompics.com from Pexels</span><span style="color:#d3d0c8;">&quot;</span><span style="color:#f99157;">)
</span></pre>
<figure><img src="../book-chapter-six-5834.jpg" alt="chapter-six" title="Photo by Kaboompics.com from Pexels"><figcaption>Photo by Kaboompics.com from Pexels</figcaption></figure>
<h2><a href="#documents" aria-hidden="true" class="anchor" id="headerdocuments"></a>Documents</h2>
<p><em>mkbook</em> works on mostly a flat directory structure, however one level of sub-directories are supported in order to create sections within chapters. Files that dont end in a <code>.md</code> extension are completely ignored. Each <code>.md</code> file in the root source directly is its own chapter. To create chapters with sub-sections, create a sub-directory in the root directory and then create a <code>README.md</code> file, which will become the root of the chapter, with all <code>.md</code> files in the sub-directory becoming sections in the chapter. The <code>title</code> in the <code>README.md</code> files frontmatter will be used as the name of the chapter.</p>
<p>The order of the book is based on the alphabetical order of the file names (actually its based on Rusts <a href="https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html#impl-PartialOrd%3Cstr%3E">implementation of <code>PartialOrd</code> for str</a>). Thus, it is recommended to lay out your book chapters with manual numbering of the file names, as such:</p>
@ -178,6 +182,7 @@
</span><span style="color:#d3d0c8;">├── mkbook.toml
</span><span style="color:#d3d0c8;">├── 00-foreword.md
</span><span style="color:#d3d0c8;">├── 01-introduction.md
</span><span style="color:#d3d0c8;">├── my-picture.jpg
</span><span style="color:#d3d0c8;">└── 02-my-first-chapter
</span><span style="color:#d3d0c8;"> ├── index.md
</span><span style="color:#d3d0c8;"> ├── 01-my-first-section.md

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>

@ -12,7 +12,7 @@
<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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

@ -14,14 +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-12-03T17:46:22.953768196+00:00" />
<meta property="book:release_date" content="2019-12-03T18:23:45.870161898+00:00" />
</head>
<body class="toc">
<header>
<h1>The mkbook Book</h1>
<h2>by Kenton Hamaluik</h2>
<time datetime="2019-12-03T17:46:22.953768196+00:00">Dec 3, 2019</time>
<time datetime="2019-12-03T18:23:45.870161898+00:00">Dec 3, 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>

File diff suppressed because one or more lines are too long

@ -272,17 +272,18 @@ fn format_markdown(src: &str) -> Result<FormatResponse, Box<dyn std::error::Erro
let first_child = &node.first_child().unwrap();
let first_value = &first_child.data.borrow().value;
if let NodeValue::Image(link) = first_value {
if first_child.children().count() == 1 {
let second_child = &first_child.first_child().unwrap();
let second_value = &second_child.data.borrow().value;
if let NodeValue::Text(t) = second_value {
let alt = String::from_utf8_lossy(&t);
let figure = wrap_image_in_figure(&link, &alt)?;
let figure: Vec<u8> = Vec::from(figure.into_bytes());
second_child.detach();
first_child.detach();
*value = NodeValue::HtmlInline(figure);
if first_child.children().count() > 0 {
let mut alt: String = String::default();
for child in first_child.children() {
if let NodeValue::Text(t) = &child.data.borrow().value {
alt.push_str(&String::from_utf8_lossy(&t));
}
child.detach();
}
first_child.detach();
let figure = wrap_image_in_figure(&link, &alt)?;
let figure: Vec<u8> = Vec::from(figure.into_bytes());
*value = NodeValue::HtmlInline(figure);
}
}
}
@ -376,7 +377,10 @@ fn format_page<W: io::Write>(book: &FrontMatter, chapter: &Chapter, chapters: &V
fn build<PIn: AsRef<Path>, POut: AsRef<Path>>(src: PIn, dest: POut, include_reload_script: bool) -> Result<(), Box<dyn std::error::Error>> {
let src = PathBuf::from(src.as_ref());
let dest = PathBuf::from(dest.as_ref());
std::fs::create_dir_all(&dest)?;
if !dest.exists() {
std::fs::create_dir_all(&dest)?;
log::info!("created directory `{}`...", dest.display());
}
// load our book
let book_readme_path = src.join("README.md");
@ -533,7 +537,31 @@ fn build<PIn: AsRef<Path>, POut: AsRef<Path>>(src: PIn, dest: POut, include_relo
}
}
// save the assets
// copy the assets
for entry in ignore::Walk::new(&src) {
let entry = entry?;
if let Some(t) = entry.file_type() {
if t.is_file() {
if let Some("md") = entry.path().extension().map(std::ffi::OsStr::to_str).flatten() {
// ignore markdown files
}
else {
// we found an asset to copy!
let dest_path: PathBuf = dest.join(entry.path().iter().skip(1).map(PathBuf::from).collect::<PathBuf>());
if let Some(parent) = dest_path.parent() {
if !parent.exists() {
fs::create_dir_all(parent)?;
log::info!("created directory `{}`...", parent.display());
}
}
fs::copy(entry.path(), &dest_path)?;
log::info!("Copied `{}` to `{}`...", entry.path().display(), dest_path.display());
}
}
}
}
// save the built-in assets
fs::write(dest.join("style.css"), STYLESHEET)?;
log::info!("Wrote {}", dest.join("style.css").display());
fs::write(dest.join("favicon.ico"), ASSET_FAVICON)?;

@ -40,7 +40,7 @@ figure {
figcaption {
display: block;
font-size: 0.75rem;
font-size: 0.9rem;
text-align: center;
}
}

@ -9,6 +9,7 @@ body {
grid-template-rows: 1fr;
justify-items: stretch;
align-items: stretch;
position: relative;
nav.big {
background: #2c2c38;
@ -94,6 +95,10 @@ body {
justify-content: space-between;
background: #2c2c38;
padding: 0;
position: fixed;
top: 0;
left: 0;
right: 0;
>* {
margin: 0.5rem;
@ -197,6 +202,7 @@ body {
}
article {
margin-top: 2rem;
padding: 1rem 0.5rem 0 0.5rem;
}
}

Loading…
Cancel
Save