Shorten line during rendering instead of in markdown
This commit is contained in:
parent
b3f01753b0
commit
b0fab966fa
2 changed files with 34 additions and 42 deletions
|
@ -785,10 +785,6 @@ impl MarkdownSummaryLine<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn plain_summary_line(md: &str) -> String {
|
pub fn plain_summary_line(md: &str) -> String {
|
||||||
plain_summary_line_full(md, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
|
|
||||||
struct ParserWrapper<'a> {
|
struct ParserWrapper<'a> {
|
||||||
inner: Parser<'a>,
|
inner: Parser<'a>,
|
||||||
is_in: isize,
|
is_in: isize,
|
||||||
|
@ -834,21 +830,7 @@ pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
|
||||||
s.push_str(&t);
|
s.push_str(&t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if limit_length && s.chars().count() > 60 {
|
|
||||||
let mut len = 0;
|
|
||||||
let mut ret = s.split_whitespace()
|
|
||||||
.take_while(|p| {
|
|
||||||
// + 1 for the added character after the word.
|
|
||||||
len += p.chars().count() + 1;
|
|
||||||
len < 60
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(" ");
|
|
||||||
ret.push('…');
|
|
||||||
ret
|
|
||||||
} else {
|
|
||||||
s
|
s
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
|
pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
|
||||||
|
|
|
@ -732,7 +732,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
|
||||||
ty: item.type_(),
|
ty: item.type_(),
|
||||||
name: item.name.clone().unwrap(),
|
name: item.name.clone().unwrap(),
|
||||||
path: fqp[..fqp.len() - 1].join("::"),
|
path: fqp[..fqp.len() - 1].join("::"),
|
||||||
desc: plain_summary_line_short(item.doc_value()),
|
desc: shorten(plain_summary_line(item.doc_value())),
|
||||||
parent: Some(did),
|
parent: Some(did),
|
||||||
parent_idx: None,
|
parent_idx: None,
|
||||||
search_type: get_index_search_type(&item),
|
search_type: get_index_search_type(&item),
|
||||||
|
@ -770,7 +770,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
let crate_doc = krate.module.as_ref().map(|module| {
|
let crate_doc = krate.module.as_ref().map(|module| {
|
||||||
plain_summary_line_short(module.doc_value())
|
shorten(plain_summary_line(module.doc_value()))
|
||||||
}).unwrap_or(String::new());
|
}).unwrap_or(String::new());
|
||||||
|
|
||||||
let mut crate_data = BTreeMap::new();
|
let mut crate_data = BTreeMap::new();
|
||||||
|
@ -1482,7 +1482,7 @@ impl DocFolder for Cache {
|
||||||
ty: item.type_(),
|
ty: item.type_(),
|
||||||
name: s.to_string(),
|
name: s.to_string(),
|
||||||
path: path.join("::"),
|
path: path.join("::"),
|
||||||
desc: plain_summary_line_short(item.doc_value()),
|
desc: shorten(plain_summary_line(item.doc_value())),
|
||||||
parent,
|
parent,
|
||||||
parent_idx: None,
|
parent_idx: None,
|
||||||
search_type: get_index_search_type(&item),
|
search_type: get_index_search_type(&item),
|
||||||
|
@ -1664,7 +1664,7 @@ impl Cache {
|
||||||
ty: item.type_(),
|
ty: item.type_(),
|
||||||
name: item_name.to_string(),
|
name: item_name.to_string(),
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
desc: plain_summary_line_short(item.doc_value()),
|
desc: shorten(plain_summary_line(item.doc_value())),
|
||||||
parent: None,
|
parent: None,
|
||||||
parent_idx: None,
|
parent_idx: None,
|
||||||
search_type: get_index_search_type(&item),
|
search_type: get_index_search_type(&item),
|
||||||
|
@ -2360,29 +2360,39 @@ fn full_path(cx: &Context, item: &clean::Item) -> String {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shorter(s: Option<&str>) -> String {
|
|
||||||
match s {
|
|
||||||
Some(s) => s.lines()
|
|
||||||
.skip_while(|s| s.chars().all(|c| c.is_whitespace()))
|
|
||||||
.take_while(|line|{
|
|
||||||
(*line).chars().any(|chr|{
|
|
||||||
!chr.is_whitespace()
|
|
||||||
})
|
|
||||||
}).collect::<Vec<_>>().join("\n"),
|
|
||||||
None => String::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn plain_summary_line(s: Option<&str>) -> String {
|
fn plain_summary_line(s: Option<&str>) -> String {
|
||||||
let line = shorter(s).replace("\n", " ");
|
let s = s.unwrap_or("");
|
||||||
markdown::plain_summary_line_full(&line[..], false)
|
// This essentially gets the first paragraph of text in one line.
|
||||||
|
let mut line = s.lines()
|
||||||
|
.skip_while(|line| line.chars().all(|c| c.is_whitespace()))
|
||||||
|
.take_while(|line| line.chars().any(|c| !c.is_whitespace()))
|
||||||
|
.fold(String::new(), |mut acc, line| {
|
||||||
|
acc.push_str(line);
|
||||||
|
acc.push(' ');
|
||||||
|
acc
|
||||||
|
});
|
||||||
|
// remove final whitespace
|
||||||
|
line.pop();
|
||||||
|
markdown::plain_summary_line(&line[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
fn shorten(s: String) -> String {
|
||||||
fn plain_summary_line_short(s: Option<&str>) -> String {
|
if s.chars().count() > 60 {
|
||||||
let line = shorter(s).replace("\n", " ");
|
let mut len = 0;
|
||||||
markdown::plain_summary_line_full(&line[..], true)
|
let mut ret = s.split_whitespace()
|
||||||
|
.take_while(|p| {
|
||||||
|
// + 1 for the added character after the word.
|
||||||
|
len += p.chars().count() + 1;
|
||||||
|
len < 60
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ");
|
||||||
|
ret.push('…');
|
||||||
|
ret
|
||||||
|
} else {
|
||||||
|
s
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn document(w: &mut fmt::Formatter<'_>, cx: &Context, item: &clean::Item) -> fmt::Result {
|
fn document(w: &mut fmt::Formatter<'_>, cx: &Context, item: &clean::Item) -> fmt::Result {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue