Skip to content

Commit 3d67939

Browse files
committed
rustdoc: Begin constructing indexes
1 parent 3923c8e commit 3d67939

File tree

5 files changed

+130
-23
lines changed

5 files changed

+130
-23
lines changed

src/rustdoc/doc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,12 @@ Fields:
122122
123123
* kind - The type of thing being indexed, e.g. 'Module'
124124
* name - The name of the thing
125-
* brief - A description
126125
* link - A format-specific string representing the link target
127126
128127
"]
129128
type index_entry = {
130129
kind: str,
131130
name: str,
132-
brief: str,
133131
link: str
134132
};
135133

src/rustdoc/markdown_index_pass.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#[doc = "Build indexes as appropriate for the markdown pass"];
2+
3+
export mk_pass;
4+
5+
fn mk_pass() -> pass {
6+
{
7+
name: "markdown_index",
8+
f: run
9+
}
10+
}
11+
12+
fn run(_srv: astsrv::srv, doc: doc::cratedoc) -> doc::cratedoc {
13+
let fold = fold::fold({
14+
fold_mod: fold_mod
15+
with *fold::default_any_fold(())
16+
});
17+
fold.fold_crate(fold, doc)
18+
}
19+
20+
fn fold_mod(fold: fold::fold<()>, doc: doc::moddoc) -> doc::moddoc {
21+
22+
let doc = fold::default_any_fold_mod(fold, doc);
23+
24+
{
25+
index: some(build_index(doc))
26+
with doc
27+
}
28+
}
29+
30+
fn build_index(doc: doc::moddoc) -> doc::index {
31+
{
32+
entries: par::anymap(doc.items, item_to_entry)
33+
}
34+
}
35+
36+
fn item_to_entry(doc: doc::itemtag) -> doc::index_entry {
37+
{
38+
kind: markdown_pass::header_kind(doc),
39+
name: markdown_pass::header_name(doc),
40+
link: pandoc_header_id(markdown_pass::header_text(doc))
41+
}
42+
}
43+
44+
fn pandoc_header_id(header: str) -> str {
45+
46+
// http://johnmacfarlane.net/pandoc/README.html#headers
47+
48+
let header = remove_formatting(header);
49+
let header = remove_punctuation(header);
50+
let header = replace_with_hyphens(header);
51+
let header = convert_to_lowercase(header);
52+
let header = remove_up_to_first_letter(header);
53+
let header = maybe_use_section_id(header);
54+
ret header;
55+
56+
fn remove_formatting(s: str) -> str { s }
57+
fn remove_punctuation(s: str) -> str {
58+
str::replace(s, "`", "")
59+
}
60+
fn replace_with_hyphens(s: str) -> str {
61+
str::replace(s, " ", "-")
62+
}
63+
fn convert_to_lowercase(s: str) -> str { str::to_lower(s) }
64+
fn remove_up_to_first_letter(s: str) -> str { s }
65+
fn maybe_use_section_id(s: str) -> str { s }
66+
}
67+
68+
#[test]
69+
fn should_index_mod_contents() {
70+
let doc = test::mk_doc("mod a { } fn b() { }");
71+
assert option::get(doc.topmod.index).entries[0] == {
72+
kind: "Module",
73+
name: "a",
74+
link: "module-a"
75+
};
76+
assert option::get(doc.topmod.index).entries[1] == {
77+
kind: "Function",
78+
name: "b",
79+
link: "function-b"
80+
};
81+
}
82+
83+
#[cfg(test)]
84+
mod test {
85+
fn mk_doc(source: str) -> doc::cratedoc {
86+
astsrv::from_str(source) {|srv|
87+
let doc = extract::from_srv(srv, "");
88+
let doc = path_pass::mk_pass().f(srv, doc);
89+
run(srv, doc)
90+
}
91+
}
92+
}

src/rustdoc/markdown_pass.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import markdown_writer::writer;
44
import markdown_writer::writer_util;
55

