summaryrefslogtreecommitdiffstats
path: root/src/generator/pandoc.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/generator/pandoc.rs')
-rw-r--r--src/generator/pandoc.rs64
1 files changed, 42 insertions, 22 deletions
diff --git a/src/generator/pandoc.rs b/src/generator/pandoc.rs
index 035acab..fe2e2f0 100644
--- a/src/generator/pandoc.rs
+++ b/src/generator/pandoc.rs
@@ -35,13 +35,7 @@ pub(crate) fn into_pandoc(entity: Entity, config: &Config) -> (Pandoc, BTreeMap<
35 35
36 let entity_kind = entity.kind; 36 let entity_kind = entity.kind;
37 for (children_kind, children) in entity.children { 37 for (children_kind, children) in entity.children {
38 if config 38 if is_inline(config, entity.language, &entity_kind, &children_kind) {
39 .inlines
40 .get(&entity.language)
41 .and_then(|lang_inlines| lang_inlines.get(&entity_kind))
42 .map(|children_inlines| children_inlines.contains(&children_kind))
43 == Some(true)
44 {
45 inline_children.insert(children_kind, children); 39 inline_children.insert(children_kind, children);
46 } else { 40 } else {
47 // By default, do not inline 41 // By default, do not inline
@@ -50,7 +44,7 @@ pub(crate) fn into_pandoc(entity: Entity, config: &Config) -> (Pandoc, BTreeMap<
50 } 44 }
51 45
52 for (section_name, children) in &separate_children { 46 for (section_name, children) in &separate_children {
53 if let Some(members_list) = member_list(children) { 47 if let Some(members_list) = member_list(children, LinkType::External) {
54 content.push(Block::Header( 48 content.push(Block::Header(
55 2, 49 2,
56 Attr::null(), 50 Attr::null(),
@@ -61,10 +55,10 @@ pub(crate) fn into_pandoc(entity: Entity, config: &Config) -> (Pandoc, BTreeMap<
61 } 55 }
62 } 56 }
63 57
64 let mut embedded_documentation = Vec::new(); 58 let mut inline_documentation = Vec::new();
65 59
66 for (section_name, children) in inline_children { 60 for (section_name, children) in inline_children {
67 if let Some(members_list) = member_list(&children) { 61 if let Some(members_list) = member_list(&children, LinkType::Anchor) {
68 content.push(Block::Header( 62 content.push(Block::Header(
69 2, 63 2,
70 Attr::null(), 64 Attr::null(),
@@ -73,20 +67,20 @@ pub(crate) fn into_pandoc(entity: Entity, config: &Config) -> (Pandoc, BTreeMap<
73 67
74 content.push(members_list); 68 content.push(members_list);
75 69
76 embedded_documentation.push(Block::Header( 70 inline_documentation.push(Block::Header(
77 2, 71 2,
78 Attr::null(), 72 Attr::null(),
79 vec![Inline::Str(section_name.0 + " Documentation")], 73 vec![Inline::Str(section_name.0 + " Documentation")],
80 )); 74 ));
81 75
82 for (_id, child) in children { 76 for (id, child) in children {
83 embedded_documentation.push(Block::Header( 77 inline_documentation.push(Block::Header(
84 3, 78 3,
85 Attr::null(), 79 Attr(id.0, vec![], vec![]),
86 vec![Inline::Code(Attr::null(), child.name)], 80 vec![Inline::Code(Attr::null(), child.name)],
87 )); 81 ));
88 82
89 embedded_documentation.push(Block::Div( 83 inline_documentation.push(Block::Div(
90 Attr(String::new(), vec![String::from("doc")], vec![]), 84 Attr(String::new(), vec![String::from("doc")], vec![]),
91 vec![raw_markdown(child.documentation)], 85 vec![raw_markdown(child.documentation)],
92 )); 86 ));
@@ -94,7 +88,7 @@ pub(crate) fn into_pandoc(entity: Entity, config: &Config) -> (Pandoc, BTreeMap<
94 } 88 }
95 } 89 }
96 90
97 content.append(&mut embedded_documentation); 91 content.append(&mut inline_documentation);
98 92
99 let leftovers = separate_children 93 let leftovers = separate_children
100 .into_iter() 94 .into_iter()
@@ -105,11 +99,25 @@ pub(crate) fn into_pandoc(entity: Entity, config: &Config) -> (Pandoc, BTreeMap<
105 (Pandoc(meta, content), leftovers) 99 (Pandoc(meta, content), leftovers)
106} 100}
107 101
102fn is_inline(
103 config: &Config,
104 language: Language,
105 parent_kind: &EntityKind,
106 children_kind: &ChildrenKind,
107) -> bool {
108 config
109 .inlines
110 .get(&language)
111 .and_then(|lang_inlines| lang_inlines.get(parent_kind))
112 .map(|children_inlines| children_inlines.contains(children_kind))
113 == Some(true)
114}
115
108fn str_block(content: String) -> Block { 116fn str_block(content: String) -> Block {
109 Block::Plain(vec![Inline::Str(content)]) 117 Block::Plain(vec![Inline::Str(content)])
110} 118}
111 119
112fn entity_link(id: &EntityId, name: String) -> Inline { 120fn entity_link(id: &EntityId, name: String, link_type: LinkType) -> Inline {
113 use pandoc_types::definition::Target; 121 use pandoc_types::definition::Target;
114 use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; 122 use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
115 123
@@ -131,9 +139,12 @@ fn entity_link(id: &EntityId, name: String) -> Inline {
131 Attr::null(), 139 Attr::null(),
132 vec![Inline::Code(Attr::null(), name)], 140 vec![Inline::Code(Attr::null(), name)],
133 Target( 141 Target(
134 once("./") 142 once(match link_type {
135 .chain(utf8_percent_encode(&id.0, FRAGMENT)) 143 LinkType::External => "./",
136 .collect(), 144 LinkType::Anchor => "#",
145 })
146 .chain(utf8_percent_encode(&id.0, FRAGMENT))
147 .collect(),
137 String::new(), 148 String::new(),
138 ), 149 ),
139 ) 150 )
@@ -144,12 +155,21 @@ fn raw_markdown(text: String) -> Block {
144 Block::RawBlock(Format(String::from("markdown")), text) 155 Block::RawBlock(Format(String::from("markdown")), text)
145} 156}
146 157
147fn member_list<'a>(members: impl IntoIterator<Item = (&'a EntityId, &'a Entity)>) -> Option<Block> { 158#[derive(Debug, Clone, Copy)]
159enum LinkType {
160 Anchor,
161 External,
162}
163
164fn member_list<'a>(
165 members: impl IntoIterator<Item = (&'a EntityId, &'a Entity)>,
166 link_type: LinkType,
167) -> Option<Block> {
148 let definitions: Vec<(Vec<Inline>, Vec<Vec<Block>>)> = members 168 let definitions: Vec<(Vec<Inline>, Vec<Vec<Block>>)> = members
149 .into_iter() 169 .into_iter()
150 .map(|(id, entity)| { 170 .map(|(id, entity)| {
151 ( 171 (
152 vec![entity_link(id, entity.name.clone())], 172 vec![entity_link(id, entity.name.clone(), link_type)],
153 vec![vec![str_block(entity.brief_description.clone())]], 173 vec![vec![str_block(entity.brief_description.clone())]],
154 ) 174 )
155 }) 175 })