aboutsummaryrefslogtreecommitdiff
path: root/sx-tag.el
blob: 5e75890aab7c70a2b72c99a49c7c7fdc5f8f21a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
;;; sx-tag.el --- retrieving list of tags and handling tags  -*- lexical-binding: t; -*-

;; 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 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:



;;; Code:
(eval-when-compile
  '(require 'cl-lib))

(require 'sx)
(require 'sx-method)


;;; Getting the list from a site
(defconst sx-tag-filter
  (sx-filter-from-nil
   (tag.name
    tag.synonyms))
  "Filter used when querying tags.")

(defun sx-tag--get-all (site &optional no-synonyms)
  "Retrieve all tags for SITE.
If NO-SYNONYMS is non-nil, don't return synonyms."
  (cl-reduce
   (lambda (so-far tag)
     (let-alist tag
       (cons .name
             (if no-synonyms so-far
               (append .synonyms so-far)))))
   (sx-method-call 'tags
     :get-all t
     :filter sx-tag-filter
     :site site)
   :initial-value nil))

(defun sx-tag--get-some-tags-containing (site string)
  "Return at most 100 tags for SITE containing STRING.
Returns an array."
  (sx-method-call 'tags
    :auth nil
    :filter sx-tag-filter
    :site site
    :keywords `((inname . ,string))))

(defun sx-tag--get-some-tag-names-containing (site string)
  "Return at most 100 tag names for SITE containing STRING.
Returns a list."
  (mapcar (lambda (x) (cdr (assoc 'name x)))
          (sx-tag--get-some-tags-containing site string)))


;;; Check tag validity
(defun sx-tag--invalid-name-p (site tags)
  "Nil if TAGS exist in SITE.
TAGS can be a string (the tag name) or a list of strings.
Fails if TAGS is a list with more than 100 items.
Return the list of invalid tags in TAGS."
  (and (listp tags) (> (length tags) 100)
       (error "Invalid argument. TAG has more than 100 items"))
  (let ((result
         (mapcar
          (lambda (x) (cdr (assoc 'name x)))
          (sx-method-call 'tags
            :id (sx--thing-as-string tags)
            :submethod 'info
            :auth nil
            :filter sx-tag-filter
            :site site))))
    (cl-remove-if (lambda (x) (member x result)) tags)))

(provide 'sx-tag)
;;; sx-tag.el ends here

;; Local Variables:
;; indent-tabs-mode: nil
;; End: