From effe4559d23b117f41448007fd870726f86d17d7 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 9 Mar 2012 17:52:06 -0800 Subject: [PATCH] rustdoc: Accept the first sentence as the brief description --- src/rustdoc/desc_to_brief_pass.rs | 43 +++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/rustdoc/desc_to_brief_pass.rs b/src/rustdoc/desc_to_brief_pass.rs index 429f0c0a05a..4ff63120efb 100644 --- a/src/rustdoc/desc_to_brief_pass.rs +++ b/src/rustdoc/desc_to_brief_pass.rs @@ -108,20 +108,32 @@ fn parse_desc(desc: str) -> option { const max_brief_len: uint = 120u; - let paras = paragraphs(desc); - - if check vec::is_not_empty(paras) { - let maybe_brief = vec::head(paras); - if str::len(maybe_brief) <= max_brief_len { - some(maybe_brief) + alt first_sentence(desc) { + some(first_sentence) { + if str::len(first_sentence) <= max_brief_len { + some(first_sentence) } else { none } + } + none { none } + } +} + +fn first_sentence(s: str) -> option { + let paras = paragraphs(s); + if vec::is_not_empty(paras) { + let first = vec::head(sentences(vec::head(paras))); + some(str::replace(first, "\n", " ")) } else { none } } +fn sentences(s: str) -> [str] { + str::split_char(s, '.') +} + fn paragraphs(s: str) -> [str] { let lines = str::lines_any(s); let whitespace_lines = 0; @@ -180,8 +192,8 @@ fn should_promote_short_descs() { #[test] fn should_not_promote_long_descs() { let desc = some("Warkworth Castle is a ruined medieval building -in the town of the same name in the English county of Northumberland. -The town and castle occupy a loop of the River Coquet, less than a mile +in the town of the same name in the English county of Northumberland, +and the town and castle occupy a loop of the River Coquet, less than a mile from England's north-east coast. When the castle was founded is uncertain, but traditionally its construction has been ascribed to Prince Henry of Scotland in the mid 12th century, although it may have been built by @@ -190,3 +202,18 @@ counties."); let brief = extract(desc); assert brief == none; } + +#[test] +fn should_promote_first_sentence() { + let desc = some("Warkworth Castle is a ruined medieval building +in the town. of the same name in the English county of Northumberland, +and the town and castle occupy a loop of the River Coquet, less than a mile +from England's north-east coast. When the castle was founded is uncertain, +but traditionally its construction has been ascribed to Prince Henry of +Scotland in the mid 12th century, although it may have been built by +King Henry II of England when he took control of England'snorthern +counties."); + let brief = extract(desc); + assert brief == some( + "Warkworth Castle is a ruined medieval building in the town"); +} \ No newline at end of file