'adblock list to Pac, based on https://github.com/iamamac/autoproxy2pac'

@staticmethod
def adblock2pac(content, func_name='FindProxyForURLByAdblock', proxy='127.0.0.1:8086', default='DIRECT', admode=1, indent=4):

    white_conditions = {'host': [], 'url.indexOf': [], 'shExpMatch': []}
    black_conditions = {'host': [], 'url.indexOf': [], 'shExpMatch': []}
    for line in content.splitlines()[1:]:
        if ((not line) or line.startswith('!') or ('##' in line) or ('#@#' in line)):
            continue
        use_proxy = True
        use_start = False
        use_end = False
        use_domain = False
        use_postfix = []
        if ('$' in line):
            posfixs = line.split('$')[(-1)].split(',')
            if any((('domain' in x) for x in posfixs)):
                continue
            if ('image' in posfixs):
                use_postfix += ['.jpg', '.gif']
            elif ('script' in posfixs):
                use_postfix += ['.js']
            else:
                continue
        line = line.split('$')[0]
        if line.startswith('@@'):
            line = line[2:]
            use_proxy = False
        if ('||' == line[:2]):
            line = line[2:]
            if ('/' not in line):
                use_domain = True
            else:
                use_start = True
        elif ('|' == line[0]):
            line = line[1:]
            use_start = True
        if (line[(-1)] in ('^', '|')):
            line = line[:(-1)]
            if (not use_postfix):
                use_end = True
        line = line.replace('^', '*').strip('*')
        conditions = (black_conditions if use_proxy else white_conditions)
        if (use_start and use_end):
            conditions['shExpMatch'] += [('*%s*' % line)]
        elif use_start:
            if ('*' in line):
                if use_postfix:
                    conditions['shExpMatch'] += [('*%s*%s' % (line, x)) for x in use_postfix]
                else:
                    conditions['shExpMatch'] += [('*%s*' % line)]
            else:
                conditions['url.indexOf'] += [line]
        elif (use_domain and use_end):
            if ('*' in line):
                conditions['shExpMatch'] += [('%s*' % line)]
            else:
                conditions['host'] += [line]
        elif use_domain:
            if (line.split('/')[0].count('.') <= 1):
                if use_postfix:
                    conditions['shExpMatch'] += [('*.%s*%s' % (line, x)) for x in use_postfix]
                else:
                    conditions['shExpMatch'] += [('*.%s*' % line)]
            elif ('*' in line):
                if use_postfix:
                    conditions['shExpMatch'] += [('*%s*%s' % (line, x)) for x in use_postfix]
                else:
                    conditions['shExpMatch'] += [('*%s*' % line)]
            elif use_postfix:
                conditions['shExpMatch'] += [('*%s*%s' % (line, x)) for x in use_postfix]
            else:
                conditions['url.indexOf'] += [('http://%s' % line)]
        elif use_postfix:
            conditions['shExpMatch'] += [('*%s*%s' % (line, x)) for x in use_postfix]
        else:
            conditions['shExpMatch'] += [('*%s*' % line)]
    templates = ["                                                                                function    %(func_name)s(url,    host)    {\n                                                                                                return    '%(default)s';\n                                                                                }", "                                                                                var    blackhole_host    =    {\n                                                                                %(blackhole_host)s\n                                                                                };\n                                                                                function    %(func_name)s(url,    host)    {\n                                                                                                //    untrusted    ablock    plus    list,    disable    whitelist    until    chinalist    come    back.\n                                                                                                if    (blackhole_host.hasOwnProperty(host))    {\n                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                }\n                                                                                                return    '%(default)s';\n                                                                                }", "                                                                                var    blackhole_host    =    {\n                                                                                %(blackhole_host)s\n                                                                                };\n                                                                                var    blackhole_url_indexOf    =    [\n                                                                                %(blackhole_url_indexOf)s\n                                                                                ];\n                                                                                function    %s(url,    host)    {\n                                                                                                //    untrusted    ablock    plus    list,    disable    whitelist    until    chinalist    come    back.\n                                                                                                if    (blackhole_host.hasOwnProperty(host))    {\n                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                }\n                                                                                                for    (i    =    0;    i    <    blackhole_url_indexOf.length;    i++)    {\n                                                                                                                if    (url.indexOf(blackhole_url_indexOf[i])    >=    0)    {\n                                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                                }\n                                                                                                }\n                                                                                                return    '%(default)s';\n                                                                                }", "                                                                                var    blackhole_host    =    {\n                                                                                %(blackhole_host)s\n                                                                                };\n                                                                                var    blackhole_url_indexOf    =    [\n                                                                                %(blackhole_url_indexOf)s\n                                                                                ];\n                                                                                var    blackhole_shExpMatch    =    [\n                                                                                %(blackhole_shExpMatch)s\n                                                                                ];\n                                                                                function    %(func_name)s(url,    host)    {\n                                                                                                //    untrusted    ablock    plus    list,    disable    whitelist    until    chinalist    come    back.\n                                                                                                if    (blackhole_host.hasOwnProperty(host))    {\n                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                }\n                                                                                                for    (i    =    0;    i    <    blackhole_url_indexOf.length;    i++)    {\n                                                                                                                if    (url.indexOf(blackhole_url_indexOf[i])    >=    0)    {\n                                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                                }\n                                                                                                }\n                                                                                                for    (i    =    0;    i    <    blackhole_shExpMatch.length;    i++)    {\n                                                                                                                if    (shExpMatch(url,    blackhole_shExpMatch[i]))    {\n                                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                                }\n                                                                                                }\n                                                                                                return    '%(default)s';\n                                                                                }"]
    template = re.sub(('(?m)^\\s{%d}' % min((len(re.search('    +', x).group()) for x in templates[admode].splitlines()))), '', templates[admode])
    template_kwargs = {'blackhole_host': ',\r\n'.join((("%s'%s':    1" % (('    ' * indent), x)) for x in sorted(black_conditions['host']))), 'blackhole_url_indexOf': ',\r\n'.join((("%s'%s'" % (('    ' * indent), x)) for x in sorted(black_conditions['url.indexOf']))), 'blackhole_shExpMatch': ',\r\n'.join((("%s'%s'" % (('    ' * indent), x)) for x in sorted(black_conditions['shExpMatch']))), 'func_name': func_name, 'proxy': proxy, 'default': default}
    return (template % template_kwargs)
