From a25a1ef209a3eb869d419839e5f85cb4309e0c1a Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Thu, 28 Jul 2022 13:58:13 +0430 Subject: [PATCH] JMDict: support examples, #383 --- doc/p/jmdict.md | 7 ++++++ plugins-meta/index.json | 20 ++++++++++++++-- pyglossary/plugins/jmdict.py | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/doc/p/jmdict.md b/doc/p/jmdict.md index d4d2aac3a..ec2abcf52 100644 --- a/doc/p/jmdict.md +++ b/doc/p/jmdict.md @@ -17,6 +17,13 @@ | Wiki | [JMdict](https://en.wikipedia.org/wiki/JMdict) | | Website | [The JMDict Project](https://www.edrdg.org/jmdict/j_jmdict.html) | +### Read options + +| Name | Default | Type | Comment | +| --------------- | ------- | ---- | ---------------------------- | +| example_padding | `10` | int | Padding for examples (in px) | +| example_color | | str | Examples color | + ### Dependencies for reading PyPI Links: [lxml](https://pypi.org/project/lxml) diff --git a/plugins-meta/index.json b/plugins-meta/index.json index 1b798f9ad..b06c06b89 100644 --- a/plugins-meta/index.json +++ b/plugins-meta/index.json @@ -1146,10 +1146,26 @@ "description": "JMDict", "extensions": [], "singleFile": true, - "optionsProp": {}, + "optionsProp": { + "example_color": { + "class": "StrOption", + "type": "str", + "customValue": true, + "comment": "Examples color" + }, + "example_padding": { + "class": "IntOption", + "type": "int", + "customValue": true, + "comment": "Padding for examples (in px)" + } + }, "canRead": true, "canWrite": false, - "readOptions": {}, + "readOptions": { + "example_padding": 10, + "example_color": "" + }, "readDepends": { "lxml": "lxml" }, diff --git a/pyglossary/plugins/jmdict.py b/pyglossary/plugins/jmdict.py index f8ac60a22..3d7d78079 100644 --- a/pyglossary/plugins/jmdict.py +++ b/pyglossary/plugins/jmdict.py @@ -18,6 +18,12 @@ "The JMDict Project", ) optionsProp = { + "example_color": StrOption( + comment="Examples color", + ), + "example_padding": IntOption( + comment="Padding for examples (in px)", + ), } @@ -27,6 +33,10 @@ class Reader(object): "lxml": "lxml", } + _example_padding: int = 10 + _example_color: str = "" + # _example_color: str = "#008FE1" + tagStyle = ( "color:white;" "background:green;" @@ -153,6 +163,42 @@ def br(): hf.write(desc) hf.write(br()) + examples = sense.findall("example") + if examples: + with hf.element("div", **{ + "class": "example", + "style": f"padding: {self._example_padding}px 0px;", + }): + hf.write("Examples:") + with hf.element("ul"): + for i, elem in enumerate(examples): + if not elem.text: + continue + if i > 0: + hf.write(" ") + # one ex_srce (id?), one ex_text, and two ex_sent tags + textElem = elem.find("ex_text") + if textElem is None: + continue + if not textElem.text: + continue + text = textElem.text + sentList = [] # type: List[str] + for sentElem in elem.findall("ex_sent"): + if not sentElem.text: + continue + sentList.append(sentElem.text) + with hf.element("li"): + style = {} + if self._example_color: + style["color"] = self._example_color + with hf.element("font", **style): + hf.write(text) + for sent in sentList: + hf.write(br()) + hf.write(sent) + + def getEntryByElem(self, entry: "lxml.etree.Element") -> "BaseEntry": from lxml import etree as ET glos = self._glos