مستخدم:DrTrigon/DrTrigonBot/subster-postproc.css

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات (إفراغ الكاش الآن).

#/* 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]