From 1ae2245a4e89d90f8bacef1d1d05fcf7461596fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Dec 2016 10:14:35 -0800 Subject: [PATCH 1/3] rustdoc: escape the deprecated and unstable reason text --- src/liballoc/boxed.rs | 8 ++++---- src/librustdoc/html/render.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index addb056f53429..5409ade292360 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -587,7 +587,7 @@ impl FusedIterator for Box {} /// ``` #[rustc_paren_sugar] #[unstable(feature = "fnbox", - reason = "will be deprecated if and when Box becomes usable", issue = "28796")] + reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] pub trait FnBox { type Output; @@ -595,7 +595,7 @@ pub trait FnBox { } #[unstable(feature = "fnbox", - reason = "will be deprecated if and when Box becomes usable", issue = "28796")] + reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] impl FnBox for F where F: FnOnce { @@ -607,7 +607,7 @@ impl FnBox for F } #[unstable(feature = "fnbox", - reason = "will be deprecated if and when Box becomes usable", issue = "28796")] + reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] impl<'a, A, R> FnOnce for Box + 'a> { type Output = R; @@ -617,7 +617,7 @@ impl<'a, A, R> FnOnce for Box + 'a> { } #[unstable(feature = "fnbox", - reason = "will be deprecated if and when Box becomes usable", issue = "28796")] + reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] impl<'a, A, R> FnOnce for Box + Send + 'a> { type Output = R; diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index e721b66779fff..29781888fdb4b 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1844,7 +1844,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec Vec Vec Date: Mon, 12 Dec 2016 15:18:22 -0800 Subject: [PATCH 2/3] Add `MarkdownHmtl` escape struct `MarkdownHtml` structs escape HTML tags from its text. --- src/librustdoc/html/markdown.rs | 24 ++++++++++++++++++++---- src/librustdoc/html/render.rs | 14 +++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 67cf12f4f4a6e..b4f86c1ae77ae 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -49,6 +49,8 @@ pub struct Markdown<'a>(pub &'a str); /// A unit struct like `Markdown`, that renders the markdown with a /// table of contents. pub struct MarkdownWithToc<'a>(pub &'a str); +/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags. +pub struct MarkdownHtml<'a>(pub &'a str); const DEF_OUNIT: libc::size_t = 64; const HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 11; @@ -58,6 +60,7 @@ const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3; const HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4; const HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8; const HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2; +const HOEDOWN_HTML_ESCAPE: libc::c_uint = 1 << 1; const HOEDOWN_EXTENSIONS: libc::c_uint = HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES | @@ -220,7 +223,11 @@ thread_local!(pub static PLAYGROUND: RefCell, String)>> = RefCell::new(None) }); -pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result { + +pub fn render(w: &mut fmt::Formatter, + s: &str, + print_toc: bool, + html_flags: libc::c_uint) -> fmt::Result { extern fn block(ob: *mut hoedown_buffer, orig_text: *const hoedown_buffer, lang: *const hoedown_buffer, data: *const hoedown_renderer_data) { unsafe { @@ -383,7 +390,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result { unsafe { let ob = hoedown_buffer_new(DEF_OUNIT); - let renderer = hoedown_html_renderer_new(0, 0); + let renderer = hoedown_html_renderer_new(html_flags, 0); let mut opaque = MyOpaque { dfltblk: (*renderer).blockcode.unwrap(), toc_builder: if print_toc {Some(TocBuilder::new())} else {None} @@ -553,14 +560,23 @@ impl<'a> fmt::Display for Markdown<'a> { let Markdown(md) = *self; // This is actually common enough to special-case if md.is_empty() { return Ok(()) } - render(fmt, md, false) + render(fmt, md, false, 0) } } impl<'a> fmt::Display for MarkdownWithToc<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let MarkdownWithToc(md) = *self; - render(fmt, md, true) + render(fmt, md, true, 0) + } +} + +impl<'a> fmt::Display for MarkdownHtml<'a> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let MarkdownHtml(md) = *self; + // This is actually common enough to special-case + if md.is_empty() { return Ok(()) } + render(fmt, md, false, HOEDOWN_HTML_ESCAPE) } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 29781888fdb4b..e21898499a3ce 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -71,7 +71,7 @@ use html::format::{TyParamBounds, WhereClause, href, AbiSpace}; use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace}; use html::format::fmt_impl_for_trait_page; use html::item_type::ItemType; -use html::markdown::{self, Markdown}; +use html::markdown::{self, Markdown, MarkdownHtml}; use html::{highlight, layout}; /// A pair of name and its optional document. @@ -1844,7 +1844,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec Vec{}", text)) }; @@ -1875,16 +1875,16 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec{}", text)) }; } else if let Some(depr) = item.deprecation.as_ref() { let note = if show_reason && !depr.note.is_empty() { - format!(": {}", Escape(&depr.note)) + format!(": {}", depr.note) } else { String::new() }; @@ -1894,7 +1894,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec{}", text)) } From 96c52d4fd86aed6320732a511c04bcbfff7d117f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 22 Dec 2016 23:12:56 -0800 Subject: [PATCH 3/3] Add unittest --- src/librustdoc/html/markdown.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index b4f86c1ae77ae..f2427008a7d45 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -629,7 +629,7 @@ pub fn plain_summary_line(md: &str) -> String { #[cfg(test)] mod tests { - use super::{LangString, Markdown}; + use super::{LangString, Markdown, MarkdownHtml}; use super::plain_summary_line; use html::render::reset_ids; @@ -735,4 +735,15 @@ mod tests { t("# top header", "top header"); t("## header", "header"); } + + #[test] + fn test_markdown_html_escape() { + fn t(input: &str, expect: &str) { + let output = format!("{}", MarkdownHtml(input)); + assert_eq!(output, expect); + } + + t("`Struct<'a, T>`", "

Struct<'a, T>

\n"); + t("Struct<'a, T>", "

Struct<'a, T>

\n"); + } }