Skip to content

Commit 23d200e

Browse files
Stabilize custom_code_classes_in_docs feature
1 parent 378a43a commit 23d200e

23 files changed

+54
-450
lines changed

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ declare_features! (
138138
(accepted, copy_closures, "1.26.0", Some(44490)),
139139
/// Allows `crate` in paths.
140140
(accepted, crate_in_paths, "1.30.0", Some(45477)),
141+
/// Allows users to provide classes for fenced code block using `class:classname`.
142+
(accepted, custom_code_classes_in_docs, "CURRENT_RUSTC_VERSION", Some(79483)),
141143
/// Allows using `#[debugger_visualizer]` attribute.
142144
(accepted, debugger_visualizer, "1.71.0", Some(95939)),
143145
/// Allows rustc to inject a default alloc_error_handler

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,6 @@ declare_features! (
424424
/// Allows function attribute `#[coverage(on/off)]`, to control coverage
425425
/// instrumentation of that function.
426426
(unstable, coverage_attribute, "1.74.0", Some(84605)),
427-
/// Allows users to provide classes for fenced code block using `class:classname`.
428-
(unstable, custom_code_classes_in_docs, "1.74.0", Some(79483)),
429427
/// Allows non-builtin attributes in inner attribute position.
430428
(unstable, custom_inner_attributes, "1.30.0", Some(54726)),
431429
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.

src/doc/rustdoc/src/unstable-features.md

-44
Original file line numberDiff line numberDiff line change
@@ -624,47 +624,3 @@ add the `--scrape-tests` flag.
624624

625625
This flag enables the generation of links in the source code pages which allow the reader
626626
to jump to a type definition.
627-
628-
### Custom CSS classes for code blocks
629-
630-
```rust
631-
#![feature(custom_code_classes_in_docs)]
632-
633-
/// ```custom,{class=language-c}
634-
/// int main(void) { return 0; }
635-
/// ```
636-
pub struct Bar;
637-
```
638-
639-
The text `int main(void) { return 0; }` is rendered without highlighting in a code block
640-
with the class `language-c`. This can be used to highlight other languages through JavaScript
641-
libraries for example.
642-
643-
Without the `custom` attribute, it would be generated as a Rust code example with an additional
644-
`language-C` CSS class. Therefore, if you specifically don't want it to be a Rust code example,
645-
don't forget to add the `custom` attribute.
646-
647-
To be noted that you can replace `class=` with `.` to achieve the same result:
648-
649-
```rust
650-
#![feature(custom_code_classes_in_docs)]
651-
652-
/// ```custom,{.language-c}
653-
/// int main(void) { return 0; }
654-
/// ```
655-
pub struct Bar;
656-
```
657-
658-
To be noted, `rust` and `.rust`/`class=rust` have different effects: `rust` indicates that this is
659-
a Rust code block whereas the two others add a "rust" CSS class on the code block.
660-
661-
You can also use double quotes:
662-
663-
```rust
664-
#![feature(custom_code_classes_in_docs)]
665-
666-
/// ```"not rust" {."hello everyone"}
667-
/// int main(void) { return 0; }
668-
/// ```
669-
pub struct Bar;
670-
```

src/doc/rustdoc/src/write-documentation/documentation-tests.md

+38
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,44 @@ that the code sample should be compiled using the respective edition of Rust.
376376
# fn foo() {}
377377
```
378378

379+
### Custom CSS classes for code blocks
380+
381+
```rust
382+
/// ```custom,{class=language-c}
383+
/// int main(void) { return 0; }
384+
/// ```
385+
pub struct Bar;
386+
```
387+
388+
The text `int main(void) { return 0; }` is rendered without highlighting in a code block
389+
with the class `language-c`. This can be used to highlight other languages through JavaScript
390+
libraries for example.
391+
392+
Without the `custom` attribute, it would be generated as a Rust code example with an additional
393+
`language-C` CSS class. Therefore, if you specifically don't want it to be a Rust code example,
394+
don't forget to add the `custom` attribute.
395+
396+
To be noted that you can replace `class=` with `.` to achieve the same result:
397+
398+
```rust
399+
/// ```custom,{.language-c}
400+
/// int main(void) { return 0; }
401+
/// ```
402+
pub struct Bar;
403+
```
404+
405+
To be noted, `rust` and `.rust`/`class=rust` have different effects: `rust` indicates that this is
406+
a Rust code block whereas the two others add a "rust" CSS class on the code block.
407+
408+
You can also use double quotes:
409+
410+
```rust
411+
/// ```"not rust" {."hello everyone"}
412+
/// int main(void) { return 0; }
413+
/// ```
414+
pub struct Bar;
415+
```
416+
379417
## Syntax reference
380418

381419
The *exact* syntax for code blocks, including the edge cases, can be found

src/librustdoc/doctest.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,6 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
13451345
def_id.to_def_id(),
13461346
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
13471347
)),
1348-
self.tcx.features().custom_code_classes_in_docs,
13491348
);
13501349
}
13511350

src/librustdoc/externalfiles.rs

-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ impl ExternalHtml {
4646
edition,
4747
playground,
4848
heading_offset: HeadingOffset::H2,
49-
// For external files, it'll be disabled until the feature is enabled by default.
50-
custom_code_classes_in_docs: false,
5149
}
5250
.into_string()
5351
);
@@ -63,8 +61,6 @@ impl ExternalHtml {
6361
edition,
6462
playground,
6563
heading_offset: HeadingOffset::H2,
66-
// For external files, it'll be disabled until the feature is enabled by default.
67-
custom_code_classes_in_docs: false,
6864
}
6965
.into_string()
7066
);

src/librustdoc/html/markdown.rs

+10-65
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
//! edition: Edition::Edition2015,
2121
//! playground: &None,
2222
//! heading_offset: HeadingOffset::H2,
23-
//! custom_code_classes_in_docs: true,
2423
//! };
2524
//! let html = md.into_string();
2625
//! // ... something using html
@@ -97,8 +96,6 @@ pub struct Markdown<'a> {
9796
/// Offset at which we render headings.
9897
/// E.g. if `heading_offset: HeadingOffset::H2`, then `# something` renders an `<h2>`.
9998
pub heading_offset: HeadingOffset,
100-
/// `true` if the `custom_code_classes_in_docs` feature is enabled.
101-
pub custom_code_classes_in_docs: bool,
10299
}
103100
/// A struct like `Markdown` that renders the markdown with a table of contents.
104101
pub(crate) struct MarkdownWithToc<'a> {
@@ -107,8 +104,6 @@ pub(crate) struct MarkdownWithToc<'a> {
107104
pub(crate) error_codes: ErrorCodes,
108105
pub(crate) edition: Edition,
109106
pub(crate) playground: &'a Option<Playground>,
110-
/// `true` if the `custom_code_classes_in_docs` feature is enabled.
111-
pub(crate) custom_code_classes_in_docs: bool,
112107
}
113108
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
114109
/// and includes no paragraph tags.
@@ -209,7 +204,6 @@ struct CodeBlocks<'p, 'a, I: Iterator<Item = Event<'a>>> {
209204
// Information about the playground if a URL has been specified, containing an
210205
// optional crate name and the URL.
211206
playground: &'p Option<Playground>,
212-
custom_code_classes_in_docs: bool,
213207
}
214208

