def find_section_definitions(word: str, section: wtp.Section, locale: str) -> List[Definitions]: """Find definitions from the given *section*, with eventual sub-definitions.""" definitions: List[Definitions] = [] # do not look for definitions in french verb form section if locale == "fr" and section.title.strip().startswith( "{{S|verbe|fr|flexion"): return definitions # es uses definition lists, not well supported by the parser... # replace them by numbered lists if locale == "es": lists = section.get_lists(pattern="[:;]") if lists: sec = "".join(a_list.string for a_list in lists) section.contents = re.sub(r";[0-9]+[ |:]+", "# ", sec) section.contents = re.sub(r":;[a-z]:+[\s]+", "## ", section.contents) lists = section.get_lists(pattern=section_patterns[locale]) if lists: for a_list in lists: for idx, code in enumerate(a_list.items): # Ignore some patterns if word not in words_to_keep[locale] and any( ignore_me in code.lower() for ignore_me in definitions_to_ignore[locale]): continue # Transform and clean the Wikicode definition = process_templates(word, clean(code), locale) # Skip empty definitions # [SV] Skip almost empty definitions if not definition or (locale == "sv" and len(definition) < 2): continue # Keep the definition ... definitions.append(definition) # ... And its eventual sub-definitions subdefinitions: List[SubDefinitions] = [] for sublist in a_list.sublists( i=idx, pattern=sublist_patterns[locale]): for idx2, subcode in enumerate(sublist.items): subdefinition = process_templates( word, clean(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( process_templates(word, clean(subsubcode), locale)) if subsubdefinitions: subdefinitions.append(tuple(subsubdefinitions)) if subdefinitions: definitions.append(tuple(subdefinitions)) return definitions
def find_section_definitions(word: str, section: wtp.Section, locale: str) -> List[Definitions]: """Find definitions from the given *section*, with eventual sub-definitions.""" definitions: List[Definitions] = [] # do not look for definitions in french verb form section if locale == "fr" and section.title.strip().startswith( "{{S|verbe|fr|flexion"): return definitions if locale == "es" and section.title.strip().startswith( ("Forma adjetiva", "Forma verbal")): return definitions # es uses definition lists, not well supported by the parser... # replace them by numbered lists if locale == "es": if lists := section.get_lists(pattern="[:;]"): sec = "".join(a_list.string for a_list in lists) section.contents = re.sub(r";[0-9]+[ |:]+", "# ", sec) section.contents = re.sub(r":;[\s]*[a-z]:+[\s]+", "## ", section.contents)
def test_setting_lead_section_contents(self): s = Section('a\nb') s.contents = 'c' self.assertEqual('c', s.string)
def test_trailing_space_setter(self): s = Section('=t= \no') s.contents = 'n' self.assertEqual('=t= \nn', s.string)
def test_set_lead_contents(self): s = Section('lead') s.contents = 'newlead' self.assertEqual('newlead', s.string)
def test_set_contents(self): s = Section('== title ==\ntext.') s.contents = ' newcontents ' self.assertEqual(' newcontents ', s.contents)
def test_setting_lead_section_contents(): s = Section('a\nb') s.contents = 'c' assert 'c' == s.string
def test_trailing_space_setter(): s = Section('=t= \no') s.contents = 'n' assert '=t= \nn' == s.string
def test_set_lead_contents(): s = Section('lead') s.contents = 'newlead' assert 'newlead' == s.string
def test_set_contents(): s = Section('== title ==\ntext.') s.contents = ' newcontents ' assert ' newcontents ' == s.contents