Exemplo n.º 1
0
def build_jinja2_environment():
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(
        str(sc.templates_dir)),
                             extensions=[AssetsExtension],
                             trim_blocks=True,
                             lstrip_blocks=True,
                             auto_reload=not config.compile_assets)
    env.assets_environment = assets.get_env()

    env.filters['date'] = util.format_date
    env.filters['time'] = util.format_time
    env.filters['max'] = max
    env.filters['min'] = min
    env.filters['datetime'] = util.format_datetime
    env.filters['timedelta'] = util.format_timedelta
    env.filters['uid_to_name'] = lambda uid: uid_to_name(uid)
    env.filters['uid_to_acro'] = lambda uid: uid_to_acro(uid)
    env.filters['json'] = json.dumps

    def sub_filter(string, pattern, repl):
        return regex.sub(pattern, repl, string)

    env.filters['sub'] = sub_filter

    def sht_expansion(string):
        """Add links from SHT vol/page references to the sht-lookup page."""
        if 'SHT' in string:
            # Replace   with spaces
            string = string.replace(' ', ' ')
            baseurl = '/sht-lookup/'

            def replacement(m):
                # Ignore 'also cf. ix p. 393ff' and ' A'
                first, second = m[1], m[2]
                if regex.match(r'.+p\.\s*', first) or \
                   regex.match(r'.+A', first):
                    return '{}{}'.format(first, second)
                else:
                    # replace n-dash with dash
                    path = second.replace('−', '-')
                    return '{}<a href="{}{}" target="_blank">{}</a>'.format(
                        first, baseurl, path, second)

            string = regex.sub(
                r'([^0-9]+)([0-9]{1,4}(?:\.?[\−\+0-9a-zA-Z]+)?)', replacement,
                string)
        return string

    env.filters['sht_expansion'] = sht_expansion
    return env
Exemplo n.º 2
0
def build_jinja2_environment():
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(str(sc.templates_dir)),
        extensions=[AssetsExtension],
        trim_blocks=True,
        lstrip_blocks=True,
        auto_reload=not config.compile_assets,
    )
    env.assets_environment = assets.get_env()

    env.filters["date"] = util.format_date
    env.filters["time"] = util.format_time
    env.filters["max"] = max
    env.filters["min"] = min
    env.filters["datetime"] = util.format_datetime
    env.filters["timedelta"] = util.format_timedelta
    env.filters["uid_to_name"] = lambda uid: uid_to_name(uid)
    env.filters["uid_to_acro"] = lambda uid: uid_to_acro(uid)
    env.filters["json"] = json.dumps

    def sub_filter(string, pattern, repl):
        return regex.sub(pattern, repl, string)

    env.filters["sub"] = sub_filter

    def sht_expansion(string):
        """Add links from SHT vol/page references to the sht-lookup page."""
        if "SHT" in string:
            # Replace &nbsp; with spaces
            string = string.replace("&nbsp;", " ")
            baseurl = "/sht-lookup/"

            def replacement(m):
                # Ignore 'also cf. ix p. 393ff' and ' A'
                first, second = m[1], m[2]
                if regex.match(r".+p\.\s*", first) or regex.match(r".+A", first):
                    return "{}{}".format(first, second)
                else:
                    # replace n-dash with dash
                    path = second.replace("−", "-")
                    return '{}<a href="{}{}" target="_blank">{}</a>'.format(first, baseurl, path, second)

            string = regex.sub(r"([^0-9]+)([0-9]{1,4}(?:\.?[\−\+0-9a-zA-Z]+)?)", replacement, string)
        return string

    env.filters["sht_expansion"] = sht_expansion
    return env
Exemplo n.º 3
0
 def brief_name(self):
     return uid_to_name(self.brief_uid)
Exemplo n.º 4
0
 def brief_name(self):
     return uid_to_name(self.brief_uid)