summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2025-07-26 17:03:52 +0800
committerJSDurand <mmemmew@gmail.com>2025-07-26 17:03:52 +0800
commit2bd09de5db48fd1cbcbffc77f5e1ab3c339dee61 (patch)
treea625abae8d5dccd86742c989672508d88bed57c2
parentaed143d3eba709405c769f49b069967b06f4d274 (diff)
durand-dict: An Emacs interface for searching dictionaries.
It is at a barebones stage now. Still improvements are needed, but it is already kind of usable now: only the interface needs improvements.
-rw-r--r--durand-dict.el145
1 files changed, 145 insertions, 0 deletions
diff --git a/durand-dict.el b/durand-dict.el
new file mode 100644
index 0000000..c40ca68
--- /dev/null
+++ b/durand-dict.el
@@ -0,0 +1,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