diff --git a/bot/yomichan/glossary/gloss.py b/bot/yomichan/glossary/gloss.py index 572ce76..5080f43 100644 --- a/bot/yomichan/glossary/gloss.py +++ b/bot/yomichan/glossary/gloss.py @@ -2,10 +2,10 @@ from css_parser import parseStyle def make_gloss(soup): - structured_content = __get_markup_structure(soup) + node = __get_markup_structure(soup) return { "type": "structured-content", - "content": structured_content + "content": node["content"], } @@ -48,8 +48,35 @@ def __get_attributes(attrs): attributes["rowSpan"] = int(attrs["rowspan"]) if "colspan" in attrs: attributes["colSpan"] = int(attrs["colspan"]) + if "height" in attrs: + attributes["height"] = float(attrs["height"]) + if "width" in attrs: + attributes["width"] = float(attrs["width"]) + if "sizeUnits" in attrs: + attributes["sizeUnits"] = attrs["sizeUnits"] + if "appearance" in attrs: + attributes["appearance"] = attrs["appearance"] + if "title" in attrs: + attributes["title"] = attrs["title"] + if "collapsible" in attrs: + attributes["collapsible"] = bool(attrs["collapsible"]) + if "collapsed" in attrs: + attributes["collapsed"] = bool(attrs["collapsed"]) + if "background" in attrs: + attributes["background"] = bool(attrs["background"]) + if "path" in attrs: + attributes["path"] = attrs["path"] if "style" in attrs: - attributes["style"] = __get_style(attrs["style"]) + style = __get_style(attrs["style"]) + if len(style) > 0: + attributes["style"] = style + data_attrs = {} + for attr_key in attrs.keys(): + if attr_key.startswith("data-"): + key = attr_key.removeprefix("data-") + data_attrs[key] = attrs[attr_key] + if len(data_attrs) > 0: + attributes["data"] = data_attrs return attributes @@ -70,4 +97,16 @@ def __get_style(inline_style_string): style["textAlign"] = parsedStyle.textAlign if parsedStyle.listStyleType != "": style["listStyleType"] = parsedStyle.listStyleType + + margins = { + "marginTop": parsedStyle.marginTop, + "marginRight": parsedStyle.marginRight, + "marginBottom": parsedStyle.marginBottom, + "marginLeft": parsedStyle.marginLeft, + } + for key, val in margins.items(): + m = re.search(r"(\d+(\.\d*)?|\.\d+)em", val) + if m: + style[key] = float(m.group(1)) + return style diff --git a/bot/yomichan/glossary/jitenon.py b/bot/yomichan/glossary/jitenon.py index ecaeac3..3407d01 100644 --- a/bot/yomichan/glossary/jitenon.py +++ b/bot/yomichan/glossary/jitenon.py @@ -20,6 +20,6 @@ def make_glossary(entry): p.name = "span" for th in soup.find_all("th"): th['style'] = "vertical-align: middle; text-align: center;" - gloss = make_gloss(soup.table) + gloss = make_gloss(soup.body) glossary = [gloss] return glossary