mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-18 18:50:05 +08:00
✨ feat(renderer): 添加 Jinja2 inline_asset 全局函数
- 新增 `RendererService._inline_asset_global` 方法,并注册为 Jinja2 全局函数 `inline_asset`。
- 允许模板通过 `{{ inline_asset('@namespace/path/to/asset.svg') }}` 直接内联已注册命名空间下的资源文件内容。
- 主要用于解决内联 SVG 时可能遇到的跨域安全问题。
- 【重构】优化 `ResourceResolver.resolve_asset_uri` 中对命名空间资源 (以 `@` 开头) 的解析逻辑,确保能够正确获取文件绝对路径并返回 URI。
- 改进 `RenderableComponent.get_extra_css`,使其在组件定义 `component_css` 时自动返回该 CSS 内容。
- 清理 `Renderable` 协议和 `RenderableComponent` 基类中已存在方法的 `[新增]` 标记。
This commit is contained in:
@@ -172,24 +172,45 @@ class ResourceResolver:
|
||||
|
||||
if asset_path.startswith("@"):
|
||||
try:
|
||||
full_asset_path = self.theme_manager.jinja_env.join_path(
|
||||
asset_path, current_template_name
|
||||
)
|
||||
_source, file_abs_path, _uptodate = (
|
||||
self.theme_manager.jinja_env.loader.get_source(
|
||||
self.theme_manager.jinja_env, full_asset_path
|
||||
if "/" not in asset_path:
|
||||
raise TemplateNotFound(f"无效的命名空间路径: {asset_path}")
|
||||
|
||||
namespace, rel_path = asset_path.split("/", 1)
|
||||
|
||||
loader = self.theme_manager.jinja_env.loader
|
||||
if (
|
||||
isinstance(loader, ChoiceLoader)
|
||||
and loader.loaders
|
||||
and isinstance(loader.loaders[0], PrefixLoader)
|
||||
):
|
||||
prefix_loader = loader.loaders[0]
|
||||
if namespace in prefix_loader.mapping:
|
||||
loader_for_namespace = prefix_loader.mapping[namespace]
|
||||
if isinstance(loader_for_namespace, FileSystemLoader):
|
||||
base_path = Path(loader_for_namespace.searchpath[0])
|
||||
file_abs_path = (base_path / rel_path).resolve()
|
||||
|
||||
if file_abs_path.is_file():
|
||||
logger.debug(
|
||||
f"Resolved namespaced asset"
|
||||
f" '{asset_path}' -> '{file_abs_path}'"
|
||||
)
|
||||
return file_abs_path.as_uri()
|
||||
else:
|
||||
raise TemplateNotFound(asset_path)
|
||||
else:
|
||||
raise TemplateNotFound(
|
||||
f"Unsupported loader type for namespace '{namespace}'."
|
||||
)
|
||||
else:
|
||||
raise TemplateNotFound(f"Namespace '{namespace}' not found.")
|
||||
else:
|
||||
raise TemplateNotFound(
|
||||
f"无法解析命名空间资源 '{asset_path}',加载器结构不符合预期。"
|
||||
)
|
||||
)
|
||||
if file_abs_path:
|
||||
logger.debug(
|
||||
f"Jinja Loader resolved asset '{asset_path}'->'{file_abs_path}'"
|
||||
)
|
||||
return Path(file_abs_path).absolute().as_uri()
|
||||
|
||||
except TemplateNotFound:
|
||||
logger.warning(
|
||||
f"资源文件在命名空间中未找到: '{asset_path}'"
|
||||
f"(在模板 '{current_template_name}' 中引用)"
|
||||
)
|
||||
logger.warning(f"资源文件在命名空间中未找到: '{asset_path}'")
|
||||
return ""
|
||||
|
||||
search_paths: list[tuple[str, Path]] = []
|
||||
|
||||
Reference in New Issue
Block a user