For the imgui-rs project am an playing with an idea:
In the mdbook markdown, you could include example snippets, for example
```imgui-example(200x100)
if ui.button("Example button") {
println!("Button was clicked");
}
```
Then in the resulting documentation, a snippet is displayed and the code is also wrapped in some supporting code and executed, which using a a software-renderer can generated an "automatic screenshot"
The idea being to show both the code snippet and what the resulting GUI would look like, without the tediousness of manually capturing screenshots (which might become outdated, be inconsistent, or simply not captured)
Half of this is quite easy to do: create an mdbook preprocessor to extract the code blocks. It can output a new code-block and an image.
Part I'm stuck with is how best to take that code snippet and actually compile it - the approaches I've half-thought of are:
- Generate a Cargo project in a temp folder, pull each snippet into an
examples/...
folder, then invokecargo run --example snippet1 --output .../snippet1.png
in the preprocessor - In the mdbook preprocessor, (mis)use the
build.rs
to pull in the example snippets and build them. In the preprocessor itself, use these preprocessed examples to output the relevant markdown
The former is trickier to get right - generating the cargo project so it links to the current working-copy of the imgui crate, invoking cargo run
properly so it picks up any toolchain overrides or whatever. I think the latter idea solves that but I'm not sure if build.rs
is flexible enough to reasonably do this, or.. if there is a better approach?
I feel like something like this has most certainly been done, but I'm not sure in which direction to search for ideas!
Thanks!
- Ben