-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfanyi-etymon.el
132 lines (115 loc) · 5.8 KB
/
fanyi-etymon.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
;;; fanyi-etymon.el --- Etymonline dictionary service -*- lexical-binding: t -*-
;; Copyright (C) 2021 Zhiwei Chen
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Author: Zhiwei Chen <condy0919@gmail.com>
;; Keywords: convenience, tools
;; URL: https://github.com/condy0919/fanyi.el
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (s "1.12.0"))
;; 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:
;;
;; Etymonline dictionary service.
;;; Code:
(require 'fanyi-base)
(require 's)
(require 'dom)
(require 'seq)
(require 'cl-lib)
(require 'button)
(defclass fanyi-etymon-service (fanyi-base-service)
((definitions :initarg :definitions
:type list
:documentation "List of (word . def).
Where def could be a list of string/(string 'face face)/(string 'button data)."))
"The etymonline service.")
;; Silence unknown slots warning.
(eieio-declare-slots :definitions)
;; Silence compile warning.
(declare-function fanyi-dwim "fanyi")
(defvar fanyi-etymon--consecutive-newlines nil)
(cl-defmethod fanyi-parse-from ((this fanyi-etymon-service) dom)
"Complete the fields of THIS from DOM tree.
If the definitions of word are not found, http 404 error is
expected.
The /italic/ and *bold* styles are borrowed from `org-mode',
while the quote style is from mailing list."
(let ((defs (dom-by-class dom "^\\(word--C9UPa\\)$")))
(oset this definitions
(cl-loop for def in defs
for title = (dom-text (dom-by-class def "word__name--TTbAA"))
for details = (dom-children (dom-by-class def "^\\(word__defination--2q7ZH\\)$"))
collect (list title
(seq-mapcat
(lambda (arg)
(pcase arg
('(p nil)
(prog1
;; Eliminate extra '\n\n' if it happens two or more times.
(if fanyi-etymon--consecutive-newlines
""
"\n\n")
(setq fanyi-etymon--consecutive-newlines t)))
((pred stringp) arg)
(_
(cl-assert (> (length arg) 2))
(setq fanyi-etymon--consecutive-newlines nil)
(seq-concatenate
'list
(when (equal (car arg) 'blockquote)
'("> "))
(seq-map (lambda (arg)
(cond ((stringp arg) arg)
((dom-by-class arg "foreign notranslate")
(cond ((dom-by-tag arg 'strong)
(concat "*" (dom-texts arg) "*"))
(t
(concat "/" (dom-text arg) "/"))))
((dom-by-class arg "crossreference notranslate")
(list (dom-text arg) 'button (dom-text arg)))
(t
(s-replace-all '(("\u00a0" . "")
("\u0009" . ""))
(dom-text arg)))))
(cddr arg))))))
details))))))
(cl-defmethod fanyi-render ((this fanyi-etymon-service))
"Render THIS page into a buffer named `fanyi-buffer-name'.
It's NOT thread-safe, caller should hold `fanyi-buffer-mtx'
before calling this method."
(fanyi-with-fanyi-buffer
;; The headline about Etymonline service.
(insert "# Etymonline\n\n")
(cl-loop for i in (oref this definitions)
for (word def) = i
do (insert "## " word "\n\n")
do (seq-do (lambda (arg)
(pcase arg
(`(,s button ,word)
(insert-button s
'action #'fanyi-dwim
'button-data word
'follow-link t))
(s
(insert s))))
def)
do (while (equal (char-before) ?\n)
(delete-char -1))
do (insert "\n\n"))))
(defconst fanyi-etymon-provider
(fanyi-etymon-service :word "dummy"
:url "https://www.etymonline.com/word/%s"
:sound-url "unused"
:api-type 'xml)
"Etymonline dictionary service instance.")
(provide 'fanyi-etymon)
;;; fanyi-etymon.el ends here