Added support for buffer-language depending on mode for some providers

This only works for providers that posts this info when creating the
paste. Currently 3/6 providers does this. The rest of them just
transforms the URL on return which is slightly different and will be
solved later.

This refs #7.

Squashed commit of the following:

commit 612c2c4a0d9278d1ed3c7d794dca29acfa91e647
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 16:24:41 2017 +0200

    Enable gist.github.com paste code highlighting by sending filename

commit f7fc4366b8a514247eaf9a6568da0632f2ee4c93
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 16:09:58 2017 +0200

    Repair github by allowing other keys so it doesn't break again

commit 16cf85a74f94f5c2ab059d99dab835de643221d1
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 16:02:17 2017 +0200

    Set clojure for emacs-lisp-mode for dpaste.com and dpaste.de

commit 442187a27fe32be028483367ca70aee797698148
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 15:55:59 2017 +0200

    Added option lang-overrides to webpaste-provider to enable overiding of language mappings

commit 2691c6690e7a4ed5bddb1fa4c27e09aea9b61887
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 15:39:18 2017 +0200

    Added ert-test for the lang-alist overrides

commit a9b651024f9360793406f51572a5732b912bb4f8
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 14:55:46 2017 +0200

    Added function to apply an override list to the default list and return it

commit e282bbb8a72ea580a8d72c67873ca0b44b7c789c
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 12:58:27 2017 +0200

    Added language field to dpaste.com and dpaste.de

commit e8671743d2ee6afc8ae9a42b64d1a9bdea800d67
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 12:14:15 2017 +0200

    Logic to add fields to add language to post data

commit 71cab8124b596d7e8e36ac71399cd64e16f48d42
Author: Elis Axelsson <elis.axelsson@gmail.com>
Date:   Wed May 3 09:18:29 2017 +0200

    Added a small first list of mode to language mappings.
master
Elis Axelsson 9 years ago
parent 4e58b0a69d
commit 0e4ac51b57
No known key found for this signature in database
GPG Key ID: D57EFA625C9A925F
  1. 31
      test/webpaste-test.el
  2. 91
      webpaste.el

@ -167,6 +167,37 @@ result from the good provider only."
;; Check that we got the expected result
(should (string= returned-result "Working: test-string"))))
(ert-deftest webpaste-test/get-lang-alist-with-overrides ()
"This test tests all cases that should happen when overriding langs."
(let ((webpaste/default-lang-alist '((python-mode . "python")
(php-mode . "php"))))
;; Test adding mode
(should (equal (webpaste/get-lang-alist-with-overrides
'((emacs-lisp-mode . "lisp")))
'((emacs-lisp-mode . "lisp")
(python-mode . "python")
(php-mode . "php"))))
;; Test removing mode / clearing it's value
(should (equal (webpaste/get-lang-alist-with-overrides
'((python-mode . nil)))
'((python-mode)
(python-mode . "python")
(php-mode . "php"))))
;; Test overriding mode
(should (equal (webpaste/get-lang-alist-with-overrides
'((python-mode . "python3")))
'((python-mode . "python3")
(python-mode . "python")
(php-mode . "php"))))))
;;; webpaste-test.el ends here

