Skip to content

Commit

Permalink
feat(org-msg-css-to-file): save current css to file
Browse files Browse the repository at this point in the history
Tests to confirm read-write-read is consistent
  • Loading branch information
WillForan committed Nov 9, 2024
1 parent 59e2042 commit 0c3afc4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
16 changes: 16 additions & 0 deletions org-msg-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ert-deftest org-msg-test-css-parsing ()
"Test css list to string and back is consistant."
(let* ((css-list (org-msg-load-css))
(css-string (org-msg-css-to-string))
(css-string-list (with-temp-buffer
(insert css-string)
(org-msg-css-to-list))))
(should (equal (car css-list) (car css-string-list)))
(should (equal css-list css-string-list))))

(ert-deftest org-msg-test-css-1 ()
"Simple css prop to string test."
(should (equal (org-msg-props-to-style
'((color . "red")
(background . "blue")))
"color:red;background:blue;")))
39 changes: 35 additions & 4 deletions org-msg.el
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,15 @@ during email generation where ''' is turned into
(push (cons (intern prop) val) props)))
(dolist (sel selectors)
(cl-multiple-value-bind (tag class) (split-string sel "\\.")
(push (list (if (string= tag "") nil (intern tag))
(if (stringp class) (intern class) nil)
props)
l)))))))
(let ((tag (if (string= tag "") nil (intern tag)) )
(class (if (stringp class) (intern class) nil)))
(push
;; to match `org-msg-default-style' we need
;; (nil table-number) not (nil table-number nil)
(if props
(list tag class props)
(list tag class))
l))))))))
l))

(defun org-msg-css-file-to-list (file)
Expand All @@ -510,6 +515,32 @@ See `org-msg-css-to-list'."
(cdr css) ";")))
(apply 'concat (mapcar #'css-str props))))

(defun org-msg-css-to-string ()
"Combine `org-msg-load-css' (likely `org-msg-default-style') as CSS."
(let (allcss)
(dolist (el (org-msg-load-css) allcss)
(cl-multiple-value-bind (tag class css) el
(push (concat
(when tag (symbol-name tag))
(when class (concat "." (symbol-name class)))
;; reverse to match how list is stored
"{" (org-msg-props-to-style (reverse css)) "}")
allcss)))
(string-join allcss "\n")))

(defun org-msg-css-to-file (fname)
"Format list output of `org-msg-load-css' as CSS and save to `FNAME'.
By default, style is from `org-msg-default-style'
and will be based on your current Emacs theme.
`FNAME' could be used as the value `org-msg-enforce-css'
to customize the your email's style based on the exported css file.
Note: the file is parsed and inlined for every html element, not linked."
(interactive "Fcss ouptut file:")
(with-temp-buffer
(insert (org-msg-css-to-string))
(write-file fname))
(find-file fname))

(defsubst org-msg-in-quote-block ()
"Whether point is in a quote block."
(let ((face (get-char-property (point) 'face)))
Expand Down

0 comments on commit 0c3afc4

Please sign in to comment.