-
Notifications
You must be signed in to change notification settings - Fork 0
/
maple-translate-bing.el
63 lines (50 loc) · 2.64 KB
/
maple-translate-bing.el
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
;;; maple-translate-bing.el --- translate with bing.com/dict. -*- lexical-binding: t -*-
;; Copyright (C) 2023-2024 lin.jiang
;; URL: https://github.com/honmaple/emacs-maple-translate
;; This file 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 file 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 file. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; translate word with bing.com/dict
;;
;;; Code:
(require 'maple-translate-core)
(defun maple-translate-bing-format(dom)
"Format seatch with bing DOM."
(when dom
(concat
(let ((result (cl-loop for child in (maple-translate-dom-find dom ".qdef/*[0]/.b_primtxt")
when (consp child)
collect (dom-text child))))
(unless (null result)
(format "读音:\n%s\n\n" (string-join result "\n"))))
(let ((result (cl-loop for child in (maple-translate-dom-find dom ".qdef/*[1]/*")
when (consp child)
collect (format "%s %s"
(dom-text (nth 2 child))
(dom-texts (nth 3 child) "")))))
(unless (null result)
(format "基本释义:\n%s\n\n" (string-join result "\n"))))
(let ((result (cl-loop for child in (maple-translate-dom-find dom "#sentenceSeg/.se_li")
when (consp child)
collect (format "- %s\n %s"
(dom-texts (maple-translate-dom-find child ".sen_en") "")
(dom-texts (maple-translate-dom-find child ".sen_cn") "")))))
(unless (null result)
(format "例句:\n%s" (string-join result "\n")))))))
(defun maple-translate-bing(word &optional callback)
"Search WORD with bing, use async request if CALLBACK non-nil."
(let ((url (format "https://www.bing.com/dict/search?mkt=zh-cn&q=%s" (url-hexify-string word))))
(maple-translate-request url
:format (maple-translate-bing-format (dom-by-class (libxml-parse-html-region (point)) "content"))
:callback callback)))
(provide 'maple-translate-bing)
;;; maple-translate-bing.el ends here