From 276c374c284f058d24e81c3f29bbbc9c72183caf Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 12 Nov 2014 22:28:54 -0500 Subject: Introduce content cleaning functions --- sx-encoding.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 0b72365..bea9071 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -74,6 +74,18 @@ (substring ss 1)))))))) (replace-regexp-in-string "&[^; ]*;" get-function string))) +(defun sx-encoding-normalize-line-endings (string) + "Normalize the line endings for STRING" + (delete ? string)) + +(defun sx-encoding-clean-content (string) + "Cleans STRING for display. +Applies `sx-encoding-normalize-line-endings' and +`sx-encoding-decode-entities'." + (sx-encoding-decode-entities + (sx-encoding-normalize-line-endings + string))) + (defun sx-encoding-gzipped-p (data) "Checks for magic bytes in DATA. -- cgit v1.2.3 From 4aa410f49ff01991fab2da59478d847a3c276844 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 13 Nov 2014 12:45:00 +0000 Subject: Replace ^M with \r ^M isn't wrong, I'm just a little paranoid. --- sx-encoding.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index bea9071..58bbd80 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -76,7 +76,7 @@ (defun sx-encoding-normalize-line-endings (string) "Normalize the line endings for STRING" - (delete ? string)) + (delete ?\r string)) (defun sx-encoding-clean-content (string) "Cleans STRING for display. -- cgit v1.2.3 From 5058ab5ba14220615c6a01911898b2c8ded987e4 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 14 Nov 2014 00:38:33 -0500 Subject: Clean content at the request level --- sx-encoding.el | 32 ++++++++++++++++++++++++++++++++ sx-question-list.el | 2 +- sx-question-mode.el | 6 +++--- sx-request.el | 2 +- 4 files changed, 37 insertions(+), 5 deletions(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 58bbd80..7f4765a 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -23,6 +23,8 @@ ;;; Code: +(require 'cl-lib) + (defcustom sx-encoding-html-entities-plist '(Aacute "Á" aacute "á" Acirc "Â" acirc "â" acute "´" AElig "Æ" aelig "æ" Agrave "À" agrave "à" alefsym "ℵ" Alpha "Α" alpha "α" amp "&" and "∧" @@ -86,6 +88,36 @@ Applies `sx-encoding-normalize-line-endings' and (sx-encoding-normalize-line-endings string))) +;;; @TODO: This is a pretty ugly implementation. It can likely be +;;; simplified and it should be improved. +(defun sx-encoding-clean-content-deep (data) + "Clean DATA recursively where necessary. + +See `sx-encoding-clean-content'." + (cond + ;; If we're looking at an atom, clean it if it's a string. + ;; Otherwise, just return it. + ((atom data) + (if (stringp data) (sx-encoding-clean-content data) data)) + ;; Looking at a vector? Recurse by mapping. + ((vectorp data) + (cl-map #'vector #'sx-encoding-clean-content-deep data)) + ;; Is our cdr a vector? Map again, but reconstruct the cons cell. + ((vectorp (cdr data)) + (cons (car data) (cl-map #'vector + #'sx-encoding-clean-content-deep + (cdr data)))) + ;; Is our cdr just a string? Clean and return it, reconstructing. + ((stringp (cdr data)) + (cons (car data) (sx-encoding-clean-content (cdr data)))) + ;; Is this a cons cell where the other part is an atom? Return the + ;; atom. If we got here, it wasn't a string. + ((and (consp data) (atom (cdr data))) data) + ;; If it's a list, map. + ((listp data) (mapcar #'sx-encoding-clean-content-deep data)) + ;; Default to identity. + (t data))) + (defun sx-encoding-gzipped-p (data) "Checks for magic bytes in DATA. diff --git a/sx-question-list.el b/sx-question-list.el index 8c558e6..ff1bdaa 100644 --- a/sx-question-list.el +++ b/sx-question-list.el @@ -231,7 +231,7 @@ Used in the questions list to indicate a question was updated \"4d ago\"." 'sx-question-list-answers)) (concat (propertize - (sx-encoding-clean-content .title) + .title 'face (if (sx-question--read-p .data) 'sx-question-list-read-question ;; Increment `sx-question-list--unread-count' for the mode-line. diff --git a/sx-question-mode.el b/sx-question-mode.el index c17d55a..4cbdf3a 100644 --- a/sx-question-mode.el +++ b/sx-question-mode.el @@ -200,7 +200,7 @@ DATA can represent a question or an answer." (if .title ;; Questions have title (propertize - (sx-encoding-clean-content .title) + .title 'font-lock-face 'sx-question-mode-title 'sx-question-mode--section 1) ;; Answers don't @@ -237,7 +237,7 @@ DATA can represent a question or an answer." '(face sx-question-mode-content-face) (insert "\n" (sx-question-mode--fill-string - (sx-encoding-clean-content .body_markdown)) + .body_markdown) sx-question-mode-separator))) ;; Comments (when .comments @@ -277,7 +277,7 @@ DATA can represent a question or an answer." (sx-assoc-let data (insert " " - (sx-encoding-clean-content .body_markdown) + .body_markdown " – " (sx-question-mode--propertized-display-name .owner) "\n"))) diff --git a/sx-request.el b/sx-request.el index dd98ead..b16fd9a 100644 --- a/sx-request.el +++ b/sx-request.el @@ -115,7 +115,7 @@ number of requests left every time it finishes a call.") sx-request-remaining-api-requests-message-threshold) (sx-message "%d API requests reamining" sx-request-remaining-api-requests)) - .items))))))) + (sx-encoding-clean-content-deep .items)))))))) ;;; Support Functions -- cgit v1.2.3 From ca39e18431537c87f71a3f67999f79aa32d97fca Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 14 Nov 2014 00:51:36 -0500 Subject: Simplify deep-clean function --- sx-encoding.el | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 7f4765a..0653d77 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -95,27 +95,23 @@ Applies `sx-encoding-normalize-line-endings' and See `sx-encoding-clean-content'." (cond - ;; If we're looking at an atom, clean it if it's a string. - ;; Otherwise, just return it. + ;; If we're looking at a cons cell, test to see if is a list. If + ;; it is, map ourselves over the entire list. If it is not, + ;; reconstruct the cons cell using a cleaned cdr. + ((consp data) + (if (listp (cdr data)) + (cl-map #'list #'sx-encoding-clean-content-deep data) + (cons (car data) (sx-encoding-clean-content-deep (cdr data))))) + ;; If we're looking at an atom, clean and return if we're looking + ;; at a string, map if we're looking at a vector, and just return + ;; if we aren't looking at either. ((atom data) - (if (stringp data) (sx-encoding-clean-content data) data)) - ;; Looking at a vector? Recurse by mapping. - ((vectorp data) - (cl-map #'vector #'sx-encoding-clean-content-deep data)) - ;; Is our cdr a vector? Map again, but reconstruct the cons cell. - ((vectorp (cdr data)) - (cons (car data) (cl-map #'vector - #'sx-encoding-clean-content-deep - (cdr data)))) - ;; Is our cdr just a string? Clean and return it, reconstructing. - ((stringp (cdr data)) - (cons (car data) (sx-encoding-clean-content (cdr data)))) - ;; Is this a cons cell where the other part is an atom? Return the - ;; atom. If we got here, it wasn't a string. - ((and (consp data) (atom (cdr data))) data) - ;; If it's a list, map. - ((listp data) (mapcar #'sx-encoding-clean-content-deep data)) - ;; Default to identity. + (cond + ((stringp data) + (sx-encoding-clean-content data)) + ((vectorp data) + (cl-map #'vector #'sx-encoding-clean-content-deep data)) + (t data))) (t data))) (defun sx-encoding-gzipped-p (data) -- cgit v1.2.3 From 57a55e2721b4f40c892e716831d4f29054f8e631 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 14 Nov 2014 00:52:47 -0500 Subject: Simplify deep-clean function further If it is not a cons cell, then it is an atom. We don't need to use `cond' for this. --- sx-encoding.el | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 0653d77..9d48e60 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -88,31 +88,26 @@ Applies `sx-encoding-normalize-line-endings' and (sx-encoding-normalize-line-endings string))) -;;; @TODO: This is a pretty ugly implementation. It can likely be -;;; simplified and it should be improved. (defun sx-encoding-clean-content-deep (data) "Clean DATA recursively where necessary. See `sx-encoding-clean-content'." - (cond + (if (consp data) ;; If we're looking at a cons cell, test to see if is a list. If ;; it is, map ourselves over the entire list. If it is not, ;; reconstruct the cons cell using a cleaned cdr. - ((consp data) - (if (listp (cdr data)) - (cl-map #'list #'sx-encoding-clean-content-deep data) - (cons (car data) (sx-encoding-clean-content-deep (cdr data))))) + (if (listp (cdr data)) + (cl-map #'list #'sx-encoding-clean-content-deep data) + (cons (car data) (sx-encoding-clean-content-deep (cdr data)))) ;; If we're looking at an atom, clean and return if we're looking ;; at a string, map if we're looking at a vector, and just return ;; if we aren't looking at either. - ((atom data) (cond ((stringp data) (sx-encoding-clean-content data)) ((vectorp data) (cl-map #'vector #'sx-encoding-clean-content-deep data)) - (t data))) - (t data))) + (t data)))) (defun sx-encoding-gzipped-p (data) "Checks for magic bytes in DATA. -- cgit v1.2.3