66
export mk_pass;
7+
export header_kind, header_name, header_text;
78

89
fn mk_pass(config: config::config) -> pass {
910
mk_pass_(config, markdown_writer::make_writer(config))
@@ -116,59 +117,73 @@ fn write_header_(ctxt: ctxt, lvl: hlvl, title: str) {
116117
ctxt.w.write_line("");
117118
}
118119

119-
fn header_text(doc: doc::itemtag) -> str {
120-
let fullpath = str::connect(doc.path() + [doc.name()], "::");
120+
fn header_kind(doc: doc::itemtag) -> str {
121121
alt doc {
122122
doc::modtag(_) {
123123
if doc.id() == rustc::syntax::ast::crate_node_id {
124-
header_text_("Crate", doc.name())
124+
"Crate"
125125
} else {
126-
header_text_("Module", fullpath)
126+
"Module"
127127
}
128128
}
129129
doc::nmodtag(_) {
130-
header_text_("Native module", fullpath)
130+
"Native module"
131131
}
132132
doc::fntag(_) {
133-
header_text_("Function", doc.name())
133+
"Function"
134134
}
135135
doc::consttag(_) {
136-
header_text_("Const", doc.name())
136+
"Const"
137137
}
138138
doc::enumtag(_) {
139-
header_text_("Enum", doc.name())
139+
"Enum"
140140
}
141141
doc::restag(_) {
142-
header_text_("Resource", doc.name())
142+
"Resource"
143143
}
144144
doc::ifacetag(_) {
145-
header_text_("Interface", doc.name())
145+
"Interface"
146+
}
147+
doc::impltag(doc) {
148+
"Implementation"
149+
}
150+
doc::tytag(_) {
151+
"Type"
152+
}
153+
}
154+
}
155+
156+
fn header_name(doc: doc::itemtag) -> str {
157+
let fullpath = str::connect(doc.path() + [doc.name()], "::");
158+
alt doc {
159+
doc::modtag(_) if doc.id() != rustc::syntax::ast::crate_node_id {
160+
fullpath
161+
}
162+
doc::nmodtag(_) {
163+
fullpath
146164
}
147165
doc::impltag(doc) {
148166
assert option::is_some(doc.self_ty);
149167
let self_ty = option::get(doc.self_ty);
150168
alt doc.iface_ty {
151169
some(iface_ty) {
152-
header_text_(
153-
"Implementation",
154-
#fmt("%s of %s for %s",
155-
doc.name(), iface_ty, self_ty)
156-
)
170+
#fmt("%s of %s for %s", doc.name(), iface_ty, self_ty)
157171
}
158172
none {
159-
header_text_(
160-
"Implementation",
161-
#fmt("%s for %s", doc.name(), self_ty)
162-
)
173+
#fmt("%s for %s", doc.name(), self_ty)
163174
}
164175
}
165176
}
166-
doc::tytag(_) {
167-
header_text_("Type", doc.name())
177+
_ {
178+
doc.name()
168179
}
169180
}
170181
}
171182

183+
fn header_text(doc: doc::itemtag) -> str {
184+
header_text_(header_kind(doc), header_name(doc))
185+
}
186+
172187
fn header_text_(kind: str, name: str) -> str {
173188
#fmt("%s `%s`", kind, name)
174189
}

src/rustdoc/rustdoc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod parse;
1717
mod extract;
1818
mod attr_parser;
1919
mod doc;
20+
mod markdown_index_pass;
2021
mod markdown_pass;
2122
mod markdown_writer;
2223
mod fold;

src/rustdoc/rustdoc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ fn run(config: config::config) {
147147
unindent_pass::mk_pass(),
148148
sort_item_name_pass::mk_pass(),
149149
sort_item_type_pass::mk_pass(),
150+
markdown_index_pass::mk_pass(),
150151
markdown_pass::mk_pass(config)
151152
]);
152153
}

0 commit comments

Comments
 (0)