Skip to content

Commit 47e4abf

Browse files
Fix plain_summary_line function
1 parent cba67ad commit 47e4abf

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

src/librustdoc/html/markdown.rs

+51-33
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,19 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
106106

107107
macro_rules! event_loop_break {
108108
($parser:expr, $toc_builder:expr, $shorter:expr, $buf:expr, $($end_event:pat)|*) => {{
109+
event_loop_break($parser, $toc_builder, $shorter, $buf, false, $($end_event:pat)|*);
110+
}};
111+
($parser:expr, $toc_builder:expr, $shorter:expr, $buf:expr, $escape:expr,
112+
$($end_event:pat)|*) => {{
109113
while let Some(event) = $parser.next() {
110114
match event {
111115
$($end_event)|* => break,
112116
Event::Text(ref s) => {
113-
$buf.push_str(s);
117+
if $escape {
118+
$buf.push_str(&escape(s));
119+
} else {
120+
$buf.push_str(s);
121+
}
114122
}
115123
Event::SoftBreak | Event::HardBreak if !$buf.is_empty() => {
116124
$buf.push(' ');
@@ -127,6 +135,13 @@ pub fn render(w: &mut fmt::Formatter,
127135
s: &str,
128136
print_toc: bool,
129137
shorter: MarkdownOutputStyle) -> fmt::Result {
138+
fn escape(entry: &str) -> String {
139+
entry.replace("<", "&lt;")
140+
.replace("'", "&#39;")
141+
.replace(">", "&gt;")
142+
.replace("&", "&amp;")
143+
}
144+
130145
fn block(parser: &mut Parser, buffer: &mut String, lang: &str) {
131146
let mut origtext = String::new();
132147
while let Some(event) = parser.next() {
@@ -208,7 +223,7 @@ pub fn render(w: &mut fmt::Formatter,
208223
fn header(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
209224
shorter: MarkdownOutputStyle, level: i32) {
210225
let mut ret = String::new();
211-
event_loop_break!(parser, toc_builder, shorter, ret, Event::End(Tag::Header(_)));
226+
event_loop_break!(parser, toc_builder, shorter, ret, true, Event::End(Tag::Header(_)));
212227
ret = ret.trim_right().to_owned();
213228

214229
let id = ret.clone();
@@ -241,28 +256,28 @@ pub fn render(w: &mut fmt::Formatter,
241256
fn codespan(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
242257
shorter: MarkdownOutputStyle) {
243258
let mut content = String::new();
244-
event_loop_break!(parser, toc_builder, shorter, content, Event::End(Tag::Code));
259+
event_loop_break!(parser, toc_builder, shorter, content, true, Event::End(Tag::Code));
245260
buffer.push_str(&format!("<code>{}</code>",
246261
Escape(&collapse_whitespace(content.trim_right()))));
247262
}
248263

249264
fn link(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
250265
shorter: MarkdownOutputStyle, url: &str, mut title: String) {
251-
event_loop_break!(parser, toc_builder, shorter, title, Event::End(Tag::Link(_, _)));
266+
event_loop_break!(parser, toc_builder, shorter, title, true, Event::End(Tag::Link(_, _)));
252267
buffer.push_str(&format!("<a href=\"{}\">{}</a>", url, title));
253268
}
254269

255270
fn paragraph(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
256271
shorter: MarkdownOutputStyle) {
257272
let mut content = String::new();
258-
event_loop_break!(parser, toc_builder, shorter, content, Event::End(Tag::Paragraph));
273+
event_loop_break!(parser, toc_builder, shorter, content, true, Event::End(Tag::Paragraph));
259274
buffer.push_str(&format!("<p>{}</p>", content.trim_right()));
260275
}
261276

262277
fn cell(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
263278
shorter: MarkdownOutputStyle) {
264279
let mut content = String::new();
265-
event_loop_break!(parser, toc_builder, shorter, content,
280+
event_loop_break!(parser, toc_builder, shorter, content, true,
266281
Event::End(Tag::TableHead) |
267282
Event::End(Tag::Table(_)) |
268283
Event::End(Tag::TableRow) |
@@ -336,7 +351,7 @@ pub fn render(w: &mut fmt::Formatter,
336351
fn blockquote(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
337352
shorter: MarkdownOutputStyle) {
338353
let mut content = String::new();
339-
event_loop_break!(parser, toc_builder, shorter, content, Event::End(Tag::BlockQuote));
354+
event_loop_break!(parser, toc_builder, shorter, content, true, Event::End(Tag::BlockQuote));
340355
buffer.push_str(&format!("<blockquote>{}</blockquote>", content.trim_right()));
341356
}
342357

@@ -347,7 +362,7 @@ pub fn render(w: &mut fmt::Formatter,
347362
match event {
348363
Event::End(Tag::Item) => break,
349364
Event::Text(ref s) => {
350-
content.push_str(s);
365+
content.push_str(&escape(s));
351366
}
352367
x => {
353368
looper(parser, &mut content, Some(x), toc_builder, shorter);
@@ -377,14 +392,14 @@ pub fn render(w: &mut fmt::Formatter,
377392
fn emphasis(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
378393
shorter: MarkdownOutputStyle) {
379394
let mut content = String::new();
380-
event_loop_break!(parser, toc_builder, shorter, content, Event::End(Tag::Emphasis));
395+
event_loop_break!(parser, toc_builder, shorter, content, true, Event::End(Tag::Emphasis));
381396
buffer.push_str(&format!("<em>{}</em>", content));
382397
}
383398

384399
fn strong(parser: &mut Parser, buffer: &mut String, toc_builder: &mut Option<TocBuilder>,
385400
shorter: MarkdownOutputStyle) {
386401
let mut content = String::new();
387-
event_loop_break!(parser, toc_builder, shorter, content, Event::End(Tag::Strong));
402+
event_loop_break!(parser, toc_builder, shorter, content, true, Event::End(Tag::Strong));
388403
buffer.push_str(&format!("<strong>{}</strong>", content));
389404
}
390405

@@ -644,9 +659,12 @@ pub fn plain_summary_line(md: &str) -> String {
644659
Event::Start(Tag::Link(_, ref t)) if !self.is_first => {
645660
(Some(t.as_ref().to_owned()), 1)
646661
}
662+
Event::Start(Tag::Code) => (Some("`".to_owned()), 1),
663+
Event::End(Tag::Code) => (Some("`".to_owned()), -1),
664+
Event::Start(Tag::Header(_)) => (None, 1),
647665
Event::Text(ref s) if self.is_in > 0 => (Some(s.as_ref().to_owned()), 0),
648666
Event::End(Tag::Link(_, ref t)) => (Some(t.as_ref().to_owned()), -1),
649-
Event::End(Tag::Paragraph) => (None, -1),
667+
Event::End(Tag::Paragraph) | Event::End(Tag::Header(_)) => (None, -1),
650668
_ => (None, 0),
651669
};
652670
if is_in > 0 || (is_in < 0 && self.is_in > 0) {
@@ -728,17 +746,17 @@ mod tests {
728746
reset_ids(true);
729747
}
730748

731-
t("# Foo bar", "\n<h1 id='foo-bar' class='section-header'>\
732-
<a href='#foo-bar'>Foo bar</a></h1>");
733-
t("## Foo-bar_baz qux", "\n<h2 id='foo-bar_baz-qux' class=\'section-\
734-
header'><a href='#foo-bar_baz-qux'>Foo-bar_baz qux</a></h2>");
749+
t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
750+
<a href=\"#foo-bar\">Foo bar</a></h1>");
751+
t("## Foo-bar_baz qux", "<h2 id=\"foo-bar_baz-qux\" class=\"section-\
752+
header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>");
735753
t("### **Foo** *bar* baz!?!& -_qux_-%",
736-
"\n<h3 id='foo-bar-baz--_qux_-' class='section-header'>\
737-
<a href='#foo-bar-baz--_qux_-'><strong>Foo</strong> \
754+
"<h3 id=\"foo-bar-baz--_qux_-\" class=\"section-header\">\
755+
<a href=\"#foo-bar-baz--_qux_-\"><strong>Foo</strong> \
738756
<em>bar</em> baz!?!&amp; -_qux_-%</a></h3>");
739757
t("####**Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
740-
"\n<h4 id='foo--bar--baz--qux' class='section-header'>\
741-
<a href='#foo--bar--baz--qux'><strong>Foo?</strong> &amp; *bar?!* \
758+
"<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\
759+
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!* \
742760
<em><code>baz</code></em> ❤ #qux</a></h4>");
743761
}
744762

@@ -750,18 +768,18 @@ mod tests {
750768
}
751769

752770
let test = || {
753-
t("# Example", "\n<h1 id='example' class='section-header'>\
754-
<a href='#example'>Example</a></h1>");
755-
t("# Panics", "\n<h1 id='panics' class='section-header'>\
756-
<a href='#panics'>Panics</a></h1>");
757-
t("# Example", "\n<h1 id='example-1' class='section-header'>\
758-
<a href='#example-1'>Example</a></h1>");
759-
t("# Main", "\n<h1 id='main-1' class='section-header'>\
760-
<a href='#main-1'>Main</a></h1>");
761-
t("# Example", "\n<h1 id='example-2' class='section-header'>\
762-
<a href='#example-2'>Example</a></h1>");
763-
t("# Panics", "\n<h1 id='panics-1' class='section-header'>\
764-
<a href='#panics-1'>Panics</a></h1>");
771+
t("# Example", "<h1 id=\"example\" class=\"section-header\">\
772+
<a href=\"#example\">Example</a></h1>");
773+
t("# Panics", "<h1 id=\"panics\" class=\"section-header\">\
774+
<a href=\"#panics\">Panics</a></h1>");
775+
t("# Example", "<h1 id=\"example-1\" class=\"section-header\">\
776+
<a href=\"#example-1\">Example</a></h1>");
777+
t("# Main", "<h1 id=\"main-1\" class=\"section-header\">\
778+
<a href=\"#main-1\">Main</a></h1>");
779+
t("# Example", "<h1 id=\"example-2\" class=\"section-header\">\
780+
<a href=\"#example-2\">Example</a></h1>");
781+
t("# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
782+
<a href=\"#panics-1\">Panics</a></h1>");
765783
};
766784
test();
767785
reset_ids(true);
@@ -789,7 +807,7 @@ mod tests {
789807
assert_eq!(output, expect);
790808
}
791809

792-
t("`Struct<'a, T>`", "<p><code>Struct&lt;&#39;a, T&gt;</code></p>\n");
793-
t("Struct<'a, T>", "<p>Struct&lt;&#39;a, T&gt;</p>\n");
810+
t("`Struct<'a, T>`", "<p><code>Struct&lt;&#39;a, T&gt;</code></p>");
811+
t("Struct<'a, T>", "<p>Struct&lt;&#39;a, T&gt;</p>");
794812
}
795813
}

src/rustc/rustdoc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rustdoc)]
1211
#![feature(rustc_private)]
1312

1413
extern crate rustdoc;

0 commit comments

Comments
 (0)