From ce5126a18d7dd275f9b72f5a6481c56c2db47cc4 Mon Sep 17 00:00:00 2001 From: hubisan Date: Sun, 28 Jul 2024 01:48:47 +0200 Subject: [PATCH] fix examples if a parent heading doesn't have examples --- woerterbuch.el | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/woerterbuch.el b/woerterbuch.el index e6d9af6..c89367b 100644 --- a/woerterbuch.el +++ b/woerterbuch.el @@ -233,6 +233,28 @@ Specify WIDTH and HEIGHT or set em to nil to not change it manually." (defconst woerterbuch--definitions-dwds-url "https://www.dwds.de/wb/%s" "Url to retrieve the definitions for a word as html from DWDS.") +(defun woerterbuch--definitions-retrieve-examples (leseart) + "Retrieve the examples for LESEART" + (when-let* ((dom (dom-children + (car (dom-by-class + leseart + "^dwdswb-lesart-content$")))) + (examples-dom (catch 'result + (dolist (element dom) + (cond + ((string= (dom-attr element 'class) + "dwdswb-verwendungsbeispiele") + (throw 'result element)) + ((string= (dom-attr element 'class) + "dwdswb-lesart") + (throw 'result nil)))))) + (examples-texts (mapcar #'dom-texts + (dom-by-class examples-dom + "^dwdswb-belegtext$")))) + (if woerterbuch-definitions-examples-max + (take woerterbuch-definitions-examples-max examples-texts) + examples-texts))) + (defun woerterbuch--definitions-retrieve-raw (word &optional add-examples) "Return a raw list of definitions for WORD. Each element is a cons with the car being the id of the defintion and the cadr @@ -274,17 +296,10 @@ TODO Refactor this, should be separated into multiple functions." (text (or text "")) (examples (when add-examples - (mapcar #'dom-texts - (dom-by-class - (car (dom-by-class - leseart - "^dwdswb-verwendungsbeispiele$")) - "^dwdswb-belegtext$")))) - (examples (if woerterbuch-definitions-examples-max - (take woerterbuch-definitions-examples-max examples) - examples))) + (woerterbuch--definitions-retrieve-examples + leseart)))) (when (and (stringp id) (stringp text)) - (if (listp examples) + (if (and examples (listp examples)) (cons id (list text examples)) (cons id text))))) lesearten)))