Skip to content

Commit b0fab96

Browse files
Shorten line during rendering instead of in markdown
1 parent b3f0175 commit b0fab96

File tree

2 files changed

+36
-44
lines changed

2 files changed

+36
-44
lines changed

src/librustdoc/html/markdown.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,6 @@ impl MarkdownSummaryLine<'_> {
785785
}
786786

787787
pub fn plain_summary_line(md: &str) -> String {
788-
plain_summary_line_full(md, false)
789-
}
790-
791-
pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
792788
struct ParserWrapper<'a> {
793789
inner: Parser<'a>,
794790
is_in: isize,
@@ -834,21 +830,7 @@ pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
834830
s.push_str(&t);
835831
}
836832
}
837-
if limit_length && s.chars().count() > 60 {
838-
let mut len = 0;
839-
let mut ret = s.split_whitespace()
840-
.take_while(|p| {
841-
// + 1 for the added character after the word.
842-
len += p.chars().count() + 1;
843-
len < 60
844-
})
845-
.collect::<Vec<_>>()
846-
.join(" ");
847-
ret.push('…');
848-
ret
849-
} else {
850-
s
851-
}
833+
s
852834
}
853835

854836
pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {

src/librustdoc/html/render.rs

+35-25
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
732732
ty: item.type_(),
733733
name: item.name.clone().unwrap(),
734734
path: fqp[..fqp.len() - 1].join("::"),
735-
desc: plain_summary_line_short(item.doc_value()),
735+
desc: shorten(plain_summary_line(item.doc_value())),
736736
parent: Some(did),
737737
parent_idx: None,
738738
search_type: get_index_search_type(&item),
@@ -770,7 +770,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
770770
}
771771

772772
let crate_doc = krate.module.as_ref().map(|module| {
773-
plain_summary_line_short(module.doc_value())
773+
shorten(plain_summary_line(module.doc_value()))
774774
}).unwrap_or(String::new());
775775

776776
let mut crate_data = BTreeMap::new();
@@ -1482,7 +1482,7 @@ impl DocFolder for Cache {
14821482
ty: item.type_(),
14831483
name: s.to_string(),
14841484
path: path.join("::"),
1485-
desc: plain_summary_line_short(item.doc_value()),
1485+
desc: shorten(plain_summary_line(item.doc_value())),
14861486
parent,
14871487
parent_idx: None,
14881488
search_type: get_index_search_type(&item),
@@ -1664,7 +1664,7 @@ impl Cache {
16641664
ty: item.type_(),
16651665
name: item_name.to_string(),
16661666
path: path.clone(),
1667-
desc: plain_summary_line_short(item.doc_value()),
1667+
desc: shorten(plain_summary_line(item.doc_value())),
16681668
parent: None,
16691669
parent_idx: None,
16701670
search_type: get_index_search_type(&item),
@@ -2360,29 +2360,39 @@ fn full_path(cx: &Context, item: &clean::Item) -> String {
23602360
s
23612361
}
23622362

2363-
fn shorter(s: Option<&str>) -> String {
2364-
match s {
2365-
Some(s) => s.lines()
2366-
.skip_while(|s| s.chars().all(|c| c.is_whitespace()))
2367-
.take_while(|line|{
2368-
(*line).chars().any(|chr|{
2369-
!chr.is_whitespace()
2370-
})
2371-
}).collect::<Vec<_>>().join("\n"),
2372-
None => String::new()
2373-
}
2374-
}
2375-
23762363
#[inline]
23772364
fn plain_summary_line(s: Option<&str>) -> String {
2378-
let line = shorter(s).replace("\n", " ");
2379-
markdown::plain_summary_line_full(&line[..], false)
2380-
}
2381-
2382-
#[inline]
2383-
fn plain_summary_line_short(s: Option<&str>) -> String {
2384-
let line = shorter(s).replace("\n", " ");
2385-
markdown::plain_summary_line_full(&line[..], true)
2365+
let s = s.unwrap_or("");
2366+
// This essentially gets the first paragraph of text in one line.
2367+
let mut line = s.lines()
2368+
.skip_while(|line| line.chars().all(|c| c.is_whitespace()))
2369+
.take_while(|line| line.chars().any(|c| !c.is_whitespace()))
2370+
.fold(String::new(), |mut acc, line| {
2371+
acc.push_str(line);
2372+
acc.push(' ');
2373+
acc
2374+
});
2375+
// remove final whitespace
2376+
line.pop();
2377+
markdown::plain_summary_line(&line[..])
2378+
}
2379+
2380+
fn shorten(s: String) -> String {
2381+
if s.chars().count() > 60 {
2382+
let mut len = 0;
2383+
let mut ret = s.split_whitespace()
2384+
.take_while(|p| {
2385+
// + 1 for the added character after the word.
2386+
len += p.chars().count() + 1;
2387+
len < 60
2388+
})
2389+
.collect::<Vec<_>>()
2390+
.join(" ");
2391+
ret.push('…');
2392+
ret
2393+
} else {
2394+
s
2395+
}
23862396
}
23872397

23882398
fn document(w: &mut fmt::Formatter<'_>, cx: &Context, item: &clean::Item) -> fmt::Result {

0 commit comments

Comments
 (0)