User:DrTrigon/DrTrigonBot/subster-postproc.css
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:DrTrigon/DrTrigonBot/subster-postproc. |
#/* Configuration DrTrigonBot */
#/* subster: postproc */
# create linked list in wiki format (shortcut and compatibility)
# syntax: ('wikilinkedlist', regex)
wikilinkedlist = lambda DATA, regex: formatedlist(DATA, regex, "* [[%s]]")
# create list in generic format (e.g. for sumcatdisc)
# syntax: ('formatedlist', regex, format [, sep])
def formatedlist(DATA, regex, format, sep="\n"):
comp = re.compile(regex, re.S | re.I)
data_list = comp.findall(DATA[0])
if comp.groupindex:
data_list = [ dict([ (j, item[comp.groupindex[j]-1]) for j in comp.groupindex ])
#+[ (str(j+1), item[j]) for j in range(len(comp.groupindex)) ])
for item in data_list ]
format = re.sub(r'\\g\<(.*?)\>', r'%(\g<1>)s', format)
DATA[0] = sep.join(map(lambda x: format % x, data_list))
# create list in generic format from generic text matrix/table
# syntax: ('formatedlist_frommatrix', regex, format, cols, head, check [, types])
def formatedlist_frommatrix(DATA, regex, format, cols, head, check, types=[]):
types = dict(types)
data_list = []
for line in DATA[0].splitlines()[head:]:
line = re.search(regex, line).groups()
if not check(line): continue
data_list.append( format % tuple([types.get(i, str)(line[i].strip()) for i in cols]) )
DATA[0] = "\n".join(data_list)
# syntax: ('formatedlist_frommatrix', regex, format, head, check [, types])
def _formatedlist_frommatrix(DATA, regex, format, head, check, types=[]):
types = dict(types)
data_list = []
for line in DATA[0].splitlines()[head:]:
comp = re.compile(regex)
line = comp.search(line)
line = dict([ (k, types.get(k, str)(line.group(k))) for k in comp.groupindex ])
if check(line):
data_list.append( format % line )
DATA[0] = "\n".join(data_list)
# remove or replace all occurences of regex in text (re.sub wrapper)
# syntax: ('replacetext', regex [, sub] [, flags])
def replacetext(DATA, regex, sub=u'', flags=re.M):
#DATA[0] = re.sub(regex, sub, DATA[0], flags=flags)
DATA[0] = re.compile(regex, flags=flags).sub(sub, DATA[0])
# reverse the list given to change sorting order (list.reverse)
# syntax: ('reverselist',)
def reverselist(DATA):
lines = DATA[0].splitlines()
lines.reverse()
DATA[0] = "\n".join(lines)
# allow to chain multiple postproc functions and combine them
# syntax: ('chain', postprocs [, index])
def chain(DATA, postprocs, index=0):
for item in postprocs:
DATA.append(DATA[0])
eval(item[0])( *((DATA,) + item[1:]) )
DATA[0] = DATA[index]