1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package takes a HTML parse tree (as provided by
26 ;; libxml-parse-html-region) and renders it in the current buffer. It
27 ;; does not do CSS, JavaScript or anything advanced: It's geared
28 ;; towards rendering typical short snippets of HTML, like what you'd
29 ;; find in HTML email and the like.
36 "Simple HTML Renderer"
39 (defcustom shr-max-image-proportion 0.9
40 "How big pictures displayed are in relation to the window they're in.
41 A value of 0.7 means that they are allowed to take up 70% of the
42 width and height of the window. If they are larger than this,
43 and Emacs supports it, then the images will be rescaled down to
49 (defcustom shr-blocked-images nil
50 "Images that have URLs matching this regexp will be blocked."
55 (defvar shr-folding-mode nil)
56 (defvar shr-state nil)
57 (defvar shr-start nil)
58 (defvar shr-indentation 0)
63 (let ((map (make-sparse-keymap)))
64 (define-key map "a" 'shr-show-alt-text)
65 (define-key map "i" 'shr-browse-image)
66 (define-key map "I" 'shr-insert-image)
67 (define-key map "u" 'shr-copy-url)
68 (define-key map "v" 'shr-browse-url)
69 (define-key map "\r" 'shr-browse-url)
72 (defun shr-transform-dom (dom)
73 (let ((result (list (pop dom))))
74 (dolist (arg (pop dom))
75 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
80 (push (cons :text sub) result)
81 (push (shr-transform-dom sub) result)))
85 (defun shr-insert-document (dom)
88 (shr-descend (shr-transform-dom dom))))
90 (defun shr-descend (dom)
91 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
92 (if (fboundp function)
93 (funcall function (cdr dom))
94 (shr-generic (cdr dom)))))
96 (defun shr-generic (cont)
100 (shr-insert (cdr sub)))
102 (shr-descend sub)))))
104 (defun shr-tag-p (cont)
105 (shr-ensure-paragraph)
107 (shr-ensure-paragraph))
109 (defun shr-ensure-paragraph ()
112 (unless (save-excursion
122 (defun shr-tag-b (cont)
123 (shr-fontize-cont cont 'bold))
125 (defun shr-tag-i (cont)
126 (shr-fontize-cont cont 'italic))
128 (defun shr-tag-u (cont)
129 (shr-fontize-cont cont 'underline))
131 (defun shr-tag-s (cont)
132 (shr-fontize-cont cont 'strike-through))
134 (defun shr-fontize-cont (cont &rest types)
138 (shr-add-font (or shr-start (point)) (point) type))))
140 (defun shr-add-font (start end type)
141 (let ((overlay (make-overlay start end)))
142 (overlay-put overlay 'face type)))
144 (defun shr-tag-a (cont)
145 (let ((url (cdr (assq :href cont)))
149 (widget-convert-button
150 'link (or shr-start start) (point)
152 (put-text-property (or shr-start start) (point) 'keymap shr-map)
153 (put-text-property (or shr-start start) (point) 'shr-url url)))
155 (defun shr-browse-url ()
156 "Browse the URL under point."
158 (let ((url (get-text-property (point) 'shr-url)))
160 (message "No link under point")
163 (defun shr-copy-url ()
164 "Copy the URL under point to the kill ring.
165 If called twice, then try to fetch the URL and see whether it
166 redirects somewhere else."
168 (let ((url (get-text-property (point) 'shr-url)))
171 (message "No URL under point"))
172 ;; Resolve redirected URLs.
173 ((equal url (car kill-ring))
178 (eq (car a) :redirect))
181 (goto-char (point-min))
182 ;; Remove common tracking junk from the URL.
183 (when (re-search-forward ".utm_.*" nil t)
184 (replace-match "" t t))
185 (message "Copied %s" (buffer-string))
186 (copy-region-as-kill (point-min) (point-max)))))))
187 ;; Copy the URL to the kill ring.
191 (copy-region-as-kill (point-min) (point-max))
192 (message "Copied %s" url))))))
195 (defalias 'shr-encode-url-chars
196 ;; Neither Emacs 22 nor XEmacs provides this function.
197 (if (fboundp 'browse-url-url-encode-chars)
198 'browse-url-url-encode-chars
200 "URL-encode the chars in TEXT that match CHARS.
201 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
202 (let ((encoded-text (copy-sequence text))
204 (while (setq s (string-match chars encoded-text s))
206 (replace-match (format "%%%x"
208 (match-string 0 encoded-text)))
213 (defun shr-tag-img (cont)
214 (when (and (> (current-column) 0)
215 (not (eq shr-state 'image)))
217 (let ((start (point-marker)))
218 (let ((alt (cdr (assq :alt cont)))
219 (url (cdr (assq :src cont))))
220 (when (zerop (length alt))
223 ((and shr-blocked-images
224 (string-match shr-blocked-images url))
226 ((url-is-cached (shr-encode-url-chars url "[&)$ ]"))
227 (shr-put-image (shr-get-image-data url) (point) alt))
230 (url-retrieve url 'shr-image-fetched
231 (list (current-buffer) start (point-marker))
234 (put-text-property start (point) 'keymap shr-map)
235 (put-text-property start (point) 'shr-alt alt)
236 (put-text-property start (point) 'shr-image url)
237 (setq shr-state 'image))))
239 (defun shr-show-alt-text ()
240 "Show the ALT text of the image under point."
242 (let ((text (get-text-property (point) 'shr-alt)))
244 (message "No image under point")
245 (message "%s" text))))
247 (defun shr-browse-image ()
248 "Browse the image under point."
250 (let ((url (get-text-property (point) 'shr-image)))
252 (message "No image under point")
253 (message "Browsing %s..." url)
256 (defun shr-image-fetched (status buffer start end)
257 (when (and (buffer-name buffer)
258 (not (plist-get status :error)))
259 (url-store-in-cache (current-buffer))
260 (when (or (search-forward "\n\n" nil t)
261 (search-forward "\r\n\r\n" nil t))
262 (let ((data (buffer-substring (point) (point-max))))
263 (with-current-buffer buffer
264 (let ((alt (buffer-substring start end))
265 (inhibit-read-only t))
266 (delete-region start end)
267 (shr-put-image data start alt))))))
268 (kill-buffer (current-buffer)))
270 (defun shr-put-image (data point alt)
271 (if (not (display-graphic-p))
273 (let ((image (ignore-errors
274 (shr-rescale-image data))))
276 (put-image image point alt)))))
278 (defun shr-rescale-image (data)
279 (if (or (not (fboundp 'imagemagick-types))
280 (not (get-buffer-window (current-buffer))))
281 (create-image data nil t)
282 (let* ((image (create-image data nil t))
283 (size (image-size image t))
286 (edges (window-inside-pixel-edges
287 (get-buffer-window (current-buffer))))
288 (window-width (truncate (* shr-max-image-proportion
289 (- (nth 2 edges) (nth 0 edges)))))
290 (window-height (truncate (* shr-max-image-proportion
291 (- (nth 3 edges) (nth 1 edges)))))
293 (when (> height window-height)
294 (setq image (or (create-image data 'imagemagick t
295 :height window-height)
297 (setq size (image-size image t)))
298 (when (> (car size) window-width)
300 (create-image data 'imagemagick t
305 (defun shr-tag-pre (cont)
306 (let ((shr-folding-mode 'none))
309 (shr-ensure-newline)))
311 (defun shr-tag-blockquote (cont)
312 (shr-ensure-paragraph)
313 (let ((shr-indentation (+ shr-indentation 4)))
315 (shr-ensure-paragraph))
317 (defun shr-ensure-newline ()
318 (unless (zerop (current-column))
321 (defun shr-insert (text)
322 (when (eq shr-state 'image)
324 (setq shr-state nil))
326 ((eq shr-folding-mode 'none)
331 (when (and (string-match "\\`[ \t\n]" text)
334 (dolist (elem (split-string text))
335 (setq column (current-column))
338 ((and (or (not first)
339 (eq shr-state 'space))
340 (> (+ column (length elem) 1) shr-width))
346 (> shr-indentation 0))
348 ;; The shr-start is a special variable that is used to pass
349 ;; upwards the first point in the buffer where the text really
352 (setq shr-start (point)))
355 (when (and (string-match "[ \t\n]\\'" text)
358 (setq shr-state 'space))))))
361 (insert (make-string shr-indentation ? )))
363 (defun shr-get-image-data (url)
364 "Get image data for URL.
365 Return a string with image data."
367 (mm-disable-multibyte)
369 (url-cache-extract (url-cache-create-filename url))
371 (when (or (search-forward "\n\n" nil t)
372 (search-forward "\r\n\r\n" nil t))
373 (buffer-substring (point) (point-max))))))
375 (defvar shr-list-mode nil)
377 (defun shr-tag-ul (cont)
378 (shr-ensure-paragraph)
379 (let ((shr-list-mode 'ul))
382 (defun shr-tag-ol (cont)
383 (let ((shr-list-mode 1))
386 (defun shr-tag-li (cont)
389 (if (numberp shr-list-mode)
391 (format "%d " shr-list-mode)
392 (setq shr-list-mode (1+ shr-list-mode)))
394 (shr-indentation (+ shr-indentation (length bullet))))
398 (defun shr-tag-br (cont)
403 (defun shr-tag-h1 (cont)
404 (shr-heading cont 'bold 'underline))
406 (defun shr-tag-h2 (cont)
407 (shr-heading cont 'bold))
409 (defun shr-tag-h3 (cont)
410 (shr-heading cont 'italic))
412 (defun shr-tag-h4 (cont)
415 (defun shr-tag-h5 (cont)
418 (defun shr-tag-h6 (cont)
421 (defun shr-heading (cont &rest types)
422 (shr-ensure-paragraph)
423 (apply #'shr-fontize-cont cont types)
424 (shr-ensure-paragraph))
426 (defun shr-tag-table (cont)
427 (shr-ensure-paragraph)
428 (setq cont (or (cdr (assq 'tbody cont))
430 (let* ((columns (shr-column-specs cont))
431 (suggested-widths (shr-pro-rate-columns columns))
432 (sketch (shr-make-table cont suggested-widths))
433 (sketch-widths (shr-table-widths sketch (length suggested-widths))))
434 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
436 (defun shr-insert-table (table widths)
437 (shr-insert-table-ruler widths)
439 (let ((start (point))
440 (height (let ((max 0))
442 (setq max (max max (cadr column))))
449 (let ((lines (split-string (nth 2 column) "\n")))
451 (when (> (length line) 0)
455 ;; Add blank lines at padding at the bottom of the TD,
457 (dotimes (i (- height (length lines)))
459 (insert (make-string (length (car lines)) ? ) "|")
461 (shr-insert-table-ruler widths)))
463 (defun shr-insert-table-ruler (widths)
466 (dotimes (i (length widths))
467 (insert (make-string (aref widths i) ?-) ?+))
470 (defun shr-table-widths (table length)
471 (let ((widths (make-vector length 0)))
475 (aset widths i (max (aref widths i)
480 (defun shr-make-table (cont widths &optional fill)
483 (when (eq (car row) 'tr)
486 (dolist (column (cdr row))
487 (when (memq (car column) '(td th))
488 (push (shr-render-td (cdr column) (aref widths i) fill)
491 (push (nreverse tds) trs))))
494 (defun shr-render-td (cont width fill)
496 (let ((shr-width width)
499 (while (re-search-backward "\n *$" nil t)
500 (delete-region (match-beginning 0) (match-end 0)))
501 (goto-char (point-min))
505 (setq max (max max (current-column)))
508 (goto-char (point-min))
511 (when (> (- width (current-column)) 0)
512 (insert (make-string (- width (current-column)) ? )))
514 (list max (count-lines (point-min) (point-max)) (buffer-string)))))
516 (defun shr-pro-rate-columns (columns)
517 (let ((total-percentage 0)
518 (widths (make-vector (length columns) 0)))
519 (dotimes (i (length columns))
520 (incf total-percentage (aref columns i)))
521 (setq total-percentage (/ 1.0 total-percentage))
522 (dotimes (i (length columns))
523 (aset widths i (max (truncate (* (aref columns i)
529 ;; Return a summary of the number and shape of the TDs in the table.
530 (defun shr-column-specs (cont)
531 (let ((columns (make-vector (shr-max-columns cont) 1)))
533 (when (eq (car row) 'tr)
535 (dolist (column (cdr row))
536 (when (memq (car column) '(td th))
537 (let ((width (cdr (assq :width (cdr column)))))
539 (string-match "\\([0-9]+\\)%" width))
541 (/ (string-to-number (match-string 1 width))
546 (defun shr-count (cont elem)
549 (when (eq (car sub) elem)
553 (defun shr-max-columns (cont)
556 (when (eq (car row) 'tr)
557 (setq max (max max (shr-count (cdr row) 'td)))))