Skip to content

Commit 10165f8

Browse files
Rollup merge of rust-lang#88093 - Kobzol:rustdoc-wrap-code-in-code-tag, r=GuillaumeGomez
[rustdoc] Wrap code blocks in <code> tag This PR modifies Rustdoc output so that fenced code snippets, items and whole file source codes are wrapped in `<pre><code>` instead of just `<pre>`. This should improve the semantic meaning of the generated content. I'm not sure what to do about `render_attributes_in_pre` and `render_attributes_in_code`. These functions were clearly expected to be used for things inside `<pre>` or `<code>`, and since I added `<code>` in this PR, some of them will be used in a different context than before. However, it seems to me that even before they were not consistent. For example, `item_constant` used `render_attributes_in_code` for its attributes, however there was no `<code>` used for constants before this PR... Should I create some `rustdoc-gui` tests? For example to check that all `<pre>` tags have a `<code>` child? Fixes: rust-lang#88020
2 parents 09d56a7 + ccd550e commit 10165f8

File tree

6 files changed

+315
-267
lines changed

6 files changed

+315
-267
lines changed

src/librustdoc/html/highlight.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
6565
out.push_buffer(extra);
6666
}
6767
if let Some(class) = class {
68-
writeln!(out, "<pre class=\"rust {}\">", class);
68+
write!(out, "<pre class=\"rust {}\">", class);
6969
} else {
70-
writeln!(out, "<pre class=\"rust\">");
70+
write!(out, "<pre class=\"rust\">");
7171
}
72+
write!(out, "<code>");
7273
}
7374

7475
/// Convert the given `src` source code into HTML by adding classes for highlighting.
@@ -101,7 +102,7 @@ fn write_code(
101102
}
102103

103104
fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
104-
writeln!(out, "</pre>{}</div>", playground_button.unwrap_or_default());
105+
writeln!(out, "</code></pre>{}</div>", playground_button.unwrap_or_default());
105106
}
106107

107108
/// How a span of text is classified. Mostly corresponds to token kinds.

src/librustdoc/html/markdown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
234234
return Some(Event::Html(
235235
format!(
236236
"<div class=\"example-wrap\">\
237-
<pre class=\"language-{}\">{}</pre>\
237+
<pre class=\"language-{}\"><code>{}</code></pre>\
238238
</div>",
239239
lang,
240240
Escape(&text),

0 commit comments

Comments
 (0)