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:
parent
368283769f
commit
10cfcbed67
|
@ -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
|
|
26
frappe/gettext/extractors/html_template.py
Normal file
26
frappe/gettext/extractors/html_template.py
Normal 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, []
|
|
@ -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
|
Loading…
Reference in New Issue
Block a user