Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language support #203

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mf2py/parse_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,16 @@ def datetime(el, default_date=None):
)


def embedded(el, base_url=""):
def embedded(el, root_lang, document_lang, base_url=""):
"""Process e-* properties"""
return {
prop_value = {
"html": el.decode_contents().strip(), # secret bs4 method to get innerHTML
"value": get_textContent(el, replace_img=True, base_url=base_url),
}
if lang := el.attrs.get("lang"):
prop_value["lang"] = lang
elif root_lang:
prop_value["lang"] = root_lang
elif document_lang:
prop_value["lang"] = document_lang
return prop_value
17 changes: 13 additions & 4 deletions mf2py/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, doc=None, url=None, html_parser=None):
"version": __version__,
},
}
self.lang = None

# use default parser if none specified
self.__html_parser__ = html_parser or "html5lib"
Expand Down Expand Up @@ -129,6 +130,8 @@ def __init__(self, doc=None, url=None, html_parser=None):

if self.__doc__ is not None:
# parse!
if root := self.__doc__.find("html"):
self.lang = root.attrs.get("lang")
self.parse()

def parse(self):
Expand Down Expand Up @@ -161,13 +164,15 @@ def handle_microformat(
el = backcompat.apply_rules(el, self.__html_parser__)
root_class_names = mf2_classes.root(el.get("class", []))

root_lang = el.attrs.get("lang")

# parse for properties and children
for child in get_children(el):
(
child_props,
child_children,
child_parsed_types_aggregation,
) = parse_props(child)
) = parse_props(child, root_lang)
for key, new_value in child_props.items():
prop_value = properties.get(key, [])
prop_value.extend(new_value)
Expand Down Expand Up @@ -239,9 +244,13 @@ def handle_microformat(
else:
microformat["value"] = simple_value

if root_lang:
microformat["lang"] = root_lang
elif self.lang:
microformat["lang"] = self.lang
return microformat

def parse_props(el):
def parse_props(el, root_lang):
"""Parse the properties from a single element"""
props = {}
children = []
Expand Down Expand Up @@ -363,7 +372,7 @@ def parse_props(el):
embedded_el = copy.copy(embedded_el)
temp_fixes.rm_templates(embedded_el)
e_value = parse_property.embedded(
embedded_el, base_url=self.__url__
embedded_el, root_lang, self.lang, base_url=self.__url__
)

if root_class_names:
Expand Down Expand Up @@ -394,7 +403,7 @@ def parse_props(el):
child_properties,
child_microformats,
child_parsed_types_aggregation,
) = parse_props(child)
) = parse_props(child, root_lang)
for prop_name in child_properties:
v = props.get(prop_name, [])
v.extend(child_properties[prop_name])
Expand Down
15 changes: 15 additions & 0 deletions test/examples/language.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html lang="it">
<div class="h-card">
<h1 class="p-name">Romero</h1>
</div>
<div class="h-entry">
<h1 class="p-name">Un titolo italiano</h1>
<div class="e-content" lang="en">With an <em>english</em> summary</div>
<div class="e-content">Con un riassunto <em>italiano</em></div>
</div>
<div class="h-entry" lang="sv">
<h1 class="p-name">En svensk titel</h1>
<div class="e-content" lang="en">With an <em>english</em> summary</div>
<div class="e-content">Och <em>svensk</em> huvudtext</div>
</div>
</html>
11 changes: 11 additions & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,14 @@ def test_all_u_cases():
make_labelled_cmp("all_u_cases_" + str(i))(
"http://example.com/test", result["items"][0]["properties"]["url"][i]
)


def test_language():
result = parse_fixture("language.html")
assert result["items"][0]["lang"] == "it"
assert result["items"][1]["lang"] == "it"
assert result["items"][1]["properties"]["content"][0]["lang"] == "en"
assert result["items"][1]["properties"]["content"][1]["lang"] == "it"
assert result["items"][2]["lang"] == "sv"
assert result["items"][2]["properties"]["content"][0]["lang"] == "en"
assert result["items"][2]["properties"]["content"][1]["lang"] == "sv"
Loading