Skip to content

Commit

Permalink
JMDict: support examples, #383
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Jul 28, 2022
1 parent 9ad994d commit a25a1ef
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/p/jmdict.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 18 additions & 2 deletions plugins-meta/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
46 changes: 46 additions & 0 deletions pyglossary/plugins/jmdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
"The JMDict Project",
)
optionsProp = {
"example_color": StrOption(
comment="Examples color",
),
"example_padding": IntOption(
comment="Padding for examples (in px)",
),
}


Expand All @@ -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;"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a25a1ef

Please sign in to comment.