@ -54,6 +54,21 @@ default to all providers in order defined in ‘webpaste-providers’ list."
:type '(repeat string))
(defcustom webpaste/default-lang-alist
'((css-mode . "css")
(fundamental-mode . "text")
(html-mode . "html")
(java-mode . "java")
(js-mode . "js")
(go-mode . "go")
(php-mode . "php")
(python-mode . "python")
(yaml-mode . "yaml"))
"Alist that maps `major-mode' names to language names."
:type '(alist :key-type symbol :value-type string)
:group 'webpaste)
(defvar webpaste-tested-providers ()
"Variable for storing which providers to try in which order while running.
This list will be re-populated each run based on webpaste-provider-priority or
@ -98,9 +113,21 @@ each run.")
(defvar webpaste/providers-default-post-field-lambda
(cl-function (lambda (&key text
post-field
(post-lang-field-name nil)
(lang-overrides nil)
(post-data '()))
(cl-pushnew (cons post-field text) post-data)
(when post-lang-field-name
;; Get language name based on major-mode
(let ((language-name (cdr (assoc major-mode (webpaste/get-lang-alist-with-overrides lang-overrides)))))
;; If not set correctly, get the fundamental-mode one which should be plaintext
(unless language-name
(setq language-name (cdr (assoc 'fundamental-mode (webpaste/get-lang-alist-with-overrides lang-overrides)))))
;; Append language to the post-data
(cl-pushnew (cons post-lang-field-name language-name) post-data)))
post-data))
"Predefined lambda for building post fields.")
@ -111,7 +138,9 @@ each run.")
success-lambda
(type "POST")
(post-data '())
(post-lang-field-name nil)
(parser 'buffer-string)
(lang-overrides '())
(error-lambda webpaste/providers-error-lambda)
(post-field-lambda webpaste/providers-default-post-field-lambda)
(sync nil))
@ -137,6 +166,13 @@ Optional params:
:post-data Default post fields sent to service. Defaults to nil.
:post-lang-field-name Fieldname for defining which language your paste should
use to the provider.
:lang-overrides Alist defining overides for languages for this provider. If
a mode is set to nil, it will use fundamental-mode's value as
fallback. Fundamental-mode's value can also be overridden.
:parser Defines how request.el parses the result. Look up :parser for
`request'. This defaults to 'buffer-string.
@ -149,7 +185,9 @@ Optional params:
:post-field-lambda Function that builds and returns the post data that should be
sent to the provider. It should accept named parameters by
the names TEXT, POST-FIELD and POST-DATA. POST-DATA should
default to `nil' or empty list.
default to `nil' or empty list. It also takes the option
LANG-OVERRIDES which is a list that enables overiding of
`webpaste/default-lang-alist'.
TEXT contains the data that should be sent.
POST-FIELD cointains the name of the field to be sent.
@ -167,6 +205,8 @@ Optional params:
:data (funcall post-field-lambda
:text text
:post-field post-field
:post-lang-field-name post-lang-field-name
:lang-overrides lang-overrides
:post-data post-data)
:parser parser
:success success-lambda
@ -198,32 +238,37 @@ Optional params:
("dpaste.com"
,(webpaste-provider
:uri "http://dpaste.com/api/v2/"
:post-data '(("syntax" . "text")
("title" . "")
:post-data '(("title" . "")
("poster" . "")
("expiry_days" . 1))
:post-field "content"
:post-lang-field-name "syntax"
:lang-overrides '((emacs-lisp-mode . "clojure"))
:success-lambda webpaste/providers-success-location-header))
("dpaste.de"
,(webpaste-provider
:uri "https://dpaste.de/api/"
:post-data '(("lexer" . "text")
("format" . "url")
("expires" . 86400))
:post-data '(("expires" . 86400))
:post-field "content"
:post-lang-field-name "lexer"
:lang-overrides '((emacs-lisp-mode . "clojure"))
:success-lambda webpaste/providers-success-returned-string))
("gist.github.com"
,(webpaste-provider
:uri "https://api.github.com/gists"
:post-field nil
:post-field-lambda (cl-function (lambda (&key text post-field (post-data '()))
(json-encode `(("description" . "Pasted from Emacs with webpaste.el")
("public" . "false")
("files" .
(("file.txt" .
(("content" . ,text)))))))))
:post-field-lambda (cl-function (lambda (&key text
post-field
(post-data '())
&allow-other-keys)
(let ((filename (or (file-name-nondirectory (buffer-file-name)) "file.txt")))
(json-encode `(("description" . "Pasted from Emacs with webpaste.el")
("public" . "false")
("files" .
((,filename .
(("content" . ,text))))))))))
:success-lambda (cl-function (lambda (&key data &allow-other-keys)
(when data
(webpaste-return-url
@ -239,6 +284,28 @@ return it to the user."
(defun webpaste/get-lang-alist-with-overrides (overrides)
"Fetches lang-alist with OVERRIDES applied."
(let ((lang-alist webpaste/default-lang-alist))
;; Go through list of overrides
(dolist (override-element overrides)
;; Set key and value from override list
(let ((key (car override-element))
(value (cdr override-element)))
;; If the element doesn't exist, add it
(unless (assoc (car override-element) lang-alist)
(cl-pushnew (cons key value) lang-alist))
;; If the element in the list is changed
(unless (equal (cdr (assoc key lang-alist)) value)
(cl-pushnew (cons key value) lang-alist))))
lang-alist))
(defun webpaste/get-provider-priority ()
"Return provider priority."

Loading…
Cancel
Save