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
use crate::ASSETS_DIR_NAME;
use std::path::PathBuf;
use crate::{
doc::module::ModuleInfo,
render::{
BlockTitle, DocLinks, DocStyle, Renderable, {ALL_DOC_FILENAME, IDENTITY, INDEX_FILENAME},
},
RenderPlan,
};
use anyhow::Result;
use horrorshow::{box_html, Raw, RenderBox, Template};
pub(crate) trait SidebarNav {
/// Create sidebar component.
fn sidebar(&self) -> Sidebar;
}
/// Sidebar component for quick navigation.
pub(crate) struct Sidebar {
version_opt: Option<String>,
style: DocStyle,
module_info: ModuleInfo,
/// support for page navigation
nav: DocLinks,
}
impl Sidebar {
pub(crate) fn new(
version_opt: Option<String>,
style: DocStyle,
module_info: ModuleInfo,
nav: DocLinks,
) -> Self {
Self {
version_opt,
style,
module_info,
nav,
}
}
}
impl Renderable for Sidebar {
fn render(self, _render_plan: RenderPlan) -> Result<Box<dyn RenderBox>> {
// For workspace sidebar, use direct path to logo (no path prefix needed)
let path_to_logo = if matches!(self.style, DocStyle::WorkspaceIndex) {
format!("{ASSETS_DIR_NAME}/sway-logo.svg")
} else {
self.module_info
.to_html_shorthand_path_string(&format!("{ASSETS_DIR_NAME}/sway-logo.svg"))
};
let style = self.style.clone();
let version_opt = self.version_opt.clone();
let location_with_prefix = match &style {
DocStyle::AllDoc(project_kind) | DocStyle::ProjectIndex(project_kind) => {
format!("{project_kind} {}", self.module_info.location())
}
DocStyle::WorkspaceIndex => {
format!("Workspace {}", self.module_info.location())
}
DocStyle::ModuleIndex => format!(
"{} {}",
BlockTitle::Modules.item_title_str(),
self.module_info.location()
),
DocStyle::Item { title, name } => {
let title = title.clone().expect("Expected a BlockTitle");
let name = name.clone().expect("Expected a BaseIdent");
format!("{} {}", title.item_title_str(), name.as_str())
}
};
let root_path = self.module_info.to_html_shorthand_path_string(
PathBuf::from(self.module_info.project_name())
.join(INDEX_FILENAME)
.to_str()
.ok_or_else(|| anyhow::anyhow!(
"found invalid root file path for {}\nmake sure your project's name contains only valid unicode characters",
self.module_info.project_name(),
))?,
);
let logo_path_to_root = match style {
DocStyle::AllDoc(_) | DocStyle::Item { .. } | DocStyle::ModuleIndex => root_path,
DocStyle::ProjectIndex(_) | DocStyle::WorkspaceIndex => IDENTITY.to_owned(),
};
// Unfortunately, match arms that return a closure, even if they are the same
// type, are incompatible. The work around is to return a String instead,
// and render it from Raw in the final output.
let styled_content = match &self.style {
DocStyle::WorkspaceIndex => {
let nav_links = &self.nav.links;
box_html! {
div(class="sidebar-elems") {
section {
div(class="block") {
ul {
@ for (_title, doc_links) in nav_links {
@ for doc_link in doc_links {
li {
a(href=format!("{}", doc_link.html_filename)) {
: doc_link.name.clone();
}
}
}
}
}
}
}
}
}
.into_string()
.unwrap()
}
DocStyle::ProjectIndex(_) => {
let nav_links = &self.nav.links;
let all_items = format!("See all {}'s items", self.module_info.project_name());
box_html! {
div(class="sidebar-elems") {
a(id="all-types", href=ALL_DOC_FILENAME) {
p: all_items;
}
section {
div(class="block") {
ul {
@ for (title, _) in nav_links {
li {
a(href=format!("{}{}", IDENTITY, title.html_title_string())) {
: title.as_str();
}
}
}
}
}
}
}
}
.into_string()
.unwrap()
}
DocStyle::AllDoc(_) => {
let nav_links = &self.nav.links;
box_html! {
div(class="sidebar-elems") {
a(id="all-types", href=INDEX_FILENAME) {
p: "Back to index";
}
section {
div(class="block") {
ul {
@ for (title, _) in nav_links {
li {
a(href=format!("{}{}", IDENTITY, title.html_title_string())) {
: title.as_str();
}
}
}
}
}
}
}
}
.into_string()
.unwrap()
}
_ => box_html! {
div(class="sidebar-elems") {
@ for (title, doc_links) in &self.nav.links {
section {
h3 {
a(href=format!("{}{}", IDENTITY, title.html_title_string())) {
: title.as_str();
}
}
ul(class="block method") {
@ for doc_link in doc_links {
li {
a(href=format!("{}", doc_link.html_filename)) {
: doc_link.name.clone();
}
}
}
}
}
}
}
}
.into_string()
.unwrap(),
};
Ok(box_html! {
nav(class="sidebar") {
a(class="sidebar-logo", href=&logo_path_to_root) {
div(class="logo-container") {
img(class="sway-logo", src=path_to_logo, alt="logo");
}
}
h2(class="location") {
: location_with_prefix;
}
@ if let DocStyle::ProjectIndex(_) = style.clone() {
@ if version_opt.is_some() {
div(class="version") {
p: version_opt.unwrap();
}
}
}
: Raw(styled_content);
}
})
}
}