feat(workflow): example stats page with embedded Chart.js, inline form validation errors
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+51
-10
@@ -129,6 +129,43 @@ std::string Environment::render_source(const std::string& name,
|
||||
return render_blocks(src, ctx, depth);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// Position of the "{% <close> %}" matching an already-open tag, honoring
|
||||
// nested "{% <open> ... %}" tags of the same kind. When else_pos is given it
|
||||
// receives the depth-zero "{% else %}" position (npos if none).
|
||||
std::size_t find_matching(const std::string& src, std::size_t from,
|
||||
const std::string& open, const std::string& close,
|
||||
std::size_t* else_pos = nullptr) {
|
||||
int depth = 0;
|
||||
if (else_pos) *else_pos = std::string::npos;
|
||||
std::size_t i = from;
|
||||
while (true) {
|
||||
std::size_t t = src.find("{%", i);
|
||||
if (t == std::string::npos) return std::string::npos;
|
||||
std::size_t e = src.find("%}", t);
|
||||
if (e == std::string::npos) return std::string::npos;
|
||||
std::size_t a = t + 2;
|
||||
while (a < e && src[a] == ' ') ++a;
|
||||
std::size_t b = e;
|
||||
while (b > a && src[b - 1] == ' ') --b;
|
||||
std::string tag = src.substr(a, b - a);
|
||||
if (tag.rfind(open + " ", 0) == 0) {
|
||||
++depth;
|
||||
} else if (tag == close) {
|
||||
if (depth == 0) return t;
|
||||
--depth;
|
||||
} else if (tag == "else" && depth == 0 && else_pos &&
|
||||
*else_pos == std::string::npos) {
|
||||
*else_pos = t;
|
||||
}
|
||||
i = e + 2;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
|
||||
int depth) const {
|
||||
std::string out;
|
||||
@@ -185,16 +222,20 @@ std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
|
||||
std::string tag = trim(src.substr(t + 2, e - t - 2));
|
||||
if (tag.rfind("if ", 0) == 0) {
|
||||
std::string cond = trim(tag.substr(3));
|
||||
std::size_t end = src.find("{% endif %}", e);
|
||||
std::size_t els_pos = std::string::npos;
|
||||
std::size_t end = find_matching(src, e + 2, "if", "endif", &els_pos);
|
||||
if (end == std::string::npos) break;
|
||||
std::string inner = src.substr(e + 2, end - e - 2);
|
||||
std::size_t els = inner.find("{% else %}");
|
||||
std::string yes = els == std::string::npos ? inner
|
||||
: inner.substr(0, els);
|
||||
std::string no =
|
||||
els == std::string::npos ? "" : inner.substr(els + 10);
|
||||
std::size_t close_end = src.find("%}", end) + 2;
|
||||
std::string yes, no;
|
||||
if (els_pos == std::string::npos) {
|
||||
yes = src.substr(e + 2, end - e - 2);
|
||||
} else {
|
||||
yes = src.substr(e + 2, els_pos - e - 2);
|
||||
no = src.substr(src.find("%}", els_pos) + 2,
|
||||
end - src.find("%}", els_pos) - 2);
|
||||
}
|
||||
out += render_blocks(ctx_truthy(ctx, cond) ? yes : no, ctx, depth + 1);
|
||||
i = end + 11;
|
||||
i = close_end;
|
||||
} else if (tag.rfind("for ", 0) == 0) {
|
||||
std::string rest = trim(tag.substr(4));
|
||||
std::size_t in = rest.find(" in ");
|
||||
@@ -204,7 +245,7 @@ std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
|
||||
std::string list = in == std::string::npos
|
||||
? ""
|
||||
: trim(rest.substr(in + 4));
|
||||
std::size_t end = src.find("{% endfor %}", e);
|
||||
std::size_t end = find_matching(src, e + 2, "for", "endfor");
|
||||
if (end == std::string::npos) break;
|
||||
std::string inner = src.substr(e + 2, end - e - 2);
|
||||
auto it = ctx.find(list);
|
||||
@@ -220,7 +261,7 @@ std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
|
||||
}
|
||||
}
|
||||
}
|
||||
i = end + 12;
|
||||
i = src.find("%}", end) + 2;
|
||||
} else if (tag.rfind("include ", 0) == 0) {
|
||||
std::string rest = trim(tag.substr(8));
|
||||
if (rest.size() >= 2 && rest.front() == '"') {
|
||||
|
||||
Reference in New Issue
Block a user