Skip to content

Commit

Permalink
fix BoboTiG#237: [FR] Sub sub list not handled correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lasconic committed Nov 21, 2020
1 parent 4c582e5 commit 880ea51
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
10 changes: 9 additions & 1 deletion scripts/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,15 @@ def save_html(
definitions += f"<li>{definition}</li>"
else:
definitions += '<ol style="list-style-type:lower-alpha">'
definitions += "".join(f"<li>{d}</li>" for d in definition)
for subdef in definition:
if isinstance(subdef, str):
definitions += f"<li>{subdef}</li>"
else:
definitions += (
'<ol style="list-style-type:lower-roman">'
)
definitions += "".join(f"<li>{d}</li>" for d in subdef)
definitions += "</ol>"
definitions += "</ol>"

pronunciation = convert_pronunciation(details.pronunciations)
Expand Down
33 changes: 26 additions & 7 deletions scripts/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
sublist_patterns,
words_to_keep,
)
from .stubs import Definitions, Word, Words
from .stubs import Definitions, SubDefinitions, Word, Words
from .utils import clean, convert_pronunciation, convert_genre

if TYPE_CHECKING: # pragma: nocover
Expand Down Expand Up @@ -183,10 +183,21 @@ def find_section_definitions(
definitions.append(definition)

# ... And its eventual sub-definitions
subdefinitions: List[str] = []
subdefinitions: List[SubDefinitions] = []
for sublist in a_list.sublists(i=idx, pattern=sublist_patterns[locale]):
for subcode in sublist.items:
subdefinitions.append(clean(word, subcode, locale))
for idx2, subcode in enumerate(sublist.items):
subdefinition = clean(word, subcode, locale)
subdefinitions.append(subdefinition)
subsubdefinitions: List[str] = []
for subsublist in sublist.sublists(
i=idx2, pattern=sublist_patterns[locale]
):
for subsubcode in subsublist.items:
subsubdefinitions.append(
clean(word, subsubcode, locale)
)
if subsubdefinitions:
subdefinitions.append(tuple(subsubdefinitions))
if subdefinitions:
definitions.append(tuple(subdefinitions))

Expand Down Expand Up @@ -356,12 +367,20 @@ def strip_html(text: str) -> str:
if details.etymology:
print(strip_html(details.etymology), "\n")

for i, definition in enumerate(details.definitions, start=1):
index = 1
for definition in details.definitions:
if isinstance(definition, tuple):
for a, subdef in zip("abcdefghijklmopqrstuvwxz", definition):
print(f"{a}.".rjust(8), strip_html(subdef))
if isinstance(subdef, tuple):
for rn, subsubdef in zip(
["i", "ii", "iii", "iv", "v", "vi", "vii"], subdef
):
print(f"{rn}.".rjust(12), strip_html(subsubdef))
else:
print(f"{a}.".rjust(8), strip_html(subdef))
else:
print(f"{i}.".rjust(4), strip_html(definition))
print(f"{index}.".rjust(4), strip_html(definition))
index = index + 1

if details.variants:
print("[variants]", ", ".join(iter(details.variants)))
Expand Down
3 changes: 2 additions & 1 deletion scripts/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from collections import namedtuple
from typing import Dict, Tuple, Union

Definitions = Union[str, Tuple[str, ...]]
SubDefinitions = Union[str, Tuple[str, ...]]
Definitions = Union[str, Tuple[str, ...], Tuple[SubDefinitions, ...]]
Parts = Tuple[str, ...]
Word = namedtuple("Word", "pronunciations, genre, etymology, definitions, variants")
Words = Dict[str, Word]
1 change: 1 addition & 0 deletions tests/test_0_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,5 @@ def test_xml_parse_word_special_character(tmp_path):

def test_get_and_parse_word():
get.get_and_parse_word("fondation", "fr")
get.get_and_parse_word("base", "fr")
get.get_and_parse_word("suis", "fr", raw=True)
4 changes: 4 additions & 0 deletions tests/test_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
"<i>(Mathématiques)</i>",
(
"<i>(Géométrie)</i> Surface sur laquelle on conçoit que certains corps solides sont appuyés.",
(
"<i>(Par extension)</i> Côté du triangle opposé à l’angle qui est regardé comme le sommet.",
"Côté d’une figure géométrique naturellement choisi comme côté principal.",
),
"<i>(Algèbre)</i> Famille libre de vecteurs, génératrice d’un espace vectoriel.",
"<i>(Analyse réelle)</i> Nombre réel élevé à une puissance variable.",
"<i>(Arithmétique)</i> Nombre de chiffres utilisé pour dénombrer.",
Expand Down

0 comments on commit 880ea51

Please sign in to comment.