]> git.lizzy.rs Git - rust.git/commitdiff
Split rustdoc summary lines in a smarter way
authorSimonas Kazlauskas <git@kazlauskas.me>
Fri, 13 Mar 2015 22:45:39 +0000 (00:45 +0200)
committerSimonas Kazlauskas <git@kazlauskas.me>
Sat, 14 Mar 2015 11:00:19 +0000 (13:00 +0200)
Previously it would fail on a trivial case like

    /// Summary line
    /// <trailing space>
    /// Regular content

Compliant markdown preprocessor would render that as two separate paragraphs, but our summary line
extractor would interpret both lines as the same paragraph and include both into the short summary.

src/librustdoc/html/render.rs

index dba7b16eceecbe922861ed7d3274e009a6d49cde..85c04d76394a5112f3444a03a224c0b8c1455e35 100644 (file)
@@ -407,7 +407,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::Result<String> {
                         ty: shortty(item),
                         name: item.name.clone().unwrap(),
                         path: fqp[..fqp.len() - 1].connect("::"),
-                        desc: shorter(item.doc_value()).to_string(),
+                        desc: shorter(item.doc_value()),
                         parent: Some(did),
                     });
                 },
@@ -876,7 +876,7 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
                         ty: shortty(&item),
                         name: s.to_string(),
                         path: path.connect("::").to_string(),
-                        desc: shorter(item.doc_value()).to_string(),
+                        desc: shorter(item.doc_value()),
                         parent: parent,
                     });
                 }
@@ -1467,13 +1467,14 @@ fn full_path(cx: &Context, item: &clean::Item) -> String {
     return s
 }
 
-fn shorter<'a>(s: Option<&'a str>) -> &'a str {
+fn shorter<'a>(s: Option<&'a str>) -> String {
     match s {
-        Some(s) => match s.find("\n\n") {
-            Some(pos) => &s[..pos],
-            None => s,
-        },
-        None => ""
+        Some(s) => s.lines().take_while(|line|{
+            (*line).chars().any(|chr|{
+                !chr.is_whitespace()
+            })
+        }).collect::<Vec<_>>().connect("\n"),
+        None => "".to_string()
     }
 }
 
@@ -1603,7 +1604,7 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
                     </tr>
                 ",
                 *myitem.name.as_ref().unwrap(),
-                Markdown(shorter(myitem.doc_value())),
+                Markdown(&shorter(myitem.doc_value())[..]),
                 class = shortty(myitem),
                 href = item_path(myitem),
                 title = full_path(cx, myitem),