@@ -108,20 +108,32 @@ fn parse_desc(desc: str) -> option<str> {
108
108
109
109
const max_brief_len: uint = 120 u;
110
110
111
- let paras = paragraphs ( desc) ;
112
-
113
- if check vec:: is_not_empty ( paras) {
114
- let maybe_brief = vec:: head ( paras) ;
115
- if str:: len ( maybe_brief) <= max_brief_len {
116
- some ( maybe_brief)
111
+ alt first_sentence ( desc) {
112
+ some ( first_sentence) {
113
+ if str:: len ( first_sentence) <= max_brief_len {
114
+ some ( first_sentence)
117
115
} else {
118
116
none
119
117
}
118
+ }
119
+ none { none }
120
+ }
121
+ }
122
+
123
+ fn first_sentence ( s : str ) -> option < str > {
124
+ let paras = paragraphs ( s) ;
125
+ if vec:: is_not_empty ( paras) {
126
+ let first = vec:: head ( sentences ( vec:: head ( paras) ) ) ;
127
+ some ( str:: replace ( first, "\n " , " " ) )
120
128
} else {
121
129
none
122
130
}
123
131
}
124
132
133
+ fn sentences ( s : str ) -> [ str ] {
134
+ str:: split_char ( s, '.' )
135
+ }
136
+
125
137
fn paragraphs ( s : str ) -> [ str ] {
126
138
let lines = str:: lines_any ( s) ;
127
139
let whitespace_lines = 0 ;
@@ -180,8 +192,8 @@ fn should_promote_short_descs() {
180
192
#[ test]
181
193
fn should_not_promote_long_descs ( ) {
182
194
let desc = some ( "Warkworth Castle is a ruined medieval building
183
- in the town of the same name in the English county of Northumberland.
184
- The town and castle occupy a loop of the River Coquet, less than a mile
195
+ in the town of the same name in the English county of Northumberland,
196
+ and the town and castle occupy a loop of the River Coquet, less than a mile
185
197
from England's north-east coast. When the castle was founded is uncertain,
186
198
but traditionally its construction has been ascribed to Prince Henry of
187
199
Scotland in the mid 12th century, although it may have been built by
@@ -190,3 +202,18 @@ counties.");
190
202
let brief = extract ( desc) ;
191
203
assert brief == none;
192
204
}
205
+
206
+ #[ test]
207
+ fn should_promote_first_sentence ( ) {
208
+ let desc = some ( "Warkworth Castle is a ruined medieval building
209
+ in the town. of the same name in the English county of Northumberland,
210
+ and the town and castle occupy a loop of the River Coquet, less than a mile
211
+ from England's north-east coast. When the castle was founded is uncertain,
212
+ but traditionally its construction has been ascribed to Prince Henry of
213
+ Scotland in the mid 12th century, although it may have been built by
214
+ King Henry II of England when he took control of England'snorthern
215
+ counties." ) ;
216
+ let brief = extract ( desc) ;
217
+ assert brief == some (
218
+ "Warkworth Castle is a ruined medieval building in the town" ) ;
219
+ }
0 commit comments