-
Notifications
You must be signed in to change notification settings - Fork 0
/
maple-translate-youdao.el
75 lines (61 loc) · 3.47 KB
/
maple-translate-youdao.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
64
65
66
67
68
69
70
71
72
73
74
75
;;; maple-translate-youdao.el --- translate with youdao. -*- 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 youdao
;;
;;; Code:
(require 'maple-translate-core)
(defun maple-translate-youdao-format(dom)
"Format seatch with bing DOM TEXT."
(when dom
(concat
(let ((result (cl-loop for child in (maple-translate-dom-find dom ".fanyi dict-module/.trans-container/.trans-content")
when (consp child)
collect (format "- %s" (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 ".ec dict-module/.trans-container/.phone_con/.per-phone")
when (consp child)
collect (dom-texts 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 ".simple dict-module/.trans-container/.word-exp")
when (consp child)
collect (dom-texts 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 ".web_trans dict-module/.trans-container/.webPhrase/.mcols-layout")
when (consp child)
collect (format "- %s\n %s"
(dom-text (maple-translate-dom-find child ".point"))
(dom-text (maple-translate-dom-find child ".sen-phrase"))))))
(unless (null result)
(format "组词:\n%s\n\n" (string-join result "\n"))))
(let ((result (cl-loop for child in (maple-translate-dom-find dom ".blng_sents_part dict-module/.trans-container/.mcols-layout")
when (consp child)
collect (format "- %s\n %s"
(dom-texts (maple-translate-dom-find child ".col2/.word-exp[0]"))
(dom-texts (maple-translate-dom-find child ".col2/.word-exp[1]"))))))
(unless (null result)
(format "例句:\n%s" (string-join result "\n")))))))
(defun maple-translate-youdao(text &optional callback)
"Search TEXT with youdao, use async request if CALLBACK non-nil."
(let ((url (format "https://dict.youdao.com/result?word=%s&lang=en" (url-hexify-string text))))
(maple-translate-request url
:format (maple-translate-youdao-format (libxml-parse-html-region (point)))
:headers t
:callback callback)))
(provide 'maple-translate-youdao)
;;; maple-translate-youdao.el ends here