Skip to content

Commit

Permalink
chore: remove col from TemplateRenderContext (ankidroid#15541)
Browse files Browse the repository at this point in the history
* rename fill_empty

* chore: remove `col` from TemplateRenderContext

* match libanki naming

rename snake_case names
  • Loading branch information
BrayanDSO authored Feb 23, 2024
1 parent 40f3b11 commit 6739dbe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,14 @@ open class CardTemplatePreviewer : AbstractFlashcardViewer() {
ord
}
val context = TemplateManager.TemplateRenderContext.fromCardLayout(
col,
note(col),
this,
model(col),
model(col).getJSONArray("tmpls")[index] as JSONObject,
fillEmpty = false
)
renderOutput =
context.render()
context.render(col)
}
return renderOutput!!
}
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ open class Card : Cloneable {
*/
open fun renderOutput(col: Collection, reload: Boolean = false, browser: Boolean = false): TemplateRenderOutput {
if (renderOutput == null || reload) {
renderOutput = TemplateManager.TemplateRenderContext.fromExistingCard(col, this, browser).render()
renderOutput = TemplateManager.TemplateRenderContext.fromExistingCard(col, this, browser).render(col)
}
return renderOutput!!
}
Expand Down
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ class Note : Cloneable {
template["ord"] = card.ord

val output = TemplateManager.TemplateRenderContext.fromCardLayout(
col = col,
note = this,
card = card,
notetype = model,
template = template,
fillEmpty = fillEmpty
).render()
).render(col)
card.renderOutput = output
card.setNote(this)
return card
Expand Down
54 changes: 22 additions & 32 deletions AnkiDroid/src/main/java/com/ichi2/libanki/TemplateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.ichi2.libanki.backend.model.toBackendNote
import com.ichi2.libanki.utils.append
import com.ichi2.libanki.utils.len
import com.ichi2.utils.deepClone
import net.ankiweb.rsdroid.RustCleanup
import net.ankiweb.rsdroid.exceptions.BackendTemplateException
import org.intellij.lang.annotations.Language
import org.json.JSONObject
Expand Down Expand Up @@ -113,51 +112,42 @@ class TemplateManager {
* using the _private fields directly.
*/
class TemplateRenderContext(
col: Collection,
card: Card,
note: Note,
browser: Boolean = false,
notetype: NotetypeJson? = null,
template: JSONObject? = null,
fill_empty: Boolean = false
private var fillEmpty: Boolean = false
) {
@RustCleanup("this was a WeakRef")
private val _col: Collection = col
private var _card: Card = card
private var _note: Note = note
private var _browser: Boolean = browser
private var _template: JSONObject? = template
private var fillEmpty: Boolean = fill_empty

// private var _fields: HashMap<String, String>? = null
private var noteType: NotetypeJson = notetype ?: note.notetype

companion object {
fun fromExistingCard(col: Collection, card: Card, browser: Boolean): TemplateRenderContext {
return TemplateRenderContext(col, card, card.note(col), browser)
return TemplateRenderContext(card, card.note(col), browser)
}

fun fromCardLayout(
col: Collection,
note: Note,
card: Card,
notetype: NotetypeJson,
template: JSONObject,
fillEmpty: Boolean
): TemplateRenderContext {
return TemplateRenderContext(
col,
card,
note,
notetype = notetype,
template = template,
fill_empty = fillEmpty
fillEmpty = fillEmpty
)
}
}

fun col() = _col

/**
* Returns the card being rendered.
* Be careful not to call .q() or .a() on the card, or you'll create an
Expand All @@ -168,10 +158,10 @@ class TemplateManager {
fun note() = _note
fun noteType() = noteType

fun render(): TemplateRenderOutput {
fun render(col: Collection): TemplateRenderOutput {
val partial: PartiallyRenderedCard
try {
partial = partiallyRender()
partial = partiallyRender(col)
} catch (e: BackendTemplateException) {
return TemplateRenderOutput(
questionText = e.localizedMessage ?: e.toString(),
Expand Down Expand Up @@ -206,25 +196,25 @@ class TemplateManager {
}
}

val mediaDir = col().media.dir
val mediaDir = col.media.dir
val qtext = parseVideos(
text = applyCustomFilters(partial.qnodes, this, front_side = null),
text = applyCustomFilters(partial.qnodes, this, frontSide = null),
mediaDir = mediaDir
)
val qout = col().backend.extractAvTags(text = qtext, questionSide = true)
val qout = col.backend.extractAvTags(text = qtext, questionSide = true)
var qoutText = qout.text

val atext = parseVideos(
text = applyCustomFilters(partial.anodes, this, front_side = qout.text),
text = applyCustomFilters(partial.anodes, this, frontSide = qout.text),
mediaDir = mediaDir
)
val aout = col().backend.extractAvTags(text = atext, questionSide = false)
val aout = col.backend.extractAvTags(text = atext, questionSide = false)
var aoutText = aout.text

if (!_browser) {
val svg = noteType.optBoolean("latexsvg", false)
qoutText = LaTeX.mungeQA(qoutText, _col, svg)
aoutText = LaTeX.mungeQA(aoutText, _col, svg)
qoutText = LaTeX.mungeQA(qoutText, col, svg)
aoutText = LaTeX.mungeQA(aoutText, col, svg)
}

return TemplateRenderOutput(
Expand All @@ -236,8 +226,8 @@ class TemplateManager {
)
}

fun partiallyRender(): PartiallyRenderedCard {
val proto = col().run {
fun partiallyRender(col: Collection): PartiallyRenderedCard {
val proto = col.run {
if (_template != null) {
// card layout screen
backend.renderUncommittedCardLegacy(
Expand Down Expand Up @@ -272,7 +262,7 @@ class TemplateManager {
fun applyCustomFilters(
rendered: TemplateReplacementList,
ctx: TemplateRenderContext,
front_side: String?
frontSide: String?
): String {
// template already fully rendered?
if (len(rendered) == 1 && rendered[0].first != null) {
Expand All @@ -286,18 +276,18 @@ class TemplateManager {
} else {
val node = union.second!!
// do we need to inject in FrontSide?
if (node.fieldName == "FrontSide" && front_side != null) {
node.currentText = front_side
if (node.fieldName == "FrontSide" && frontSide != null) {
node.currentText = frontSide
}

var field_text = node.currentText
for (filter_name in node.filters) {
fieldFilters[filter_name]?.let {
field_text = it.apply(field_text, node.fieldName, filter_name, ctx)
var fieldText = node.currentText
for (filterName in node.filters) {
fieldFilters[filterName]?.let {
fieldText = it.apply(fieldText, node.fieldName, filterName, ctx)
}
}

res += field_text
res += fieldText
}
}
return res
Expand Down

0 comments on commit 6739dbe

Please sign in to comment.