2012-12-04 01:48:01 +01:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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-12-23 23:41:37 +01:00
|
|
|
use astsrv;
|
2012-09-19 01:48:40 +02:00
|
|
|
use doc::ItemUtils;
|
2012-12-23 23:41:37 +01:00
|
|
|
use doc;
|
2012-12-06 00:06:54 +01:00
|
|
|
use fold::Fold;
|
2012-12-23 23:41:37 +01:00
|
|
|
use fold;
|
2013-01-09 04:37:25 +01:00
|
|
|
use pass::Pass;
|
2012-12-23 23:41:37 +01:00
|
|
|
|
2012-11-20 03:00:12 +01:00
|
|
|
pub fn mk_pass() -> Pass {
|
2013-01-08 23:00:45 +01:00
|
|
|
Pass {
|
2012-07-14 07:57:48 +02:00
|
|
|
name: ~"sectionalize",
|
2012-03-09 20:47:31 +01:00
|
|
|
f: run
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:32:36 +01:00
|
|
|
pub fn run(_srv: astsrv::Srv, doc: doc::Doc) -> doc::Doc {
|
2012-12-06 00:06:54 +01:00
|
|
|
let fold = Fold {
|
2012-03-09 20:47:31 +01:00
|
|
|
fold_item: fold_item,
|
2012-07-04 01:30:42 +02:00
|
|
|
fold_trait: fold_trait,
|
2012-09-04 22:29:32 +02:00
|
|
|
fold_impl: fold_impl,
|
2012-12-06 00:06:54 +01:00
|
|
|
.. fold::default_any_fold(())
|
|
|
|
};
|
2012-11-30 02:51:16 +01:00
|
|
|
(fold.fold_doc)(&fold, doc)
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 04:32:36 +01:00
|
|
|
fn fold_item(fold: &fold::Fold<()>, doc: doc::ItemDoc) -> doc::ItemDoc {
|
2012-03-09 20:47:31 +01:00
|
|
|
let doc = fold::default_seq_fold_item(fold, doc);
|
2013-01-31 03:52:31 +01:00
|
|
|
let (desc, sections) = sectionalize(copy doc.desc);
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2013-01-26 01:57:39 +01:00
|
|
|
doc::ItemDoc {
|
2012-03-09 20:47:31 +01:00
|
|
|
desc: desc,
|
2012-09-04 22:29:32 +02:00
|
|
|
sections: sections,
|
|
|
|
.. doc
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:32:36 +01:00
|
|
|
fn fold_trait(fold: &fold::Fold<()>, doc: doc::TraitDoc) -> doc::TraitDoc {
|
2012-07-04 01:30:42 +02:00
|
|
|
let doc = fold::default_seq_fold_trait(fold, doc);
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2013-01-26 01:57:39 +01:00
|
|
|
doc::TraitDoc {
|
2013-02-01 02:12:29 +01:00
|
|
|
methods: do doc.methods.map |method| {
|
2013-01-31 03:52:31 +01:00
|
|
|
let (desc, sections) = sectionalize(copy method.desc);
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2013-01-26 01:57:39 +01:00
|
|
|
doc::MethodDoc {
|
2012-03-09 20:47:31 +01:00
|
|
|
desc: desc,
|
2012-09-04 22:29:32 +02:00
|
|
|
sections: sections,
|
2013-01-30 22:14:35 +01:00
|
|
|
.. copy *method
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
2012-09-04 22:29:32 +02:00
|
|
|
},
|
|
|
|
.. doc
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:32:36 +01:00
|
|
|
fn fold_impl(fold: &fold::Fold<()>, doc: doc::ImplDoc) -> doc::ImplDoc {
|
2012-03-09 20:47:31 +01:00
|
|
|
let doc = fold::default_seq_fold_impl(fold, doc);
|
|
|
|
|
2013-01-26 01:57:39 +01:00
|
|
|
doc::ImplDoc {
|
2013-02-01 02:12:29 +01:00
|
|
|
methods: do doc.methods.map |method| {
|
2013-01-31 03:52:31 +01:00
|
|
|
let (desc, sections) = sectionalize(copy method.desc);
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2013-01-26 01:57:39 +01:00
|
|
|
doc::MethodDoc {
|
2012-03-09 20:47:31 +01:00
|
|
|
desc: desc,
|
2012-09-04 22:29:32 +02:00
|
|
|
sections: sections,
|
2013-01-30 22:14:35 +01:00
|
|
|
.. copy *method
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
2012-09-04 22:29:32 +02:00
|
|
|
},
|
|
|
|
.. doc
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:32:36 +01: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
|
|
|
|
2012-09-22 04:37:57 +02:00
|
|
|
if desc.is_none() {
|
2012-08-20 21:23:37 +02:00
|
|
|
return (None, ~[]);
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
2013-03-26 04:39:10 +01:00
|
|
|
let mut lines = ~[];
|
|
|
|
for str::each_line_any(*desc.get_ref()) |line| { lines.push(line.to_owned()); }
|
2012-03-09 20:47:31 +01:00
|
|
|
|
2012-08-20 21:23:37 +02:00
|
|
|
let mut new_desc = None::<~str>;
|
|
|
|
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| {
|
2013-01-30 22:14:35 +01:00
|
|
|
match parse_header(copy *line) {
|
2012-08-20 21:23:37 +02:00
|
|
|
Some(header) => {
|
2012-09-22 04:37:57 +02:00
|
|
|
if current_section.is_some() {
|
2013-01-30 22:14:35 +01:00
|
|
|
sections += ~[(¤t_section).get()];
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
2013-01-26 01:57:39 +01:00
|
|
|
current_section = Some(doc::Section {
|
2012-03-09 20:47:31 +01:00
|
|
|
header: header,
|
2012-07-14 07:57:48 +02:00
|
|
|
body: ~""
|
2012-03-09 20:47:31 +01:00
|
|
|
});
|
|
|
|
}
|
2012-08-20 21:23:37 +02:00
|
|
|
None => {
|
2012-08-06 21:34:08 +02:00
|
|
|
match copy current_section {
|
2012-08-20 21:23:37 +02:00
|
|
|
Some(section) => {
|
2013-01-26 01:57:39 +01:00
|
|
|
current_section = Some(doc::Section {
|
2012-09-20 01:55:01 +02:00
|
|
|
body: section.body + ~"\n" + *line,
|
2012-09-04 22:29:32 +02:00
|
|
|
.. section
|
2012-03-09 20:47:31 +01:00
|
|
|
});
|
|
|
|
}
|
2012-08-20 21:23:37 +02:00
|
|
|
None => {
|
2013-01-30 22:14:35 +01:00
|
|
|
new_desc = match copy new_desc {
|
2012-08-20 21:23:37 +02:00
|
|
|
Some(desc) => {
|
2012-09-20 01:55:01 +02:00
|
|
|
Some(desc + ~"\n" + *line)
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
2012-08-20 21:23:37 +02:00
|
|
|
None => {
|
2013-01-30 22:14:35 +01:00
|
|
|
Some(copy *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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-22 04:37:57 +02:00
|
|
|
if current_section.is_some() {
|
|
|
|
sections += ~[current_section.get()];
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
(new_desc, sections)
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:32:36 +01:00
|
|
|
fn parse_header(line: ~str) -> Option<~str> {
|
2012-07-14 07:57:48 +02:00
|
|
|
if str::starts_with(line, ~"# ") {
|
2013-03-21 12:36:21 +01:00
|
|
|
Some(str::slice(line, 2u, str::len(line)).to_owned())
|
2012-03-09 20:47:31 +01:00
|
|
|
} else {
|
2012-08-20 21:23:37 +02:00
|
|
|
None
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-04-15 17:09:55 +02:00
|
|
|
mod test {
|
2012-12-30 06:17:06 +01:00
|
|
|
use astsrv;
|
|
|
|
use attr_pass;
|
|
|
|
use doc;
|
|
|
|
use extract;
|
2013-01-09 04:37:25 +01:00
|
|
|
use sectionalize_pass::run;
|
2012-12-30 06:17:06 +01:00
|
|
|
|
2013-04-15 17:09:55 +02:00
|
|
|
fn mk_doc(source: ~str) -> doc::Doc {
|
2013-01-30 22:14:35 +01:00
|
|
|
do astsrv::from_str(copy source) |srv| {
|
2013-02-01 05:24:03 +01:00
|
|
|
let doc = extract::from_srv(srv.clone(), ~"");
|
|
|
|
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
|
|
|
|
run(srv.clone(), doc)
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-15 17:09:55 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_create_section_headers() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a {
|
|
|
|
}");
|
|
|
|
assert!(str::contains(
|
|
|
|
doc.cratemod().mods()[0].item.sections[0].header,
|
|
|
|
~"Header"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_create_section_bodies() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a {
|
|
|
|
}");
|
|
|
|
assert!(str::contains(
|
|
|
|
doc.cratemod().mods()[0].item.sections[0].body,
|
|
|
|
~"Body"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_create_sections_from_indented_headers() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \"\n\
|
|
|
|
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 = mk_doc(
|
|
|
|
~"#[doc = \"\
|
|
|
|
Description\n\n\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a {
|
|
|
|
}");
|
|
|
|
assert!(!str::contains(
|
|
|
|
doc.cratemod().mods()[0].desc().get(),
|
|
|
|
~"Header"));
|
|
|
|
assert!(!str::contains(
|
|
|
|
doc.cratemod().mods()[0].desc().get(),
|
|
|
|
~"Body"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_eliminate_desc_if_it_is_just_whitespace() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
mod a {
|
|
|
|
}");
|
|
|
|
assert!(doc.cratemod().mods()[0].desc() == None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_sectionalize_trait_methods() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"trait i {
|
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
fn a(); }");
|
|
|
|
assert!(doc.cratemod().traits()[0].methods[0].sections.len() == 1u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_sectionalize_impl_methods() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl bool {
|
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body\"]\
|
|
|
|
fn a() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].methods[0].sections.len() == 1u);
|
|
|
|
}
|
2012-03-09 20:47:31 +01:00
|
|
|
}
|