2012-07-04 23:53:12 +02:00
|
|
|
//! Breaks rustdocs into sections according to their headers
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2012-07-12 00:00:40 +02:00
|
|
|
import doc::item_utils;
|
|
|
|
|
2012-03-09 20:47:31 +01:00
|
|
|
export mk_pass;
|
|
|
|
|
|
|
|
fn mk_pass() -> pass {
|
|
|
|
{
|
2012-07-14 07:57:48 +02:00
|
|
|
name: ~"sectionalize",
|
2012-03-09 20:47:31 +01:00
|
|
|
f: run
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(_srv: astsrv::srv, doc: doc::doc) -> doc::doc {
|
|
|
|
let fold = fold::fold({
|
|
|
|
fold_item: fold_item,
|
2012-07-04 01:30:42 +02:00
|
|
|
fold_trait: fold_trait,
|
2012-03-09 20:47:31 +01:00
|
|
|
fold_impl: fold_impl
|
|
|
|
with *fold::default_any_fold(())
|
|
|
|
});
|
|
|
|
fold.fold_doc(fold, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_item(fold: fold::fold<()>, doc: doc::itemdoc) -> doc::itemdoc {
|
|
|
|
let doc = fold::default_seq_fold_item(fold, doc);
|
|
|
|
let (desc, sections) = sectionalize(doc.desc);
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: desc,
|
|
|
|
sections: sections
|
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 01:30:42 +02:00
|
|
|
fn fold_trait(fold: fold::fold<()>, doc: doc::traitdoc) -> doc::traitdoc {
|
|
|
|
let doc = fold::default_seq_fold_trait(fold, doc);
|
2012-03-09 20:47:31 +01:00
|
|
|
|
|
|
|
{
|
2012-07-13 19:36:35 +02:00
|
|
|
methods: do par::map(doc.methods) |method| {
|
2012-03-09 20:47:31 +01:00
|
|
|
let (desc, sections) = sectionalize(method.desc);
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: desc,
|
|
|
|
sections: sections
|
|
|
|
with method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_impl(fold: fold::fold<()>, doc: doc::impldoc) -> doc::impldoc {
|
|
|
|
let doc = fold::default_seq_fold_impl(fold, doc);
|
|
|
|
|
|
|
|
{
|
2012-07-13 19:36:35 +02:00
|
|
|
methods: do par::map(doc.methods) |method| {
|
2012-03-09 20:47:31 +01:00
|
|
|
let (desc, sections) = sectionalize(method.desc);
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: desc,
|
|
|
|
sections: sections
|
|
|
|
with method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 07:57:48 +02:00
|
|
|
fn sectionalize(desc: option<~str>) -> (option<~str>, ~[doc::section]) {
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2012-07-04 23:53:12 +02:00
|
|
|
/*!
|
|
|
|
* Take a description of the form
|
|
|
|
*
|
|
|
|
* General text
|
|
|
|
*
|
|
|
|
* # Section header
|
|
|
|
*
|
|
|
|
* Section text
|
|
|
|
*
|
|
|
|
* # Section header
|
|
|
|
*
|
|
|
|
* Section text
|
|
|
|
*
|
|
|
|
* and remove each header and accompanying text into section records.
|
|
|
|
*/
|
2012-03-09 20:47:31 +01:00
|
|
|
|
|
|
|
if option::is_none(desc) {
|
2012-06-30 01:26:56 +02:00
|
|
|
ret (none, ~[]);
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let lines = str::lines(option::get(desc));
|
|
|
|
|
2012-07-14 07:57:48 +02:00
|
|
|
let mut new_desc = none::<~str>;
|
2012-03-22 16:39:41 +01:00
|
|
|
let mut current_section = none;
|
2012-06-30 01:26:56 +02:00
|
|
|
let mut sections = ~[];
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2012-07-01 01:19:07 +02:00
|
|
|
for lines.each |line| {
|
2012-03-09 20:47:31 +01:00
|
|
|
alt parse_header(line) {
|
|
|
|
some(header) {
|
|
|
|
if option::is_some(current_section) {
|
2012-06-30 01:26:56 +02:00
|
|
|
sections += ~[option::get(current_section)];
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
current_section = some({
|
|
|
|
header: header,
|
2012-07-14 07:57:48 +02:00
|
|
|
body: ~""
|
2012-03-09 20:47:31 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
none {
|
2012-05-30 01:33:48 +02:00
|
|
|
alt copy current_section {
|
2012-03-09 20:47:31 +01:00
|
|
|
some(section) {
|
|
|
|
current_section = some({
|
2012-07-14 07:57:48 +02:00
|
|
|
body: section.body + ~"\n" + line
|
2012-03-09 20:47:31 +01:00
|
|
|
with section
|
|
|
|
});
|
|
|
|
}
|
|
|
|
none {
|
2012-05-30 01:33:48 +02:00
|
|
|
new_desc = alt new_desc {
|
2012-03-09 20:47:31 +01:00
|
|
|
some(desc) {
|
2012-07-14 07:57:48 +02:00
|
|
|
some(desc + ~"\n" + line)
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
none {
|
2012-05-30 01:33:48 +02:00
|
|
|
some(line)
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
2012-05-30 01:33:48 +02:00
|
|
|
};
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if option::is_some(current_section) {
|
2012-06-30 01:26:56 +02:00
|
|
|
sections += ~[option::get(current_section)];
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
(new_desc, sections)
|
|
|
|
}
|
|
|
|
|
2012-07-14 07:57:48 +02:00
|
|
|
fn parse_header(line: ~str) -> option<~str> {
|
|
|
|
if str::starts_with(line, ~"# ") {
|
2012-03-09 20:47:31 +01:00
|
|
|
some(str::slice(line, 2u, str::len(line)))
|
|
|
|
} else {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_create_section_headers() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"#[doc = \"\
|
2012-03-09 20:47:31 +01:00
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a { }");
|
|
|
|
assert str::contains(
|
|
|
|
doc.cratemod().mods()[0].item.sections[0].header,
|
2012-07-14 07:57:48 +02:00
|
|
|
~"Header");
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_create_section_bodies() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"#[doc = \"\
|
2012-03-09 20:47:31 +01:00
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a { }");
|
|
|
|
assert str::contains(
|
|
|
|
doc.cratemod().mods()[0].item.sections[0].body,
|
2012-07-14 07:57:48 +02:00
|
|
|
~"Body");
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_create_sections_from_indented_headers() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"#[doc = \"\n\
|
2012-03-09 20:47:31 +01:00
|
|
|
Text\n # Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a { }");
|
|
|
|
assert vec::is_empty(doc.cratemod().mods()[0].item.sections);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_remove_section_text_from_main_desc() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"#[doc = \"\
|
2012-03-09 20:47:31 +01:00
|
|
|
Description\n\n\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a { }");
|
|
|
|
assert !str::contains(
|
|
|
|
option::get(doc.cratemod().mods()[0].desc()),
|
2012-07-14 07:57:48 +02:00
|
|
|
~"Header");
|
2012-03-09 20:47:31 +01:00
|
|
|
assert !str::contains(
|
|
|
|
option::get(doc.cratemod().mods()[0].desc()),
|
2012-07-14 07:57:48 +02:00
|
|
|
~"Body");
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_eliminate_desc_if_it_is_just_whitespace() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"#[doc = \"\
|
2012-03-09 20:47:31 +01:00
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a { }");
|
|
|
|
assert doc.cratemod().mods()[0].desc() == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-04 01:30:42 +02:00
|
|
|
fn should_sectionalize_trait_methods() {
|
2012-03-09 20:47:31 +01:00
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"iface i {
|
2012-03-09 20:47:31 +01:00
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
fn a(); }");
|
2012-07-04 01:30:42 +02:00
|
|
|
assert doc.cratemod().traits()[0].methods[0].sections.len() == 1u;
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_sectionalize_impl_methods() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 07:57:48 +02:00
|
|
|
~"impl i for bool {
|
2012-03-09 20:47:31 +01:00
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
fn a() { } }");
|
|
|
|
assert doc.cratemod().impls()[0].methods[0].sections.len() == 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2012-07-14 07:57:48 +02:00
|
|
|
fn mk_doc(source: ~str) -> doc::doc {
|
2012-07-01 01:19:07 +02:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-14 07:57:48 +02:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-03-09 20:47:31 +01:00
|
|
|
let doc = attr_pass::mk_pass().f(srv, doc);
|
|
|
|
run(srv, doc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|