Skip to content

Commit

Permalink
Rollup merge of #88093 - Kobzol:rustdoc-wrap-code-in-code-tag, r=Guil…
Browse files Browse the repository at this point in the history
…laumeGomez

[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: #88020
  • Loading branch information
GuillaumeGomez authored Aug 19, 2021
2 parents 09d56a7 + ccd550e commit 10165f8
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 267 deletions.
7 changes: 4 additions & 3 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
out.push_buffer(extra);
}
if let Some(class) = class {
writeln!(out, "<pre class=\"rust {}\">", class);
write!(out, "<pre class=\"rust {}\">", class);
} else {
writeln!(out, "<pre class=\"rust\">");
write!(out, "<pre class=\"rust\">");
}
write!(out, "<code>");
}

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

fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
writeln!(out, "</pre>{}</div>", playground_button.unwrap_or_default());
writeln!(out, "</code></pre>{}</div>", playground_button.unwrap_or_default());
}

/// How a span of text is classified. Mostly corresponds to token kinds.
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
return Some(Event::Html(
format!(
"<div class=\"example-wrap\">\
<pre class=\"language-{}\">{}</pre>\
<pre class=\"language-{}\"><code>{}</code></pre>\
</div>",
lang,
Escape(&text),
Expand Down
Loading

0 comments on commit 10165f8

Please sign in to comment.