def translate_wikilinks(wikitext, context): for wikilink in clean_links(wikitext.wikilinks, wikitext.language, prefixes=context.prefixes): if not str(wikilink) in wikitext.wikitext: continue new_wikilink = Wikilink(wikilink.title + "/" + wikitext.language) for link, value in context.wikilink_cache.items(): if wikilink.title == link: new_wikilink.anchor = wikilink.anchor elif wikilink.title in value["aliases"]: new_wikilink.anchor = value["aliases"][wikilink.title] else: continue if wikilink.label: new_wikilink.label = wikilink.label else: if value["displaytitle"][wikitext.language] is None: new_wikilink.label = wikilink.title else: new_wikilink.label = value["displaytitle"][ wikitext.language] break wikitext.wikitext.replace(str(wikilink), str(new_wikilink)) if str(wikilink) in wikitext.wikitext: # Yes we're doing it twice because mw doesn't catch everything in # the first round wikitext.wikitext.replace(str(wikilink), str(new_wikilink)) return wikitext.wikitext
def get_template_links(self): template_links = set() for template in self.wikitext.ifilter_templates( matches=lambda x: str(x.name).lower() in ("see also", "main") ): for link in template.params: if not link.showkey: template_links.add(Wikilink(link)) return template_links
def translate_wikipedia_links(wikitext, stack): for wikipedia_link in wikitext.wikipedia_links: if not str(wikipedia_link) in wikitext.wikitext: continue new_link = Wikilink(wikipedia_link.title, interwiki=wikipedia_link.interwiki) for link, value in stack.wikipedia_links_cache.items(): if wikipedia_link.title == link: new_link.anchor = wikipedia_link.anchor elif wikipedia_link.title in value["aliases"]: new_link.anchor = value["aliases"][wikipedia_link.title] else: continue if value["localized"][wikitext.language] is None: new_link.label = wikipedia_link.label else: new_link.label = value["localized"][wikitext.language] new_link.interwiki += ":"+wikitext.language break wikitext.wikitext.replace(str(wikipedia_link), str(new_link)) return wikitext.wikitext
def get_wikipedia_links(self): wikipedia_links = set() for wikipedia_link in self.wikitext.filter_wikilinks(): title = str(wikipedia_link.title) if not re.match("^(w|wikipedia):", title, flags=re.I): continue label = wikipedia_link.text if wikipedia_link.text else "" link, anchor = title.rsplit("#", maxsplit=1) if "#" in title else (title, "") interwiki, link = link.split(":", maxsplit=1) wikipedia_links.add(Wikilink( link, anchor=anchor, label=label, interwiki=interwiki )) return wikipedia_links
def get_wikilinks(self): wikilinks = set() # mwparserfromhell doesn't parse inside tags so we do that manually tag_wikilinks = [ wikilink for tag in self.wikitext.ifilter_tags() if tag.contents is not None for wikilink in mw.parse(str(tag.contents)).ifilter_wikilinks() ] for wikilink in itertools.chain(self.wikitext.ifilter_wikilinks(), tag_wikilinks): title = str(wikilink.title) label = wikilink.text if wikilink.text else "" # We don't need to check the links, clean_links() does that link, anchor = title.rsplit("#", maxsplit=1) if "#" in title else (title, "") wikilinks.add(Wikilink( link, anchor=anchor, label=label, )) return wikilinks
def translate_wikipedia_links(wikitext, context): for wikipedia_link in wikitext.wikipedia_links: if not str(wikipedia_link) in wikitext.wikitext: continue new_link = Wikilink(wikipedia_link.title, interwiki=wikipedia_link.interwiki) for link, value in context.wikipedia_links_cache.items(): if wikipedia_link.title == link: new_link.anchor = wikipedia_link.anchor elif wikipedia_link.title in value["aliases"]: new_link.anchor = value["aliases"][wikipedia_link.title] else: continue if value["localized"][wikitext.language] is None: new_link.label = wikipedia_link.label else: new_link.label = value["localized"][wikitext.language] new_link.interwiki += ":" + wikitext.language break wikitext.wikitext.replace(str(wikipedia_link), str(new_link)) return wikitext.wikitext
def translate_wikilinks(wikitext, stack): for wikilink in clean_links(wikitext.wikilinks, wikitext.language, prefixes=stack.prefixes): if not str(wikilink) in wikitext.wikitext: continue new_wikilink = Wikilink(wikilink.title+"/"+wikitext.language) for link, value in stack.wikilink_cache.items(): if wikilink.title == link: new_wikilink.anchor = wikilink.anchor elif wikilink.title in value["aliases"]: new_wikilink.anchor = value["aliases"][wikilink.title] else: continue if wikilink.label: new_wikilink.label = wikilink.label else: if value["displaytitle"][wikitext.language] is None: new_wikilink.label = wikilink.title else: new_wikilink.label = value["displaytitle"][wikitext.language] break wikitext.wikitext.replace(str(wikilink), str(new_wikilink)) return wikitext.wikitext