2
0
mirror of https://github.com/frappe/frappe.git synced 2024-06-01 14:20:48 +00:00

feat: turn jinja extractor into a generic HTML template extractor

... supporting both jinja and JS microtemplates
This commit is contained in:
barredterra 2024-01-19 16:22:54 +01:00
parent 368283769f
commit 10cfcbed67
3 changed files with 27 additions and 12 deletions

View File

@ -6,4 +6,4 @@ hooks.py,frappe.gettext.extractors.navbar.extract
**/report/*/*.json,frappe.gettext.extractors.report.extract
**.py,frappe.gettext.extractors.python.extract
**.js,frappe.gettext.extractors.javascript.extract
**.html,frappe.gettext.extractors.jinja2.extract
**.html,frappe.gettext.extractors.html_template.extract
1 hooks.py frappe.gettext.extractors.navbar.extract
6 **/report/*/*.json frappe.gettext.extractors.report.extract
7 **.py frappe.gettext.extractors.python.extract
8 **.js frappe.gettext.extractors.javascript.extract
9 **.html frappe.gettext.extractors.jinja2.extract frappe.gettext.extractors.html_template.extract

View File

@ -0,0 +1,26 @@
from jinja2.ext import babel_extract
from .utils import extract_messages_from_code
def extract(*args, **kwargs):
"""Extract messages from Jinja and JS microtemplates.
Reuse the babel_extract function from jinja2.ext, but handle our own implementation of `_()`.
To handle JS microtemplates, parse all code again using regex."""
fileobj = args[0] or kwargs["fileobj"]
print(fileobj.name)
code = fileobj.read().decode("utf-8")
for lineno, funcname, messages, comments in babel_extract(*args, **kwargs):
if funcname == "_" and isinstance(messages, tuple) and len(messages) > 1:
funcname = "pgettext"
messages = (messages[-1], messages[0]) # (context, message)
yield lineno, funcname, messages, comments
for lineno, message, context in extract_messages_from_code(code):
if context:
yield lineno, "pgettext", (context, message), []
else:
yield lineno, "_", message, []

View File

@ -1,11 +0,0 @@
from jinja2.ext import babel_extract
def extract(*args, **kwargs):
"""Reuse the babel_extract function from jinja2.ext, but handle our own implementation of `_()`"""
for lineno, funcname, messages, comments in babel_extract(*args, **kwargs):
if funcname == "_" and isinstance(messages, tuple) and len(messages) > 1:
funcname = "pgettext"
messages = (messages[-1], messages[0]) # (context, message)
yield lineno, funcname, messages, comments