From 310ab098112ec0d0709f8d6818865e40ae54da4e Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Mon, 15 Jul 2024 06:47:13 +0300 Subject: cloze: Update display conds, add smaller scratch funcs, update docs --- gnosis.el | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) (limited to 'gnosis.el') diff --git a/gnosis.el b/gnosis.el index 4f77ac0..f155904 100644 --- a/gnosis.el +++ b/gnosis.el @@ -475,10 +475,9 @@ revealed with `gnosis-face-false' and the ones before with do (let* ((hint (nth idx hints)) (display-cloze (cond ((< idx success) cloze) - ((and failure (>= idx success)) cloze) - (t (if (or (not hint) (string-empty-p hint)) - gnosis-cloze-string - hint)))) + (failure cloze) + ((and hint (not (string-empty-p hint))) (format "[%s]" hint)) + (t gnosis-cloze-string))) (face (cond ((< idx success) 'gnosis-face-correct) ((and failure (= idx success)) 'gnosis-face-false) @@ -496,6 +495,32 @@ revealed with `gnosis-face-false' and the ones before with (gnosis-apply-syntax-overlay) (gnosis-insert-separator)))) +;; TODO: Reconsider using smaller functions for display cloze notes. + +;; (defun gnosis-add-clozes (sentence clozes &optional cloze-string) +;; "Replace CLOZES in SENTENCE with CLOZE-STRING." +;; (let ((cloze-string (or cloze-string gnosis-cloze-string))) +;; (with-temp-buffer +;; (insert sentence) +;; (goto-char (point-min)) +;; (dolist (cloze clozes) +;; (when (search-forward cloze nil t) +;; (replace-match (propertize cloze-string 'face 'gnosis-face-cloze) nil t))) +;; (buffer-string)))) + +;; (defun gnosis-replace-clozes-with-hints (sentence hints &optional cloze-string) +;; "Replace CLOZE-STRING in SENTENCE with HINTS." +;; (let ((cloze-string (or cloze-string gnosis-cloze-string)) +;; (count 0)) +;; (with-temp-buffer +;; (insert sentence) +;; (goto-char (point-min)) +;; (while (search-forward cloze-string nil t) +;; (when (and (nth count hints) (search-backward cloze-string nil t)) +;; (replace-match (propertize (format "[%s]" (nth count hints)) 'face 'gnosis-face-cloze))) +;; (setq count (1+ count))) +;; (buffer-string)))) + (defun gnosis-display-basic-answer (answer success user-input) "Display ANSWER. @@ -1079,12 +1104,13 @@ TYPE: Type of gnosis note, must be one of `gnosis-note-types'" (gnosis-completing-read "Answer: " choices))) (defun gnosis-cloze-remove-tags (string) - "Replace cx-tags in STRING. + "Replace cloze tags and hints in STRING. -Works both with {} and {{}} to make easier to import anki notes." - (let* ((regex "{\\{1,2\\}c[0-9]+::\\([^:}]*?\\)\\(::.*?\\)?}\\{1,2\\}") +Works with both single (:), double colons (::), single braces ({}) and +double braces ({{}})." + (let* ((regex "{\\{1,2\\}c[0-9]+:\\{1,2\\}\\([^:}]*\\).*?\\(}\\|::}\\)\\{1,2\\}") (result (replace-regexp-in-string regex "\\1" string))) - (string-remove-suffix "::" result))) + result)) (defun gnosis-cloze-extract-contents (str) "Extract cloze contents for STR. @@ -1093,8 +1119,6 @@ Return a list of cloze tag contents for STR, organized by cX-tag. Valid cloze formats include: \"This is an {c1:example}\" -\"This is an {c1::example}\" -\"This is an {{c1:example}}\" \"This is an {{c1::example}}\"" (let ((result-alist '()) (start 0)) -- cgit v1.2.3