我正在尝试创建一个可以修改 A 标记的class属性的nunjucks扩展。标签必须根据某些逻辑分配新类。
这样的事情:
import os
from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration
from django.conf import settings
from django.http import HttpResponse
from django.template.loader import get_template
from django.urls import reverse
def render_to_pdf(template_src, context_dict={}):
font_config = FontConfiguration()
font_string = '''
@font-face {
font-family: 'Titillium Web';
font-style: normal;
font-weight: 300;
src: local('Titillium Web Light'), local('TitilliumWeb-Light'), url('http://X.X.X.X:8000/static/fonts/titillium_web.woff2') format('woff2');
}
*, div {font-family: 'Titillium Web';}
'''
template = get_template(template_src)
rendered_html = template.render(context_dict)
pdf_file = HTML(string=rendered_html).write_pdf(stylesheets=[
CSS(settings.BASE_DIR + '/gui/executive_summary.css'),
CSS(string=font_string)],font_config=font_config)
response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = 'filename="report.pdf"'
return response
扩展程序必须“搜索” href 属性,如果它等于上下文中的某个值,则指定css类“active”
结果必须是这样的:
<a href="/some/page" {% activeLink } >I am link</a>
如何让解析器“移动”到 A 标记的开头,然后从左向右查找 class 属性?有人做过这样的事吗?