-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TemplateManager.kt
330 lines (289 loc) · 12.5 KB
/
TemplateManager.kt
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2021 David Allison <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* This file incorporates code under the following license
* https://github.com/ankitects/anki/blob/2.1.34/pylib/anki/template.py
*
* Copyright: Ankitects Pty Ltd and contributors
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
*/
package com.ichi2.libanki
import BackendProto.Backend
import com.ichi2.libanki.TemplateManager.PartiallyRenderedCard.Companion.av_tags_to_native
import com.ichi2.libanki.utils.append
import com.ichi2.libanki.utils.len
import com.ichi2.utils.StringUtil
import net.ankiweb.rsdroid.RustCleanup
import net.ankiweb.rsdroid.exceptions.BackendTemplateException
import timber.log.Timber
import java.util.*
private typealias Union<A, B> = Pair<A, B>
private typealias TemplateReplacementList = MutableList<Union<str?, TemplateManager.TemplateReplacement?>>
/**
* Template.py in python. Called TemplateManager for technical reasons (conflict with Kotlin typealias)
*
* This file contains the Kotlin portion of the template rendering code.
* Templates can have filters applied to field replacements.
*
* The Rust template rendering code will apply any built in filters, and stop at the first
* unrecognized filter. The remaining filters are returned to Kotlin, and applied using the hook system.
*
* For example, {{myfilter:hint:text:Field}} will apply the built in text and hint filters,
* and then attempt to apply myfilter. If no add-ons have provided the filter,
* the filter is skipped.
*/
class TemplateManager {
data class TemplateReplacement(val field_name: str, var current_text: str, val filters: List<str>)
data class PartiallyRenderedCard(val qnodes: TemplateReplacementList, val anodes: TemplateReplacementList) {
companion object {
fun from_proto(out: Backend.RenderCardOut): PartiallyRenderedCard {
val qnodes = nodes_from_proto(out.questionNodesList)
val anodes = nodes_from_proto(out.answerNodesList)
return PartiallyRenderedCard(qnodes, anodes)
}
fun nodes_from_proto(nodes: List<Backend.RenderedTemplateNode>): TemplateReplacementList {
val results: TemplateReplacementList = mutableListOf()
for (node in nodes) {
if (node.valueCase == Backend.RenderedTemplateNode.ValueCase.TEXT) {
results.append(Pair(node.text, null))
} else {
results.append(
Pair(
null,
TemplateReplacement(
field_name = node.replacement.fieldName,
current_text = node.replacement.currentText,
filters = node.replacement.filtersList,
)
)
)
}
}
return results
}
fun av_tag_to_native(tag: Backend.AVTag): AvTag {
val value = tag.valueCase
return if (value == Backend.AVTag.ValueCase.SOUND_OR_VIDEO) {
SoundOrVideoTag(filename = tag.soundOrVideo)
} else {
TTSTag(
fieldText = tag.tts.fieldText,
lang = tag.tts.lang,
voices = tag.tts.voicesList,
otherArgs = tag.tts.otherArgsList,
speed = tag.tts.speed,
)
}
}
fun av_tags_to_native(tags: List<Backend.AVTag>): List<AvTag> {
return tags.map { av_tag_to_native(it) }.toList()
}
}
}
/**
* Holds information for the duration of one card render.
* This may fetch information lazily in the future, so please avoid
* using the _private fields directly.
*/
class TemplateRenderContext(
col: Collection,
card: Card,
note: Note,
browser: bool = false,
notetype: NoteType? = null,
template: Dict<str, str>? = null,
fill_empty: bool = false
) {
@RustCleanup("internal variables should be private, revert them once we're on V16")
@RustCleanup("this was a WeakRef")
internal val _col: Collection = col
internal var _card: Card = card
internal var _note: Note = note
internal var _browser: bool = browser
internal var _template: Dict<str, str>? = template
internal var _fill_empty: bool = fill_empty
private var _fields: Dict<str, str>? = null
private var _note_type: NoteType = notetype ?: note.model()
/**
* if you need to store extra state to share amongst rendering
* hooks, you can insert it into this dictionary
*/
private var extra_state: HashMap<str, Any> = Dict()
companion object {
fun from_existing_card(card: Card, browser: bool): TemplateRenderContext {
return TemplateRenderContext(card.col, card, card.note(), browser)
}
fun from_card_layout(
note: Note,
card: Card,
notetype: NoteType,
template: Dict<str, str>,
fill_empty: bool,
): TemplateRenderContext {
return TemplateRenderContext(
note.col,
card,
note,
notetype = notetype,
template = template,
fill_empty = fill_empty,
)
}
}
fun col() = _col
fun fields(): Dict<str, str> {
Timber.w(".fields() is obsolete, use .note() or .card()")
if (_fields == null) {
// fields from note
val fields = _note.items().map { Pair(it[0], it[1]) }.toMap().toMutableMap()
// add (most) special fields
fields["Tags"] = StringUtil.strip(_note.stringTags())
fields["Type"] = _note_type.name
fields["Deck"] = _col.decks.name(_card.oDid or _card.did)
fields["Subdeck"] = Decks.basename(fields["Deck"])
if (_template != null) {
fields["Card"] = _template!!["name"]
} else {
fields["Card"] = ""
}
val flag = _card.userFlag()
fields["CardFlag"] = if (flag != 0) "flag$flag" else ""
_fields = HashMap(fields)
}
return _fields!!
}
/**
* Returns the card being rendered.
* Be careful not to call .q() or .a() on the card, or you'll create an
* infinite loop.
*/
fun card() = _card
fun note() = _note
fun note_type() = _note_type
@RustCleanup("legacy")
fun qfmt(): str {
return templates_for_card(card(), _browser).first
}
@RustCleanup("legacy")
fun afmt(): str {
return templates_for_card(card(), _browser).second
}
fun render(): TemplateRenderOutput {
val partial: PartiallyRenderedCard
try {
partial = _partially_render()
} catch (e: BackendTemplateException) {
return TemplateRenderOutput(
question_text = e.localizedMessage ?: e.toString(),
answer_text = e.localizedMessage ?: e.toString(),
question_av_tags = emptyList(),
answer_av_tags = emptyList(),
)
}
val qtext = apply_custom_filters(partial.qnodes, this, front_side = null)
val qout = extract_av_tags(text = qtext, question_side = true)
val atext = apply_custom_filters(partial.anodes, this, front_side = qout.text)
val aout = extract_av_tags(text = atext, question_side = false)
val output = TemplateRenderOutput(
question_text = qout.text,
answer_text = aout.text,
question_av_tags = av_tags_to_native(qout.avTagsList),
answer_av_tags = av_tags_to_native(aout.avTagsList),
css = note_type().getString("css"),
)
if (!_browser) {
// hooks.card_did_render(output, self)
}
return output
}
@RustCleanup("Remove when DroidBackend supports named arguments")
private fun extract_av_tags(text: str, question_side: Boolean) =
col().backend.extract_av_tags(text, question_side)
fun _partially_render(): PartiallyRenderedCard {
val out: Backend.RenderCardOut = _col.backend.renderCardForTemplateManager(this)
return PartiallyRenderedCard.from_proto(out)
}
/** Stores the rendered templates and extracted AV tags. */
data class TemplateRenderOutput(
@get:JvmName("getQuestionText")
@set:JvmName("setQuestionText")
var question_text: str,
@get:JvmName("getAnswerText")
@set:JvmName("setAnswerText")
var answer_text: str,
@RustCleanup("make non-null")
val question_av_tags: List<AvTag>?,
@RustCleanup("make non-null")
val answer_av_tags: List<AvTag>?,
val css: str = ""
) {
fun question_and_style() = "<style>$css</style>$question_text"
fun answer_and_style() = "<style>$css</style>$answer_text"
}
@RustCleanup("legacy")
fun templates_for_card(card: Card, browser: bool): Tuple<str, str> {
val template = card.template()
var a: String? = null
var q: String? = null
if (browser) {
q = template.getString("bqfmt")
a = template.getString("bafmt")
}
q = q ?: template.getString("qfmt")
a = a ?: template.getString("afmt")
return Pair(q!!, a!!)
}
/** Complete rendering by applying any pending custom filters. */
fun apply_custom_filters(
rendered: TemplateReplacementList,
@Suppress("unused_parameter") ctx: TemplateRenderContext,
front_side: str?
): str {
// template already fully rendered?
if (len(rendered) == 1 && rendered[0].first != null) {
return rendered[0].first!!
}
var res = ""
for (union in rendered) {
if (union.first != null) {
res += union.first!!
} else {
val node = union.second!!
// do we need to inject in FrontSide?
if (node.field_name == "FrontSide" && front_side != null) {
node.current_text = front_side
}
val field_text = node.current_text
// AnkiDroid: ignored hook-based code
// for (filter_name in node.filters) {
// field_text = hooks.field_filter(field_text, node.field_name, filter_name, ctx
// )
// // legacy hook - the second and fifth argument are no longer used.
// field_text = anki.hooks.runFilter(
// "fmod_" + filter_name,
// field_text,
// "",
// ctx.note().items(),
// node.field_name,
// "",
// )
// }
res += field_text
}
}
return res
}
}
}