1 ;;; org-tag-eldoc-urban-dictionary.el --- Urban Dictionary -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
8 ;; search API: https://www.urbandictionary.com/define.php?term=[query]. The `query' is "prone%20bone".
13 (require 'url-http
) ; for `url-http-end-of-headers'
16 (require 'elquery nil t
) ; optionally use elquery.el to replace dom.el.
17 (require 'org-tag-eldoc-common
)
20 (defun org-tag-eldoc-urban-dictionary--request (tag)
21 "Send HTTP GET request to get TAG explanation dtaa on www.urbandictionary.com."
22 (let ((request-backend 'url-retrieve
) ; use `url-retrieve' backend for proxy.
23 (request-message-level -
1)
24 (url-proxy-services org-tag-eldoc-request-proxy
)
25 (url (format "https://www.urbandictionary.com/define.php?term=%s"
26 (url-hexify-string (string-replace "_" " " tag
)))))
31 ((featurep 'elquery
) ; convert HTML -> elquery object structure
32 (elquery-read-buffer (current-buffer)))
33 ((fboundp 'libxml-parse-html-region
) ; convert HTML -> Elisp alist structure
34 (libxml-parse-html-region (point-min) (point-max)))))
36 (lambda (&key data
&allow-other-keys
)
37 ;; DEBUG: (setq request-result data)
40 ;; for `elquery' parser
41 (setq org-tag-eldoc--explanation
45 (elquery-$
"section > div:nth-child(1) > div > div.break-words.meaning.mb-4" data
))))))
47 ;; for `libxml' parser
48 (setq org-tag-eldoc--explanation
49 (org-tag-eldoc-common--format-explanation
51 ;; FIXME: The CSS selector is not right.
52 (dom-search (dom-by-class data
"div.break-words.meaning.mb-4")
53 (lambda (node) (and (eq (cl-first node
) 'p
) (null (cl-second node
)))))))))
56 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
57 ;; (message "[org-tag-eldoc] (Urban Dictionary) request error %s!" error-thrown)
59 :status-code
'((404 .
(lambda (&rest _
) nil
))
60 (443 .
(lambda (&rest _
) nil
))
61 (500 .
(lambda (&rest _
) nil
))))
63 ;; (with-current-buffer (url-retrieve-synchronously url)
65 ;; (goto-char url-http-end-of-headers)
66 ;; (libxml-parse-html-region (point) (point-max)))))
67 ;; ;; DEBUG: (setq request-result dom)
68 ;; (org-tag-eldoc-common--format-explanation
72 ;; (dom-search (dom-by-class dom "mw-content-ltr\\ mw-parser-output")
73 ;; (lambda (node) (and (eq (cl-first node) 'p) (null (cl-second node)))))))
74 ;; ((featurep 'elquery)
75 ;; (elquery-text (car (elquery-$ "#mw-content-text > div.mw-content-ltr.mw-parser-output > p" data))))))))
79 ;;; TEST: https://www.urbandictionary.com/define.php?term=bed%20time
80 ;; (org-tag-eldoc-urban-dictionary--request "bed_time")
83 ;;; #ud-root > div > main > div > div.flex.flex-col.lg\:flex-row.mx-0.gap-4 > section > div:nth-child(1) > div > div.break-words.meaning.mb-4
86 ;;; //*[@id="ud-root"]/div/main/div/div[4]/section/div[1]/div/div[2]
88 ;;; /html/body/div[1]/div/main/div/div[4]/section/div[1]/div/div[2]
91 ;;; document.querySelector("#ud-root > div > main > div > div.flex.flex-col.lg\\:flex-row.mx-0.gap-4 > section > div:nth-child(1) > div > div.break-words.meaning.mb-4")
93 ;;; TEST: `request-result'
96 ;; (when (featurep 'dom)
97 ;; ;; for `libxml' parser
98 ;; (setq org-tag-eldoc--explanation
99 ;; (org-tag-eldoc-common--format-explanation
101 ;; (dom-search (dom-by-class request-result "section > div:nth-child(1) > div > div.break-words.meaning.mb-4")
102 ;; (lambda (node) (and (eq (cl-first node) 'p) (null (cl-second node)))))))))
105 ;; (when (featurep 'elquery)
106 ;; ;; for `elquery' parser
107 ;; (setq org-tag-eldoc--explanation
110 ;; (mapcar 'elquery-text
111 ;; (elquery-$ "section > div:nth-child(1) > div > div.break-words.meaning.mb-4" request-result))))))
113 (defun org-tag-eldoc-urban-dictionary-query (tag)
114 "Query TAG on Urban Dictionary then return a cons cell of tag and explanation."
115 (if (stringp org-tag-eldoc--explanation
)
116 org-tag-eldoc--explanation
117 (org-tag-eldoc-urban-dictionary--request tag
)
119 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation
)))
123 (provide 'org-tag-eldoc-urban-dictionary
)
125 ;;; org-tag-eldoc-urban-dictionary.el ends here