Files
step3/scripts/ui/flashcard_cloze.gd
hz 48a8c8361d Add flashcards feature with spaced repetition scheduling
Adds a full flashcards system: FSRS-based scheduling, cloze/basic
card types, CSV import/export, media storage, and study UI, wired
into navigation and the dashboard alongside existing question flows.
2026-07-21 15:08:52 -05:00

42 lines
1.3 KiB
GDScript

extends RefCounted
# Mirrors api/QuestionsApi/Services/Flashcards/FlashcardClozeParser.cs so the study screen can
# render a cloze note's front/back without a round trip to the server.
static func _regex() -> RegEx:
var regex := RegEx.new()
regex.compile("(?s)\\{\\{c(\\d+)::((?:(?!\\}\\}).)*)\\}\\}")
return regex
static func render_front(text: String, cloze_number: int) -> String:
var regex := _regex()
var result := ""
var last_end := 0
for match in regex.search_all(text):
result += text.substr(last_end, match.get_start() - last_end)
var number := int(match.get_string(1))
var inner := match.get_string(2)
var parts := inner.split("::", true, 1)
if number == cloze_number:
var hint := parts[1].strip_edges() if parts.size() > 1 else ""
result += ("[%s]" % hint) if not hint.is_empty() else "[...]"
else:
result += parts[0].strip_edges()
last_end = match.get_end()
result += text.substr(last_end)
return result
static func render_back(text: String) -> String:
var regex := _regex()
var result := ""
var last_end := 0
for match in regex.search_all(text):
result += text.substr(last_end, match.get_start() - last_end)
var inner := match.get_string(2)
result += inner.split("::", true, 1)[0].strip_edges()
last_end = match.get_end()
result += text.substr(last_end)
return result