home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

paste-sbr.el (2561B)


      1 ;;; paste-sbr.el --- Paste to sbr.pm -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020 Vincent Demeester
      4 
      5 ;; Author: Vincent Demeester <vincent@sbr.pm>
      6 ;; Keywords: org link github
      7 ;;
      8 ;; This file is not part of GNU Emacs.
      9 
     10 ;; This program is free software; you can redistribute it and/or
     11 ;; modify it under the terms of the GNU General Public License as
     12 ;; published by the Free Software Foundation; either version 3.0, or
     13 ;; (at your option) any later version.
     14 
     15 ;; This program is distributed in the hope that it will be useful,
     16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 ;; GNU General Public License for more details.
     19 
     20 ;; You should have received a copy of the GNU General Public License
     21 ;; along with this program; if not, write to the Free Software
     22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     24 ;;
     25 ;;; Commentary:
     26 
     27 ;; Take selection and share it to paste.sbr.pm
     28 
     29 ;;; Code:
     30 
     31 (defvar htmlize-paste-it-target-directory
     32   "desktop/sites/paste.sbr.pm")
     33 (defvar htmlize-paste-it-base-url
     34   "https://paste.sbr.pm/")
     35 
     36 (defun htmlize-paste-it ()
     37   "Htmlize region-or-buffer and copy to directory."
     38   (interactive)
     39   (let* ((start (if (region-active-p)
     40                     (region-beginning) (point-min)))
     41          (end (if (region-active-p)
     42                   (region-end) (point-max)))
     43 
     44          ;; We use a basename-hash.ext.html format
     45          (basename (file-name-base (buffer-name)))
     46          (extension (file-name-extension (buffer-name)))
     47          (hash (sha1 (current-buffer) start end))
     48          (file-name (concat basename
     49                             "-" (substring hash 0 6)
     50                             "." extension
     51                             ".html"))
     52 
     53          (new-file (expand-file-name (concat
     54                                       htmlize-paste-it-target-directory
     55                                       "/"
     56                                       file-name) "~"))
     57 
     58          (access-url (concat
     59                       htmlize-paste-it-base-url
     60                       file-name)))
     61     ;; Region messes with clipboard, so deactivate it
     62     (deactivate-mark)
     63     (with-current-buffer (htmlize-region start end)
     64       ;; Copy htmlized contents to target
     65       (write-file new-file)
     66       ;; Ensure target can be accessed by web server
     67       (chmod new-file #o755))
     68     ;; Put URL into clipboard
     69     (kill-new access-url)))
     70 
     71 (provide 'paste-sbr)
     72 ;;; paste-sbr.el ends here