aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemsen2012-07-27 01:44:14 +0200
committerGravatar Tom Willemsen2012-07-27 01:44:14 +0200
commit939a7f94ba215e7a88d1bec5c6bcea81ab9459f7 (patch)
tree375e317ff7833b650133c963799d0ce8d560abf0
parent529c17e7c1f5427ce6ef8dd0527495f0103279f7 (diff)
downloadavandu-939a7f94ba215e7a88d1bec5c6bcea81ab9459f7.tar.gz
avandu-939a7f94ba215e7a88d1bec5c6bcea81ab9459f7.zip
Pass-off cleaning up of text to separate function
* avandu.el (avandu-entity-replacement-alist): An alist that keeps track which titled entities should be replaced by which character. (avandu-clean-text): New function. (avandu--insert-article-excerpt): Don't run endless chains of `replace-regexp-in-string', leave (almost) all cleanup to `avandu-clean-text'. Don't print an excerpt if it is empty.
-rw-r--r--avandu.el61
1 files changed, 50 insertions, 11 deletions
diff --git a/avandu.el b/avandu.el
index 11a1688..073a24b 100644
--- a/avandu.el
+++ b/avandu.el
@@ -105,6 +105,13 @@
map)
"Keymap for `avandu-overview-mode'.")
+(defconst avandu-entity-replacement-alist
+ '(("hellip" . 8230)
+ ("qout" . 34)
+ ("amp" . 38))
+ "What to replace the part between & and ; of HTML entities with
+ names.")
+
(define-derived-mode avandu-overview-mode special-mode "Avandu:Overview"
"Major mode fo the avandu overview screen.
@@ -309,20 +316,52 @@ the article-id and link properties, respectively."
(message "%s" (button-get button 'link))))
(insert-char ?\n 1))
+(defun avandu-clean-text (text)
+ "Go through TEXT and remove any trailing and leading whitespace
+from it, then look for any HTML entities and either replace them
+with their char value or with the value in
+`avandu-entity-replacement-alist'."
+ (with-temp-buffer
+ (insert text)
+ (while (re-search-forward
+ "\\`[[:space:][:cntrl:]]+\\|[[:space:][:cntrl:]]+\\'" nil t)
+ (replace-match ""))
+
+ (goto-char (point-min))
+ (while (search-forward "&" nil t)
+ (let ((pos (point)))
+ (save-excursion
+ (when (search-forward ";" nil t)
+ (let* ((sstring (buffer-substring pos (1- (point))))
+ (char-code
+ (if (= (char-after pos) ?#)
+ (unless (string-match-p "[^[:digit:]]"
+ (substring sstring 1))
+ (string-to-number (substring sstring 1)))
+ (assoc sstring avandu-entity-replacement-alist))))
+ (when char-code
+ (delete-region (1- pos) (point))
+ (insert-char (if (consp char-code)
+ (cdr char-code)
+ char-code) 1)))))))
+
+ (setq text (buffer-string)))
+ text)
+
(defun avandu--insert-article-excerpt (excerpt)
"Insert the excerpt of an article."
(let ((start-pos (point))
- end-pos)
- (insert
- (propertize
- (replace-regexp-in-string
- "…" "..."
- (replace-regexp-in-string "\\`[ \t\n]*\\|[ \t\n]*$" ""
- excerpt))
- 'face 'avandu-overview-excerpt))
- (indent-region start-pos (point) tab-width)
- (fill-region start-pos (point)))
- (insert-char ?\n 1))
+ end-pos
+ (text (replace-regexp-in-string
+ "[ \t\n]*$" "" (avandu-clean-text excerpt))))
+ (unless (or (not text) (string= text ""))
+ (insert
+ (propertize
+ text
+ 'face 'avandu-overview-excerpt))
+ (indent-region start-pos (point) tab-width)
+ (fill-region start-pos (point))
+ (insert-char ?\n 1))))
;;;###autoload
(defun avandu-list ()