summaryrefslogtreecommitdiffstats
path: root/src/pandoc.rs
blob: 5ca84e732a372381876985dcafdfc3ad9ae9ba19 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//mod types;

use crate::entities::*;

use pandoc_types::definition::{Attr, Block, Inline, Meta, MetaValue, Pandoc};

use std::collections::HashMap;

impl Described<Entity> {
    pub fn into_pandoc(
        self,
        descriptions: &HashMap<Usr, Description>,
    ) -> (Pandoc, HashMap<Usr, Entity>) {
        let mut meta = Meta::null();

        let title = self.title();
        let mut content_before = self.content_before(&descriptions);
        let mut content_after = self.content_after(&descriptions);
        let leftovers = self.leftovers();

        meta.0.insert(
            "title".to_string(),
            MetaValue::MetaString(self.description.name),
        );

        let mut content = Vec::new();

        content.push(Block::Header(1, Attr::null(), title));

        content.append(&mut content_before);

        if !self.description.detailed.is_empty() {
            content.push(Block::Header(
                2,
                Attr::null(),
                vec![Inline::Str(String::from("Description"))],
            ));

            content.push(Block::Div(
                Attr(String::new(), vec![String::from("doc")], vec![]),
                vec![raw_markdown(self.description.detailed)],
            ));
        }

        content.append(&mut content_after);

        (Pandoc(meta, content), leftovers)
    }
}

// TODO: replace with single function so we can move out, and remove all of those clones
trait PandocDisplay {
    fn content_before(&self, _descriptions: &HashMap<Usr, Description>) -> Vec<Block> {
        vec![]
    }

    fn content_after(&self, _descriptions: &HashMap<Usr, Description>) -> Vec<Block> {
        vec![]
    }

    fn leftovers(&self) -> HashMap<Usr, Entity> {
        HashMap::new()
    }
}

impl Described<Entity> {
    fn title(&self) -> Vec<Inline> {
        match &self.entity {
            Entity::Variable(variable) => vec![Inline::Code(
                Attr(String::new(), vec![String::from("cpp")], vec![]),
                variable.r#type.clone() + " " + &self.description.name,
            )],
            _ => vec![Inline::Code(Attr::null(), self.description.name.clone())],
        }
    }
}

impl PandocDisplay for Described<Entity> {
    fn content_before(&self, descriptions: &HashMap<Usr, Description>) -> Vec<Block> {
        match &self.entity {
            Entity::NameSpace(ns) => ns.content_before(descriptions),
            Entity::Variable(var) => var.content_before(descriptions),
            Entity::Function(func) => func.content_before(descriptions),
            Entity::Class(class) => class.content_before(descriptions),
        }
    }

    fn content_after(&self, descriptions: &HashMap<Usr, Description>) -> Vec<Block> {
        match &self.entity {
            Entity::NameSpace(ns) => ns.content_after(descriptions),
            Entity::Variable(var) => var.content_after(descriptions),
            Entity::Function(func) => func.content_after(descriptions),
            Entity::Class(class) => class.content_after(descriptions),
        }
    }

    fn leftovers(&self) -> HashMap<Usr, Entity> {
        match &self.entity {
            Entity::NameSpace(ns) => ns.leftovers(),
            Entity::Variable(var) => var.leftovers(),
            Entity::Function(func) => func.leftovers(),
            Entity::Class(class) => class.leftovers(),
        }
    }
}

impl PandocDisplay for NameSpace {
    fn content_after(&self, descriptions: &HashMap<Usr, Description>) -> Vec<Block> {
        let mut content = Vec::new();

        content.push(Block::Header(
            2,
            Attr::null(),
            vec![Inline::Str("Members".to_string())],
        ));

        if let Some(member_list) = member_list(self.members.keys(), descriptions) {
            content.push(member_list);
        } else {
            content.push(str_block(String::from("None")));
        }

        content
    }

    fn leftovers(&self) -> HashMap<Usr, Entity> {
        self.members.clone()
    }
}

impl PandocDisplay for Class {
    fn content_after(&self, descriptions: &HashMap<Usr, Description>) -> Vec<Block> {
        let mut content = Vec::new();

        if let Some(member_types) = member_list(&self.member_types, descriptions) {
            content.push(Block::Header(
                2,
                Attr::null(),
                vec![Inline::Str("Member Types".to_string())],
            ));

            content.push(member_types);
        }

        if let Some(member_functions) = member_list(self.member_functions.keys(), descriptions) {
            content.push(Block::Header(
                2,
                Attr::null(),
                vec![Inline::Str("Member Functions".to_string())],
            ));

            content.push(member_functions);
        }

        if let Some(member_variables) = member_list(self.member_variables.keys(), descriptions) {
            content.push(Block::Header(
                2,
                Attr::null(),
                vec![Inline::Str("Member Variables".to_string())],
            ));

            content.push(member_variables);
        }

        content
    }

    fn leftovers(&self) -> HashMap<Usr, Entity> {
        self.member_functions
            .iter()
            .map(|(usr, func)| (usr.clone(), Entity::from(func.clone())))
            .chain(
                self.member_variables
                    .iter()
                    .map(|(usr, var)| (usr.clone(), Entity::from(var.clone()))),
            )
            .collect()
    }
}

impl PandocDisplay for Variable {}

impl PandocDisplay for Function {}

fn str_block(content: String) -> Block {
    Block::Plain(vec![Inline::Str(content)])
}

fn entity_link(usr: &Usr, name: String) -> Inline {
    use pandoc_types::definition::Target;
    use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};

    use std::iter::once;

    // https://url.spec.whatwg.org/#fragment-percent-encode-set
    const FRAGMENT: &AsciiSet = &CONTROLS
        .add(b' ')
        .add(b'"')
        .add(b'<')
        .add(b'>')
        .add(b'`')
        .add(b'#')
        .add(b'?')
        .add(b'{')
        .add(b'}');

    Inline::Link(
        Attr::null(),
        vec![Inline::Code(Attr::null(), name)],
        Target(
            once("./")
                .chain(utf8_percent_encode(&usr.0, FRAGMENT))
                .collect(),
            String::new(),
        ),
    )
}

fn raw_markdown(text: String) -> Block {
    use pandoc_types::definition::Format;
    Block::RawBlock(Format(String::from("markdown")), text)
}

fn member_list<'a>(
    members: impl IntoIterator<Item = &'a Usr>,
    descriptions: &HashMap<Usr, Description>,
) -> Option<Block> {
    let definitions: Vec<(Vec<Inline>, Vec<Vec<Block>>)> = members
        .into_iter()
        .filter_map(|usr| {
            let name = &descriptions.get(usr)?.name;
            Some((
                vec![entity_link(usr, name.clone())],
                vec![vec![str_block(
                    descriptions.get(usr).unwrap().brief.clone(),
                )]],
            ))
        })
        .collect();

    if definitions.is_empty() {
        None
    } else {
        Some(Block::DefinitionList(definitions))
    }
}