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.
This commit is contained in:
parent
529c17e7c1
commit
939a7f94ba
1 changed files with 50 additions and 11 deletions
61
avandu.el
61
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 ()
|
||||
|
|
Loading…
Reference in a new issue