import arsd.dom;
string toD(string s) {
return `append(` ~ "`" ~ s ~ "`" ~ `);`;
}
template loadTemplateMixin(string doc) {
string helper() {
Document document = new Document;
document.parseSawAspCode = (string) => true;
document.parseStrict(doc);
string code;
void expand(Element element) {
if(auto asp = cast(AspCode) element) {
if(asp.source.length > 3 && asp.source[1] == '=')
code ~= `append(` ~ asp.source[2 .. $-1] ~ `);`;
else
code ~= asp.source[1 .. $-1];
} else if(auto tn = cast(TextNode) element) {
code ~= toD(tn.toString());
} else if(auto sn = cast(SpecialElement) element) {
code ~= toD(sn.toString());
} else {
code ~= toD("<" ~ element.tagName);
foreach(k, v; element.attributes) {
code ~= toD(" ");
code ~= toD(k.htmlEntitiesEncode);
code ~= toD("=\"");
code ~= toD(v.htmlEntitiesEncode);
code ~= toD("\"");
}
code ~= toD(">");
foreach(child; element.children)
expand(child);
code ~= toD("</" ~ element.tagName ~ ">");
}
}
expand(document.root);
return code;
}
enum loadTemplateMixin = helper();
}
enum doc = `<html><script> foo</script><style>css</style><test id="main"><%= my_string[0 .. 5] %></test>
<span>foo</span>
<span>foo</span>
<span>foo</span>
<% foreach(item; strings)
append(item);
%>
</html>`;
void main() {
string html;
string my_string = "你好.世界";
string[] strings = ["omg", "wtf", "lol"];
void append(string s) {
html ~= s;
}
mixin(loadTemplateMixin!doc);
import std.stdio;
writeln(html);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现