From ddbd02aaf2804b39762bdbe5e4c6b73b84f139bb Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Mar 2012 14:50:15 -0700 Subject: [PATCH] rustdoc: Allow elipses to appear in brief descriptions. Closes #2003 --- src/rustdoc/desc_to_brief_pass.rs | 67 ++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/src/rustdoc/desc_to_brief_pass.rs b/src/rustdoc/desc_to_brief_pass.rs index 4ff63120efb..2f3262f459b 100644 --- a/src/rustdoc/desc_to_brief_pass.rs +++ b/src/rustdoc/desc_to_brief_pass.rs @@ -123,15 +123,42 @@ fn parse_desc(desc: str) -> option { 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", " ")) + let first_para = vec::head(paras); + some(str::replace(first_sentence_(first_para), "\n", " ")) } else { none } } -fn sentences(s: str) -> [str] { - str::split_char(s, '.') +fn first_sentence_(s: str) -> str { + let dotcount = 0; + // The index of the character following a single dot. This allows + // Things like [0..1) to appear in the brief description + let idx = str::find(s) {|ch| + if ch == '.' { + dotcount += 1; + false + } else { + if dotcount == 1 { + true + } else { + dotcount = 0; + false + } + } + }; + alt idx { + some(idx) if idx > 2u { + str::slice(s, 0u, idx - 1u) + } + _ { + if str::ends_with(s, ".") { + str::slice(s, 0u, str::len(s)) + } else { + s + } + } + } } fn paragraphs(s: str) -> [str] { @@ -216,4 +243,34 @@ counties."); let brief = extract(desc); assert brief == some( "Warkworth Castle is a ruined medieval building in the town"); -} \ No newline at end of file +} + +#[test] +fn should_not_consider_double_period_to_end_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"); +} + +#[test] +fn should_not_consider_triple_period_to_end_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"); +}