Skip to content

Instantly share code, notes, and snippets.

@jpf
Created August 26, 2019 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpf/dd8c40139dd30cb7bfe2588256b04704 to your computer and use it in GitHub Desktop.
Save jpf/dd8c40139dd30cb7bfe2588256b04704 to your computer and use it in GitHub Desktop.
Clean up passwords and tokens for use in documentation
;; Turns a password like "qWXNNXb6m(TM77WiVtC87c*" into "aABCDEb0c(FG12HdIeJ34f*"
;;
;; Use the clean-token function by marking a region you want to be "cleaned" and then running M-x clean-token
(defun token-cleaner (token)
"Given a TOKEN as a string, return a string where the letters in TOKEN have been replaced with letters from the same type"
(defun next-letter (letter smallest-letter largest-letter)
"Given LETTER, return the next letter in the sequence between SMALLEST-LETTER and LARGEST-LETTER"
(let ((proposed-letter (char-to-string (1+ (string-to-char letter)))))
(if (string> proposed-letter largest-letter)
smallest-letter
proposed-letter)))
(defun char-type-equal-p (char expected-type)
"Check if CHAR is EXPECTED-TYPE"
(equal (get-char-code-property (string-to-char char) 'general-category) expected-type))
(defun char-type-replace (char)
"Replace letter CHAR with the next letter in the sequence"
(cond ((char-type-equal-p char 'Ll)
(setq letter-lowercase (next-letter letter-lowercase "a" "z")))
((char-type-equal-p char 'Lu)
(setq letter-uppercase (next-letter letter-uppercase "A" "Z")))
((char-type-equal-p char 'Nd)
(setq numeric-digit (next-letter numeric-digit "0" "9")))
(t char)))
(let ((letter-lowercase "z") (letter-uppercase "Z") (numeric-digit "9"))
(mapconcat 'identity (mapcar 'char-type-replace (mapcar 'string token)) "")))
(defun clean-token (beg end)
"message region or \"empty string\" if none highlighted"
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list nil nil)))
(let ((region-contents))
(setf region-contents (buffer-substring-no-properties beg end))
(delete-region beg end)
(insert (token-cleaner region-contents))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment