about summary refs log tree commit diff
path: root/src/rustdoc/markdown_index_pass.rs
blob: ef8acd39df62862b96939a6f143ef0248a9298f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#[doc = "Build indexes as appropriate for the markdown pass"];

export mk_pass;

fn mk_pass(config: config::config) -> pass {
    {
        name: "markdown_index",
        f: fn~(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
            run(srv, doc, config)
        }
    }
}

fn run(
    _srv: astsrv::srv,
    doc: doc::doc,
    config: config::config
) -> doc::doc {
    let fold = fold::fold({
        fold_mod: fold_mod,
        fold_nmod: fold_nmod
        with *fold::default_any_fold(config)
    });
    fold.fold_doc(fold, doc)
}

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_mod_index(doc, fold.ctxt))
        with doc
    }
}

fn fold_nmod(
    fold: fold::fold<config::config>,
    doc: doc::nmoddoc
) -> doc::nmoddoc {

    let doc = fold::default_any_fold_nmod(fold, doc);

    {
        index: some(build_nmod_index(doc, fold.ctxt))
        with doc
    }
}

fn build_mod_index(
    doc: doc::moddoc,
    config: config::config
) -> doc::index {
    {
        entries: par::anymap(doc.items) {|doc|
            item_to_entry(doc, config)
        }
    }
}

fn build_nmod_index(
    doc: doc::nmoddoc,
    config: config::config
) -> doc::index {
    {
        entries: par::anymap(doc.fns) {|doc|
            item_to_entry(doc::fntag(doc), config)
        }
    }
}

fn item_to_entry(
    doc: doc::itemtag,
    config: config::config
) -> doc::index_entry {
    let link = alt doc {
      doc::modtag(_) | doc::nmodtag(_)
      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),
        brief: doc.brief(),
        link: link
    }
}

fn pandoc_header_id(header: str) -> str {

    // http://johnmacfarlane.net/pandoc/README.html#headers

    let header = remove_formatting(header);
    let header = remove_punctuation(header);
    let header = replace_with_hyphens(header);
    let header = convert_to_lowercase(header);
    let header = remove_up_to_first_letter(header);
    let header = maybe_use_section_id(header);
    ret header;

    fn remove_formatting(s: str) -> str {
        str::replace(s, "`", "")
    }
    fn remove_punctuation(s: str) -> str {
        let s = str::replace(s, "<", "");
        let s = str::replace(s, ">", "");
        let s = str::replace(s, "[", "");
        let s = str::replace(s, "]", "");
        let s = str::replace(s, "(", "");
        let s = str::replace(s, ")", "");
        let s = str::replace(s, "@", "");
        let s = str::replace(s, "~", "");
        let s = str::replace(s, "/", "");
        ret s;
    }
    fn replace_with_hyphens(s: str) -> str {
        str::replace(s, " ", "-")
    }
    fn convert_to_lowercase(s: str) -> str { str::to_lower(s) }
    fn remove_up_to_first_letter(s: str) -> str { s }
    fn maybe_use_section_id(s: str) -> str { s }
}

#[test]
fn should_remove_punctuation_from_headers() {
    assert pandoc_header_id("impl foo of bar<A>") == "impl-foo-of-bara";
    assert pandoc_header_id("fn@([~A]/~)") == "fna";
}

#[test]
fn should_index_mod_contents() {
    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",
        brief: none,
        link: "#module-a"
    };
    assert option::get(doc.cratemod().index).entries[1] == {
        kind: "Function",
        name: "b",
        brief: none,
        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",
        brief: none,
        link: "a.html"
    };
    assert option::get(doc.cratemod().index).entries[1] == {
        kind: "Function",
        name: "b",
        brief: none,
        link: "#function-b"
    };
}

#[test]
fn should_index_native_mod_pages() {
    let doc = test::mk_doc(
        config::doc_per_mod,
        "native mod a { }"
    );
    assert option::get(doc.cratemod().index).entries[0] == {
        kind: "Native module",
        name: "a",
        brief: none,
        link: "a.html"
    };
}

#[test]
fn should_add_brief_desc_to_index() {
    let doc = test::mk_doc(
        config::doc_per_mod,
        "#[doc = \"test\"] mod a { }"
    );
    assert option::get(doc.cratemod().index).entries[0].brief == some("test");
}

#[test]
fn should_index_native_mod_contents() {
    let doc = test::mk_doc(
        config::doc_per_crate,
        "native mod a { fn b(); }"
    );
    assert option::get(doc.cratemod().nmods()[0].index).entries[0] == {
        kind: "Function",
        name: "b",
        brief: none,
        link: "#function-b"
    };
}

#[cfg(test)]
mod test {
    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 = attr_pass::mk_pass().f(srv, doc);
            let doc = desc_to_brief_pass::mk_pass().f(srv, doc);
            let doc = path_pass::mk_pass().f(srv, doc);
            run(srv, doc, config)
        }
    }
}