|
| 1 | +#[doc = " |
| 2 | +
|
| 3 | +Pulls a brief description out of a long description. |
| 4 | +
|
| 5 | +If the first paragraph of a long description is short enough then it |
| 6 | +is interpreted as the brief description. |
| 7 | +
|
| 8 | +"]; |
| 9 | + |
| 10 | +export mk_pass; |
| 11 | + |
| 12 | +fn mk_pass() -> pass { |
| 13 | + run |
| 14 | +} |
| 15 | + |
| 16 | +fn run( |
| 17 | + _srv: astsrv::srv, |
| 18 | + doc: doc::cratedoc |
| 19 | +) -> doc::cratedoc { |
| 20 | + let fold = fold::fold({ |
| 21 | + fold_mod: fold_mod, |
| 22 | + fold_const: fold_const, |
| 23 | + fold_fn: fold_fn |
| 24 | + with *fold::default_seq_fold(()) |
| 25 | + }); |
| 26 | + fold.fold_crate(fold, doc) |
| 27 | +} |
| 28 | + |
| 29 | +fn fold_mod(fold: fold::fold<()>, doc: doc::moddoc) -> doc::moddoc { |
| 30 | + let doc = fold::default_seq_fold_mod(fold, doc); |
| 31 | + let (brief, desc) = modify(doc.brief, doc.desc); |
| 32 | + |
| 33 | + ~{ |
| 34 | + brief: brief, |
| 35 | + desc: desc |
| 36 | + with *doc |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn fold_const(fold: fold::fold<()>, doc: doc::constdoc) -> doc::constdoc { |
| 41 | + let doc = fold::default_seq_fold_const(fold, doc); |
| 42 | + let (brief, desc) = modify(doc.brief, doc.desc); |
| 43 | + |
| 44 | + ~{ |
| 45 | + brief: brief, |
| 46 | + desc: desc |
| 47 | + with *doc |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn fold_fn(fold: fold::fold<()>, doc: doc::fndoc) -> doc::fndoc { |
| 52 | + let doc = fold::default_seq_fold_fn(fold, doc); |
| 53 | + let (brief, desc) = modify(doc.brief, doc.desc); |
| 54 | + |
| 55 | + ~{ |
| 56 | + brief: brief, |
| 57 | + desc: desc |
| 58 | + with *doc |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn should_promote_mod_desc() { |
| 64 | + let source = "#[doc(desc = \"desc\")] mod m { }"; |
| 65 | + let srv = astsrv::mk_srv_from_str(source); |
| 66 | + let doc = extract::from_srv(srv, ""); |
| 67 | + let doc = attr_pass::mk_pass()(srv, doc); |
| 68 | + let doc = run(srv, doc); |
| 69 | + assert doc.topmod.mods[0].brief == some("desc"); |
| 70 | + assert doc.topmod.mods[0].desc == none; |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn should_promote_const_desc() { |
| 75 | + let source = "#[doc(desc = \"desc\")] const a: bool = true;"; |
| 76 | + let srv = astsrv::mk_srv_from_str(source); |
| 77 | + let doc = extract::from_srv(srv, ""); |
| 78 | + let doc = attr_pass::mk_pass()(srv, doc); |
| 79 | + let doc = run(srv, doc); |
| 80 | + assert doc.topmod.consts[0].brief == some("desc"); |
| 81 | + assert doc.topmod.consts[0].desc == none; |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +fn should_promote_fn_desc() { |
| 86 | + let source = "#[doc(desc = \"desc\")] fn a() { }"; |
| 87 | + let srv = astsrv::mk_srv_from_str(source); |
| 88 | + let doc = extract::from_srv(srv, ""); |
| 89 | + let doc = attr_pass::mk_pass()(srv, doc); |
| 90 | + let doc = run(srv, doc); |
| 91 | + assert doc.topmod.fns[0].brief == some("desc"); |
| 92 | + assert doc.topmod.fns[0].desc == none; |
| 93 | +} |
| 94 | + |
| 95 | +fn modify( |
| 96 | + brief: option<str>, |
| 97 | + desc: option<str> |
| 98 | +) -> (option<str>, option<str>) { |
| 99 | + |
| 100 | + if option::is_some(brief) || option::is_none(desc) { |
| 101 | + ret (brief, desc); |
| 102 | + } |
| 103 | + |
| 104 | + parse_desc(option::get(desc)) |
| 105 | +} |
| 106 | + |
| 107 | +fn parse_desc(desc: str) -> (option<str>, option<str>) { |
| 108 | + |
| 109 | + const max_brief_len: uint = 120u; |
| 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::char_len(maybe_brief) <= max_brief_len { |
| 116 | + let desc_paras = vec::tail(paras); |
| 117 | + let desc = if vec::is_not_empty(desc_paras) { |
| 118 | + some(str::connect(desc_paras, "\n\n")) |
| 119 | + } else { |
| 120 | + none |
| 121 | + }; |
| 122 | + (some(maybe_brief), desc) |
| 123 | + } else { |
| 124 | + (none, some(desc)) |
| 125 | + } |
| 126 | + } else { |
| 127 | + (none, none) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +fn paragraphs(s: str) -> [str] { |
| 132 | + let lines = str::lines_any(s); |
| 133 | + let whitespace_lines = 0; |
| 134 | + let accum = ""; |
| 135 | + let paras = vec::foldl([], lines) {|paras, line| |
| 136 | + let res = paras; |
| 137 | + |
| 138 | + if str::is_whitespace(line) { |
| 139 | + whitespace_lines += 1; |
| 140 | + } else { |
| 141 | + if whitespace_lines > 0 { |
| 142 | + if str::is_not_empty(accum) { |
| 143 | + res += [accum]; |
| 144 | + accum = ""; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + whitespace_lines = 0; |
| 149 | + |
| 150 | + accum = if str::is_empty(accum) { |
| 151 | + line |
| 152 | + } else { |
| 153 | + accum + "\n" + line |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + res |
| 158 | + }; |
| 159 | + |
| 160 | + if str::is_not_empty(accum) { |
| 161 | + paras + [accum] |
| 162 | + } else { |
| 163 | + paras |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +#[test] |
| 168 | +fn test_paragraphs_1() { |
| 169 | + let paras = paragraphs("1\n\n2"); |
| 170 | + assert paras == ["1", "2"]; |
| 171 | +} |
| 172 | + |
| 173 | +#[test] |
| 174 | +fn test_paragraphs_2() { |
| 175 | + let paras = paragraphs("\n\n1\n1\n\n2\n\n"); |
| 176 | + assert paras == ["1\n1", "2"]; |
| 177 | +} |
| 178 | + |
| 179 | +#[test] |
| 180 | +fn should_promote_short_descs() { |
| 181 | + let brief = none; |
| 182 | + let desc = some("desc"); |
| 183 | + let (newbrief, newdesc) = modify(brief, desc); |
| 184 | + assert newbrief == desc; |
| 185 | + assert newdesc == none; |
| 186 | +} |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn should_not_promote_long_descs() { |
| 190 | + let brief = none; |
| 191 | + let desc = some("Warkworth Castle is a ruined medieval building |
| 192 | +in the town of the same name in the English county of Northumberland. |
| 193 | +The town and castle occupy a loop of the River Coquet, less than a mile |
| 194 | +from England's north-east coast. When the castle was founded is uncertain, |
| 195 | +but traditionally its construction has been ascribed to Prince Henry of |
| 196 | +Scotland in the mid 12th century, although it may have been built by |
| 197 | +King Henry II of England when he took control of England'snorthern |
| 198 | +counties."); |
| 199 | + let (newbrief, _) = modify(brief, desc); |
| 200 | + assert newbrief == none; |
| 201 | +} |
| 202 | + |
| 203 | +#[test] |
| 204 | +fn should_not_promote_descs_over_brief() { |
| 205 | + let brief = some("brief"); |
| 206 | + let desc = some("desc"); |
| 207 | + let (newbrief, newdesc) = modify(brief, desc); |
| 208 | + assert newbrief == brief; |
| 209 | + assert newdesc == desc; |
| 210 | +} |
| 211 | + |
| 212 | +#[test] |
| 213 | +fn should_extract_brief_from_desc() { |
| 214 | + let brief = none; |
| 215 | + let desc = some("brief\n\ndesc"); |
| 216 | + let (newbrief, newdesc) = modify(brief, desc); |
| 217 | + assert newbrief == some("brief"); |
| 218 | + assert newdesc == some("desc"); |
| 219 | +} |
0 commit comments