def get_tail_text(root: Wikicode, lb_idx: int) -> Optional[Wikicode]: """ If an {{l|fi ...}} is followed by some text after a colon, we might want to put it in its description. """ gathered = [] idx = lb_idx idx += 1 try: node = root.get(idx) except IndexError: return None if not isinstance(node, Text) or not node.value.startswith(":"): return None node.value = node.value[1:].strip() gathered.append(node) idx += 1 while 1: try: node = root.get(idx) except IndexError: break if is_l_fi(node): break gathered.append(node) idx += 1 return Wikicode(gathered)
def is_l_fi(template: Wikicode): if not isinstance(template, Template): return False if template.name != "l": return False lang = str(template.get(1).value) return lang == "fi"
def handle_der_container(ctx: ParseContext, template: Wikicode) -> Iterator[Tuple[str, Any]]: """ e.g. {{der2|fi|title=phrasal verbs |{{l|fi|[[pitää ääntä|''pitää'' ääntä]] + ''elative''|t=to [[make]] (a) [[noise]] about something {{q|e.g. about an egregious problem}}}} or e.g. {{der3|fi|title=nouns |pidike |pidin """ idx = 1 cats = [] if template.has("title"): cats.append(str(template.get("title").value).strip()) while 1: if not template.has(idx): break bit = template.get(idx).value idx += 1 str_bit = str(bit) if str_bit == "fi": continue l_fi_templates = [ idx for idx, template in bit._indexed_ifilter(recursive=False, forcetype=Template) if is_l_fi(template) ] if len(l_fi_templates) == 1: res = get_deriv_lb_template(ctx, bit, l_fi_templates[0]) if res is not None: yield res elif len(l_fi_templates) == 0: yield handle_deriv(ctx, bit, cats=cats) else: assert False, "too many {{l|fi ... }} templates"
def get_deriv_lb_template(ctx: ParseContext, parent: Wikicode, tmpl_idx: int): template = parent.get(tmpl_idx) if not template.has(2): return None link = template.get(2).value disp = None gloss = None cats = [] if template.has(3): disp = str(template.get(3).value) if template.has("gloss"): gloss = str(template.get("gloss").value) if template.has("pos"): cats.append(str(template.get("pos").value)) # TODO: Categories outside template if not gloss: tail_wikicode = get_tail_text(parent, tmpl_idx) gloss = str(tail_wikicode) if tail_wikicode else None return handle_deriv(ctx, link, disp, gloss, cats)