Add styling utilities

This commit is contained in:
louie 2020-03-02 00:10:53 +08:00
parent c76166e318
commit 58cad3a287
3 changed files with 92 additions and 2 deletions

View file

@ -17,8 +17,13 @@
6. Added more utility commands/functions 6. Added more utility commands/functions
7. ~anki-editor-use-math-jax~ was replaced with 7. ~anki-editor-use-math-jax~ was replaced with
~anki-editor-latex-style~ ~anki-editor-latex-style~
8. Minor internal code refactoring 8. Code refactoring
9. Allows to put one field in heading 9. Allows to put one field in heading
10. Added options to copy custom head tags to card stylings
1. Variable ~anki-editor-include-default-style~
2. Variable ~anki-editor-html-head~
3. Command ~anki-editor-copy-styles~
4. Command ~anki-editor-remove-styles~
** v0.3.3 ** v0.3.3

View file

@ -84,11 +84,13 @@ there are any ambiguity or grammatical mistakes ;-)/
|-----------------------------------------------+------------------------+----------------------------------------------------------------------------------------------------------| |-----------------------------------------------+------------------------+----------------------------------------------------------------------------------------------------------|
| anki-editor-api-host | "127.0.0.1" | The network address AnkiConnect is listening. | | anki-editor-api-host | "127.0.0.1" | The network address AnkiConnect is listening. |
| anki-editor-api-port | "8765" | The port number AnkiConnect is listening. | | anki-editor-api-port | "8765" | The port number AnkiConnect is listening. |
| anki-editor-break-consecutive-braces-in-latex | nil | If non-nil, consecutive `}' will be automatically separated by spaces to prevent early-closing of cloze. | | anki-editor-break-consecutive-braces-in-latex | nil | If non-nil, consecutive "}" will be automatically separated by spaces to prevent early-closing of cloze. |
| anki-editor-ignored-org-tags | '("export" "noexport") | A list of Org tags that are ignored when constructing notes form entries. | | anki-editor-ignored-org-tags | '("export" "noexport") | A list of Org tags that are ignored when constructing notes form entries. |
| anki-editor-org-tags-as-anki-tags | t | If nil, tags of entries wont't be counted as Anki tags. | | anki-editor-org-tags-as-anki-tags | t | If nil, tags of entries wont't be counted as Anki tags. |
| anki-editor-protected-tags | '("marked" "leech") | A list of tags that won't be deleted from Anki even though they're absent in Org entries. | | anki-editor-protected-tags | '("marked" "leech") | A list of tags that won't be deleted from Anki even though they're absent in Org entries. |
| anki-editor-latex-style | builtin | The style of latex to translate into. | | anki-editor-latex-style | builtin | The style of latex to translate into. |
| anki-editor-include-default-style | t | Wheter or not to include `org-html-style-default' when using `anki-editor-copy-styles'. |
| anki-editor-html-head | nil | Additional html tags to append to card stylings when using `anki-editor-copy-styles'. |
** Functions and Macros ** Functions and Macros
@ -141,6 +143,14 @@ there are any ambiguity or grammatical mistakes ;-)/
Find notes with QUERY. Find notes with QUERY.
*** anki-editor-copy-styles
Copy `org-html-style-default' and `anki-editor-html-head' to Anki card stylings.
*** anki-editor-remove-styles
Remove from card stylings html tags generated by this mode.
* Limitations * Limitations
** Tags between Anki and Org ** Tags between Anki and Org

View file

@ -107,6 +107,17 @@ form entries."
:type '(radio (const :tag "Built-in" builtin) :type '(radio (const :tag "Built-in" builtin)
(const :tag "MathJax" mathjax))) (const :tag "MathJax" mathjax)))
(defcustom anki-editor-include-default-style t
"Wheter or not to include `org-html-style-default' when using `anki-editor-copy-styles'.
For example, you might want to turn this off when you are going to
provide your custom styles in `anki-editor-html-head'."
:type 'boolean)
(defcustom anki-editor-html-head nil
"Additional html tags to append to card stylings when using `anki-editor-copy-styles'.
For example, you can put custom styles or scripts in this variable."
:type 'string)
;;; AnkiConnect ;;; AnkiConnect
@ -937,6 +948,70 @@ entry."
(message "%S" nids) (message "%S" nids)
nids))) nids)))
(defvar anki-editor--style-start "</style>\n<!-- {{ Emacs Org-mode -->")
(defvar anki-editor--style-end "<!-- Emacs Org-mode }} -->\n<style>")
(defun anki-editor-copy-styles ()
"Copy `org-html-style-default' and `anki-editor-html-head' to Anki card stylings."
(interactive)
(let ((head (concat (org-element-normalize-string anki-editor--style-start)
(org-element-normalize-string (format "<!-- Updated: %s -->" (current-time-string)))
(when anki-editor-include-default-style
(org-element-normalize-string org-html-style-default))
(org-element-normalize-string anki-editor-html-head)
anki-editor--style-end)))
(cl-loop for model in (anki-editor-note-types)
for style = (let* ((css (alist-get 'css (anki-editor-api-call-result 'modelStyling :modelName model)))
(start (string-match
(regexp-quote anki-editor--style-start)
css))
(end (string-match
(regexp-quote anki-editor--style-end)
css)))
(if (and start end)
(progn
(cl-incf end (length anki-editor--style-end))
;; skip whitespaces
(when-let ((newend (string-match "[[:graph:]]" css end)))
(setq end newend))
(concat
(substring css 0 start)
(substring css end)))
css))
do
(message "Updating styles for \"%s\"..." model)
(anki-editor-api-call-result 'updateModelStyling
:model (list :name model
:css (concat (concat head "\n\n") style)))
finally do (message "Updating styles...Done"))))
(defun anki-editor-remove-styles ()
"Remove from card stylings html tags generated by this mode."
(interactive)
(cl-loop for model in (anki-editor-note-types)
for css = (alist-get 'css (anki-editor-api-call-result 'modelStyling :modelName model))
for start = (string-match
(regexp-quote anki-editor--style-start)
css)
for end = (string-match
(regexp-quote anki-editor--style-end)
css)
if (and start end)
do
(cl-incf end (length anki-editor--style-end))
;; also remove whitespaces
(when-let ((newend (string-match "[[:graph:]]" css end)))
(setq end newend))
(message "Resetting styles for \"%s\"..." model)
(anki-editor-api-call-result
'updateModelStyling
:model (list :name model
:css (concat
(substring css 0 start)
(substring css end))))
finally do (message "Resetting styles...Done")))
(provide 'anki-editor) (provide 'anki-editor)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;