Usage

The parser works as simple as follows:

from mwtp import TitleParser as Parser

parser = Parser(namespaces_data, namespace_aliases)
title = parser.parse(' _ FoO: this/is A__/talk page _ ')

print(repr(title))  # Title('Talk:This/is A /talk page')

namespaces_data and namespace_aliases can be obtained by making a query to a wiki’s API with action=query&meta=siteinfo&siprop=namespaces|namespacealiases. Here’s how they might look like:

namespaces_data = {
  '0': {
    'id': 0,
    'case': 'first-letter',
    'name': '',
    'subpages': False,
    'content': True,
    'nonincludable': False
 },
 '1': {
    'id': 1,
    'case': 'first-letter',
    'name': 'Talk',
    'subpages': True,
    'canonical': 'Talk',
    'content': False,
    'nonincludable': False
  },
  ...: ...
}
namespace_aliases = [
  { 'id': 1, 'alias': 'Foo' },
  ...
]

Note that the following format (&formatversion=1) is not supported. Always use &formatversion=2 or &formatversion=latest.

namespaces_data = {
  '0': { 'id': 0, 'case': 'first-letter', '*': '',          ...: ... },
  '1': { 'id': 1, 'case': 'first-letter', '*': 'Thảo luận', ...: ... },
  ...: ...
}
namespace_aliases = [
  { 'id': 1, '*': 'Foo' },
  ...
]

Parser.parse() returns a Title object which has a bunch of convenient properties for title manipulation:

title.namespace             # 1
title.in_content_namespace  # False
title.associated            # Title('This/is A /talk page')

A Title can be converted back to a str using either:

str(title)                  # 'Talk:This/is A /talk page'
title.full_name             # 'Talk:This/is A /talk page'

Path-like operations are also supported:

title + '/Foo'              # Title('Talk:This/is A /talk page/Foo')
title / 'Foo'               # Title('Talk:This/is A /talk page/Foo')

See the class’s full method list for more information.