rust/tests/rustdoc-ui
bors 05965ae238 Auto merge of #124577 - GuillaumeGomez:stabilize-custom_code_classes_in_docs, r=rustdoc
Stabilize `custom_code_classes_in_docs` feature

Fixes #79483.

This feature has been around for quite some time now, I think it's fine to stabilize it now.

## Summary

## What is the feature about?

In short, this PR changes two things, both related to codeblocks in doc comments in Rust documentation:

 * Allow to disable generation of `language-*` CSS classes with the `custom` attribute.
 * Add your own CSS classes to a code block so that you can use other tools to highlight them.

#### The `custom` attribute

Let's start with the new `custom` attribute: it will disable the generation of the `language-*` CSS class on the generated HTML code block. For example:

```rust
/// ```custom,c
/// int main(void) {
///     return 0;
/// }
/// ```
```

The generated HTML code block will not have `class="language-c"` because the `custom` attribute has been set. The `custom` attribute becomes especially useful with the other thing added by this feature: adding your own CSS classes.

#### Adding your own CSS classes

The second part of this feature is to allow users to add CSS classes themselves so that they can then add a JS library which will do it (like `highlight.js` or `prism.js`), allowing to support highlighting for other languages than Rust without increasing burden on rustdoc. To disable the automatic `language-*` CSS class generation, you need to use the `custom` attribute as well.

This allow users to write the following:

```rust
/// Some code block with `{class=language-c}` as the language string.
///
/// ```custom,{class=language-c}
/// int main(void) {
///     return 0;
/// }
/// ```
fn main() {}
```

This will notably produce the following HTML:

```html
<pre class="language-c">
int main(void) {
    return 0;
}</pre>
```

Instead of:

```html
<pre class="rust rust-example-rendered">
<span class="ident">int</span> <span class="ident">main</span>(<span class="ident">void</span>) {
    <span class="kw">return</span> <span class="number">0</span>;
}
</pre>
```

To be noted, we could have written `{.language-c}` to achieve the same result. `.` and `class=` have the same effect.

One last syntax point: content between parens (`(like this)`) is now considered as comment and is not taken into account at all.

In addition to this, I added an `unknown` field into `LangString` (the parsed code block "attribute") because of cases like this:

```rust
/// ```custom,class:language-c
/// main;
/// ```
pub fn foo() {}
```

Without this `unknown` field, it would generate in the DOM: `<pre class="language-class:language-c language-c">`, which is quite bad. So instead, it now stores all unknown tags into the `unknown` field and use the first one as "language". So in this case, since there is no unknown tag, it'll simply generate `<pre class="language-c">`. I added tests to cover this.

EDIT(camelid): This description is out-of-date. Using `custom,class:language-c` will generate the output `<pre class="language-class:language-c">` as would be expected; it treats `class:language-c` as just the name of a language (similar to the langstring `c` or `js` or what have you) since it does not use the designed class syntax.

Finally, I added a parser for the codeblock attributes to make it much easier to maintain. It'll be pretty easy to extend.

As to why this syntax for adding attributes was picked: it's [Pandoc's syntax](https://pandoc.org/MANUAL.html#extension-fenced_code_attributes). Even if it seems clunkier in some cases, it's extensible, and most third-party Markdown renderers are smart enough to ignore Pandoc's brace-delimited attributes (from [this comment](https://github.com/rust-lang/rust/pull/110800#issuecomment-1522044456)).

r? `@notriddle`
2024-06-01 10:18:01 +00:00
..
argfile compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
auxiliary Rollup merge of #123574 - notriddle:notriddle/issue-d, r=fmease 2024-04-16 15:19:12 +02:00
coverage [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
doctest non_local_defs: improve exception note for impl and macro_rules! 2024-05-27 23:59:18 +02:00
error-in-impl-trait [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
generate-link-to-definition [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
intra-doc rustdoc: point at span in include_str!-ed md file 2024-03-29 13:31:35 -07:00
issues Auto merge of #124577 - GuillaumeGomez:stabilize-custom_code_classes_in_docs, r=rustdoc 2024-06-01 10:18:01 +00:00
lints When displaying multispans, ignore empty lines adjacent to ... 2024-03-18 16:25:36 +00:00
scrape-examples [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
suggestions [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
synthetic-auto-trait-impls Rollup merge of #123375 - fmease:rustdoc-sati-re-hotfix, r=GuillaumeGomez 2024-04-02 21:22:04 +02:00
ambiguous-inherent-assoc-ty.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
apit-46976.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
bounded-hr-lifetime.rs
bounded-hr-lifetime.stderr
check-cfg.rs Stabilize checking of cfgs at compile-time: --check-cfg option 2024-04-15 21:49:55 +02:00
check-cfg.stderr Move --check-cfg documentation to stable books 2024-04-15 21:49:56 +02:00
check-doc-alias-attr-location.rs
check-doc-alias-attr-location.stderr
check-doc-alias-attr.rs
check-doc-alias-attr.stderr
circular-intra-doc-link-48414.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
const_arg_in_type_position.rs
const_arg_in_type_position.stderr
const-evalutation-ice.rs
const-evalutation-ice.stderr
crate-reference-in-block-module.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
crate-reference-in-block-module.stderr
custom_code_classes_in_docs-warning3.rs Stabilize custom_code_classes_in_docs feature 2024-05-01 16:45:27 +02:00
custom_code_classes_in_docs-warning3.stderr Stabilize custom_code_classes_in_docs feature 2024-05-01 16:45:27 +02:00
deprecated-attrs.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
deprecated-attrs.stderr
deref-generic.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
diagnostic-width.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
diagnostic-width.stderr
doc-alias-assoc-const.rs
doc-alias-assoc-const.stderr
doc-alias-crate-level.rs
doc-alias-crate-level.stderr
doc-alias-same-name.rs
doc-alias-same-name.stderr
doc-cfg.rs
doc-cfg.stderr
doc-include-suggestion.rs Update ui tests 2024-02-29 14:43:43 +01:00
doc-include-suggestion.stderr Update ui tests 2024-02-29 14:43:43 +01:00
feature-gate-doc_cfg_hide.rs
feature-gate-doc_cfg_hide.stderr
hidden-trait-method-34423.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
ice-assoc-const-for-primitive-31808.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
ice-blanket-impl-52873.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
ice-blanket-impl-56701.rs Move ice tests to rustdoc-ui 2024-04-15 15:11:49 -07:00
ice-blanket-impl-selection-55001.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
ice-bug-report-url.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
ice-bug-report-url.stderr tests: bless ui and rustdoc-ui tests for ICE messages 2024-04-09 13:58:52 +00:00
ice-cross-crate-opaque-assoc-type-73061.rs Move ice tests to rustdoc-ui 2024-04-15 15:11:49 -07:00
ignore-block-help.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
ignore-block-help.stderr
impl-fn-nesting.rs
impl-fn-nesting.stderr
include-str-bare-urls.rs rustdoc: point at span in include_str!-ed md file 2024-03-29 13:31:35 -07:00
include-str-bare-urls.stderr rustdoc: point at span in include_str!-ed md file 2024-03-29 13:31:35 -07:00
infinite-recursive-type.rs
infinite-recursive-type.stderr
inherent-assoc-consts-36031.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
invalid_associated_const.rs Rename HIR TypeBinding to AssocItemConstraint and related cleanup 2024-05-30 22:52:33 +02:00
invalid_associated_const.stderr Rename HIR TypeBinding to AssocItemConstraint and related cleanup 2024-05-30 22:52:33 +02:00
invalid_const_in_lifetime_position.rs
invalid_const_in_lifetime_position.stderr
invalid_infered_static_and_const.rs
invalid_infered_static_and_const.stderr
invalid-cfg.rs
invalid-cfg.stderr
invalid-keyword.rs
invalid-keyword.stderr
invalid-redundant-explicit-link.rs Add regression test for #123158 2024-03-28 11:09:08 +01:00
invalid-syntax.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
invalid-syntax.stderr rustdoc: point at span in include_str!-ed md file 2024-03-29 13:31:35 -07:00
invalid-theme-name.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
invalid-theme-name.stderr
issue-102467.rs Rename HIR TypeBinding to AssocItemConstraint and related cleanup 2024-05-30 22:52:33 +02:00
issue-102467.stderr Rename HIR TypeBinding to AssocItemConstraint and related cleanup 2024-05-30 22:52:33 +02:00
issue-110629-private-type-cycle-dyn.rs
issue-110629-private-type-cycle-dyn.stderr Merge collect_mod_item_types query into check_well_formed 2024-03-07 14:26:31 +00:00
issue-110629-private-type-cycle.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
macro-docs.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
macro-docs.stderr
macro-docs.stdout
mismatched_arg_count.rs
mismatched_arg_count.stderr
nested-extern-crate-46271.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
nested-macro-rules-47639.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
normalize-cycle.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
normalize-in-inlined-type-alias.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
normalize-overflow.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
not-wf-ambiguous-normalization.rs Merge check_mod_impl_wf and check_mod_type_wf 2024-03-07 06:27:09 +00:00
not-wf-ambiguous-normalization.stderr Merge check_mod_impl_wf and check_mod_type_wf 2024-03-07 06:27:09 +00:00
output-format-html-stable.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
proc_macro_bug.rs
proc_macro_bug.stderr
pub-use-primitive-document-private-items-95633.rs Move tests into appropriate subdirectories 2024-05-21 21:21:26 -07:00
range-pattern.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
recursive-deref-ice.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
redundant-explicit-links-123677.rs rustdoc: check redundant explicit links with correct itemid 2024-04-13 19:32:39 -07:00
rustc-check-passes.rs
rustc-check-passes.stderr
search-index-generics-recursion-bug-issue-59502.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
super-glob-40936.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
track-diagnostics.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
track-diagnostics.stderr
tuple-variadic-check.rs
tuple-variadic-check.stderr
unable-fulfill-trait.rs
unable-fulfill-trait.stderr
unescaped_backticks.rs
unescaped_backticks.stderr rustdoc: point at span in include_str!-ed md file 2024-03-29 13:31:35 -07:00
unused-extern-crate.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
use_both_out_dir_and_output_options.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
use_both_out_dir_and_output_options.stderr
wasm-safe.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00