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
//! HTML renderers for the YomitanEntry type.
use crate::models::yomitan::*;
use maud::{Markup, html};
pub trait Renderer {
fn render_entry(entry: &YomitanEntry) -> Markup {
match entry {
YomitanEntry::TermInfo(t) => Self::render_term_info(t),
YomitanEntry::TermInfoForm(t) => Self::render_term_info_form(t),
YomitanEntry::TermMeta(t) => Self::render_term_meta(t),
}
}
// 1. Reading
// A simple reading can be rendered with
// div class="entry" {
// h2 { (self.term) }
// div class="reading" { (self.reading) }
// ...
// but yomitan renders them as ruby.
// See https://github.com/yomidevs/yomitan/blob/master/ext/js/display/display-generator.js#L1050
//
// WARN: rendering as ruby may not be supported in some readers.
// See https://github.com/koreader/koreader/issues/15259#issuecomment-4231135351
//
//
// 2. Multiple definitions
// This part works in yomitan because they group multiple definitions...
// but other formats may not.
//
// It is unclear to me if we want to merge them or not, prior to this rendering.
//
// Note that, for the main dictionary, we always have exactly one definition in lemmas.
// This is NOT true for forms in the main dictionary, nor for the glossary dictionary.
fn render_term_info(entry: &TermInfo) -> Markup {
html! {
div class="entry" {
div class="headword" {
(Self::render_headword(&entry.term, &entry.reading))
}
div class="definition-tag-list tag-list" {
@for tag in &entry.definition_tags {
(Self::render_definition_tag(tag))
}
}
@if entry.definitions.len() == 1 {
(Self::render_detailed_definition(&entry.definitions[0]))
} @else {
ol class="definition-list" {
@for def in &entry.definitions {
li {
(Self::render_detailed_definition(def))
}
}
}
}
}
}
}
fn render_headword(term: &str, reading: &str) -> Markup {
html!(
ruby {
(term)
@if !reading.is_empty() {
rt { (reading) }
}
}
)
}
fn render_definition_tag(tag: &TagInfo) -> Markup {
html!(
span
class="tag"
title=(tag.long_tag)
data-details=(tag.long_tag)
data-category=(tag.category)
{
span class="tag-label" {
span class="tag-label-content" { (tag.short_tag) }
}
}
)
}
fn render_term_info_form(entry: &TermInfoForm) -> Markup {
html! {
div class="entry form" {
h2 { (&entry.term) }
div class="reading" { (&entry.reading) }
ul {
@for def in &entry.definitions {
li { (Self::render_detailed_definition(def)) }
}
}
}
}
}
fn render_term_meta(entry: &TermMeta) -> Markup {
let TermMeta::TermPhoneticTranscription(tm) = entry;
html! {
div class="entry form" {
h2 { (tm.term) }
h3 { (Self::render_phonetic_transcription(&tm.transcription)) }
}
}
}
fn render_phonetic_transcription(pt: &PhoneticTranscription) -> Markup {
html! {
b { (pt.reading) }
ul {
@for tr in &pt.transcriptions {
li { (tr.ipa) (tr.tags.join("|")) }
}
}
}
}
fn render_detailed_definition(def: &DetailedDefinition) -> Markup {
match def {
DetailedDefinition::Text(s) => html! { (s) },
DetailedDefinition::StructuredContent(s) => Self::render_structured_content(s),
DetailedDefinition::Inflection((label, forms)) => html! {
b { (label) } ": " (forms.join(", "))
},
}
}
fn render_structured_content(sc: &StructuredContent) -> Markup {
Self::render_node(&sc.content)
}
fn render_node(node: &Node) -> Markup {
match node {
Node::Text(t) => html! { (t) },
Node::Array(nodes) => html! {
@for n in nodes { (Self::render_node(n)) }
},
Node::Generic(g) => Self::render_generic_node(g),
Node::Backlink(b) => Self::render_backlink(b),
}
}
// Whether to skip rendering for this node or not.
// This is the simplest way to skip some nodes for other formats.
#[allow(unused_variables)]
fn skip_render_generic_node(node: &GenericNode) -> bool {
false
}
fn render_generic_node(node: &GenericNode) -> Markup {
if Self::skip_render_generic_node(node) {
return html! {};
}
let content = Self::render_node(&node.content);
let data = node.data.as_ref();
let content_attr = data
.and_then(|d| d.0.get(&NodeDataKey::Content))
.map(|s| s.as_str());
let category_attr = data
.and_then(|d| d.0.get(&NodeDataKey::Category))
.map(|s| s.as_str());
let class = match content_attr {
Some("tag") => Some("gloss-sc-span"),
_ => None,
};
// https://github.com/lambda-fairy/maud/issues/240
// The attr=[value] syntax skips the attribute if the value is None
match node.tag {
NTag::Span => html! {
span
class=[class]
title=[node.title.clone()]
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
NTag::Div => html! {
div
class=[class]
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
NTag::Ol => html! {
ol
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
NTag::Ul => html! {
ul
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
NTag::Li => html! {
li
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
NTag::Details => html! {
details
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
NTag::Summary => html! {
summary
data-sc-content=[content_attr]
data-sc-category=[category_attr]
{ (content) }
},
}
}
fn render_backlink(b: &BacklinkContent) -> Markup {
let label = match b.content {
BacklinkContentKind::Wiktionary => "Wiktionary",
BacklinkContentKind::Kaikki => "Kaikki",
};
html! {
a href=(b.href) data-sc-content="backlink" { (label) }
}
}
}