1 ;;; org-tag-eldoc-moegirl.el --- org-tag-eldoc backend with moegril.org.cn -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
11 (require 'url-http
) ; for `url-http-end-of-headers'
14 (require 'elquery nil t
) ; optionally use elquery.el to replace dom.el.
15 (require 'org-tag-eldoc-common
)
18 (defvar org-tag-eldoc-moegirl-url nil
19 "A variable to store the url of moegirl.")
21 (defun org-tag-eldoc-moegirl--request (tag)
22 "Send HTTP GET request to get TAG explanation data."
23 (let ((request-backend 'url-retrieve
) ; use `url-retrieve' backend for proxy.
24 (url-proxy-services org-tag-eldoc-request-proxy
)
25 (url (format "https://zh.moegirl.org.cn/%s" (url-hexify-string tag
))))
30 ((fboundp 'libxml-parse-html-region
) ; convert HTML -> Elisp alist structure
31 (libxml-parse-html-region (point-min) (point-max)))
32 ((featurep 'elquery
) ; convert HTML -> elquery object structure
33 (elquery-read-buffer (current-buffer)))))
35 (lambda (&key data
&allow-other-keys
)
36 ;; DEBUG: (setq request-result data)
39 ;; for `libxml' parser
40 (setq org-tag-eldoc--explanation
41 (org-tag-eldoc-common--format-explanation
43 ;; FIXME: The content text is dynamically loaded by JavaScript.
44 (dom-search (dom-by-id data
"mw-content-text")
45 (lambda (node) (and (eq (cl-first node
) 'p
) (null (cl-second node
)))))))))
47 ;; for `elquery' parser
48 (setq org-tag-eldoc--explanation
49 (elquery-text (car (elquery-$
"#mw-content-text > div > p" data
))))))))
51 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
52 (error "[org-tag-eldoc] (萌娘百科) request error %s!" error-thrown
)
54 :status-code
'((404 .
(lambda (&rest _
) nil
))
55 (500 .
(lambda (&rest _
) nil
))))
57 ;; (with-current-buffer (url-retrieve-synchronously url)
59 ;; (goto-char url-http-end-of-headers)
60 ;; (libxml-parse-html-region (point) (point-max)))))
62 ;; (setq request-result dom)
63 ;; (org-tag-eldoc-common--format-explanation
67 ;; ;; FIXME: The content text is dynamically loaded by JavaScript.
68 ;; (dom-search (dom-by-id dom "mw-content-text")
69 ;; (lambda (node) (and (eq (cl-first node) 'p) (null (cl-second node)))))))
70 ;; ((featurep 'elquery)
71 ;; (elquery-text (car (elquery-$ "#mw-content-text > div > p" data))))))))
75 ;;; search tag: https://zh.moegirl.org.cn/index.php?search=linux
76 ;;; tag page: https://zh.moegirl.org.cn/Linux娘, https://zh.moegirl.org.cn/Linux%E5%A8%98
77 ;; (org-tag-eldoc-moegirl--request "Linux娘")
81 ;; (dom-search (dom-by-id request-result "mw-content-text")
82 ;; (lambda (node) (and (eq (car node) 'p) (null (cl-second node))))))
85 (defun org-tag-eldoc-moegirl-query (tag)
86 "Query TAG on moegirl.org.cn then return a cons cell of tag and explanation."
87 (org-tag-eldoc-database-query tag
)
88 (if (stringp org-tag-eldoc--explanation
)
89 org-tag-eldoc--explanation
90 (org-tag-eldoc-moegirl--request tag
)
92 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation
)))
96 (provide 'org-tag-eldoc-moegirl
)
98 ;;; org-tag-eldoc-moegirl.el ends here