;;; 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 ;; 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 . ;;; 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