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
/*
* render/html/element/bibliography.rs
*
* ftml - Library to parse Wikidot text
* Copyright (C) 2019-2026 Wikijump Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use super::prelude::*;
use crate::tree::Bibliography;
pub fn render_bibcite(ctx: &mut HtmlContext, label: &str, brackets: bool) {
debug!("Rendering bibliography citation (label {label}, brackets {brackets})");
match ctx.get_bibliography_ref(label) {
// Valid bibliography reference, render it
Some((index, contents)) => {
// TODO make this into a locale template string
let reference_string = ctx
.handle()
.get_message(ctx.language(), "bibliography-reference");
let label = format!("{reference_string} {index}.");
// TODO: For now, copied from footnotes
ctx.html()
.span()
.attr(attr!("class" => "wj-bibliography-ref"))
.inner(|ctx| {
let id = str!(index);
// Bibliography marker that is hoverable
if brackets {
ctx.push_raw('[');
}
ctx.html()
.element("wj-bibliography-ref-marker")
.attr(attr!(
"class" => "wj-bibliography-ref-marker",
"role" => "link",
"aria-label" => &label,
"data-id" => &id,
))
.contents(&id);
if brackets {
ctx.push_raw(']');
}
// Tooltip shown on hover.
// Is aria-hidden due to difficulty in getting a simultaneous
// tooltip and link to work. A screen reader can still navigate
// through to the link and read the bibliography directly.
ctx.html()
.span()
.attr(attr!(
"class" => "wj-bibliography-ref-tooltip",
"aria-hidden" => "true",
))
.inner(|ctx| {
// Tooltip label
ctx.html()
.span()
.attr(
attr!("class" => "wj-bibliography-ref-tooltip-label"),
)
.contents(&label);
// Actual tooltip contents
ctx.html()
.span()
.attr(attr!("class" => "wj-bibliography-ref-contents"))
.contents(contents);
});
});
}
None => {
// We need to produce an error for invalid bibliography references
let message = ctx
.handle()
.get_message(ctx.language(), "bibliography-cite-not-found");
ctx.html()
.span()
.attr(attr!("class" => "wj-error-inline"))
.contents(message);
}
}
}
pub fn render_bibliography(
ctx: &mut HtmlContext,
title: Option<&str>,
bibliography_index: usize,
bibliography: &Bibliography,
) {
debug!(
"Rendering bibliography block (title {}, items {})",
title.unwrap_or("<default>"),
bibliography.slice().len(),
);
let title_default;
let title: &str = match title {
Some(title) => title,
None => {
title_default = ctx
.handle()
.get_message(ctx.language(), "bibliography-block-title");
title_default
}
};
ctx.html()
.div()
.attr(attr!("class" => "wj-bibliography bibitems"))
.inner(|ctx| {
ctx.html()
.div()
.attr(attr!("class" => "wj-bibliography-title title"))
.contents(title);
let mut id = String::new();
for (entry_index, (_, elements)) in bibliography.slice().iter().enumerate() {
// Convert to 1-indexing
let bibliography_index = bibliography_index + 1;
let entry_index = entry_index + 1;
// Produce HTML ID
id.clear();
str_write!(
id,
"wj-bibliography-item-{}-{} bibitem-{}-{}",
bibliography_index,
entry_index,
bibliography_index,
entry_index,
);
// Make bibliography row
ctx.html()
.div()
.attr(attr!("class" => "wj-bibliography-item bibitem", "id" => &id))
.inner(|ctx| {
// Number and clickable anchor
ctx.html()
.element("wj-bibliography-item-marker")
.attr(attr!(
"class" => "wj-bibliography-item-marker",
"type" => "button",
"role" => "link",
))
.inner(|ctx| {
str_write!(ctx, "{entry_index}");
// Period after entry number. Has special class to permit styling.
ctx.html()
.span()
.attr(attr!("class" => "wj-bibliography-sep"))
.contents(".");
});
render_elements(ctx, elements);
});
}
});
}