def from_citeweb(wikicode: Wikicode) -> Wikicode: count = 0 for t in wikicode.filter_templates(): if t.name == 'Cite web' and not t.has(format) and not t.has('author'): title = getvalue(t, 'title') url = getvalue(t, 'url') date = format_date(getvalue(t, 'date')) website = getvalue(t, 'website') publisher = getvalue(t, 'publisher') accessdate = format_date(getvalue(t, 'accessdate')) if getvalue( t, 'accessdate') else format_date(getvalue(t, 'access-date')) if all(title, url, accessdate): new = f'“[{url} {title}]”' if publisher and website: new += f'. \'\'{website}\'\'. {publisher} ({date}). ' if date else f'. \'\'{website}\'\'. {publisher}. ' elif publisher and not website: new += f'. {publisher} ({date}). ' if date else f'. {publisher}. ' elif not publisher and website: new += f'. \'\'{website}\'\' ({date}). ' if date else f'. \'\'{website}\'\'. ' else: new += f' ({date}) .' if date else f'. ' new += f'{accessdate}閲覧。' wikicode.replace(t, new) count += 1 pywikibot.output(f'{{{{Cite web}}}} を {count} 回置換しました') return wikicode
def repl_conditional(self, arg: Template, code: Wikicode, index: Union[str, int]): if arg.has(index): param = arg.get(index) self.apply_wikitext(param.value) code.replace( arg, str(param.value).strip() if param.showkey else param.value) else: code.remove(arg)
def merge_etyl_templates(wc: Wikicode) -> Wikicode: """ Given a chunk of wikicode, finds instances where the deprecated `etyl` template is immediately followed by either a word in free text, a linked word, or a generic `mention`/`link`/`langname-mention` template. It replaces this pattern with a new `derived-parsed` template -- meaning the same thing as the `derived` template but namespaced to differentiate. For cases where the `mention` language is different from the `etyl` language, we use the former. The template is removed if we can't parse it effectively. """ etyl_indices = [ i for i, node in enumerate(wc.nodes) if isinstance(node, Template) and node.name == "etyl" and i < len(wc.nodes) - 1 ] nodes_to_remove = [] for i in etyl_indices: make_new_template = False etyl: Template = wc.nodes[i] related_language = etyl.params[0] if len(etyl.params) == 1: language = "en" else: language = etyl.params[1] node = wc.nodes[i + 1] if isinstance(node, Text): val = re.split(",| |", node.value.strip())[0] if val: make_new_template = True elif isinstance(node, Wikilink): val = node.text or node.title val = re.split(",| |", val.strip())[0] if val: make_new_template = True elif isinstance(node, Template): if node.name in ("m", "mention", "m+", "langname-mention", "l", "link"): related_language = node.params[0] if len(node.params) > 1: val = node.params[1].value make_new_template = True nodes_to_remove.append(node) if make_new_template: params = [ Parameter(str(i + 1), str(param), showkey=False) for i, param in enumerate([language, related_language, val]) ] new_template = Template("derived-parsed", params=params) wc.replace(etyl, new_template, recursive=False) else: nodes_to_remove.append(etyl) for node in nodes_to_remove: wc.remove(node, recursive=False) return wc
def flag_template(self: TemplateParser, code: Wikicode, template: Template, flag, index=None): if index and template.has(index): param = template.get(index) self.apply_wikitext(param.value) code.replace(template, param) else: code.remove(template) self.state.flags.add(flag)
def remove_links(wc: Wikicode) -> None: """ Given a chunk of wikicode, replaces all inner links with their text representation """ for link in wc.filter_wikilinks(): wc.replace(link, link.text)
def apply_value(self, code: Wikicode, arg: Union[Node, Template]): typ = type(arg) if typ == Text: return elif typ == Argument: self.apply_wikitext(arg.name) arg_name = str(arg.name) if arg_name in self.arguments: code.replace(arg, self.arguments[arg_name]) elif arg.default is not None: self.apply_wikitext(arg.default) code.replace(arg, str(arg.default).strip()) elif typ == Template: self.apply_wikitext(arg.name) name = self.to_template_name(str(arg.name).strip()) if name == '': self.warn(f"Template name is blank in {arg}") code.remove(arg) return if name.startswith('safesubst:'): name = name[len('safesubst:'):].strip() if name.startswith('#'): if name.startswith('#if:'): self.repl_conditional( arg, code, 2 if len(arg.name.nodes) == 1 or arg.name.get(1).strip() == '' else 1) elif name.startswith('#ifeq:'): if not arg.has('1'): code.remove(arg) else: val1 = name[len('#ifeq:'):].strip() val2 = arg.get('1') self.apply_wikitext(val2.value) val2 = str(val2.value).strip() self.repl_conditional(arg, code, 3 if val1 == val2 else 2) elif name.startswith('#switch:'): key = name[len('#switch:'):].strip() if not arg.has(key): key = '#default' if not arg.has(key): key = '1' # if not arg.has(key): # self.warn(f'switch value "{key}" not found in {arg}') self.repl_conditional(arg, code, key) elif name.startswith('#ifexist:'): key = name[len('#ifexist:'):].strip().replace( self.state.page_parser.template_ns, '').strip() self.repl_conditional( arg, code, 1 if key in self.state.page_parser.templates_no_ns else 2) else: raise ValueError(f'Unhandled special {name}') else: for param in arg.params: self.apply_value(code, param) if name in custom_templates: custom_templates[name](self, code, arg) elif ( (name in self.state.page_parser.expand_template and not self.state.page_parser.expand_template[name](arg)) or name in self.state.page_parser.ignore_templates): # self.warn(f"Template {name} should not be expanded") return else: template_page = self.state.get_template(name) if template_page: sub_template_params = params_to_dict(arg.params) self.state.add_result('_' + name, sub_template_params) new_text = TemplateParser( f'{self.template_name}.{name}', self.word, template_page.content, sub_template_params, self.state).run() new_arg = mw_parse(str(new_text).strip()) code.replace(arg, new_arg) return new_arg else: self.warn(f"Template {name} is not known") elif typ == Parameter: self.apply_wikitext(arg.name) self.apply_wikitext(arg.value) elif typ == Tag: if str(arg.tag).strip() == 'noinclude': code.remove(arg) else: self.apply_wikitext(arg.contents) elif typ == Wikilink: self.apply_wikitext(arg.title) self.apply_wikitext(arg.text) elif typ == Heading: self.apply_wikitext(arg.title) elif typ == HTMLEntity: code.replace(arg, unescape(str(arg))) elif typ == Comment: code.remove(arg) elif typ == ExternalLink: return else: raise ValueError(f'Unknown type {typ} in {arg}')