-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
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 @@ | ||
book | ||
target | ||
.icebergs |
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,9 @@ | ||
[book] | ||
authors = ["Héctor Ramón Jiménez"] | ||
language = "en" | ||
multilingual = false | ||
src = "src" | ||
title = "mdbook-iced" | ||
|
||
[preprocessor.iced] | ||
rev = "441e9237cd1c9c9b61d9b144b5b4dafa236ace28" |
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 @@ | ||
# Summary | ||
|
||
- [Guide](./guide.md) |
File renamed without changes
File renamed without changes
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,152 @@ | ||
<div align="center"> | ||
|
||
<img src="assets/logo.svg" height="150"> | ||
|
||
# mdbook-iced | ||
|
||
[](https://docs.rs/mdbook-iced) | ||
[](https://crates.io/crates/mdbook-iced) | ||
[](https://github.com/iced-rs/mdbook-iced/blob/master/LICENSE) | ||
[](https://crates.io/crates/mdbook-iced) | ||
[](https://github.com/iced-rs/mdbook-iced/actions) | ||
[](https://discourse.iced.rs/) | ||
[](https://discord.gg/3xZJ65GAhd) | ||
|
||
An mdBook preprocessor to turn [iced] code blocks into interactive examples. | ||
|
||
<img alt="An interactive example" src="assets/example.gif"> | ||
|
||
</div> | ||
|
||
## Overview | ||
This is a simple mdBook preprocessor that can add a play button to any [iced] code block. | ||
|
||
Pressing the play button loads and embeds the resulting Wasm application in a fully interactive `<canvas>` right under the code block. | ||
|
||
It is compatible with any code block that features a `main` function where an [iced] program is run—even if it is hidden! This means | ||
it can be used to create interactive examples even for books completely unrelated to [iced]. | ||
|
||
Currently, this preprocessor is mainly being used in [the official guide] to [iced]. Check it out! | ||
|
||
## Installation | ||
Install the `mdbook-iced` preprocessor and the [`wasm-bindgen-cli`] tool with `cargo install`: | ||
|
||
``` | ||
cargo install mdbook-iced wasm-bindgen-cli | ||
``` | ||
|
||
Also, make sure your toolchain has the `wasm32-unknown-unknown` target: | ||
|
||
``` | ||
rustup target add wasm32-unknown-unknown | ||
``` | ||
|
||
## Usage | ||
Add a `[preprocessor.iced]` entry to your `book.toml` and specify the revision of `iced` your book will use: | ||
|
||
```toml | ||
[preprocessor.iced] | ||
# You can use a commit hash | ||
rev = "9db6ac8f202ebdc1453edee01da0b30aee0949d8" | ||
# ... a branch | ||
branch = "master" | ||
# ... or a tag! | ||
tag = "0.13.0" # Not yet released! | ||
``` | ||
|
||
Then, simply add an `iced` label to any executable code block you want to make playable. For instance, the | ||
classical counter: | ||
|
||
````markdown | ||
# A cool example | ||
|
||
This is an mdBook and here is an iced counter: | ||
|
||
```rust,ignore,iced | ||
# use iced::widget::{button, column, text, Column}; | ||
# | ||
pub fn main() -> iced::Result { | ||
iced::run("A counter", update, view) | ||
} | ||
# | ||
# #[derive(Debug, Clone)] | ||
# enum Message { | ||
# Increment, | ||
# } | ||
# | ||
# fn update(value: &mut u64, message: Message) { | ||
# match message { | ||
# Message::Increment => *value += 1, | ||
# } | ||
# } | ||
# | ||
# fn view(value: &u64) -> Column<Message> { | ||
# column![ | ||
# text(value), | ||
# button("+").on_press(Message::Increment), | ||
# ] | ||
# } | ||
``` | ||
```` | ||
|
||
This produces the following code block: | ||
|
||
```rust,ignore,iced | ||
# use iced::widget::{button, column, text, Column}; | ||
# | ||
pub fn main() -> iced::Result { | ||
iced::run("A counter", update, view) | ||
} | ||
# | ||
# #[derive(Debug, Clone)] | ||
# enum Message { | ||
# Increment, | ||
# } | ||
# | ||
# fn update(value: &mut u64, message: Message) { | ||
# match message { | ||
# Message::Increment => *value += 1, | ||
# } | ||
# } | ||
# | ||
# fn view(value: &u64) -> Column<Message> { | ||
# column![ | ||
# text(value), | ||
# button("+").on_press(Message::Increment), | ||
# ] | ||
# } | ||
``` | ||
|
||
You can control the height of the embedded application by using `iced(height=<CSS height>)` as a label (e.g. `iced(height=100px)`). | ||
For instance: | ||
|
||
```rust,ignore,iced(height=100px) | ||
# use iced::widget::{button, column, text, Column}; | ||
# | ||
pub fn main() -> iced::Result { | ||
iced::run("A counter", update, view) | ||
} | ||
# | ||
# #[derive(Debug, Clone)] | ||
# enum Message { | ||
# Decrement, | ||
# } | ||
# | ||
# fn update(value: &mut u64, message: Message) { | ||
# match message { | ||
# Message::Decrement => *value -= 1, | ||
# } | ||
# } | ||
# | ||
# fn view(value: &u64) -> Column<Message> { | ||
# column![ | ||
# text(value), | ||
# button("-").on_press(Message::Decrement), | ||
# ] | ||
# } | ||
``` | ||
|
||
[iced]: https://github.com/iced-rs/iced | ||
[`wasm-bindgen-cli`]: https://rustwasm.github.io/wasm-bindgen/reference/cli.html | ||
[the official guide]: https://book.iced.rs/ | ||
|