diff --git a/anki-editor.el b/anki-editor.el index 8035c9b..a5546e2 100644 --- a/anki-editor.el +++ b/anki-editor.el @@ -64,19 +64,6 @@ (require 'ox) (require 'ox-html) -(defconst anki-editor-prop-note-type "ANKI_NOTE_TYPE") -(defconst anki-editor-prop-note-id "ANKI_NOTE_ID") -(defconst anki-editor-prop-exporter "ANKI_EXPORTER") -(defconst anki-editor-prop-deck "ANKI_DECK") -(defconst anki-editor-prop-tags "ANKI_TAGS") -(defconst anki-editor-prop-tags-plus (concat anki-editor-prop-tags "+")) -(defconst anki-editor-prop-failure-reason "ANKI_FAILURE_REASON") -(defconst anki-editor-buffer-html-output "*AnkiEditor HTML Output*") -(defconst anki-editor-org-tag-regexp "^\\([[:alnum:]_@#%]+\\)+$") -(defconst anki-editor-exporter-raw "raw") -(defconst anki-editor-exporter-default "default") -(defconst anki-editor-api-version 5) - (defgroup anki-editor nil "Customizations for anki-editor." :group 'org) @@ -117,6 +104,8 @@ form entries." ;;; AnkiConnect +(defconst anki-editor-api-version 5) + (cl-defun anki-editor--fetch (url &rest settings &key type data success error parser @@ -246,95 +235,68 @@ The result is the path to the newly stored media file." media-file-name)) -;;; Org Export Backend +;;; Org export backend (defconst anki-editor--ox-anki-html-backend - (if anki-editor-use-math-jax - (org-export-create-backend - :parent 'html - :transcoders '((latex-fragment . anki-editor--ox-latex-for-mathjax) - (latex-environment . anki-editor--ox-latex-for-mathjax))) - (org-export-create-backend - :parent 'html - :transcoders '((latex-fragment . anki-editor--ox-latex) - (latex-environment . anki-editor--ox-latex))))) + (org-export-create-backend + :parent 'html + :transcoders '((latex-fragment . anki-editor--ox-latex) + (latex-environment . anki-editor--ox-latex)))) (defconst anki-editor--ox-export-ext-plist '(:with-toc nil :anki-editor-mode t)) -(defun anki-editor--translate-latex-delimiters (latex-code) - (catch 'done - (let ((delimiter-map (list (list (cons (format "^%s" (regexp-quote "$$")) "[$$]") - (cons (format "%s$" (regexp-quote "$$")) "[/$$]")) - (list (cons (format "^%s" (regexp-quote "$")) "[$]") - (cons (format "%s$" (regexp-quote "$")) "[/$]")) - (list (cons (format "^%s" (regexp-quote "\\(")) "[$]") - (cons (format "%s$" (regexp-quote "\\)")) "[/$]")) - (list (cons (format "^%s" (regexp-quote "\\[")) "[$$]") - (cons (format "%s$" (regexp-quote "\\]")) "[/$$]")))) - (matched nil)) - (save-match-data - (dolist (pair delimiter-map) - (dolist (delimiter pair) - (when (setq matched (string-match (car delimiter) latex-code)) - (setq latex-code (replace-match (cdr delimiter) t t latex-code)))) - (when matched (throw 'done latex-code))))) - latex-code)) +(cl-macrolet ((with-table (table) + `(cl-loop for delims in ,table + collect + (list (concat "^" (regexp-quote (cl-first delims))) + (cl-second delims) + (concat (regexp-quote (cl-third delims)) "$") + (cl-fourth delims))))) -(defun anki-editor--translate-latex-delimiters-to-anki-mathjax-delimiters (latex-code) - (catch 'done - (let ((delimiter-map (list (list (cons (format "^%s" (regexp-quote "$$")) "\\[") - (cons (format "%s$" (regexp-quote "$$")) "\\]")) - (list (cons (format "^%s" (regexp-quote "$")) "\\(") - (cons (format "%s$" (regexp-quote "$")) "\\)")))) - (matched nil)) - (save-match-data - (dolist (pair delimiter-map) - (dolist (delimiter pair) - (when (setq matched (string-match (car delimiter) latex-code)) - (setq latex-code (replace-match (cdr delimiter) t t latex-code)))) - (when matched (throw 'done latex-code))))) - latex-code)) + (defconst anki-editor--native-latex-delimiters + (with-table '(("$$" "[$$]" + "$$" "[/$$]") + ("$" "[$]" + "$" "[/$]") + ("\\(" "[$]" + "\\)" "[/$]") + ("\\[" "[$$]" + "\\]" "[/$$]")))) -(defun anki-editor--wrap-latex (content) - "Wrap CONTENT with Anki-style latex markers." - (format "
%s
" content)) +(defun anki-editor--translate-latex-fragment (latex-code) + (let ((table (if anki-editor-use-math-jax + anki-editor--mathjax-delimiters + anki-editor--native-latex-delimiters))) + (cl-loop for delims in table + for matches = (string-match (cl-first delims) latex-code) + when matches + do + (setq latex-code (replace-match (cl-second delims) t t latex-code)) + (string-match (cl-third delims) latex-code) + (setq latex-code (replace-match (cl-fourth delims) t t latex-code)) + until matches + finally return latex-code))) -(defun anki-editor--wrap-div (content) - (format "