Skip to content

Commit 0ef8988

Browse files
Fix display for non-rust code blocks
1 parent 1f0db5e commit 0ef8988

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::str;
3333

3434
use crate::clean::RenderedLink;
3535
use crate::doctest;
36+
use crate::html::escape::Escape;
3637
use crate::html::highlight;
3738
use crate::html::toc::TocBuilder;
3839

@@ -207,26 +208,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
207208
let should_panic;
208209
let ignore;
209210
let edition;
210-
if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
211-
let parse_result = match kind {
212-
CodeBlockKind::Fenced(ref lang) => {
213-
LangString::parse_without_check(&lang, self.check_error_codes, false)
214-
}
215-
CodeBlockKind::Indented => Default::default(),
216-
};
217-
if !parse_result.rust {
218-
return Some(Event::Start(Tag::CodeBlock(kind)));
219-
}
220-
compile_fail = parse_result.compile_fail;
221-
should_panic = parse_result.should_panic;
222-
ignore = parse_result.ignore;
223-
edition = parse_result.edition;
211+
let kind = if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
212+
kind
224213
} else {
225214
return event;
226-
}
227-
228-
let explicit_edition = edition.is_some();
229-
let edition = edition.unwrap_or(self.edition);
215+
};
230216

231217
let mut origtext = String::new();
232218
for event in &mut self.inner {
@@ -241,6 +227,35 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
241227
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
242228
let text = lines.collect::<Vec<Cow<'_, str>>>().join("\n");
243229

230+
let parse_result = match kind {
231+
CodeBlockKind::Fenced(ref lang) => {
232+
let parse_result =
233+
LangString::parse_without_check(&lang, self.check_error_codes, false);
234+
if !parse_result.rust {
235+
return Some(Event::Html(
236+
format!(
237+
"<div class=\"example-wrap\">\
238+
<pre{}>{}</pre>\
239+
</div>",
240+
format!(" class=\"language-{}\"", lang),
241+
Escape(&text),
242+
)
243+
.into(),
244+
));
245+
}
246+
parse_result
247+
}
248+
CodeBlockKind::Indented => Default::default(),
249+
};
250+
251+
compile_fail = parse_result.compile_fail;
252+
should_panic = parse_result.should_panic;
253+
ignore = parse_result.ignore;
254+
edition = parse_result.edition;
255+
256+
let explicit_edition = edition.is_some();
257+
let edition = edition.unwrap_or(self.edition);
258+
244259
let playground_button = self.playground.as_ref().and_then(|playground| {
245260
let krate = &playground.crate_name;
246261
let url = &playground.url;

src/librustdoc/html/static/css/rustdoc.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ nav.sub {
435435
border-bottom-left-radius: 5px;
436436
}
437437

438-
.rustdoc:not(.source) .example-wrap > pre.rust {
438+
.rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
439439
width: 100%;
440440
overflow-x: auto;
441441
}

0 commit comments

Comments
 (0)