earwig / mwparserfromhell

A Python parser for MediaWiki wikicode
https://mwparserfromhell.readthedocs.io/
MIT License
741 stars 74 forks source link

Allow passing available tags and namespace names to parser #305

Open NguoiDungKhongDinhDanh opened 1 year ago

NguoiDungKhongDinhDanh commented 1 year ago

Currently I don't see an option for this.

By default, mwparserfromhell treats everything that looks like a XML tag as one, regardless of whether MediaWiki recognizes it or not. The same applies to wikilinks and file links. That makes sense since mph has no way to know what MW would do, but what if we provide necessary information as arguments to the Parser constructor?

For example:

# Namespaces taken from ptwiki, a wiki whose language has grammatical gender.
parser = Parser(
  recognized_tags = ['categorytree', 'charinsert', ...]  # Or any kind of Iterable[str],
  namespaces = {  # Or any kind of Mapping[int, str | Iterable[str]]
    0: '',
    1: 'Discussão',
    2: ['Usuário', 'Usuária', 'Usuário(a)'],
    ...: ...
  }
)

Namespace 0 would have a default value of '' (I'm not sure if MediaWiki allows configuring namespace prefix for this one or not). A non-recognized namespace would be considered to be a page in namespace 0 if that has no prefix, or cause an exception otherwise.