rustdoc: Write links correctly for multi-page docs

This commit is contained in:
Brian Anderson 2012-03-06 15:57:36 -08:00
parent 801b02b25d
commit a4ff220133
4 changed files with 91 additions and 26 deletions

View File

@ -2,42 +2,68 @@
export mk_pass;
fn mk_pass() -> pass {
fn mk_pass(config: config::config) -> pass {
{
name: "markdown_index",
f: run
f: fn~(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
run(srv, doc, config)
}
}
}
fn run(_srv: astsrv::srv, doc: doc::doc) -> doc::doc {
fn run(
_srv: astsrv::srv,
doc: doc::doc,
config: config::config
) -> doc::doc {
let fold = fold::fold({
fold_mod: fold_mod
with *fold::default_any_fold(())
with *fold::default_any_fold(config)
});
fold.fold_doc(fold, doc)
}
fn fold_mod(fold: fold::fold<()>, doc: doc::moddoc) -> doc::moddoc {
fn fold_mod(
fold: fold::fold<config::config>,
doc: doc::moddoc
) -> doc::moddoc {
let doc = fold::default_any_fold_mod(fold, doc);
{
index: some(build_index(doc))
index: some(build_index(doc, fold.ctxt))
with doc
}
}
fn build_index(doc: doc::moddoc) -> doc::index {
fn build_index(
doc: doc::moddoc,
config: config::config
) -> doc::index {
{
entries: par::anymap(doc.items, item_to_entry)
entries: par::anymap(doc.items) {|item|
item_to_entry(item, config)
}
}
}
fn item_to_entry(doc: doc::itemtag) -> doc::index_entry {
fn item_to_entry(
doc: doc::itemtag,
config: config::config
) -> doc::index_entry {
let link = alt doc {
doc::modtag(_) if config.output_style == config::doc_per_mod {
markdown_writer::make_filename(config, doc::itempage(doc))
}
_ {
"#" + pandoc_header_id(markdown_pass::header_text(doc))
}
};
{
kind: markdown_pass::header_kind(doc),
name: markdown_pass::header_name(doc),
link: pandoc_header_id(markdown_pass::header_text(doc))
link: link
}
}
@ -67,26 +93,51 @@ fn pandoc_header_id(header: str) -> str {
#[test]
fn should_index_mod_contents() {
let doc = test::mk_doc("mod a { } fn b() { }");
let doc = test::mk_doc(
config::doc_per_crate,
"mod a { } fn b() { }"
);
assert option::get(doc.cratemod().index).entries[0] == {
kind: "Module",
name: "a",
link: "module-a"
link: "#module-a"
};
assert option::get(doc.cratemod().index).entries[1] == {
kind: "Function",
name: "b",
link: "function-b"
link: "#function-b"
};
}
#[test]
fn should_index_mod_contents_multi_page() {
let doc = test::mk_doc(
config::doc_per_mod,
"mod a { } fn b() { }"
);
assert option::get(doc.cratemod().index).entries[0] == {
kind: "Module",
name: "a",
link: "a.html"
};
assert option::get(doc.cratemod().index).entries[1] == {
kind: "Function",
name: "b",
link: "#function-b"
};
}
#[cfg(test)]
mod test {
fn mk_doc(source: str) -> doc::doc {
fn mk_doc(output_style: config::output_style, source: str) -> doc::doc {
astsrv::from_str(source) {|srv|
let config = {
output_style: output_style
with config::default_config("whatever")
};
let doc = extract::from_srv(srv, "");
let doc = path_pass::mk_pass().f(srv, doc);
run(srv, doc)
run(srv, doc, config)
}
}
}

View File

@ -283,7 +283,7 @@ fn write_index(ctxt: ctxt, index: doc::index) {
for entry in index.entries {
let header = header_text_(entry.kind, entry.name);
let id = entry.link;
ctxt.w.write_line(#fmt("* [%s](#%s)", header, id));
ctxt.w.write_line(#fmt("* [%s](%s)", header, id));
}
ctxt.w.write_line("");
}
@ -954,6 +954,12 @@ mod test {
fn create_doc_srv(source: str) -> (astsrv::srv, doc::doc) {
astsrv::from_str(source) {|srv|
let config = {
output_style: config::doc_per_crate
with config::default_config("whatever")
};
let doc = extract::from_srv(srv, "");
#debug("doc (extract): %?", doc);
let doc = tystr_pass::mk_pass().f(srv, doc);
@ -962,7 +968,7 @@ mod test {
#debug("doc (path): %?", doc);
let doc = attr_pass::mk_pass().f(srv, doc);
#debug("doc (attr): %?", doc);
let doc = markdown_index_pass::mk_pass().f(srv, doc);
let doc = markdown_index_pass::mk_pass(config).f(srv, doc);
#debug("doc (index): %?", doc);
(srv, doc)
}

View File

@ -4,6 +4,7 @@ export writer_factory;
export writer_util;
export make_writer_factory;
export future_writer_factory;
export make_filename;
enum writeinstr {
write(str),
@ -54,7 +55,7 @@ fn markdown_writer(
config: config::config,
page: doc::page
) -> writer {
let filename = make_filename(config, page);
let filename = make_local_filename(config, page);
generic_writer {|markdown|
write_file(filename, markdown);
}
@ -66,7 +67,7 @@ fn pandoc_writer(
) -> writer {
assert option::is_some(config.pandoc_cmd);
let pandoc_cmd = option::get(config.pandoc_cmd);
let filename = make_filename(config, page);
let filename = make_local_filename(config, page);
let pandoc_args = [
"--standalone",
@ -159,12 +160,18 @@ fn generic_writer(process: fn~(markdown: str)) -> writer {
}
}
fn make_local_filename(
config: config::config,
page: doc::page
) -> str {
let filename = make_filename(config, page);
std::fs::connect(config.output_dir, filename)
}
fn make_filename(
config: config::config,
page: doc::page
) -> str {
import std::fs;
let filename = {
alt page {
doc::cratepage(doc) {
@ -185,7 +192,8 @@ fn make_filename(
config::markdown { "md" }
config::pandoc_html { "html" }
};
fs::connect(config.output_dir, filename + "." + ext)
filename + "." + ext
}
#[test]
@ -198,7 +206,7 @@ fn should_use_markdown_file_name_based_off_crate() {
};
let doc = test::mk_doc("test", "");
let page = doc::cratepage(doc.cratedoc());
let filename = make_filename(config, page);
let filename = make_local_filename(config, page);
assert filename == "output/dir/test.md";
}
@ -212,7 +220,7 @@ fn should_name_html_crate_file_name_index_html_when_doc_per_mod() {
};
let doc = test::mk_doc("", "");
let page = doc::cratepage(doc.cratedoc());
let filename = make_filename(config, page);
let filename = make_local_filename(config, page);
assert filename == "output/dir/index.html";
}
@ -227,7 +235,7 @@ fn should_name_mod_file_names_by_path() {
let doc = test::mk_doc("", "mod a { mod b { } }");
let modb = doc.cratemod().mods()[0].mods()[0];
let page = doc::itempage(doc::modtag(modb));
let filename = make_filename(config, page);
let filename = make_local_filename(config, page);
assert filename == "output/dir/a_b.html";
}

View File

@ -151,7 +151,7 @@ fn run(config: config::config) {
unindent_pass::mk_pass(),
sort_item_name_pass::mk_pass(),
sort_item_type_pass::mk_pass(),
markdown_index_pass::mk_pass(),
markdown_index_pass::mk_pass(config),
page_pass::mk_pass(config.output_style),
markdown_pass::mk_pass(
markdown_writer::make_writer_factory(config)