;;; trampaste.el --- Paste to the web via tramp.

;; Copyright (C) 2008 Phil Hagelberg

;; Author: Phil Hagelberg
;; URL: http://www.emacswiki.org/cgi-bin/wiki/TRAMPaste
;; Version: 0.6
;; Created: 2008-04-02
;; Keywords: convenience hypermedia
;; EmacsWiki: TRAMPaste
;; Package-Requires: ((htmlize "1.39"))

;; This file is NOT part of GNU Emacs.

;;; Pasted Files

;; * <http://dme.org/data/paste/ffap.html>
;; * <http://dme.org/data/paste/index.html>
;; * <http://dme.org/data/paste/movement-bindings.patch.html>
;; * <http://dme.org/data/paste/next-open-with-tag.el.html>

;;; Commentary:

;; This will place an HTML copy of a buffer on the web on a server
;; that the user has shell access on.

;; It's similar in purpose to services such as http://paste.lisp.org
;; or http://rafb.net, but it's much simpler since it assumes the user
;; has an account on a publicly-accessible HTTP server. It uses `tramp'
;; as its transport and uses Emacs' font-lock as its syntax
;; highlighter instead of relying on a third-party syntax highlighter
;; for which individual language support must be added one-by-one.

;;; Install

;; To install, copy this file into your Emacs source directory, set
;; `trampaste-http-destination' and `trampaste-file-destination' to
;; appropriate values, and add this to your .emacs file:

;; (autoload 'trampaste "trampaste" "Paste the current buffer." t nil)
;; (setq trampaste-http-destination "http://p.hagelb.org"
;;       trampaste-file-destination "p.hagelb.org:p.hagelb.org")

;;; Usage

;; M-x trampaste, enter a name, and press return. The name will be
;; incorporated into the URL by escaping it and adding it to the end
;; of `trampaste-http-destination'. The URL for the pasted file will be
;; pushed onto the kill ring.

;; You can autogenerate a splash page that gets uploaded as index.html
;; in `trampaste-http-destination' by invoking M-x trampaste-index. This
;; will upload an explanation as well as a listing of existing
;; pastes. If a paste's filename includes "private" it will be skipped.

;; You probably want to set up SSH keys for your destination to avoid
;; having to enter your password once for each paste. Also be sure the
;; key of the host referenced in `trampaste-file-destination' is in your
;; known hosts file--trampaste will not prompt you to add it but will
;; simply hang.

;;; 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
;; 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)
(require 'htmlize)

(defvar trampaste-tramp-port

(defvar trampaste-http-destination
  "Publicly-accessible (via HTTP) location for pasted files.")

(defvar trampaste-file-destination
  "SSH-accessible directory corresponding to `trampaste-http-destination'.
You must have write-access to this directory via tramp.")

(defvar trampaste-paste-hook
  "Hook run after creating a new paste.")

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

;; To set defvar while developing: (load-file (buffer-file-name))
(defvar trampaste-el-location load-file-name)

(defun trampaste (original-name)
  "Paste the current buffer to `trampaste-http-destination'."
  (interactive "MName (defaults to buffer name): ")
  (let* ((b (htmlize-buffer))
         (name (url-hexify-string (if (equal "" original-name)
         (full-url (concat trampaste-http-destination "/" (url-hexify-string name) ".html"))
         (file-destination (concat trampaste-file-destination "/" name ".html"))
         (tramp-original-destination (concat trampaste-file-destination "/" name))
         (tmp-file (concat temporary-file-directory "/" name)))

    ;; Save the file (while adding footer)
      (switch-to-buffer b)
      (goto-char (point-min))
      (search-forward "</body>\n</html>")
      (insert (format trampaste-footer
                      (substring full-url 0 -5)))
      (write-file tmp-file)
      (kill-buffer b))

    (tramp-handle-copy-file tmp-file file-destination t)
    (tramp-handle-copy-file (buffer-file-name (current-buffer))
                            tramp-original-destination t)

    (run-hooks 'trampaste-paste-hook)

    ;; Notify user and put the URL on the kill ring
    (let ((x-select-enable-primary t))
      (kill-new full-url))
    (message "Pasted to %s (on kill ring)" full-url)))

(defun trampaste-region (name)
  "Paste the current region via `trampaste'."
  (interactive "MName: ")
  (let ((region-contents (buffer-substring (mark) (point))))
      (insert region-contents)
      (trampaste name))))

(defun trampaste-index ()
  "Generate an index of all existing pastes on server on the splash page."
  (let ((file-list (directory-files trampaste-file-destination nil "\\.html$")))
        (insert-file-contents trampaste-el-location)
        (goto-char (point-min))
        (search-forward ";;; Commentary")
        (forward-line -1)
        (insert "\n;;; Pasted Files\n\n")
        (dolist (file file-list)
          (when (not (string-match "private" file))
            (insert (concat ";; * <" trampaste-http-destination "/" file ">\n"))))
        (rename-buffer "TRAMPaste")
        (write-file "/tmp/trampaste-index")
        (trampaste "index")))))

(provide 'trampaste)
;;; trampaste.el ends here

Generated by David Edmondson using trampaste at Tue Jan 15 14:06:15 2013. GMT. (original)