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)
62 (defun shr-transform-dom (dom)
63 (let ((result (list (pop dom))))
64 (dolist (arg (pop dom))
65 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
70 (push (cons :text sub) result)
71 (push (shr-transform-dom sub) result)))
75 (defun shr-insert-document (dom)
78 (shr-descend (shr-transform-dom dom))))
80 (defun shr-descend (dom)
81 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
82 (if (fboundp function)
83 (funcall function (cdr dom))
84 (shr-generic (cdr dom)))))
86 (defun shr-generic (cont)
90 (shr-insert (cdr sub)))
94 (defun shr-tag-p (cont)
95 (shr-ensure-paragraph)
97 (shr-ensure-paragraph))
99 (defun shr-ensure-paragraph ()
102 (unless (save-excursion
112 (defun shr-tag-b (cont)
113 (shr-fontize-cont cont 'bold))
115 (defun shr-tag-i (cont)
116 (shr-fontize-cont cont 'italic))
118 (defun shr-tag-u (cont)
119 (shr-fontize-cont cont 'underline))
121 (defun shr-tag-s (cont)
122 (shr-fontize-cont cont 'strike-through))
124 (defun shr-fontize-cont (cont &rest types)
128 (shr-add-font (or shr-start (point)) (point) type))))
130 (defun shr-add-font (start end type)
131 (let ((overlay (make-overlay start end)))
132 (overlay-put overlay 'face type)))
134 (defun shr-tag-a (cont)
135 (let ((url (cdr (assq :href cont)))
138 (widget-convert-button
139 'link shr-start (point)
140 :action 'shr-browse-url
142 :keymap widget-keymap
145 (defun shr-browse-url (widget &rest stuff)
146 (browse-url (widget-get widget :url)))
148 (defun shr-tag-img (cont)
149 (when (and (> (current-column) 0)
150 (not (eq shr-state 'image)))
152 (let ((start (point-marker)))
153 (let ((alt (cdr (assq :alt cont)))
154 (url (cdr (assq :src cont))))
155 (when (zerop (length alt))
158 ((and shr-blocked-images
159 (string-match shr-blocked-images url))
161 ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
162 (shr-put-image (shr-get-image-data url) (point) alt))
165 (url-retrieve url 'shr-image-fetched
166 (list (current-buffer) start (point-marker))
169 (setq shr-state 'image))))
171 (defun shr-image-fetched (status buffer start end)
172 (when (and (buffer-name buffer)
173 (not (plist-get status :error)))
174 (url-store-in-cache (current-buffer))
175 (when (or (search-forward "\n\n" nil t)
176 (search-forward "\r\n\r\n" nil t))
177 (let ((data (buffer-substring (point) (point-max))))
178 (with-current-buffer buffer
179 (let ((alt (buffer-substring start end))
180 (inhibit-read-only t))
181 (delete-region start end)
182 (shr-put-image data start alt))))))
183 (kill-buffer (current-buffer)))
185 (defun shr-put-image (data point alt)
186 (if (not (display-graphic-p))
188 (let ((image (ignore-errors
189 (shr-rescale-image data))))
191 (put-image image point alt)))))
193 (defun shr-rescale-image (data)
194 (if (or (not (fboundp 'imagemagick-types))
195 (not (get-buffer-window (current-buffer))))
196 (create-image data nil t)
197 (let* ((image (create-image data nil t))
198 (size (image-size image t))
201 (edges (window-inside-pixel-edges
202 (get-buffer-window (current-buffer))))
203 (window-width (truncate (* shr-max-image-proportion
204 (- (nth 2 edges) (nth 0 edges)))))
205 (window-height (truncate (* shr-max-image-proportion
206 (- (nth 3 edges) (nth 1 edges)))))
208 (when (> height window-height)
209 (setq image (or (create-image data 'imagemagick t
210 :height window-height)
212 (setq size (image-size image t)))
213 (when (> (car size) window-width)
215 (create-image data 'imagemagick t
220 (defun shr-tag-pre (cont)
221 (let ((shr-folding-mode 'none))
224 (shr-ensure-newline)))
226 (defun shr-tag-blockquote (cont)
227 (shr-ensure-paragraph)
228 (let ((shr-indentation (+ shr-indentation 4)))
231 (defun shr-ensure-newline ()
232 (unless (zerop (current-column))
235 (defun shr-insert (text)
236 (when (eq shr-state 'image)
238 (setq shr-state nil))
240 ((eq shr-folding-mode 'none)
245 (when (and (string-match "\\`[ \t\n]" text)
248 (dolist (elem (split-string text))
249 (setq column (current-column))
252 ((and (or (not first)
253 (eq shr-state 'space))
254 (> (+ column (length elem) 1) shr-width))
260 (> shr-indentation 0))
262 ;; The shr-start is a special variable that is used to pass
263 ;; upwards the first point in the buffer where the text really
266 (setq shr-start (point)))
269 (when (and (string-match "[ \t\n]\\'" text)
272 (setq shr-state 'space))))))
275 (insert (make-string shr-indentation ? )))
277 (defun shr-get-image-data (url)
278 "Get image data for URL.
279 Return a string with image data."
281 (mm-disable-multibyte)
282 (url-cache-extract (url-cache-create-filename url))
283 (when (or (search-forward "\n\n" nil t)
284 (search-forward "\r\n\r\n" nil t))
285 (buffer-substring (point) (point-max)))))
287 (defvar shr-list-mode nil)
289 (defun shr-tag-ul (cont)
290 (shr-ensure-paragraph)
291 (let ((shr-list-mode 'ul))
294 (defun shr-tag-ol (cont)
295 (let ((shr-list-mode 1))
298 (defun shr-tag-li (cont)
301 (if (numberp shr-list-mode)
303 (format "%d " shr-list-mode)
304 (setq shr-list-mode (1+ shr-list-mode)))
306 (shr-indentation (+ shr-indentation (length bullet))))
310 (defun shr-tag-br (cont)
315 (defun shr-tag-h1 (cont)
316 (shr-heading cont 'bold 'underline))
318 (defun shr-tag-h2 (cont)
319 (shr-heading cont 'bold))
321 (defun shr-tag-h3 (cont)
322 (shr-heading cont 'italic))
324 (defun shr-tag-h4 (cont)
327 (defun shr-tag-h5 (cont)
330 (defun shr-tag-h6 (cont)
333 (defun shr-heading (cont &rest types)
334 (shr-ensure-paragraph)
335 (apply #'shr-fontize-cont cont types)
336 (shr-ensure-paragraph))
338 (defun shr-tag-table (cont)
339 (shr-ensure-paragraph)
340 (setq cont (or (cdr (assq 'tbody cont))
342 (let* ((columns (shr-column-specs cont))
343 (suggested-widths (shr-pro-rate-columns columns))
344 (sketch (shr-make-table cont suggested-widths))
345 (sketch-widths (shr-table-widths sketch (length suggested-widths))))
346 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
348 (defun shr-insert-table (table widths)
349 (shr-insert-table-ruler widths)
351 (let ((start (point))
352 (height (let ((max 0))
354 (setq max (max max (cadr column))))
361 (let ((lines (split-string (nth 2 column) "\n")))
363 (when (> (length line) 0)
367 ;; Add blank lines at padding at the bottom of the TD,
369 (dotimes (i (- height (length lines)))
371 (insert (make-string (length (car lines)) ? ) "|")
373 (shr-insert-table-ruler widths)))
375 (defun shr-insert-table-ruler (widths)
378 (dotimes (i (length widths))
379 (insert (make-string (aref widths i) ?-) ?+))
382 (defun shr-table-widths (table length)
383 (let ((widths (make-vector length 0)))
387 (aset widths i (max (aref widths i)
392 (defun shr-make-table (cont widths &optional fill)
395 (when (eq (car row) 'tr)
398 (dolist (column (cdr row))
399 (when (memq (car column) '(td th))
400 (push (shr-render-td (cdr column) (aref widths i) fill)
403 (push (nreverse tds) trs))))
406 (defun shr-render-td (cont width fill)
408 (let ((shr-width width))
410 (while (re-search-backward "\n *$" nil t)
411 (delete-region (match-beginning 0) (match-end 0)))
412 (goto-char (point-min))
416 (setq max (max max (current-column)))
419 (goto-char (point-min))
422 (insert (make-string (- width (current-column)) ? ))
424 (list max (count-lines (point-min) (point-max)) (buffer-string)))))
426 (defun shr-pro-rate-columns (columns)
427 (let ((total-percentage 0)
428 (widths (make-vector (length columns) 0)))
429 (dotimes (i (length columns))
430 (incf total-percentage (aref columns i)))
431 (setq total-percentage (/ 1.0 total-percentage))
432 (dotimes (i (length columns))
433 (aset widths i (max (truncate (* (aref columns i)
439 ;; Return a summary of the number and shape of the TDs in the table.
440 (defun shr-column-specs (cont)
441 (let ((columns (make-vector (shr-max-columns cont) 1)))
443 (when (eq (car row) 'tr)
445 (dolist (column (cdr row))
446 (when (memq (car column) '(td th))
447 (let ((width (cdr (assq :width (cdr column)))))
449 (string-match "\\([0-9]+\\)%" width))
451 (/ (string-to-number (match-string 1 width))
456 (defun shr-count (cont elem)
459 (when (eq (car sub) elem)
463 (defun shr-max-columns (cont)
466 (when (eq (car row) 'tr)
467 (setq max (max max (shr-count (cdr row) 'td)))))