mdbook/tests/testsuite/includes.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3.8 KiB
Rust
Raw Normal View History

//! Tests for include preprocessor.
use crate::prelude::*;
// Basic test for #include.
#[test]
fn include() {
BookTest::from_dir("includes/all_includes")
.check_main_file(
"book/includes.html",
str![[r##"
<h1 id="basic-includes"><a class="header" href="#basic-includes">Basic Includes</a></h1>
<h2 id="sample"><a class="header" href="#sample">Sample</a></h2>
<p>This is a sample include.</p>
"##]],
)
.check_main_file(
"book/relative/includes.html",
str![[r##"
<h1 id="relative-includes"><a class="header" href="#relative-includes">Relative Includes</a></h1>
<h2 id="sample"><a class="header" href="#sample">Sample</a></h2>
<p>This is a sample include.</p>
"##]],
);
}
// Checks for anchored includes.
#[test]
fn anchored_include() {
BookTest::from_dir("includes/all_includes").check_main_file(
"book/anchors.html",
str![[r##"
<h1 id="include-anchors"><a class="header" href="#include-anchors">Include Anchors</a></h1>
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>let x = 1;
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
<span class="boring">}</span></code></pre>
"##]],
);
}
// Checks behavior of recursive include.
#[test]
fn recursive_include() {
BookTest::from_dir("includes/all_includes")
.run("build", |cmd| {
cmd.expect_stderr(str![[r#"
INFO Book building has started
ERROR Stack depth exceeded in recursive.md. Check for cyclic includes
INFO Running the html backend
INFO HTML book written to `[ROOT]/book`
"#]]);
})
.check_main_file(
"book/recursive.html",
str![[r#"
<p>Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world</p>
"#]],
);
}
// Checks the behavior of `{{#playground}}` include.
#[test]
fn playground_include() {
BookTest::from_dir("includes/all_includes")
.check_main_file("book/playground.html",
str![[r##"
<h1 id="playground-includes"><a class="header" href="#playground-includes">Playground Includes</a></h1>
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
<pre class="playground"><code class="language-rust">fn main() {
println!("Hello World!");
<span class="boring">
</span><span class="boring"> // You can even hide lines! :D
</span><span class="boring"> println!("I am hidden! Expand the code snippet to see me");
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
</span>}</code></pre>
"##]]);
}
// Checks the behavior of `{{#rustdoc_include}}`.
#[test]
fn rustdoc_include() {
BookTest::from_dir("includes/all_includes")
.check_main_file("book/rustdoc.html",
str![[r##"
<h1 id="rustdoc-includes"><a class="header" href="#rustdoc-includes">Rustdoc Includes</a></h1>
<h2 id="rustdoc-include-adds-the-rest-of-the-file-as-hidden"><a class="header" href="#rustdoc-include-adds-the-rest-of-the-file-as-hidden">Rustdoc include adds the rest of the file as hidden</a></h2>
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
<pre class="playground"><code class="language-rust"><span class="boring">fn some_function() {
</span><span class="boring"> println!("some function");
</span><span class="boring">}
</span><span class="boring">
</span>fn main() {
some_function();
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
}</code></pre>
<h2 id="rustdoc-include-works-with-anchors-too"><a class="header" href="#rustdoc-include-works-with-anchors-too">Rustdoc include works with anchors too</a></h2>
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
<pre class="playground"><code class="language-rust"><span class="boring">fn some_other_function() {
</span><span class="boring"> println!("unused anchor");
</span><span class="boring">}
</span><span class="boring">
</span>fn main() {
some_other_function();
Add a new HTML rendering pipeline This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier. This is a large change, but I'll try to briefly summarize what's changing: - All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future. - Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See `tree.rs`. - HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See `tokenizer.rs`. - Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines. - Serialization is a simple process of writing out the nodes to a string. See `serialize.rs`. - The search indexer works on the tree structure instead of re-rendering every chapter twice. See `html_handlebars/search.rs`. - The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See `print.rs`. - I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page. - There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights: - Code blocks no longer have a second layer of `<pre>` tags wrapping it. - Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition. - Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now. - Drops `pretty_assertions`, they are no longer used (mostly being migrated to the testsuite). There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far. I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build. I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure). Closes https://github.com/rust-lang/mdBook/issues/1736
2025-09-16 20:14:00 -07:00
}</code></pre>
"##]]);
}