;;; rwd-paste.el --- Personal Paste Service.

;; Copyright (C) Ryan Davis

;; Author: Ryan Davis
;; URL: TODO
;; Version: 1.0
;; Created: 2011-01-11
;; Keywords: convenience hypermedia
;; EmacsWiki: TODO

;; This file is NOT part of GNU Emacs.

;;; Pasted Files

;; * <http://p.zenspider.com/activate.diff.html>
;; * <http://p.zenspider.com/amb-filter.scm.html>
;; * <http://p.zenspider.com/bad-macro.scm.html>
;; * <http://p.zenspider.com/bucket.html>
;; * <http://p.zenspider.com/dir.html>
;; * <http://p.zenspider.com/dwatch.rb.html>
;; * <http://p.zenspider.com/eval.scm.html>
;; * <http://p.zenspider.com/exclusions.diff.html>
;; * <http://p.zenspider.com/exercise.5.33.scm-17394-18201.html>
;; * <http://p.zenspider.com/exercise.5.41.scm.html>
;; * <http://p.zenspider.com/fetch_command.diff.html>
;; * <http://p.zenspider.com/fuck_bundler.rb.html>
;; * <http://p.zenspider.com/fuck_gem_prelude.diff.html>
;; * <http://p.zenspider.com/fuck_you_bundler.rb.html>
;; * <http://p.zenspider.com/gosu-extensions.rb.html>
;; * <http://p.zenspider.com/index.html>
;; * <http://p.zenspider.com/isolate_tests.rb.html>
;; * <http://p.zenspider.com/macruby-minitest-failure.rb.html>
;; * <http://p.zenspider.com/matching_paths.html>
;; * <http://p.zenspider.com/minitest-pride2.rb.html>
;; * <http://p.zenspider.com/multi-replace-regexp.el.html>
;; * <http://p.zenspider.com/multiple-choice.rb.html>
;; * <http://p.zenspider.com/nuke_my_test.rb.html>
;; * <http://p.zenspider.com/omnifocus-wtf.html>
;; * <http://p.zenspider.com/paths.diff.html>
;; * <http://p.zenspider.com/project_review_schedule.txt.html>
;; * <http://p.zenspider.com/register_spec_type.diff.html>
;; * <http://p.zenspider.com/release-schedule.txt.html>
;; * <http://p.zenspider.com/rename.html>
;; * <http://p.zenspider.com/rspec-to-minispec-to-minitest.el.html>
;; * <http://p.zenspider.com/rspec-to-minitest.el.html>
;; * <http://p.zenspider.com/rubygems-rakefile-hack.html>
;; * <http://p.zenspider.com/rubygems.diff.html>
;; * <http://p.zenspider.com/rubygemssafe.diff.html>
;; * <http://p.zenspider.com/rwd-hide-region.el.html>
;; * <http://p.zenspider.com/rwd-paste-region.html>
;; * <http://p.zenspider.com/rwd-paste.el.html>
;; * <http://p.zenspider.com/scanner.rb.html>
;; * <http://p.zenspider.com/scheme-is-sexy.html>
;; * <http://p.zenspider.com/stub.rb.html>
;; * <http://p.zenspider.com/tags.diff.html>
;; * <http://p.zenspider.com/test_wtf.rb.html>
;; * <http://p.zenspider.com/threaded-engine.c.html>
;; * <http://p.zenspider.com/traverse.rb.html>
;; * <http://p.zenspider.com/unified-cmd.rb.html>
;; * <http://p.zenspider.com/wfm.rb.html>
;; * <http://p.zenspider.com/wtf.html>

;;; Commentary:

;; This was heavily plagerized from Phil Hagelberg's scpaste.

;;; Install

;; TODO

;;; Usage

;; TODO

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

(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-buffer (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)))

;;;###autoload
(defun rwd-paste (begin end original-name)
  "Paste the current region to `rwd-paste-http-destination'."
  (interactive "r\nMName (defaults to buffer-name-begin-end): ")
  (let* ((b (htmlize-region begin end))
         (name (url-hexify-string (if (equal "" original-name)
                                      (format "%s-%d-%d"
                                              (buffer-name) begin end)
                                    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)
    (save-excursion
      (let ((file-list (directory-files rwd-paste-destination nil "^[^.]")))
        (with-temp-buffer
          (insert-file-contents (find-library-name "rwd-paste"))
          (goto-char (point-min))
          (search-forward ";;; Commentary")
          (previous-line)
          (insert "\n;;; Pasted Files\n\n")
          (mapc (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-buffer "index")))))

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

Generated by Ryan Davis using rwd-paste at Thu Jun 28 15:20:51 2012 PDT.