Skip to content

Commit

Permalink
Generate Zola Rust code block hidden lines annotations in the book (#922
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TrialDragon authored Feb 6, 2024
1 parent a5dc026 commit ff940d6
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 59 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ These may be appropriate to link in "next steps" however at the end of the examp
9. If additional code block attributes like `no_run` or `hide-lines=x-y` need to be specified, you should always order these so that the language is the last attribute. If we would specify `rust,no_run` the syntax highlighting wouldn't work, but changing it to `no_run,rust` makes it work.
10. To validate if local code changes are compiling you can `cd` into the `code-validation` folder and test your code using `cargo test`.
11. To make sure your markdown files are formatted correctly run `markdownlint -f -c .github/linters/.markdown-lint.yml .` in the root directory of your local Bevy website repository.
12. To reference Rust API docs you can use markdown's reference-style links like so:
12. To hide lines of code in Zola Rust code blocks of the book you should: a. Mark each line you wish to hide with a `#` with an empty space afterwards like `# //...line_of_code_here...` although you were hiding lines in rustdoc. b. Run the [utility tool](write-rustdoc-hide-lines) in `<ROOT_OF_WEBSITE>/write_rustdoc-hide-lines/` named `write_rustdoc_hide_lines.sh`.
13. To reference Rust API docs you can use markdown's reference-style links like so:
[`HashMap`]

```md
Expand Down
18 changes: 6 additions & 12 deletions content/learn/book/ecs/entities-components/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ Once entities exist, they can likewise be despawned, deleting all of the data st
Spawning and despawning entities can have far-reaching effects, and so cannot be done immediately (unless you are using an [exclusive system](../exclusive-world-access)).
As a result, we must use [`Commands`], which queue up work to do later.

```rust
```rust,hide_lines=1
# use bevy::ecs::system::Commands;

// The `Commands` system parameter allows us to generate commands
// which operate on the `World` once all of the current systems have finished running
fn spawning_system(mut commands: Commands){
Expand Down Expand Up @@ -58,9 +57,8 @@ You will almost always want to use the `#[derive(Component)]` [macro](https://do

With the theory out of the way, let's define some components!

```rust
```rust,hide_lines=1
# use bevy::ecs::component::Component;

// This is a "unit struct", which holds no data of its own.
#[derive(Component)]
struct Combatant;
Expand Down Expand Up @@ -91,7 +89,7 @@ enum Allegiance {

Now that we have some components defined, let's try adding them to our entities using [`Commands`].

```rust
```rust,hide_lines=1-20
# use bevy::ecs::prelude::*;
#
# #[derive(Component)]
Expand All @@ -112,7 +110,6 @@ Now that we have some components defined, let's try adding them to our entities
# Friendly,
# Hostile
# }

fn spawn_combatants_system(mut commands: Commands) {
commands.spawn((
// This inserts a data-less `Combatant` component into the entity we're spawning
Expand Down Expand Up @@ -150,12 +147,11 @@ fn spawn_combatants_system(mut commands: Commands) {

Once an entity is spawned, you can use [`Commands`] to add and remove components from them dynamically.

```rust
```rust,hide_lines=1-4
# use bevy::ecs::prelude::*;
#
# #[derive(Component)]
# struct Combatant;

#[derive(Component)]
struct InCombat;
Expand Down Expand Up @@ -189,7 +185,7 @@ These are defined by deriving the [`Bundle`] trait for a struct; turning each of

Let's try rewriting that code from above.

```rust
```rust,hide_lines=1-20
# use bevy::prelude::*;
#
# #[derive(Component)]
Expand All @@ -210,7 +206,6 @@ Let's try rewriting that code from above.
# Friendly,
# Hostile
# }

#[derive(Bundle)]
struct CombatantBundle {
combatant: Combatant,
Expand Down Expand Up @@ -279,7 +274,7 @@ Including duplicate components in your bundles in this way will cause a panic.

With those caveats out of the way, let's take a look at the syntax by converting the bundle above to a nested one by creating a bundle of components that deal with related functionality.

```rust
```rust,hide_lines=1-19
# use bevy::prelude::*;
#
# #[derive(Component)]
Expand All @@ -299,7 +294,6 @@ With those caveats out of the way, let's take a look at the syntax by converting
# Friendly,
# Hostile
# }

#[derive(Bundle)]
struct AttackableBundle{
life: Life,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ weight = 4
template = "docs-section.html"
[extra]
status = 'hidden'
+++
+++
5 changes: 2 additions & 3 deletions content/learn/quick-start/getting-started/apps/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ aliases = ["learn/book/getting-started/apps"]

Bevy programs are referred to as [`App`]s. The simplest Bevy app looks like this:

```rs
use bevy::prelude::*;

```rs,hide_lines=1
# use bevy::prelude::*;
fn main() {
App::new().run();
}
Expand Down
41 changes: 14 additions & 27 deletions content/learn/quick-start/getting-started/ecs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS impl

* **Components**: Rust structs that implement the [`Component`] trait

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

#[derive(Component)]
struct Position { x: f32, y: f32 }
```
* **Systems**: normal Rust functions
```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn print_position_system(query: Query<&Position>) {
for position in &query {
println!("position: {} {}", position.x, position.y);
Expand All @@ -41,9 +39,8 @@ Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS impl
* **Entities**: a simple type containing a unique integer
```rs
```rs,hide_lines=1
# use bevy::prelude::*;

struct Entity(u64);
```
Expand All @@ -55,19 +52,17 @@ Now let's see how this works in practice!
Paste the following function into your `main.rs` file:
```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn hello_world() {
println!("hello world!");
}
```

This will be our first system. The only remaining step is to add it to our [`App`]!

```rs
use bevy::prelude::*;

```rs,hide_lines=1
# use bevy::prelude::*;
fn main() {
App::new()
.add_systems(Update, hello_world)
Expand All @@ -90,27 +85,24 @@ Greeting the whole world is great, but what if we want to greet specific people?

Add this struct to your `main.rs` file:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

#[derive(Component)]
struct Person;
```

But what if we want our people to have a name? In a more traditional design, we might just tack on a `name: String` field to `Person`. But other entities might have names too! For example, dogs should probably also have a name. It often makes sense to break datatypes up in to small pieces to encourage code reuse. So let's make `Name` its own component:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

#[derive(Component)]
struct Name(String);
```

We can then add people to our [`World`] using a "startup system". Startup systems are just like normal systems, but they run exactly once, before all other systems, right when our app starts. Let's use [`Commands`] to spawn some entities into our [`World`]\:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("Elaina Proctor".to_string())));
commands.spawn((Person, Name("Renzo Hume".to_string())));
Expand All @@ -120,9 +112,8 @@ fn add_people(mut commands: Commands) {

Now register the startup system like this:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn main() {
App::new()
.add_systems(Startup, add_people)
Expand All @@ -138,9 +129,8 @@ fn main() {

We could run this now and the `add_people` system would run first, followed by `hello_world`. But our new people don't have anything to do yet! Let's make a system that properly greets the new citizens of our [`World`]:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
Expand All @@ -155,9 +145,8 @@ You can interpret the [`Query`] above as: "iterate over every `Name` component f
Now we just register the system in our `App`. Note that you can pass more than one system into an `add_systems` call by using a tuple!
[`Query`]: <https://docs.rs/bevy/latest/bevy/ecs/system/struct.Query.html>

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn main() {
App::new()
.add_systems(Startup, add_people)
Expand All @@ -183,9 +172,8 @@ Marvelous!

If we want to change the names of some people (perhaps they got married!), for example, we can do this using a mutable query:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn update_people(mut query: Query<&mut Name, With<Person>>) {
for mut name in &mut query {
if name.0 == "Elaina Proctor" {
Expand All @@ -200,9 +188,8 @@ We need to make `query` mutable, and use a mutable reference (`&mut`) to the com

Don’t forget to add the system to the [`Update`] schedule:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn main() {
App::new()
.add_systems(Startup, add_people)
Expand Down
12 changes: 4 additions & 8 deletions content/learn/quick-start/getting-started/plugins/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ However, most developers don't need a custom experience and just want the "full
Let's make our app more interesting by adding Bevy's [`DefaultPlugins`] which are a [`PluginGroup`] containing core engine features. (For those needing minimal features, [`MinimalPlugins`] exists).
`add_plugins(DefaultPlugins)` adds the features most people expect from an engine, such as a 2D / 3D renderer, asset loading, a UI system, windows, and input.

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
Expand All @@ -49,9 +48,8 @@ You should hopefully notice two things:

For better organization, let's move all of our "hello" logic to a plugin. To create a plugin we just need to implement the [`Plugin`] interface. Add the following code to your `main.rs` file:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

pub struct HelloPlugin;
impl Plugin for HelloPlugin {
Expand All @@ -63,9 +61,8 @@ impl Plugin for HelloPlugin {

Then register the plugin in your App like this:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn main() {
App::new()
.add_plugins((DefaultPlugins, HelloPlugin))
Expand All @@ -77,9 +74,8 @@ fn main() {

Note `add_plugins` can add any number of plugins (or plugin groups like `DefaultPlugins`) by passing in a tuple of them. Now all that's left is to move our systems into `HelloPlugin`, which is just a matter of cut and paste. The `app` variable in our plugin's `build()` function is the same builder type we use in our `main()` function:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, add_people)
Expand Down
9 changes: 3 additions & 6 deletions content/learn/quick-start/getting-started/resources/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ For simplicity, remove the `hello_world` system from your App. This way we only

Resources are accessed in much the same way that we access components. You can access the [`Time`] resource in your system like this:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

fn greet_people(time: Res<Time>, query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
Expand All @@ -42,9 +41,8 @@ fn greet_people(time: Res<Time>, query: Query<&Name, With<Person>>) {

The `delta` field on [`Time`] gives us the time that has passed since the last update. But in order to run our system once every two seconds, we must track the amount of time that has passed over a series of updates. To make this easier, Bevy provides the [`Timer`] type. Let's create a new Resource for our system to track elapsed time with a [`Timer`]:

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

#[derive(Resource)]
struct GreetTimer(Timer);
Expand All @@ -65,9 +63,8 @@ fn greet_people(

Now all that's left is adding a `GreetTimer` Resource to our `HelloPlugin`. Use [`TimerMode::Repeating`] to make the timer repeat.

```rs
```rs,hide_lines=1
# use bevy::prelude::*;

impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
Expand Down
2 changes: 1 addition & 1 deletion content/learn/quick-start/plugin-development/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ It can be useful to allow your users to supply generic types to your plugins. It

You can define a generic plugin like so:

```rust
```rust,hide_lines=1-2
# use bevy::prelude::*;
# use std::marker::PhantomData;
// Example with a generic type that implements `Component`
Expand Down
1 change: 1 addition & 0 deletions write-rustdoc-hide-lines/write_rustdoc_hide_lines.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
cargo run --release -- ../content/learn/book
cargo run --release -- ../content/learn/quick-start

0 comments on commit ff940d6

Please sign in to comment.