A mdbook preprocessor that allows the re-usability of template files with dynamic arguments
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
Go to file
sgoudham 29037ac545
[TEM #2] - Update README.md
2 years ago
.github [v0.1.0] - Add deploy.yml script 2 years ago
examples/readme [v0.1.0] - Add readme-example 2 years ago
src [TEM #2] - Add FileReader trait for testing 2 years ago
.gitignore Update .gitignore 2 years ago
CONTRIBUTING.md Add CONTRIBUTING.md 2 years ago
Cargo.toml [TEM #1] - Remove dependency on aha-corasick 2 years ago
LICENSE Initial commit 2 years ago
README.md [TEM #2] - Update README.md 2 years ago

README.md

mdbook-template

build crate.io downloads license

A mdbook preprocessor that allows the re-usability of template files with dynamic arguments

Table of Contents

Author Notes

I'm still a beginner in terms of my Rust skills, so I'm definitely... probably sure that there are edge cases within this preprocessor.

Installation

Install Through Cargo

$ cargo install mdbook-template

Add the following line into your book.toml

[preprocessor.template]

You're good to go :D Continue building your mdbook normally!

$ mdbook build

About

Given the following directory structure

book.toml
src
├── rust.md
├── go.md
├── friends
│   └── hazel.md
├── images
│   ├── ferris.png
│   └── corro.png
└── SUMMARY.md

If we wanted to include the images ferris.png and corro.png within all the files through a footer, we'd have to copy the same piece of markdown/code in every file and set a unique path back to the images/ directory.

This is where mdbook-template can help with the introduction of {{#template ...}.

Being based on the {{#include ... }} feature of mdbook, mdbook-template allows you to use familiar syntax to include files while passing in arguments to allow for dynamic generation of text.

Please view the given example which demonstrates it in action.

Format

The format is as follows

        1             2           3
    {{#template     <file>      <args>}}
  1. The identifier that this text should be replaced
  2. The relative path to the template file
  3. Any arguments that should be substituted within the template file. Arguments should be seperated by whitespace and should be in the key=value format.

Arguments

Arguments to be replaced within the template files should be wrapped in {{$ ...}}

Default Values

Valid Configurations

Template

{{#template file.txt path=../images author=Goudham}}
{{#template
    file.txt
    path=../images
    author=Goudham
}}
// _Not recommended but valid_
{{#template     file.txt   path=../images author=Goudham}}
// _Not recommended but valid_
{{#template
file.txt
        path=../images
    author=Goudham
}}

Arguments

\[[#escaped]]
[[#width]]
[[#width 200px]]
// _Not recommended but valid_
[[   #width   400px   ]]

Example

Given the following directory

book.toml
src
├── rust.md
├── go.md
├── friends
│   └── hazel.md
├── images
│   ├── ferris.png
│   └── corro.png
├── templates
│   └── footer.md
└── SUMMARY.md

and the following content

templates/footer.md

-- Designed By [[#authors]] --
![ferris]([[#path]]/ferris.png)
![corro]([[#path]]/corro.png)

rust.md

# Rust

Some Content...

{{#template templates/footer.md authors=Goudham, Hazel path=images}}

go.md

# Go

Some Content...

{{#template templates/footer.md path=images authors=Goudham, Hazel}}

friends/hazel.md

# Hazel

Some Content...

{{#template
    ../templates/footer.md
    path=../images
    authors=Goudham, Hazel
}}

After running mdbook build with the mdbook-template preprocessor enabled, the files will have dynamic paths to the images and contain identical content.

rust.md

# Rust

Some Content...

-- Designed By Goudham, Hazel --
![ferris](images/ferris.png)
![corro](images/corro.png)

go.md

# Go

Some Content...

-- Designed By Goudham, Hazel --
![ferris](images/ferris.png)
![corro](images/corro.png)

friends/hazel.md

# Hazel

Some Content...

-- Designed By Goudham, Hazel --
![ferris](../images/ferris.png)
![corro](../images/corro.png)

Further examples are included within the examples directory which demonstrate a variety of usages.

GitHub Actions

TODO

License

MIT License

Contributing

First, thanks for your interest in contributing to this project! Please read the CONTRIBUTING.md before contributing!

Acknowledgement

This preprocessor is heavily based off the links.rs file within mdbook itself. I definitely wouldn't have been able to mock up something like this without the strong foundations that mdbook already implemented.