Diane Murray

Web within Emacs

Configuration examples

Here are some hook examples from my personal configuration.

Change HTML (wwe-change-html-functions)

Change the HTML of Astronomy Picture of the Day pages to add accesskeys to the previous and next links.

(defun dsm:wwe-apod-add-accesskeys (url)
  "Add previous and next accesskeys to apod pages."
  (when (string-match "^https://apod.nasa.gov/apod/" (car url))
    ;; find previous link
    (when (wwe-render-find
           "<a href=\"\\(ap[[:digit:]]\\{6\\}.html\\)\">&lt;</a>")
      (replace-match
       (format "<a href=\"%s\" accesskey=\"p\">&lt;</a>" (match-string 1))))
    ;; find next link
    (when (wwe-render-find
           "<a href=\"\\(ap[[:digit:]]\\{6\\}.html\\)\">&gt;</a>")
      (replace-match
       (format "<a href=\"%s\" accesskey=\"n\">&gt;</a>" (match-string 1))))))

;; add the function to the hook
(add-hook 'wwe-change-html-functions 'dsm:wwe-apod-add-accesskeys)

Page ready (wwe-page-ready-functions)

Go to a section target when the page is ready. Add string-match queries to the `cond' as needed.

(defun dsm:wwe-go-to-section (buffer error-message)
  "Go to a section in certain pages."
  (with-current-buffer buffer
    (unless (or (null wwe-response-status)
                (= wwe-response-status 404)
                (= wwe-response-status 500))
      ;; use [^#]* at end to not match urls that already have sections
      (cond
       ;; github project homepages
       ((string-match 
         "^https://github.com/[-_[:alnum:]]+/[-_[:alnum:]]+/?[^#]*"
         wwe-url)
        (if (member "readme" wwe-render-sections)
            (wwe-goto-section "readme" t)
          (wwe-goto-section "files")))
       (t nil)))))

;; add the function to the hook
(add-hook 'wwe-page-ready-functions 'dsm:wwe-go-to-section)

Insert the main image for certain pages when the page is ready.

(defun dsm:wwe-auto-insert-main-image (buffer error-message)
  "Insert the page's main image after the page is ready."
  (with-current-buffer buffer
    (let ((wwe-display-inline-images t)
          (old-point (point)))
      (cond
       ;; xkcd
       ((string-match "^https://\\(?:www\\.\\)?xkcd.com/" wwe-url)
        ;; assuming the first image after section "comic" is the comic
        (wwe-goto-section "comic" t)
        (goto-char (next-single-char-property-change (point) 'src))
        (if (and (get-char-property (point) 'src)
                 (string-match "^https://imgs.xkcd.com/comics/"
                               (get-char-property (point) 'src)))
            (wwe-image-at-point)
          (goto-char old-point)
          (message "Couldn't find xkcd image")))
       ;; emacswiki image pages
       ((string-match 
         (concat "^https?://www\\.emacswiki\\.org/emacs[^/]*/"
                 "\\(?:\\?action=browse;id=\\)?\\([\\.-a-z0-9]+\\)$")
         wwe-url)
        (cond ((and (wwe-render-find 
                     (concat "^" (match-string 1 wwe-url) "\n\n"
                             "\\(?:This page contains an uploaded file\\):\n\n"
                             (match-string 1 wwe-url)))
                    (get-char-property (1- (point)) 'src))
               (beginning-of-line)
               (wwe-image-at-point))
              (t (goto-char old-point))))
       ;; no match
       (t nil)))))

;; add the function to the hook, setting it to run last
(add-hook 'wwe-page-ready-functions 'dsm:wwe-auto-insert-main-image t)