Skip to content

Commit 778b3cb

Browse files
committed
Add MarkdownHmtl escape struct
`MarkdownHtml` structs escape HTML tags from its text.
1 parent 1ae2245 commit 778b3cb

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/librustdoc/html/markdown.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub struct Markdown<'a>(pub &'a str);
4949
/// A unit struct like `Markdown`, that renders the markdown with a
5050
/// table of contents.
5151
pub struct MarkdownWithToc<'a>(pub &'a str);
52+
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
53+
pub struct MarkdownHtml<'a>(pub &'a str);
5254

5355
const DEF_OUNIT: libc::size_t = 64;
5456
const HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 11;
@@ -58,6 +60,7 @@ const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
5860
const HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
5961
const HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
6062
const HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
63+
const HOEDOWN_HTML_ESCAPE: libc::c_uint = 1 << 1;
6164

6265
const HOEDOWN_EXTENSIONS: libc::c_uint =
6366
HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
@@ -220,7 +223,11 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
220223
RefCell::new(None)
221224
});
222225

223-
pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
226+
227+
pub fn render(w: &mut fmt::Formatter,
228+
s: &str,
229+
print_toc: bool,
230+
html_flags: libc::c_uint) -> fmt::Result {
224231
extern fn block(ob: *mut hoedown_buffer, orig_text: *const hoedown_buffer,
225232
lang: *const hoedown_buffer, data: *const hoedown_renderer_data) {
226233
unsafe {
@@ -383,7 +390,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
383390

384391
unsafe {
385392
let ob = hoedown_buffer_new(DEF_OUNIT);
386-
let renderer = hoedown_html_renderer_new(0, 0);
393+
let renderer = hoedown_html_renderer_new(html_flags, 0);
387394
let mut opaque = MyOpaque {
388395
dfltblk: (*renderer).blockcode.unwrap(),
389396
toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
@@ -553,14 +560,23 @@ impl<'a> fmt::Display for Markdown<'a> {
553560
let Markdown(md) = *self;
554561
// This is actually common enough to special-case
555562
if md.is_empty() { return Ok(()) }
556-
render(fmt, md, false)
563+
render(fmt, md, false, 0)
557564
}
558565
}
559566

560567
impl<'a> fmt::Display for MarkdownWithToc<'a> {
561568
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
562569
let MarkdownWithToc(md) = *self;
563-
render(fmt, md, true)
570+
render(fmt, md, true, 0)
571+
}
572+
}
573+
574+
impl<'a> fmt::Display for MarkdownHtml<'a> {
575+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
576+
let MarkdownHtml(md) = *self;
577+
// This is actually common enough to special-case
578+
if md.is_empty() { return Ok(()) }
579+
render(fmt, md, false, HOEDOWN_HTML_ESCAPE)
564580
}
565581
}
566582

src/librustdoc/html/render.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use html::format::{TyParamBounds, WhereClause, href, AbiSpace};
7171
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
7272
use html::format::fmt_impl_for_trait_page;
7373
use html::item_type::ItemType;
74-
use html::markdown::{self, Markdown};
74+
use html::markdown::{self, Markdown, MarkdownHtml};
7575
use html::{highlight, layout};
7676

7777
/// A pair of name and its optional document.
@@ -1844,7 +1844,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
18441844

18451845
if let Some(stab) = item.stability.as_ref() {
18461846
let deprecated_reason = if show_reason && !stab.deprecated_reason.is_empty() {
1847-
format!(": {}", Escape(&stab.deprecated_reason))
1847+
format!(": {}", stab.deprecated_reason)
18481848
} else {
18491849
String::new()
18501850
};
@@ -1854,7 +1854,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
18541854
} else {
18551855
String::new()
18561856
};
1857-
let text = format!("Deprecated{}{}", since, Markdown(&deprecated_reason));
1857+
let text = format!("Deprecated{}{}", since, MarkdownHtml(&deprecated_reason));
18581858
stability.push(format!("<em class='stab deprecated'>{}</em>", text))
18591859
};
18601860

@@ -1875,16 +1875,16 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
18751875
String::new()
18761876
};
18771877
let unstable_reason = if show_reason && !stab.unstable_reason.is_empty() {
1878-
format!(": {}", Escape(&stab.unstable_reason))
1878+
format!(": {}", stab.unstable_reason)
18791879
} else {
18801880
String::new()
18811881
};
1882-
let text = format!("Unstable{}{}", unstable_extra, Markdown(&unstable_reason));
1882+
let text = format!("Unstable{}{}", unstable_extra, MarkdownHtml(&unstable_reason));
18831883
stability.push(format!("<em class='stab unstable'>{}</em>", text))
18841884
};
18851885
} else if let Some(depr) = item.deprecation.as_ref() {
18861886
let note = if show_reason && !depr.note.is_empty() {
1887-
format!(": {}", Escape(&depr.note))
1887+
format!(": {}", depr.note)
18881888
} else {
18891889
String::new()
18901890
};
@@ -1894,7 +1894,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
18941894
String::new()
18951895
};
18961896

1897-
let text = format!("Deprecated{}{}", since, Markdown(&note));
1897+
let text = format!("Deprecated{}{}", since, MarkdownHtml(&note));
18981898
stability.push(format!("<em class='stab deprecated'>{}</em>", text))
18991899
}
19001900

0 commit comments

Comments
 (0)