feat: added linked article to context
This commit is contained in:
@@ -178,13 +178,25 @@ class ArticleTranslation:
|
||||
if self.language_code in c.article_translations_map
|
||||
]
|
||||
|
||||
self.linked_context = self.context["linked"] = []
|
||||
|
||||
def __init_content_context__(self):
|
||||
template = jinja2.Template(self.html_content)
|
||||
template.environment.accessed_keys = []
|
||||
template.environment.context_class = ContextDict
|
||||
|
||||
self.html_content = template.render({
|
||||
**CROSS_ARTICLE_CONTEXT,
|
||||
**TRANSLATED_CROSS_ARTICLE_CONTEXT[self.language_code],
|
||||
})
|
||||
|
||||
template.environment.context_class = jinja2.runtime.Context
|
||||
accessed_keys = template.environment.accessed_keys
|
||||
for key in accessed_keys:
|
||||
a = ARTICLE_LAKE[key]
|
||||
if self.language_code in a.article_translations_map:
|
||||
self.linked_context.append(a.article_translations_map[self.language_code].context)
|
||||
|
||||
self.context["content"] = self.html_content
|
||||
self.context["preview"] = get_preview_text(html_string=self.html_content)
|
||||
|
||||
@@ -297,6 +309,13 @@ class Article:
|
||||
ac.build()
|
||||
|
||||
|
||||
class ContextDict(jinja2.runtime.Context):
|
||||
def resolve_or_missing(self, key: str) -> Any:
|
||||
self.environment.accessed_keys.append(key)
|
||||
return super().resolve_or_missing(key)
|
||||
|
||||
|
||||
|
||||
# GLOBALS
|
||||
logger = logging.getLogger("stsg.build")
|
||||
CROSS_ARTICLE_CONTEXT: Dict[str, Dict[str, Any]] = {}
|
||||
|
||||
Reference in New Issue
Block a user