Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example decorators #566

Merged
merged 10 commits into from
Oct 8, 2024
Merged

Example decorators #566

merged 10 commits into from
Oct 8, 2024

Conversation

Bzero
Copy link
Contributor

@Bzero Bzero commented Sep 15, 2024

This PR resolves #503, implements the @example decorator and adds basic examples to most standard library functions.

The @example decorator takes the example code as the first positional argument and optionally an example description as second argument. Multiple examples can be defined by using @example multiple times:

@example("example_function(0)")
@example("example_function(1)", "Evaluate the function at $1$.")
fn example_function(x) = x

At the moment the examples are only used to compile the documentation, however it might be nice to make them accessible in the CLI or web interface in the future. In the documentation the example code is run trough the Numbat interpreter and is shown together with the results as if it was typed in the CLI interface. Additionally the example description is displayed (if available) and a "Run this example" link is shown. Since the examples can be a bit lengthy, I chose to display them collapsed by default.

I have not added any examples to non pure functions like random(), or now() for the moment. While this would work in principle, it would modify the documentation every time someone re-runs book/build.py which doesn't feel very practical.

@Bzero Bzero force-pushed the example_decorator branch 2 times, most recently from 1ce5be4 to 054b64e Compare September 15, 2024 18:26
numbat/examples/inspect.rs Outdated Show resolved Hide resolved
Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, as always! Thank you. It might take a while (> 2 weeks) before I can do a full review.

numbat/examples/inspect.rs Outdated Show resolved Hide resolved
@Bzero
Copy link
Contributor Author

Bzero commented Sep 16, 2024

Of course, take your time!

numbat/examples/inspect.rs Outdated Show resolved Hide resolved
numbat/modules/core/functions.nbt Outdated Show resolved Hide resolved
numbat/modules/core/lists.nbt Outdated Show resolved Hide resolved
numbat/modules/core/lists.nbt Outdated Show resolved Hide resolved
@sharkdp
Copy link
Owner

sharkdp commented Sep 24, 2024

Thank you for the update. Will continue my review over the next days.

This is great!

Comment on lines 140 to 152
//Print the example
if let Some(example_description) = example_description {
println!(
"* {}\n\n <a href=\"{}\"><i class=\"fa fa-play\"></i> Run this example</a>",
replace_equation_delimiters(example_description),
example_url
);
} else {
println!(
"* <a href=\"{}\"><i class=\"fa fa-play\"></i> Run this example</a>\n",
example_url
);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please move the "Run this example" links below the code snippets? I think that would look better because the "play" icons wouldn't line up with the bullets. And most of the time, users will only read the example output instead of trying it interactively.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that is fine for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, after trying it out I think it doesn't look exactly nice with the "Run this example" links below the code snippets either, at least when there is no example description. Maybe we should not use a bullet list here at all.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the display and tried to mimic the mdBook Rust Playground feature "play button":

image

The implementation is a little hacky as it requires to insert html code blocks in the markdown documents, however since they are entirely autogenerated I think it should be okay.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice! Thank you 👍

numbat/examples/inspect.rs Outdated Show resolved Hide resolved
numbat/examples/inspect.rs Outdated Show resolved Hide resolved
Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I finished my review. Again: thank you very much for implementing this — and adding examples to all existing functions!

@Bzero
Copy link
Contributor Author

Bzero commented Sep 28, 2024

I finished my review. Again: thank you very much for implementing this — and adding examples to all existing functions!

Thanks a lot for the review.

@sharkdp sharkdp merged commit 11c4d86 into sharkdp:master Oct 8, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add @example decorator
2 participants