Skip to content

Instantly share code, notes, and snippets.

@amasad
Created October 20, 2017 22:09
Show Gist options
  • Save amasad/8e838f48c582f0d0654be3dc461a6932 to your computer and use it in GitHub Desktop.
Save amasad/8e838f48c582f0d0654be3dc461a6932 to your computer and use it in GitHub Desktop.
css-in-js to css elisp command
(defun un-camelcase-string (s)
(let ((case-fold-search nil))
(while (string-match "[A-Z]" s 1)
(setq s (replace-match (concat "-"
(downcase (match-string 0 s)))
t nil s)))
(downcase s)))
(defun cssjs-to-css ()
(interactive)
(insert (let ((lines (split-string (delete-and-extract-region (region-beginning) (region-end)) "\n")))
(mapconcat (lambda (line)
(if (= (length line) 0) ""
(let ((parts (split-string line ":")))
(if (/= (length parts) 2)
(error "Invalid line %s" line)
(concat
(un-camelcase-string (car parts))
": "
(string-trim
(replace-regexp-in-string "'" ""(replace-regexp-in-string "," ";" (car (cdr parts))))))))))
lines
"\n"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment