[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Save in Lnum format with links footnoted [CODE INCLUDED]




The attached code offers an ability to save a URL as rendered HTML, but
with the advantage of having links marked as footnotes.

Be forewarned that for this purpose, the code commits the heresy of
using `lynx', a competitor command line program of w3m, but which is
probably on your linux distribution by default.

I propose adding it to the project, but don't yet have a suggestion for
a key-binding.


-- 
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
(defun w3m-lynx-dump (&optional url name)
  "Save URL as a lynx-style dump (requires external program `lynx').

Save the URL as a rendered page in `Lnum' format, ie. with links
identified as footnotes. This is a feature natively offered by
the command line program `lynx', which is installed by default in
many common linux distributions."
  ; Based upon functions `w3m-pipe-source' and `w3m-save-buffer'
  ; TODO w3m-pipe-source: review handling of output redirection
  (interactive
   (let* ((dummy (when (/= 0 (call-process "which" nil nil nil "lynx"))
                   (error "external program 'lynx' not found.")))
          (url (or (if current-prefix-arg
                    (or (w3m-image) (w3m-anchor))
                   (or (w3m-anchor) (w3m-image)))
                  (and w3m-current-url
                    (prog1
                      (y-or-n-p (format "Lynx -dump %s ? " w3m-current-url))
                      (message nil))
                    w3m-current-url)))
          (name (or (and (stringp w3m-current-title)
                      (not (string-match "\\`[\C-@- ]*\\'" w3m-current-title))
                      w3m-current-title)
                    (and (not (string-match "\\`[\C-@- ]*\\'" w3m-current-url))
                      (file-name-nondirectory w3m-current-url))
                    (make-temp-name "w3m-")))
          (name (w3m-replace-in-string name "[\C-@- \"*/:<>?\|]+" "_"))
          (name (read-file-name "Save to: "
                  (file-name-as-directory w3m-save-buffer-directory)
                  name nil (concat name ".html.txt"))))
    (list url name)))
  (cond ((eq url 'none) nil)
    ((and (stringp url)
          (w3m-url-valid url))
       (w3m-message "Lynx -dump <%s> to \"| %s\"..." url name)
       (with-temp-buffer
         (set-buffer-multibyte nil)
         (w3m-process-with-wait-handler
           (let ((inhibit-message t))
             (w3m-retrieve
               (cond
                 ((string-match "\\`about://source/" url)
                    url)
                 ((string-match "\\`about://header/" url)
                    (concat "about://source/"
                        (substring url (match-end 0))))
                 (t (concat "about://source/" url)))))
         (call-process-region (point-min) (point-max) shell-file-name
            nil nil nil shell-command-switch (concat "lynx -dump -stdin >" name))
         (w3m-message "Completed: Lynx -dump"))))))