summaryrefslogtreecommitdiff
path: root/durand-dict.el
blob: c40ca686b8d6948b50bb737c72d6b34e466db907 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; durand-dict.el --- An Emacs interface to dictionaries  -*- lexical-binding: t; -*-

;; Copyright (C) 2025  Jean Sévère Durand

;; Author: Jean Sévère Durand <durand@jsdurand.xyz>
;; Keywords: convenience, emulations

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This aims to provide a little interface to work with dictionaries
;; of the choice.  One can customize the dictionaries used and how we
;; parse them.
;;
;; In summary, this package lets the user send a request to some
;; specified dictionary website and parse the output into some
;; customized format, so that Emacs can function as an interface to
;; multiple dictionaries at once.  The supposed benefit is to check
;; the dictionaries without opening the browser.

;;; Code:

(defun dudict-parse-agarathi (word)
  "Request for the definition of Tamil WORD from the dictionary
website agarathi.

WORD should be acceptable by agarathi: no processing is done to
convert from some transliteration to the Tamil script."
  (let ((buffer
         (url-retrieve-synchronously
          (concat "https://agarathi.com/word/" word)
          t t 20))
        result)
    (with-current-buffer buffer
      (let* ((dom (libxml-parse-html-region
                   (point-min) (point-max)))
             (timeline-ul (delq
                           nil
                           (mapcar
                            (lambda (node)
                              (cond ((string= (dom-attr node 'class)
                                              "timeline")
                                     node)))
                            (dom-by-tag dom 'ul))))
             (timelines (delq
                         nil
                         (mapcar
                          (lambda (node)
                            (let ((class (dom-attr node 'class)))
                              (cond
                               ((or
                                 (string= class "timeline-normal")
                                 (string= class "timeline-inverted"))
                                node))))
                          (dom-by-tag timeline-ul 'li))))
             (timeline-panels
              (mapcar
               (lambda (node) (dom-by-class node "timeline-panel"))
               timelines))
             (timeline-panels
              (delq
               nil
               (mapcar (lambda (node)
                         (let ((result
                                (dudict-parse-agarathi-panel node)))
                           (cond
                            ((string=
                              (car result)
                              "Sponsored Links")
                             nil)
                            (result))))
                       timeline-panels))))
        (setq result timeline-panels)
        (kill-buffer buffer)))
    (setq buffer (get-buffer-create (concat "dudict - " word)))
    (with-current-buffer buffer
      (delete-region (point-min) (point-max))
      (goto-char (point-min))
      (mapc
       (lambda (panel)
         ;; insert panel source
         (insert "* " (car panel))
         (setq panel (cdr panel))
         (mapc
          (lambda (def)
            (newline)
            (insert "** def\n\n")
            (mapc
             (lambda (def)
               (insert
                (mapconcat
                 #'identity
                 (split-string def "\n" t "\\s-+")
                 "  ")
                "  ")
               ;; (newline)
               )
             def)
            (newline))
          panel)
         (newline 2))
       result))
    (switch-to-buffer buffer)
    (org-mode)
    (setq-local truncate-lines nil)
    (goto-char (point-min))))

(defun dudict-parse-agarathi-panel (panel)
  "A helper function to find the source of the panel and parse the
contents of PANEL into a list of definitions.

This is designed to be used by the function `dudict-parse-agarathi'."
  (let* ((panel (car panel))
         (panel-title
          (dom-texts (car (dom-by-class panel "timeline-title"))))
         (description
          (delq
           nil
           (mapcar
            (lambda (desc)
              (mapcar
               (lambda (node)
                 (cond
                  ((listp node) (dom-texts node))
                  ((stringp node) node)))
               (dom-children desc)))
            (dom-by-class panel "description")))))
         (cons panel-title (mapcar #'flatten-list description))))

(dudict-parse-agarathi "கறுப்பு")

(provide 'durand-dict)
;;; durand-dict.el ends here