1
Fork 0

rustdoc: Accept the first sentence as the brief description

This commit is contained in:
Brian Anderson 2012-03-09 17:52:06 -08:00
parent 4fc5b822e2
commit effe4559d2

View file

@ -108,20 +108,32 @@ fn parse_desc(desc: str) -> option<str> {
const max_brief_len: uint = 120u; const max_brief_len: uint = 120u;
let paras = paragraphs(desc); alt first_sentence(desc) {
some(first_sentence) {
if check vec::is_not_empty(paras) { if str::len(first_sentence) <= max_brief_len {
let maybe_brief = vec::head(paras); some(first_sentence)
if str::len(maybe_brief) <= max_brief_len {
some(maybe_brief)
} else { } else {
none none
} }
}
none { none }
}
}
fn first_sentence(s: str) -> option<str> {
let paras = paragraphs(s);
if vec::is_not_empty(paras) {
let first = vec::head(sentences(vec::head(paras)));
some(str::replace(first, "\n", " "))
} else { } else {
none none
} }
} }
fn sentences(s: str) -> [str] {
str::split_char(s, '.')
}
fn paragraphs(s: str) -> [str] { fn paragraphs(s: str) -> [str] {
let lines = str::lines_any(s); let lines = str::lines_any(s);
let whitespace_lines = 0; let whitespace_lines = 0;
@ -180,8 +192,8 @@ fn should_promote_short_descs() {
#[test] #[test]
fn should_not_promote_long_descs() { fn should_not_promote_long_descs() {
let desc = some("Warkworth Castle is a ruined medieval building let desc = some("Warkworth Castle is a ruined medieval building
in the town of the same name in the English county of Northumberland. 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 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, from England's north-east coast. When the castle was founded is uncertain,
but traditionally its construction has been ascribed to Prince Henry of but traditionally its construction has been ascribed to Prince Henry of
Scotland in the mid 12th century, although it may have been built by Scotland in the mid 12th century, although it may have been built by
@ -190,3 +202,18 @@ counties.");
let brief = extract(desc); let brief = extract(desc);
assert brief == none; 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");
}