User:Huhu9001Bot/import tempParser

From Wiktionary, the free dictionary
Jump to navigation Jump to search
brackets_temp = {'{{': '}}'}
brackets_temp_and_link = {'{{': '}}', '[[': ']]'}

def find_Bracket(str, brackets, p_start = 0):
    def find_left(str, brackets, pos_start):
        p1_result = len(str)
        for bl in iter(brackets):
            p1_this = str.find(bl, pos_start)
            if p1_this != -1 and p1_this < p1_result:
                p1_result = p1_this
                p2_result = p1_this + len(bl)
                br_result = bl
        if p1_result == len(str): return [-1]
        return [p1_result, p2_result, br_result]
        
    p0 = find_left(str, brackets, p_start)
    if p0[0] == -1: return [-1]
    nest = [brackets[p0[2]]]
    
    while len(nest) > 0:
        p0[2] = nest[-1]
        p1 = str.find(p0[2], p0[1])
        if p1 == -1: return [-1]
        if len(p0[2]) == 0: raise Exception('Any bracket must not have zero length.')
        p2 = find_left(str, brackets, p0[1])
        if p2[0] == -1:
            n = len(nest) - 1
            while n > 0:
                p1 = str.find(nest[n - 1], p1 + len(nest[n]))
                if p1 == -1: return [-1]
                n -= 1
            p0[1] = p1 + len(nest[0])
            break
        else:
            if len(p2[2]) == 0: raise Exception('Any bracket must not have zero length.')
            if p2[0] < p1:
                nest.append(brackets[p2[2]])
                p0[1] = p2[1]
            else:
                p0[1] = p1 + len(nest[-1])
                del nest[-1]
            
    p0[2] = str[p0[0]:p0[1]]
    return p0
    
def gfind_Bracket(str, brackets):
    p0 = find_Bracket(str, brackets)
    while p0[0] != -1:
        yield p0
        p0 = find_Bracket(str, brackets, p0[1])
        
def find_IgnoringBrackets(str, brackets, pat, init = 0, *args):
    p0 = str.find(pat, init, *args)
    if p0 == -1: return -1
    
    p1 = find_Bracket(str, brackets)
    while p1[0] != -1 and p1[1] <= p0 + len(pat): p1 = find_Bracket(str, brackets, p1[1])
    
    while p1[0] != -1 and p1[0] <= p0 + len(pat) - 1:
        p0 = str.find(pat, p1[1], *args)
        if p0 == -1: return -1
        while p1[0] != -1 and p1[1] <= p0 + len(pat): p1 = find_Bracket(str, brackets, p1[1])
    
    return p0

def gsplit_IgnoringBrackets(str, brackets, sep):
    if len(sep) > 0:
        p0 = 0
        p1 = find_IgnoringBrackets(str, brackets, sep)
        while p1 != -1:
            yield str[p0:p1]
            p0 = p1 + len(sep)
            p1 = find_IgnoringBrackets(str, brackets, sep, p0)
        yield str[p0:]
    else:
        p0 = 0
        for i in range(len(str)):
            if i == find_IgnoringBrackets(str, brackets, sep, i):
                if p0 < i: yield str[p0:i]
                yield str[i]
                p0 = i + 1

def parseTemp(str):
    if str[:2] != '{{' or str[-2:] != '}}': return None
    stri = str[2:-2]
    
    p_titleEnd = find_IgnoringBrackets(stri, brackets_temp_and_link, '|')
    if p_titleEnd == -1: return stri, {}
    args_result = {}
    
    count = 0
    for arg in gsplit_IgnoringBrackets(stri[p_titleEnd + 1:], brackets_temp_and_link, '|'):
        p_eqsign = find_IgnoringBrackets(arg, brackets_temp_and_link, '=')
        if p_eqsign != -1:
            arg_name = arg[:p_eqsign]
            if arg_name.isdecimal() and arg_name.isascii():
                arg_name = int(arg_name)
                if arg_name == 0: arg_name = '0'
            else: arg_name = arg_name.strip(' \x09\x0A\x0B\x0C\x0D')
            args_result[arg_name] = arg[p_eqsign + 1:].strip(' \x09\x0A\x0B\x0C\x0D')
        else:
            count += 1
            args_result[count] = arg

    return stri[:p_titleEnd], args_result
    
def iterNumArg(args):
    if len(args) > 0:
        def func_sort(k): return type(k) is int and k or 0
        max_index = max(args, key = func_sort)
        if type(max_index) is int:
            for i in range(1, max_index + 1):
                if i in args: yield i, args[i]
    
def glueTemp(title, args):
    result = [title]
    for i, v in iterNumArg(args):
        if i == len(result): result.append(v)
        else: result.append('{0}={1}'.format(i, v))
    for k in iter(args):
        if type(k) is str:
            result.append('{0}={1}'.format(k, args[k]))
    return '{{{{{}}}}}'.format('|'.join(result))
    
import re

def subTemp(pat, repl, str):
    result = []
    p0 = 0
    for p1, p2, t_temp in gfind_Bracket(str, brackets_temp):
        title, args = parseTemp(t_temp)
        if re.search(pat, title):
            result.append(str[p0:p1])
            if callable(repl): result.append(glueTemp(*repl(title, args)))
            elif type(repl) is dict: result.append(glueTemp(title, repl))
            else: result.append('{}'.format(repl))
            p0 = p2
    result.append(str[p0:])
    return ''.join(result)