From 51d3cec38794beb644f67e80b1e7718ee14facf0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 31 Mar 2017 11:02:46 -0600 Subject: [PATCH] Fix hard break issue --- src/librustdoc/html/markdown.rs | 36 ++++++++++++++++++++-- src/librustdoc/passes/unindent_comments.rs | 13 +++++++- src/test/rustdoc/check-hard-break.rs | 19 ++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/test/rustdoc/check-hard-break.rs diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index e8acabde408..d1f2948bc25 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -116,6 +116,7 @@ macro_rules! event_loop_break { match event { $($end_event)|* => break, Event::Text(ref s) => { + debug!("Text"); inner($id, s); if $escape { $buf.push_str(&format!("{}", Escape(s))); @@ -123,8 +124,11 @@ macro_rules! event_loop_break { $buf.push_str(s); } } - Event::SoftBreak | Event::HardBreak if !$buf.is_empty() => { - $buf.push(' '); + Event::SoftBreak => { + debug!("SoftBreak"); + if !$buf.is_empty() { + $buf.push(' '); + } } x => { looper($parser, &mut $buf, Some(x), $toc_builder, $shorter, $id); @@ -165,6 +169,7 @@ pub fn render(w: &mut fmt::Formatter, print_toc: bool, shorter: MarkdownOutputStyle) -> fmt::Result { fn code_block(parser: &mut ParserWrapper, buffer: &mut String, lang: &str) { + debug!("CodeBlock"); let mut origtext = String::new(); while let Some(event) = parser.next() { match event { @@ -244,6 +249,7 @@ pub fn render(w: &mut fmt::Formatter, fn heading(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, level: i32) { + debug!("Heading"); let mut ret = String::new(); let mut id = String::new(); event_loop_break!(parser, toc_builder, shorter, ret, true, &mut Some(&mut id), @@ -279,6 +285,7 @@ pub fn render(w: &mut fmt::Formatter, fn inline_code(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) { + debug!("InlineCode"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, false, id, Event::End(Tag::Code)); buffer.push_str(&format!("{}", @@ -288,6 +295,7 @@ pub fn render(w: &mut fmt::Formatter, fn link(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, url: &str, title: &str, id: &mut Option<&mut String>) { + debug!("Link"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, true, id, Event::End(Tag::Link(_, _))); @@ -302,6 +310,7 @@ pub fn render(w: &mut fmt::Formatter, fn image(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, url: &str, mut title: String, id: &mut Option<&mut String>) { + debug!("Image"); event_loop_break!(parser, toc_builder, shorter, title, true, id, Event::End(Tag::Image(_, _))); buffer.push_str(&format!("\"{}\"", url, title)); @@ -310,6 +319,7 @@ pub fn render(w: &mut fmt::Formatter, fn paragraph(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) { + debug!("Paragraph"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, true, id, Event::End(Tag::Paragraph)); @@ -318,6 +328,7 @@ pub fn render(w: &mut fmt::Formatter, fn table_cell(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("TableCell"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, true, &mut None, Event::End(Tag::TableHead) | @@ -329,6 +340,7 @@ pub fn render(w: &mut fmt::Formatter, fn table_row(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("TableRow"); let mut content = String::new(); while let Some(event) = parser.next() { match event { @@ -348,6 +360,7 @@ pub fn render(w: &mut fmt::Formatter, fn table_head(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("TableHead"); let mut content = String::new(); while let Some(event) = parser.next() { match event { @@ -367,6 +380,7 @@ pub fn render(w: &mut fmt::Formatter, fn table(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("Table"); let mut content = String::new(); let mut rows = String::new(); while let Some(event) = parser.next() { @@ -392,6 +406,7 @@ pub fn render(w: &mut fmt::Formatter, fn blockquote(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("BlockQuote"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, true, &mut None, Event::End(Tag::BlockQuote)); @@ -400,6 +415,7 @@ pub fn render(w: &mut fmt::Formatter, fn list_item(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("ListItem"); let mut content = String::new(); while let Some(event) = parser.next() { match event { @@ -417,6 +433,7 @@ pub fn render(w: &mut fmt::Formatter, fn list(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle) { + debug!("List"); let mut content = String::new(); while let Some(event) = parser.next() { match event { @@ -435,6 +452,7 @@ pub fn render(w: &mut fmt::Formatter, fn emphasis(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) { + debug!("Emphasis"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, false, id, Event::End(Tag::Emphasis)); @@ -443,6 +461,7 @@ pub fn render(w: &mut fmt::Formatter, fn strong(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) { + debug!("Strong"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, false, id, Event::End(Tag::Strong)); @@ -452,6 +471,7 @@ pub fn render(w: &mut fmt::Formatter, fn footnote(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) { + debug!("FootnoteDefinition"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, true, id, Event::End(Tag::FootnoteDefinition(_))); @@ -460,6 +480,7 @@ pub fn render(w: &mut fmt::Formatter, fn rule(parser: &mut ParserWrapper, buffer: &mut String, toc_builder: &mut Option, shorter: MarkdownOutputStyle, id: &mut Option<&mut String>) { + debug!("Rule"); let mut content = String::new(); event_loop_break!(parser, toc_builder, shorter, content, true, id, Event::End(Tag::Rule)); @@ -508,6 +529,7 @@ pub fn render(w: &mut fmt::Formatter, rule(parser, buffer, toc_builder, shorter, id); } Event::Start(Tag::FootnoteDefinition(ref def)) => { + debug!("FootnoteDefinition"); let mut content = String::new(); let def = def.as_ref(); footnote(parser, &mut content, toc_builder, shorter, id); @@ -523,12 +545,22 @@ pub fn render(w: &mut fmt::Formatter, })); } Event::FootnoteReference(ref reference) => { + debug!("FootnoteReference"); let entry = parser.get_entry(reference.as_ref()); buffer.push_str(&format!("{0}\ ", (*entry).1)); } + Event::HardBreak => { + debug!("HardBreak"); + if shorter.is_fancy() { + buffer.push_str("
"); + } else if !buffer.is_empty() { + buffer.push(' '); + } + } Event::Html(h) | Event::InlineHtml(h) => { + debug!("Html/InlineHtml"); buffer.push_str(&*h); } _ => {} diff --git a/src/librustdoc/passes/unindent_comments.rs b/src/librustdoc/passes/unindent_comments.rs index 4d94c308478..59fef8d2027 100644 --- a/src/librustdoc/passes/unindent_comments.rs +++ b/src/librustdoc/passes/unindent_comments.rs @@ -82,7 +82,7 @@ fn unindent(s: &str) -> String { }); if !lines.is_empty() { - let mut unindented = vec![ lines[0].trim().to_string() ]; + let mut unindented = vec![ lines[0].trim_left().to_string() ]; unindented.extend_from_slice(&lines[1..].iter().map(|&line| { if line.chars().all(|c| c.is_whitespace()) { line.to_string() @@ -160,4 +160,15 @@ mod unindent_tests { let r = unindent(&s); assert_eq!(r, "line1\nline2"); } + + #[test] + fn should_not_trim() { + let s = "\t line1 \n\t line2".to_string(); + let r = unindent(&s); + assert_eq!(r, "line1 \nline2"); + + let s = " \tline1 \n \tline2".to_string(); + let r = unindent(&s); + assert_eq!(r, "line1 \nline2"); + } } diff --git a/src/test/rustdoc/check-hard-break.rs b/src/test/rustdoc/check-hard-break.rs new file mode 100644 index 00000000000..4604639c3c8 --- /dev/null +++ b/src/test/rustdoc/check-hard-break.rs @@ -0,0 +1,19 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name = "foo"] + +// ignore-tidy-linelength + +// @has foo/fn.f.html +// @has - '

hard break:
after hard break

' +/// hard break: +/// after hard break +pub fn f() {}