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
|
use crate::entities::*;
use pandoc_types::definition::{Attr, Block, Inline, Meta, MetaValue, Pandoc};
use std::collections::HashMap;
pub(crate) fn into_pandoc<'e>(
entity: &'e dyn Entity,
description: &Description,
descriptions: &HashMap<Usr, Description>,
) -> (Pandoc, HashMap<&'e Usr, &'e DynEntity>) {
let mut meta = Meta::null();
let title = vec![Inline::Code(Attr::null(), description.name.clone())];
meta.0.insert(
"title".to_string(),
MetaValue::MetaString(description.name.clone()),
);
let mut content = Vec::new();
content.push(Block::Header(1, Attr::null(), title));
if !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(description.detailed.clone())],
));
}
let separate_children = entity.separate_children();
let embeddable_children = entity.embeddable_children();
for section in &separate_children {
if let Some(members_list) = member_list(
section.children.iter().map(|&(usr, _child)| usr),
descriptions,
) {
content.push(Block::Header(
2,
Attr::null(),
vec![Inline::Str(String::from(section.name))],
));
content.push(members_list);
}
}
let mut embedded_documentation = Vec::new();
for section in &embeddable_children {
if let Some(members_list) = member_list(
section.children.iter().map(|&(usr, _child)| usr),
descriptions,
) {
content.push(Block::Header(
2,
Attr::null(),
vec![Inline::Str(String::from(section.name))],
));
content.push(members_list);
embedded_documentation.push(Block::Header(
2,
Attr::null(),
vec![Inline::Str(String::from(section.name) + " Documentation")],
));
for (usr, _child) in §ion.children {
let child_doc = descriptions.get(usr).unwrap();
embedded_documentation.push(Block::Header(
3,
Attr::null(),
vec![Inline::Code(Attr::null(), String::from(&child_doc.name))],
));
embedded_documentation.push(Block::Div(
Attr(String::new(), vec![String::from("doc")], vec![]),
vec![raw_markdown(child_doc.detailed.clone())],
));
}
}
}
content.append(&mut embedded_documentation);
let leftovers = separate_children
.iter()
.map(|section| section.children.clone())
.flatten()
.collect();
(Pandoc(meta, content), leftovers)
}
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))
}
}
|