def replace_gist_tags(generator):
    """Replace gist tags in the article content."""
    from jinja2 import Template
    template = Template(gist_template)

    should_cache = generator.context.get('GIST_CACHE_ENABLED')
    cache_location = generator.context.get('GIST_CACHE_LOCATION')
    pygments_style = generator.context.get('GIST_PYGMENTS_STYLE')
    
    body = None

    for article in generator.articles:
        for match in gist_regex.findall(article._content):
            gist_id = match[1]
            filename = None
            filetype = None
            if match[3]:
                filename = match[3]
            if match[5]:
                filetype = match[5]
            logger.info('[gist]: Found gist id {} with filename {} and filetype {}'.format(
                gist_id,
                filename,
                filetype,
            ))

            if should_cache:
                body = get_cache(cache_location, gist_id, filename)

            # Fetch the gist
            if not body:
                logger.info('[gist]: Gist did not exist in cache, fetching...')
                body = fetch_gist(gist_id, filename)

                if should_cache:
                    logger.info('[gist]: Saving gist to cache...')
                    set_cache(cache_location, gist_id, body, filename)
            else:
                logger.info('[gist]: Found gist in cache.')

            # Create a context to render with
            context = generator.context.copy()
            context.update({
                'script_url': script_url(gist_id, filename),
                'code': render_code(body, filetype, pygments_style)
            })

            # Render the template
            replacement = template.render(context)

            article._content = article._content.replace(match[0], replacement)
def replace_gist_tags ( generator ) : from jinja2 import Template template = Template ( gist_template ) should_cache = generator . context . get ( 'GIST_CACHE_ENABLED' ) cache_location = generator . context . get ( 'GIST_CACHE_LOCATION' ) pygments_style = generator . context . get ( 'GIST_PYGMENTS_STYLE' ) body = None for article in generator . articles : for match in gist_regex . findall ( article . _content ) : gist_id = match [ 1 ] filename = None filetype = None if match [ 3 ] : filename = match [ 3 ] if match [ 5 ] : filetype = match [ 5 ] logger . info ( '[gist]: Found gist id {} with filename {} and filetype {}' . format ( gist_id , filename , filetype , ) ) if should_cache : body = get_cache ( cache_location , gist_id , filename ) if not body : logger . info ( '[gist]: Gist did not exist in cache, fetching...' ) body = fetch_gist ( gist_id , filename ) if should_cache : logger . info ( '[gist]: Saving gist to cache...' ) set_cache ( cache_location , gist_id , body , filename ) else : logger . info ( '[gist]: Found gist in cache.' ) context = generator . context . copy ( ) context . update ( { 'script_url' : script_url ( gist_id , filename ) , 'code' : render_code ( body , filetype , pygments_style ) } ) replacement = template . render ( context ) article . _content = article . _content . replace ( match [ 0 ] , replacement )
Replace gist tags in the article content.