Skip to content

Commit faef6d1

Browse files
authored
Split example file docblock and code when generating web examples markdown (#18191)
# Objective Separate example explanation (file docblock) and the code so they can be layout differently in the website and we can give a higher importance to the explanation on the [website search tool](bevyengine/bevy-website#1935). This would also allow us to improve the examples so they become even more like a cookbook. ## Solution Update the `example-showcase` tool to extract the example file docblock and write it as the example markdown content. This allows us to access the explanation via `page.content` in Zola. ## Testing I've checked that the output is correct after running the tool and it doesn't throw any error. I've also validated that the approach will work on the website. ## Showcase This is a quick and dirty example of what we could do in the web examples after the change. When we implement the real thing we can put the explanation on a sidebar or explore other layout options. <img width="1362" alt="image" src="https://github.com/user-attachments/assets/6738542e-31c3-41cd-972a-7fa2e942e85d" />
1 parent 06bae05 commit faef6d1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

tools/example-showcase/src/main.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,9 @@ weight = {}
548548
let _ = fs::create_dir_all(&example_path);
549549

550550
let code_path = example_path.join(Path::new(&to_show.path).file_name().unwrap());
551-
let _ = fs::copy(&to_show.path, &code_path);
551+
let code = fs::read_to_string(&to_show.path).unwrap();
552+
let (docblock, code) = split_docblock_and_code(&code);
553+
let _ = fs::write(&code_path, code);
552554

553555
let mut example_index = File::create(example_path.join("index.md")).unwrap();
554556
example_index
@@ -572,7 +574,10 @@ code_path = \"content/examples{}/{}\"
572574
shader_code_paths = {:?}
573575
github_code_path = \"{}\"
574576
header_message = \"Examples ({})\"
575-
+++",
577+
+++
578+
579+
{}
580+
",
576581
to_show.name,
577582
match api {
578583
WebApi::Webgpu => "-webgpu",
@@ -610,6 +615,7 @@ header_message = \"Examples ({})\"
610615
WebApi::Webgpu => "WebGPU",
611616
WebApi::Webgl2 => "WebGL2",
612617
},
618+
docblock,
613619
)
614620
.as_bytes(),
615621
)
@@ -731,6 +737,23 @@ header_message = \"Examples ({})\"
731737
}
732738
}
733739

740+
fn split_docblock_and_code(code: &str) -> (String, &str) {
741+
let mut docblock_lines = Vec::new();
742+
let mut code_byte_start = 0;
743+
744+
for line in code.lines() {
745+
if line.starts_with("//!") {
746+
docblock_lines.push(line.trim_start_matches("//!").trim());
747+
} else if !line.trim().is_empty() {
748+
break;
749+
}
750+
751+
code_byte_start += line.len() + 1;
752+
}
753+
754+
(docblock_lines.join("\n"), &code[code_byte_start..])
755+
}
756+
734757
fn parse_examples() -> Vec<Example> {
735758
let manifest_file = fs::read_to_string("Cargo.toml").unwrap();
736759
let manifest = manifest_file.parse::<DocumentMut>().unwrap();

0 commit comments

Comments
 (0)