diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2021-07-11 12:40:31 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2021-07-14 12:03:55 +0200 |
| commit | 0ef8988835012f643890a25db5dc8f35bbca0e86 (patch) | |
| tree | e7791fffa926cd07ec49804ec99d28b4ebd77411 | |
| parent | 1f0db5e0a315a252946a9e52a76c73579da53dd0 (diff) | |
| download | rust-0ef8988835012f643890a25db5dc8f35bbca0e86.tar.gz rust-0ef8988835012f643890a25db5dc8f35bbca0e86.zip | |
Fix display for non-rust code blocks
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 51 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 2 |
2 files changed, 34 insertions, 19 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 9fff508165a..3b599e4997a 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -33,6 +33,7 @@ use std::str; use crate::clean::RenderedLink; use crate::doctest; +use crate::html::escape::Escape; use crate::html::highlight; use crate::html::toc::TocBuilder; @@ -207,26 +208,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> { let should_panic; let ignore; let edition; - if let Some(Event::Start(Tag::CodeBlock(kind))) = event { - let parse_result = match kind { - CodeBlockKind::Fenced(ref lang) => { - LangString::parse_without_check(&lang, self.check_error_codes, false) - } - CodeBlockKind::Indented => Default::default(), - }; - if !parse_result.rust { - return Some(Event::Start(Tag::CodeBlock(kind))); - } - compile_fail = parse_result.compile_fail; - should_panic = parse_result.should_panic; - ignore = parse_result.ignore; - edition = parse_result.edition; + let kind = if let Some(Event::Start(Tag::CodeBlock(kind))) = event { + kind } else { return event; - } - - let explicit_edition = edition.is_some(); - let edition = edition.unwrap_or(self.edition); + }; let mut origtext = String::new(); for event in &mut self.inner { @@ -241,6 +227,35 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> { let lines = origtext.lines().filter_map(|l| map_line(l).for_html()); let text = lines.collect::<Vec<Cow<'_, str>>>().join("\n"); + let parse_result = match kind { + CodeBlockKind::Fenced(ref lang) => { + let parse_result = + LangString::parse_without_check(&lang, self.check_error_codes, false); + if !parse_result.rust { + return Some(Event::Html( + format!( + "<div class=\"example-wrap\">\ + <pre{}>{}</pre>\ + </div>", + format!(" class=\"language-{}\"", lang), + Escape(&text), + ) + .into(), + )); + } + parse_result + } + CodeBlockKind::Indented => Default::default(), + }; + + compile_fail = parse_result.compile_fail; + should_panic = parse_result.should_panic; + ignore = parse_result.ignore; + edition = parse_result.edition; + + let explicit_edition = edition.is_some(); + let edition = edition.unwrap_or(self.edition); + let playground_button = self.playground.as_ref().and_then(|playground| { let krate = &playground.crate_name; let url = &playground.url; diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 66dfd2fac84..208e8f723f4 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -435,7 +435,7 @@ nav.sub { border-bottom-left-radius: 5px; } -.rustdoc:not(.source) .example-wrap > pre.rust { +.rustdoc:not(.source) .example-wrap > pre:not(.line-number) { width: 100%; overflow-x: auto; } |
