Sources/OrgCore/Flashcards/FlashcardFaces.swift
107 lines · 5293 bytes
1import Foundation
2
3extension Flashcards {
4 /// What a card shows for one position, before and after it is flipped, as org-fc's card
5 /// types set up and flip a heading during a review.
6 public struct Face: Sendable, Equatable {
7 public struct Side: Sendable, Equatable {
8 /// The heading's title; nil while it is hidden.
9 public var title: String?
10 public var text: String
11 /// Ranges of `title + "\n" + text`, in UTF-16 offsets, to emphasize: a cloze hole.
12 public var marks: [Range<Int>]
13
14 public init(title: String?, text: String, marks: [Range<Int>] = []) {
15 self.title = title
16 self.text = text
17 self.marks = marks
18 }
19 }
20
21 public var front: Side
22 public var back: Side
23 /// For text-input cards: the answer to type, without emphasis markers.
24 public var expected: String?
25 }
26
27 /// `org-fc-type-cloze-hole-re`: 1 text, 2 hint, 3 the hole's position.
28 static let holePattern = "\\{\\{((?:\\$[^$]+\\$|[^}])*)\\}(?:\\{((?:\\$[^$]\\$|[^}])*)\\})?(?:@([0-9]+))?\\}"
29 static let holeRegex = try! NSRegularExpression(pattern: holePattern)
30
31 /// The card's face for `position`; nil for a type orgstar doesn't know or a position the
32 /// card doesn't have.
33 public static func face(_ card: Card, position: String) -> Face? {
34 func join(_ parts: [String]) -> String { parts.filter { !$0.isEmpty }.joined(separator: "\n\n") }
35 let answer = join([card.body, card.children])
36 switch card.cardType {
37 case .normal, .textInput:
38 let front = Face.Side(title: card.title, text: card.back == nil ? "" : card.body)
39 let expected = card.cardType == .textInput ? card.answerLine.map(deemphasize) : nil
40 return Face(front: front, back: Face.Side(title: card.title, text: answer), expected: expected)
41 case .double:
42 switch position {
43 case "front": return Face(front: Face.Side(title: card.title, text: card.back == nil ? "" : card.body), back: Face.Side(title: card.title, text: answer))
44 case "back":
45 let front = card.back.map { Face.Side(title: card.title, text: $0) } ?? Face.Side(title: nil, text: card.body)
46 return Face(front: front, back: Face.Side(title: card.title, text: answer))
47 default: return nil
48 }
49 case .cloze:
50 guard let id = Int(position), let type = card.clozeType else { return nil }
51 return cloze(card, hole: id, type: type)
52 case nil:
53 return nil
54 }
55 }
56
57 /// `org-fc-type-cloze-hide-holes` and the flip that follows it.
58 static func cloze(_ card: Card, hole: Int, type: ClozeType) -> Face? {
59 let source = card.title + "\n" + card.body as NSString
60 let matches = holeRegex.matches(in: source as String, range: NSRange(location: 0, length: source.length)).filter { $0.range(at: 3).location != NSNotFound }
61 guard let current = matches.firstIndex(where: { Int(source.substring(with: $0.range(at: 3))) == hole }) else { return nil }
62 func visible(_ i: Int) -> Bool {
63 switch type {
64 case .enumeration: i < current
65 case .deletion: true
66 case .single: false
67 case .context: abs(i - current) <= clozeContext
68 }
69 }
70 func render(flipped: Bool) -> (String, [Range<Int>]) {
71 var out = ""
72 var marks: [Range<Int>] = []
73 var at = 0
74 for (i, match) in matches.enumerated() {
75 out += source.substring(with: NSRange(location: at, length: match.range.location - at))
76 let text = source.substring(with: match.range(at: 1))
77 let shown: String
78 if i == current {
79 let hint = match.range(at: 2).location == NSNotFound ? "" : source.substring(with: match.range(at: 2))
80 shown = flipped ? text : "[..." + hint + "]"
81 let start = (out as NSString).length
82 marks.append(start..<(start + (shown as NSString).length))
83 } else {
84 shown = visible(i) ? text : "..."
85 }
86 out += shown
87 at = NSMaxRange(match.range)
88 }
89 out += source.substring(from: at)
90 return (out, marks)
91 }
92 func side(flipped: Bool) -> Face.Side {
93 let (text, marks) = render(flipped: flipped)
94 let split = text.firstIndex(of: "\n")!
95 var body = String(text[text.index(after: split)...])
96 if flipped, !card.children.isEmpty { body += (body.isEmpty ? "" : "\n\n") + card.children }
97 return Face.Side(title: String(text[..<split]), text: body, marks: marks)
98 }
99 return Face(front: side(flipped: false), back: side(flipped: true))
100 }
101
102 /// `org-fc-deemphasize`: the text of the line's first emphasis, else the line.
103 static func deemphasize(_ line: String) -> String {
104 guard let match = line.firstMatch(of: /(?:^|[\s\-('"{])([*\/_=~+])(\S|\S.*?\S)\1(?:[\s\-.,:!?;'")}\[\\]|$)/) else { return line }
105 return String(match.2)
106 }
107}