;;; rwd-paste.el --- Paste to the web.

;; Copyright (C) 2008 Phil Hagelberg

;; Author: Phil Hagelberg
;; URL: http://www.emacswiki.org/cgi-bin/wiki/Rwd-Paste
;; Version: 0.3
;; Created: 2008-04-02
;; Keywords: convenience hypermedia
;; EmacsWiki: Rwd-Paste

;; This file is NOT part of GNU Emacs.

;;; Commentary:

;; ... TODO

;;; Install

;; TODO

;;; Usage

;; TODO

;;; Todo:

;; Make htmlize convert all URLs to hyperlinks

;;; License:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Code:

(require 'url) ;; Included in recent version of Emacs; available for pre-22.
(require 'htmlize) ;; http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html

(defvar rwd-paste-http-destination
  "http://p.zenspider.com"
  "Publicly-accessible (via HTTP) location for pasted files.")

(defvar rwd-paste-destination
  "~/Sites/pastes/"
  "Directory corresponding to `rwd-paste-http-destination'.
You must have write-access to this directory.")

(defvar rwd-paste-sync-command
  "cd ~/Sites; ./sync.sh"
  "The command to use to sync to your paste website")

(defvar rwd-paste-footer
  (concat "<p style='font-size: 8pt; font-family: monospace;'>Generated by "
    user-full-name
    " using <a href='"
    rwd-paste-http-destination
    "'>rwd-paste</a> at %s "
    (cadr (current-time-zone)) ".</p>")
  "HTML message to place at the bottom of each file.")

;;;###autoload
(defun rwd-paste (original-name)
  "Paste the current buffer to `rwd-paste-http-destination'."
  (interactive "MName (defaults to buffer name): ")
  (let* ((b (htmlize-buffer))
   (name (url-hexify-string (if (equal "" original-name)
              (buffer-name)
            original-name)))
   (full-url (concat rwd-paste-http-destination "/" name ".html"))
   (destination (concat rwd-paste-destination "/" name ".html")))

    ;; Save the file (while adding footer)
    (save-excursion
      (switch-to-buffer b)
      (search-forward "  </body>\n</html>")
      (insert (format rwd-paste-footer (current-time-string)))
      (write-file destination)
      (kill-buffer b))

    (message "syncing...")
    (shell-command rwd-paste-sync-command)

    ;; Notify user and put the URL on the kill ring
    (kill-new full-url)
    (message "Pasted to %s (on kill ring)" full-url)))

(defun rwd-paste-index ()
  "Generate an index of all existing pastes on server on the splash page."
  (interactive)
  (let ((dest-parts (split-string rwd-paste-destination ":")))
    (eshell-command (concat "ssh " (car dest-parts) " ls " (cadr dest-parts)))
    (save-excursion
      (switch-to-buffer "*EShell Command Output*")
      (flush-lines "^Password: $" (point-min) (point-max))
      (flush-lines "private" (point-min) (point-max))
      (let ((file-list (split-string (buffer-string) "\n")))
  (with-temp-buffer
    (insert-file-contents "~/.emacs.d/rwd-paste.el") ;; TODO: find elisp's __FILE__
    (goto-char (point-min))
    (search-forward ";;; Commentary")
    (previous-line)
    (insert "\n;;; Pasted Files\n\n")
    (mapcar (lambda (file) (insert (concat ";; * <" rwd-paste-http-destination "/" file ">\n"))) file-list)
    (emacs-lisp-mode) (font-lock-fontify-buffer) (rename-buffer "rwd-paste")
    (rwd-paste "index")))
      (ignore-errors (kill-buffer "*EShell Command Output*")))))

(provide 'rwd-paste)
;;; rwd-paste.el ends here

Generated by Ryan Davis using rwd-paste at Tue Jan 11 15:44:21 2011 PST.