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.
34 "Simple HTML Renderer"
37 (defcustom shr-max-image-proportion 0.9
38 "How big pictures displayed are in relation to the window they're in.
39 A value of 0.7 means that they are allowed to take up 70% of the
40 width and height of the window. If they are larger than this,
41 and Emacs supports it, then the images will be rescaled down to
47 (defcustom shr-blocked-images nil
48 "Images that have URLs matching this regexp will be blocked."
53 (defvar shr-folding-mode nil)
54 (defvar shr-state nil)
55 (defvar shr-start nil)
56 (defvar shr-indentation 0)
60 (defun shr-transform-dom (dom)
61 (let ((result (list (pop dom))))
62 (dolist (arg (pop dom))
63 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
68 (push (cons :text sub) result)
69 (push (shr-transform-dom sub) result)))
73 (defun shr-insert-document (dom)
76 (shr-descend (shr-transform-dom dom))))
78 (defun shr-descend (dom)
79 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
80 (if (fboundp function)
81 (funcall function (cdr dom))
82 (shr-generic (cdr dom)))))
84 (defun shr-generic (cont)
88 (shr-insert (cdr sub)))
92 (defun shr-tag-p (cont)
93 (shr-ensure-paragraph)
95 (shr-ensure-paragraph))
97 (defun shr-ensure-paragraph ()
100 (unless (eql (char-after (- (point) 2)) ?\n)
108 (defun shr-tag-b (cont)
109 (shr-fontize-cont cont 'bold))
111 (defun shr-tag-i (cont)
112 (shr-fontize-cont cont 'italic))
114 (defun shr-tag-u (cont)
115 (shr-fontize-cont cont 'underline))
117 (defun shr-tag-s (cont)
118 (shr-fontize-cont cont 'strike-through))
120 (defun shr-fontize-cont (cont &rest types)
124 (shr-add-font (or shr-start (point)) (point) type))))
126 (defun shr-add-font (start end type)
127 (let ((overlay (make-overlay start end)))
128 (overlay-put overlay 'face type)))
130 (defun shr-tag-a (cont)
131 (let ((url (cdr (assq :href cont)))
134 (widget-convert-button
135 'link shr-start (point)
136 :action 'shr-browse-url
138 :keymap widget-keymap
141 (defun shr-browse-url (widget &rest stuff)
142 (browse-url (widget-get widget :url)))
144 (defun shr-tag-img (cont)
145 (when (and (> (current-column) 0)
146 (not (eq shr-state 'image)))
148 (let ((start (point-marker)))
149 (let ((alt (cdr (assq :alt cont)))
150 (url (cdr (assq :src cont))))
151 (when (zerop (length alt))
154 ((and shr-blocked-images
155 (string-match shr-blocked-images url))
157 ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
158 (shr-put-image (shr-get-image-data url) (point) alt))
161 (url-retrieve url 'shr-image-fetched
162 (list (current-buffer) start (point-marker))
165 (setq shr-state 'image))))
167 (defun shr-image-fetched (status buffer start end)
168 (when (and (buffer-name buffer)
169 (not (plist-get status :error)))
170 (url-store-in-cache (current-buffer))
171 (when (or (search-forward "\n\n" nil t)
172 (search-forward "\r\n\r\n" nil t))
173 (let ((data (buffer-substring (point) (point-max))))
174 (with-current-buffer buffer
175 (let ((alt (buffer-substring start end))
176 (inhibit-read-only t))
177 (delete-region start end)
178 (shr-put-image data start alt))))))
179 (kill-buffer (current-buffer)))
181 (defun shr-put-image (data point alt)
182 (if (not (display-graphic-p))
184 (let ((image (ignore-errors
185 (shr-rescale-image data))))
187 (put-image image point alt)))))
189 (defun shr-rescale-image (data)
190 (if (or (not (fboundp 'imagemagick-types))
191 (not (get-buffer-window (current-buffer))))
192 (create-image data nil t)
193 (let* ((image (create-image data nil t))
194 (size (image-size image t))
197 (edges (window-inside-pixel-edges
198 (get-buffer-window (current-buffer))))
199 (window-width (truncate (* shr-max-image-proportion
200 (- (nth 2 edges) (nth 0 edges)))))
201 (window-height (truncate (* shr-max-image-proportion
202 (- (nth 3 edges) (nth 1 edges)))))
204 (when (> height window-height)
205 (setq image (or (create-image data 'imagemagick t
206 :height window-height)
208 (setq size (image-size image t)))
209 (when (> (car size) window-width)
211 (create-image data 'imagemagick t
216 (defun shr-tag-pre (cont)
217 (let ((shr-folding-mode 'none))
220 (shr-ensure-newline)))
222 (defun shr-tag-blockquote (cont)
223 (shr-ensure-paragraph)
224 (let ((shr-indentation (+ shr-indentation 4)))
227 (defun shr-ensure-newline ()
228 (unless (zerop (current-column))
231 (defun shr-insert (text)
232 (when (eq shr-state 'image)
234 (setq shr-state nil))
236 ((eq shr-folding-mode 'none)
241 (when (and (string-match "\\`[ \t\n]" text)
244 (dolist (elem (split-string text))
245 (setq column (current-column))
248 ((and (or (not first)
249 (eq shr-state 'space))
250 (> (+ column (length elem) 1) shr-width))
256 (> shr-indentation 0))
258 ;; The shr-start is a special variable that is used to pass
259 ;; upwards the first point in the buffer where the text really
262 (setq shr-start (point)))
265 (when (and (string-match "[ \t\n]\\'" text)
268 (setq shr-state 'space))))))
271 (insert (make-string shr-indentation ? )))
273 (defun shr-get-image-data (url)
274 "Get image data for URL.
275 Return a string with image data."
277 (mm-disable-multibyte)
278 (url-cache-extract (url-cache-create-filename url))
279 (when (or (search-forward "\n\n" nil t)
280 (search-forward "\r\n\r\n" nil t))
281 (buffer-substring (point) (point-max)))))
283 (defvar shr-list-mode nil)
285 (defun shr-tag-ul (cont)
286 (shr-ensure-paragraph)
287 (let ((shr-list-mode 'ul))
290 (defun shr-tag-ol (cont)
291 (let ((shr-list-mode 1))
294 (defun shr-tag-li (cont)
297 (if (numberp shr-list-mode)
299 (format "%d " shr-list-mode)
300 (setq shr-list-mode (1+ shr-list-mode)))
302 (shr-indentation (+ shr-indentation (length bullet))))
306 (defun shr-tag-br (cont)
311 (defun shr-tag-h1 (cont)
312 (shr-heading cont 'bold 'underline))
314 (defun shr-tag-h2 (cont)
315 (shr-heading cont 'bold))
317 (defun shr-tag-h3 (cont)
318 (shr-heading cont 'italic))
320 (defun shr-tag-h4 (cont)
323 (defun shr-tag-h5 (cont)
326 (defun shr-tag-h6 (cont)
329 (defun shr-heading (cont &rest types)
330 (shr-ensure-paragraph)
331 (apply #'shr-fontize-cont cont types)
332 (shr-ensure-paragraph))
334 (defun shr-tag-table (cont)
335 (shr-ensure-paragraph)
336 (setq cont (or (cdr (assq 'tbody cont))
338 (let* ((columns (shr-column-specs cont))
339 (suggested-widths (shr-pro-rate-columns columns))
340 (sketch (shr-make-table cont suggested-widths))
341 (sketch-widths (shr-table-widths sketch (length suggested-widths))))
342 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
344 (defun shr-insert-table (table widths)
345 (shr-insert-table-ruler widths)
347 (let ((start (point))
348 (height (let ((max 0))
350 (setq max (max max (cadr column))))
357 (let ((lines (split-string (nth 2 column) "\n")))
359 (when (> (length line) 0)
363 ;; Add blank lines at padding at the bottom of the TD,
365 (dotimes (i (- height (length lines)))
367 (insert (make-string (length (car lines)) ? ) "|")
369 (shr-insert-table-ruler widths)))
371 (defun shr-insert-table-ruler (widths)
374 (dotimes (i (length widths))
375 (insert (make-string (aref widths i) ?-) ?+))
378 (defun shr-table-widths (table length)
379 (let ((widths (make-vector length 0)))
383 (aset widths i (max (aref widths i)
388 (defun shr-make-table (cont widths &optional fill)
391 (when (eq (car row) 'tr)
394 (dolist (column (cdr row))
395 (when (memq (car column) '(td th))
396 (push (shr-render-td (cdr column) (aref widths i) fill)
399 (push (nreverse tds) trs))))
402 (defun shr-render-td (cont width fill)
404 (let ((shr-width width))
406 (while (re-search-backward "\n *$" nil t)
407 (delete-region (match-beginning 0) (match-end 0)))
408 (goto-char (point-min))
412 (setq max (max max (current-column)))
415 (goto-char (point-min))
418 (insert (make-string (- width (current-column)) ? ))
420 (list max (count-lines (point-min) (point-max)) (buffer-string)))))
422 (defun shr-pro-rate-columns (columns)
423 (let ((total-percentage 0)
424 (widths (make-vector (length columns) 0)))
425 (dotimes (i (length columns))
426 (incf total-percentage (aref columns i)))
427 (setq total-percentage (/ 1.0 total-percentage))
428 (dotimes (i (length columns))
429 (aset widths i (max (truncate (* (aref columns i)
435 ;; Return a summary of the number and shape of the TDs in the table.
436 (defun shr-column-specs (cont)
437 (let ((columns (make-vector (shr-max-columns cont) 1)))
439 (when (eq (car row) 'tr)
441 (dolist (column (cdr row))
442 (when (memq (car column) '(td th))
443 (let ((width (cdr (assq :width (cdr column)))))
445 (string-match "\\([0-9]+\\)%" width))
447 (/ (string-to-number (match-string 1 width))
452 (defun shr-count (cont elem)
455 (when (eq (car sub) elem)
459 (defun shr-max-columns (cont)
462 (when (eq (car row) 'tr)
463 (setq max (max max (shr-count (cdr row) 'td)))))