215209
impl<'p, 'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'p, 'a, I> {
@@ -218,14 +212,12 @@ impl<'p, 'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'p, 'a, I> {
218212
error_codes: ErrorCodes,
219213
edition: Edition,
220214
playground: &'p Option<Playground>,
221-
custom_code_classes_in_docs: bool,
222215
) -> Self {
223216
CodeBlocks {
224217
inner: iter,
225218
check_error_codes: error_codes,
226219
edition,
227220
playground,
228-
custom_code_classes_in_docs,
229221
}
230222
}
231223
}
@@ -257,7 +249,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
257249
lang,
258250
self.check_error_codes,
259251
false,
260-
self.custom_code_classes_in_docs,
261252
);
262253
if !parse_result.rust {
263254
let added_classes = parse_result.added_classes;
@@ -733,7 +724,6 @@ pub(crate) fn find_testable_code<T: doctest::Tester>(
733724
error_codes: ErrorCodes,
734725
enable_per_target_ignores: bool,
735726
extra_info: Option<&ExtraInfo<'_>>,
736-
custom_code_classes_in_docs: bool,
737727
) {
738728
find_codes(
739729
doc,
@@ -742,7 +732,6 @@ pub(crate) fn find_testable_code<T: doctest::Tester>(
742732
enable_per_target_ignores,
743733
extra_info,
744734
false,
745-
custom_code_classes_in_docs,
746735
)
747736
}
748737

@@ -753,7 +742,6 @@ pub(crate) fn find_codes<T: doctest::Tester>(
753742
enable_per_target_ignores: bool,
754743
extra_info: Option<&ExtraInfo<'_>>,
755744
include_non_rust: bool,
756-
custom_code_classes_in_docs: bool,
757745
) {
758746
let mut parser = Parser::new(doc).into_offset_iter();
759747
let mut prev_offset = 0;
@@ -772,7 +760,6 @@ pub(crate) fn find_codes<T: doctest::Tester>(
772760
error_codes,
773761
enable_per_target_ignores,
774762
extra_info,
775-
custom_code_classes_in_docs,
776763
)
777764
}
778765
}
@@ -1167,29 +1154,6 @@ impl<'a, 'tcx> Iterator for TagIterator<'a, 'tcx> {
11671154
}
11681155
}
11691156

