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-string)
68 (define-key map "v" 'shr-browse-url)
71 (defun shr-transform-dom (dom)
72 (let ((result (list (pop dom))))
73 (dolist (arg (pop dom))
74 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
79 (push (cons :text sub) result)
80 (push (shr-transform-dom sub) result)))
84 (defun shr-insert-document (dom)
87 (shr-descend (shr-transform-dom dom))))
89 (defun shr-descend (dom)
90 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
91 (if (fboundp function)
92 (funcall function (cdr dom))
93 (shr-generic (cdr dom)))))
95 (defun shr-generic (cont)
99 (shr-insert (cdr sub)))
101 (shr-descend sub)))))
103 (defun shr-tag-p (cont)
104 (shr-ensure-paragraph)
106 (shr-ensure-paragraph))
108 (defun shr-ensure-paragraph ()
111 (unless (save-excursion
121 (defun shr-tag-b (cont)
122 (shr-fontize-cont cont 'bold))
124 (defun shr-tag-i (cont)
125 (shr-fontize-cont cont 'italic))
127 (defun shr-tag-u (cont)
128 (shr-fontize-cont cont 'underline))
130 (defun shr-tag-s (cont)
131 (shr-fontize-cont cont 'strike-through))
133 (defun shr-fontize-cont (cont &rest types)
137 (shr-add-font (or shr-start (point)) (point) type))))
139 (defun shr-add-font (start end type)
140 (let ((overlay (make-overlay start end)))
141 (overlay-put overlay 'face type)))
143 (defun shr-tag-a (cont)
144 (let ((url (cdr (assq :href cont)))
147 (widget-convert-button
148 'link shr-start (point)
149 :action 'shr-browse-url
151 :keymap widget-keymap
154 (defun shr-browse-url (widget &rest stuff)
155 (browse-url (widget-get widget :url)))
157 (defun shr-tag-img (cont)
158 (when (and (> (current-column) 0)
159 (not (eq shr-state 'image)))
161 (let ((start (point-marker)))
162 (let ((alt (cdr (assq :alt cont)))
163 (url (cdr (assq :src cont))))
164 (when (zerop (length alt))
167 ((and shr-blocked-images
168 (string-match shr-blocked-images url))
170 ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
171 (shr-put-image (shr-get-image-data url) (point) alt))
174 (url-retrieve url 'shr-image-fetched
175 (list (current-buffer) start (point-marker))
177 (put-text-property start (point) 'keymap shr-map)
178 (put-text-property start (point) 'shr-alt alt)
179 (put-text-property start (point) 'shr-image url)
181 (setq shr-state 'image))))
183 (defun shr-show-alt-text ()
184 "Show the ALT text of the image under point."
186 (let ((text (get-text-property (point) 'shr-alt)))
188 (message "No image under point")
189 (message "%s" text))))
191 (defun shr-browse-image ()
192 "Browse the image under point."
194 (let ((url (get-text-property (point) 'shr-image)))
196 (message "No image under point")
197 (message "Browsing %s..." url)
200 (defun shr-image-fetched (status buffer start end)
201 (when (and (buffer-name buffer)
202 (not (plist-get status :error)))
203 (url-store-in-cache (current-buffer))
204 (when (or (search-forward "\n\n" nil t)
205 (search-forward "\r\n\r\n" nil t))
206 (let ((data (buffer-substring (point) (point-max))))
207 (with-current-buffer buffer
208 (let ((alt (buffer-substring start end))
209 (inhibit-read-only t))
210 (delete-region start end)
211 (shr-put-image data start alt))))))
212 (kill-buffer (current-buffer)))
214 (defun shr-put-image (data point alt)
215 (if (not (display-graphic-p))
217 (let ((image (ignore-errors
218 (shr-rescale-image data))))
220 (put-image image point alt)))))
222 (defun shr-rescale-image (data)
223 (if (or (not (fboundp 'imagemagick-types))
224 (not (get-buffer-window (current-buffer))))
225 (create-image data nil t)
226 (let* ((image (create-image data nil t))
227 (size (image-size image t))
230 (edges (window-inside-pixel-edges
231 (get-buffer-window (current-buffer))))
232 (window-width (truncate (* shr-max-image-proportion
233 (- (nth 2 edges) (nth 0 edges)))))
234 (window-height (truncate (* shr-max-image-proportion
235 (- (nth 3 edges) (nth 1 edges)))))
237 (when (> height window-height)
238 (setq image (or (create-image data 'imagemagick t
239 :height window-height)
241 (setq size (image-size image t)))
242 (when (> (car size) window-width)
244 (create-image data 'imagemagick t
249 (defun shr-tag-pre (cont)
250 (let ((shr-folding-mode 'none))
253 (shr-ensure-newline)))
255 (defun shr-tag-blockquote (cont)
256 (shr-ensure-paragraph)
257 (let ((shr-indentation (+ shr-indentation 4)))
260 (defun shr-ensure-newline ()
261 (unless (zerop (current-column))
264 (defun shr-insert (text)
265 (when (eq shr-state 'image)
267 (setq shr-state nil))
269 ((eq shr-folding-mode 'none)
274 (when (and (string-match "\\`[ \t\n]" text)
277 (dolist (elem (split-string text))
278 (setq column (current-column))
281 ((and (or (not first)
282 (eq shr-state 'space))
283 (> (+ column (length elem) 1) shr-width))
289 (> shr-indentation 0))
291 ;; The shr-start is a special variable that is used to pass
292 ;; upwards the first point in the buffer where the text really
295 (setq shr-start (point)))
298 (when (and (string-match "[ \t\n]\\'" text)
301 (setq shr-state 'space))))))
304 (insert (make-string shr-indentation ? )))
306 (defun shr-get-image-data (url)
307 "Get image data for URL.
308 Return a string with image data."
310 (mm-disable-multibyte)
311 (url-cache-extract (url-cache-create-filename url))
312 (when (or (search-forward "\n\n" nil t)
313 (search-forward "\r\n\r\n" nil t))
314 (buffer-substring (point) (point-max)))))
316 (defvar shr-list-mode nil)
318 (defun shr-tag-ul (cont)
319 (shr-ensure-paragraph)
320 (let ((shr-list-mode 'ul))
323 (defun shr-tag-ol (cont)
324 (let ((shr-list-mode 1))
327 (defun shr-tag-li (cont)
330 (if (numberp shr-list-mode)
332 (format "%d " shr-list-mode)
333 (setq shr-list-mode (1+ shr-list-mode)))
335 (shr-indentation (+ shr-indentation (length bullet))))
339 (defun shr-tag-br (cont)
344 (defun shr-tag-h1 (cont)
345 (shr-heading cont 'bold 'underline))
347 (defun shr-tag-h2 (cont)
348 (shr-heading cont 'bold))
350 (defun shr-tag-h3 (cont)
351 (shr-heading cont 'italic))
353 (defun shr-tag-h4 (cont)
356 (defun shr-tag-h5 (cont)
359 (defun shr-tag-h6 (cont)
362 (defun shr-heading (cont &rest types)
363 (shr-ensure-paragraph)
364 (apply #'shr-fontize-cont cont types)
365 (shr-ensure-paragraph))
367 (defun shr-tag-table (cont)
368 (shr-ensure-paragraph)
369 (setq cont (or (cdr (assq 'tbody cont))
371 (let* ((columns (shr-column-specs cont))
372 (suggested-widths (shr-pro-rate-columns columns))
373 (sketch (shr-make-table cont suggested-widths))
374 (sketch-widths (shr-table-widths sketch (length suggested-widths))))
375 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
377 (defun shr-insert-table (table widths)
378 (shr-insert-table-ruler widths)
380 (let ((start (point))
381 (height (let ((max 0))
383 (setq max (max max (cadr column))))
390 (let ((lines (split-string (nth 2 column) "\n")))
392 (when (> (length line) 0)
396 ;; Add blank lines at padding at the bottom of the TD,
398 (dotimes (i (- height (length lines)))
400 (insert (make-string (length (car lines)) ? ) "|")
402 (shr-insert-table-ruler widths)))
404 (defun shr-insert-table-ruler (widths)
407 (dotimes (i (length widths))
408 (insert (make-string (aref widths i) ?-) ?+))
411 (defun shr-table-widths (table length)
412 (let ((widths (make-vector length 0)))
416 (aset widths i (max (aref widths i)
421 (defun shr-make-table (cont widths &optional fill)
424 (when (eq (car row) 'tr)
427 (dolist (column (cdr row))
428 (when (memq (car column) '(td th))
429 (push (shr-render-td (cdr column) (aref widths i) fill)
432 (push (nreverse tds) trs))))
435 (defun shr-render-td (cont width fill)
437 (let ((shr-width width))
439 (while (re-search-backward "\n *$" nil t)
440 (delete-region (match-beginning 0) (match-end 0)))
441 (goto-char (point-min))
445 (setq max (max max (current-column)))
448 (goto-char (point-min))
451 (insert (make-string (- width (current-column)) ? ))
453 (list max (count-lines (point-min) (point-max)) (buffer-string)))))
455 (defun shr-pro-rate-columns (columns)
456 (let ((total-percentage 0)
457 (widths (make-vector (length columns) 0)))
458 (dotimes (i (length columns))
459 (incf total-percentage (aref columns i)))
460 (setq total-percentage (/ 1.0 total-percentage))
461 (dotimes (i (length columns))
462 (aset widths i (max (truncate (* (aref columns i)
468 ;; Return a summary of the number and shape of the TDs in the table.
469 (defun shr-column-specs (cont)
470 (let ((columns (make-vector (shr-max-columns cont) 1)))
472 (when (eq (car row) 'tr)
474 (dolist (column (cdr row))
475 (when (memq (car column) '(td th))
476 (let ((width (cdr (assq :width (cdr column)))))
478 (string-match "\\([0-9]+\\)%" width))
480 (/ (string-to-number (match-string 1 width))
485 (defun shr-count (cont elem)
488 (when (eq (car sub) elem)
492 (defun shr-max-columns (cont)
495 (when (eq (car row) 'tr)
496 (setq max (max max (shr-count (cdr row) 'td)))))