-
Notifications
You must be signed in to change notification settings - Fork 21
/
_config.py
266 lines (248 loc) · 6.82 KB
/
_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Utilities to override default configurations."""
from collections import defaultdict as _defaultdict
from typing import Iterable as _Iterable
def _plant_trie(strings: _Iterable[str]) -> dict:
"""Create a Trie out of a list of words and return an atomic regex pattern.
The corresponding Regex should match much faster than a simple Regex union.
"""
# plant the trie
trie = {}
for string in strings:
d = trie
for char in string:
d[char] = char in d and d[char] or {}
d = d[char]
d[''] = None # EOS
return trie
def _pattern(trie: dict) -> str:
"""Convert a trie to a regex pattern."""
if '' in trie:
if len(trie) == 1:
return ''
optional = True
del trie['']
else:
optional = False
subpattern_to_chars = _defaultdict(list)
for char, sub_trie in trie.items():
subpattern = _pattern(sub_trie)
subpattern_to_chars[subpattern].append(char)
alts = []
for subpattern, chars in subpattern_to_chars.items():
if len(chars) == 1:
alts.append(chars[0] + subpattern)
else:
chars.sort(reverse=True)
alts.append('[' + ''.join(chars) + ']' + subpattern)
if len(alts) == 1:
result = alts[0]
if optional:
if len(result) == 1:
result += '?+'
else: # more than one character in alts[0]
result = '(?:' + result + ')?+'
else:
alts.sort(reverse=True)
result = '(?>' + '|'.join(alts) + ')'
if optional:
result += '?+'
return result
def regex_pattern(words: _Iterable[str]) -> bytes:
"""Convert words to a regex pattern that matches any of them."""
return _pattern(_plant_trie(words)).encode()
# Contents of the some of the extension tags can be parsed as wikitext.
# For example, templates are valid inside the poem tag:
# <poem>{{text|Hi!}}</poem>
# But not within math or source or ...
# for more information about the <categorytree> tag see:
# https://www.mediawiki.org/wiki/Extension:CategoryTree#
# The_.7B.7B.23categorytree.7D.7D_parser_function
_parsable_tag_extensions = {
'categorytree',
'gallery',
'imagemap',
'includeonly',
'indicator',
'inputbox',
'noinclude',
'onlyinclude',
'poem',
'ref',
'references',
'section',
}
# For a complete list of extension tags on your wiki, see the
# "Parser extension tags" section at the end of [[Special:Version]].
# <templatedata> and <includeonly> were manually added to the following lists.
# A simple trick to find out if a tag should be listed here or not is as
# follows:
# Create the {{text}} template in your wiki (You can copy the source code from
# English Wikipedia). Then save the following in a test page:
# {{text|0<tagname>1}}2</tagname>3}}4
# If the ending braces in the rendered result appear between 3 and 4, then
# `tagname` is not an extension tag (e.g. <small>). Otherwise, i.e. if those
# braces appear between 1 and 2 or completely don't show up, `tagname` is
# a tag extension (e.g.: <pre>).
_unparsable_tag_extensions = {
'ce',
'charinsert',
'chem',
'graph',
'hiero',
'languages', # Extension:Translate
'mapframe',
'maplink',
'math',
'nowiki',
'pagelist',
'pagequality',
'pages',
'pre',
'score',
'source',
'syntaxhighlight',
'templatedata',
'templatestyles',
'timeline',
}
_tag_extensions = _parsable_tag_extensions | _unparsable_tag_extensions
# Copied from DefaultSettings.php
# https://phabricator.wikimedia.org/source/mediawiki/browse/master/includes/DefaultSettings.php
# See also: https://www.mediawiki.org/wiki/Help:Links#External_links
_bare_external_link_schemes = {
'bitcoin:', 'ftp://', 'ftps://', 'geo:', 'git://', 'gopher://', 'http://',
'https://', 'irc://', 'ircs://', 'magnet:', 'mailto:', 'mms://', 'news:',
'nntp://', 'redis://', 'sftp://', 'sip:', 'sips:', 'sms:', 'ssh://',
'svn://', 'tel:', 'telnet://', 'urn:', 'worldwind://', 'xmpp:', # '//'
}
# generated using dev/html_tag_names.py
_valid_html_tag_names = {
's', 'ins', 'code', 'b', 'ol', 'i', 'h5', 'th', 'dt', 'td',
'wbr', 'div', 'big', 'p', 'small', 'h4', 'tt', 'span', 'font',
'ruby', 'h3', 'dfn', 'rb', 'li', 'h1', 'cite', 'dl', 'rtc', 'em',
'q', 'h2', 'samp', 'strike', 'time', 'blockquote', 'bdi', 'del',
'br', 'rp', 'hr', 'abbr', 'sub', 'u', 'kbd', 'table', 'rt', 'dd',
'var', 'ul', 'tr', 'center', 'data', 'strong', 'mark',
'h6', 'bdo', 'caption', 'sup'}
_HTML_TAG_NAME = regex_pattern(_valid_html_tag_names) + br'\b'
_parser_functions = {
'ARTICLEPAGENAME',
'ARTICLEPAGENAMEE',
'ARTICLESPACE',
'ARTICLESPACEE',
'BASEPAGENAME',
'BASEPAGENAMEE',
'CASCADINGSOURCES',
'CONTENTLANG',
'CONTENTLANGUAGE',
'CURRENTDAY',
'CURRENTDAY2',
'CURRENTDAYNAME',
'CURRENTDOW',
'CURRENTHOUR',
'CURRENTMONTH',
'CURRENTMONTH1',
'CURRENTMONTHABBREV',
'CURRENTMONTHNAME',
'CURRENTMONTHNAMEGEN',
'CURRENTTIME',
'CURRENTTIMESTAMP',
'CURRENTVERSION',
'CURRENTWEEK',
'CURRENTYEAR',
'DEFAULTCATEGORYSORT',
'DEFAULTSORT',
'DEFAULTSORTKEY',
'DIRECTIONMARK',
'DIRMARK',
'DISPLAYTITLE',
'FULLPAGENAME',
'FULLPAGENAMEE',
'LOCALDAY',
'LOCALDAY2',
'LOCALDAYNAME',
'LOCALDOW',
'LOCALHOUR',
'LOCALMONTH',
'LOCALMONTH1',
'LOCALMONTHABBREV',
'LOCALMONTHNAME',
'LOCALMONTHNAMEGEN',
'LOCALTIME',
'LOCALTIMESTAMP',
'LOCALWEEK',
'LOCALYEAR',
'NAMESPACE',
'NAMESPACEE',
'NAMESPACENUMBER',
'NUMBERINGROUP',
'NUMBEROFACTIVEUSERS',
'NUMBEROFADMINS',
'NUMBEROFARTICLES',
'NUMBEROFEDITS',
'NUMBEROFFILES',
'NUMBEROFPAGES',
'NUMBEROFUSERS',
'NUMBEROFVIEWS',
'NUMINGROUP',
'PAGEID',
'PAGELANGUAGE',
'PAGENAME',
'PAGENAMEE',
'PAGESINCAT',
'PAGESINCATEGORY',
'PAGESINNAMESPACE',
'PAGESINNS',
'PAGESIZE',
'PROTECTIONEXPIRY',
'PROTECTIONLEVEL',
'REVISIONDAY',
'REVISIONDAY2',
'REVISIONID',
'REVISIONMONTH',
'REVISIONMONTH1',
'REVISIONTIMESTAMP',
'REVISIONUSER',
'REVISIONYEAR',
'ROOTPAGENAME',
'ROOTPAGENAMEE',
'SCRIPTPATH',
'SERVER',
'SERVERNAME',
'SITENAME',
'STYLEPATH',
'SUBJECTPAGENAME',
'SUBJECTPAGENAMEE',
'SUBJECTSPACE',
'SUBJECTSPACEE',
'SUBPAGENAME',
'SUBPAGENAMEE',
'TALKPAGENAME',
'TALKPAGENAMEE',
'TALKSPACE',
'TALKSPACEE',
'anchorencode',
'canonicalurl',
'filepath',
'formatnum',
'fullurl',
'gender',
'grammar',
'int',
'lc',
'lcfirst',
'localurl',
'msg',
'msgnw',
'ns',
'nse',
'padleft',
'padright',
'plural',
'raw',
'safesubst',
'subst',
'uc',
'ucfirst',
'urlencode',
}