1170-
fn tokens(string: &str) -> impl Iterator<Item = LangStringToken<'_>> {
1171-
// Pandoc, which Rust once used for generating documentation,
1172-
// expects lang strings to be surrounded by `{}` and for each token
1173-
// to be proceeded by a `.`. Since some of these lang strings are still
1174-
// loose in the wild, we strip a pair of surrounding `{}` from the lang
1175-
// string and a leading `.` from each token.
1176-
1177-
let string = string.trim();
1178-
1179-
let first = string.chars().next();
1180-
let last = string.chars().last();
1181-
1182-
let string =
1183-
if first == Some('{') && last == Some('}') { &string[1..string.len() - 1] } else { string };
1184-
1185-
string
1186-
.split(|c| c == ',' || c == ' ' || c == '\t')
1187-
.map(str::trim)
1188-
.map(|token| token.strip_prefix('.').unwrap_or(token))
1189-
.filter(|token| !token.is_empty())
1190-
.map(|token| LangStringToken::LangToken(token))
1191-
}
1192-
11931157
impl Default for LangString {
11941158
fn default() -> Self {
11951159
Self {
@@ -1213,14 +1177,12 @@ impl LangString {
12131177
string: &str,
12141178
allow_error_code_check: ErrorCodes,
12151179
enable_per_target_ignores: bool,
1216-
custom_code_classes_in_docs: bool,
12171180
) -> Self {
12181181
Self::parse(
12191182
string,
12201183
allow_error_code_check,
12211184
enable_per_target_ignores,
12221185
None,
1223-
custom_code_classes_in_docs,
12241186
)
12251187
}
12261188

@@ -1229,7 +1191,6 @@ impl LangString {
12291191
allow_error_code_check: ErrorCodes,
12301192
enable_per_target_ignores: bool,
12311193
extra: Option<&ExtraInfo<'_>>,
1232-
custom_code_classes_in_docs: bool,
12331194
) -> Self {
12341195
let allow_error_code_check = allow_error_code_check.as_bool();
12351196
let mut seen_rust_tags = false;
@@ -1266,11 +1227,7 @@ impl LangString {
12661227
seen_rust_tags = true;
12671228
}
12681229
LangStringToken::LangToken("custom") => {
1269-
if custom_code_classes_in_docs {
1270-
seen_custom_tag = true;
1271-
} else {
1272-
seen_other_tags = true;
1273-
}
1230+
seen_custom_tag = true;
12741231
}
12751232
LangStringToken::LangToken("test_harness") => {
12761233
data.test_harness = true;
@@ -1361,16 +1318,12 @@ impl LangString {
13611318
data.unknown.push(x.to_owned());
13621319
}
13631320
LangStringToken::KeyValueAttribute(key, value) => {
1364-
if custom_code_classes_in_docs {
1365-
if key == "class" {
1366-
data.added_classes.push(value.to_owned());
1367-
} else if let Some(extra) = extra {
1368-
extra.error_invalid_codeblock_attr(format!(
1369-
"unsupported attribute `{key}`"
1370-
));
1371-
}
1372-
} else {
1373-
seen_other_tags = true;
1321+
if key == "class" {
1322+
data.added_classes.push(value.to_owned());
1323+
} else if let Some(extra) = extra {
1324+
extra.error_invalid_codeblock_attr(format!(
1325+
"unsupported attribute `{key}`"
1326+
));
13741327
}
13751328
}
13761329
LangStringToken::ClassAttribute(class) => {
@@ -1380,11 +1333,7 @@ impl LangString {
13801333
}
13811334
};
13821335

1383-
if custom_code_classes_in_docs {
1384-
call(&mut TagIterator::new(string, extra))
1385-
} else {
1386-
call(&mut tokens(string))
1387-
}
1336+
call(&mut TagIterator::new(string, extra));
13881337

13891338
// ignore-foo overrides ignore
13901339
if !ignores.is_empty() {
@@ -1407,7 +1356,6 @@ impl Markdown<'_> {
14071356
edition,
14081357
playground,
14091358
heading_offset,
1410-
custom_code_classes_in_docs,
14111359
} = self;
14121360

14131361
// This is actually common enough to special-case
@@ -1430,7 +1378,7 @@ impl Markdown<'_> {
14301378
let p = Footnotes::new(p);
14311379
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
14321380
let p = TableWrapper::new(p);
1433-
let p = CodeBlocks::new(p, codes, edition, playground, custom_code_classes_in_docs);
1381+
let p = CodeBlocks::new(p, codes, edition, playground);
14341382
html::push_html(&mut s, p);
14351383

14361384
s
@@ -1445,7 +1393,6 @@ impl MarkdownWithToc<'_> {
14451393
error_codes: codes,
14461394
edition,
14471395
playground,
1448-
custom_code_classes_in_docs,
14491396
} = self;
14501397

14511398
let p = Parser::new_ext(md, main_body_opts()).into_offset_iter();
@@ -1458,7 +1405,7 @@ impl MarkdownWithToc<'_> {
14581405
let p = HeadingLinks::new(p, Some(&mut toc), ids, HeadingOffset::H1);
14591406
let p = Footnotes::new(p);
14601407
let p = TableWrapper::new(p.map(|(ev, _)| ev));
1461-
let p = CodeBlocks::new(p, codes, edition, playground, custom_code_classes_in_docs);
1408+
let p = CodeBlocks::new(p, codes, edition, playground);
14621409
html::push_html(&mut s, p);
14631410
}
14641411

@@ -1902,7 +1849,6 @@ pub(crate) struct RustCodeBlock {
19021849
pub(crate) fn rust_code_blocks(
19031850
md: &str,
19041851
extra_info: &ExtraInfo<'_>,
1905-
custom_code_classes_in_docs: bool,
19061852
) -> Vec<RustCodeBlock> {
19071853
let mut code_blocks = vec![];
19081854

@@ -1925,7 +1871,6 @@ pub(crate) fn rust_code_blocks(
19251871
ErrorCodes::Yes,
19261872
false,
19271873
Some(extra_info),
1928-
custom_code_classes_in_docs,
19291874
)
19301875
};
19311876
if !lang_string.rust {

0 commit comments

Comments
 (0)