160 template_t(std::string body):
161 body_(std::move(body))
168 std::string tag_name(
const Action& action)
const
170 return body_.substr(action.start, action.end - action.start);
172 auto find_context(
const std::string& name,
const std::vector<const context*>& stack,
bool shouldUseOnlyFirstStackValue =
false)
const -> std::pair<bool, const context&>
176 return {
true, *stack.back()};
181 size_t dotPosition = name.find(
".");
182 if (dotPosition == std::string::npos)
184 for (
auto it = stack.rbegin(); it != stack.rend(); ++it)
186 if ((*it)->t() == json::type::Object)
188 if ((*it)->count(name))
189 return {
true, (**it)[name]};
195 std::vector<size_t> dotPositions;
196 dotPositions.push_back(std::string::npos);
197 while (dotPosition != std::string::npos)
199 dotPositions.push_back(dotPosition);
200 dotPosition = name.find(
".", dotPosition + 1);
202 dotPositions.push_back(name.size());
203 std::vector<std::string> names;
204 names.reserve(dotPositions.size() - 1);
205 for (
size_t i = 1; i < dotPositions.size(); i++)
206 names.emplace_back(name.substr(dotPositions[i - 1] + 1, dotPositions[i] - dotPositions[i - 1] - 1));
208 for (
auto it = stack.rbegin(); it != stack.rend(); ++it)
210 const context* view = *it;
212 for (
auto jt = names.begin(); jt != names.end(); ++jt)
214 if (view->t() == json::type::Object &&
217 view = &(*view)[*jt];
221 if (shouldUseOnlyFirstStackValue)
223 return {
false, empty_str};
230 return {
true, *view};
234 return {
false, empty_str};
237 void escape(
const std::string& in, std::string& out)
const
239 out.reserve(out.size() + in.size());
240 for (
auto it = in.begin(); it != in.end(); ++it)
244 case '&': out +=
"&";
break;
245 case '<': out +=
"<";
break;
246 case '>': out +=
">";
break;
247 case '"': out +=
""";
break;
248 case '\'': out +=
"'";
break;
249 case '/': out +=
"/";
break;
250 case '`': out +=
"`";
break;
251 case '=': out +=
"=";
break;
252 default: out += *it;
break;
257 bool isTagInsideObjectBlock(
const int& current,
const std::vector<const context*>& stack)
const
260 for (
int i = current; i > 0; --i)
262 auto& action = actions_[i - 1];
264 if (action.t == ActionType::OpenBlock)
266 if (openedBlock == 0 && (*stack.rbegin())->t() == json::type::Object)
272 else if (action.t == ActionType::CloseBlock)
281 static bool is_number_zero(
const context& ctx)
285 case json::num_type::Signed_integer:
286 return ctx.num.si == 0;
287 case json::num_type::Unsigned_integer:
288 return ctx.num.ui == 0;
289 case json::num_type::Floating_point:
290 case json::num_type::Double_precision_floating_point:
291 return std::fpclassify(ctx.num.d) == FP_ZERO;
297 void render_internal(
int actionBegin,
int actionEnd, std::vector<const context*>& stack, std::string& out,
int indent)
const
299 int current = actionBegin;
302 out.insert(out.size(), indent,
' ');
304 while (current < actionEnd)
306 auto& fragment = fragments_[current];
307 auto& action = actions_[current];
308 render_fragment(fragment, indent, out);
311 case ActionType::Ignore:
314 case ActionType::Partial:
316 std::string partial_name = tag_name(action);
317 auto partial_templ =
load(partial_name);
318 int partial_indent = action.pos;
319 partial_templ.render_internal(0, partial_templ.fragments_.size() - 1, stack, out, partial_indent ? indent + partial_indent : 0);
322 case ActionType::UnescapeTag:
323 case ActionType::Tag:
325 bool shouldUseOnlyFirstStackValue =
false;
326 if (isTagInsideObjectBlock(current, stack))
328 shouldUseOnlyFirstStackValue =
true;
330 auto optional_ctx = find_context(tag_name(action), stack, shouldUseOnlyFirstStackValue);
331 auto& ctx = optional_ctx.second;
334 case json::type::False:
335 case json::type::True:
336 case json::type::Number:
339 case json::type::String:
340 if (action.t == ActionType::Tag)
345 case json::type::Function:
347 std::string execute_result = ctx.execute();
350 if (execute_result.find(
"{{") != std::string::npos)
352 template_t result_plug(execute_result);
356 if (action.t == ActionType::Tag)
357 escape(execute_result, out);
359 out += execute_result;
363 throw std::runtime_error(
"not implemented tag type" + utility::lexical_cast<std::string>(
static_cast<int>(ctx.t())));
367 case ActionType::ElseBlock:
369 static context nullContext;
370 auto optional_ctx = find_context(tag_name(action), stack);
371 if (!optional_ctx.first)
373 stack.emplace_back(&nullContext);
377 auto& ctx = optional_ctx.second;
380 case json::type::List:
381 if (ctx.l && !ctx.l->empty())
382 current = action.pos;
384 stack.emplace_back(&nullContext);
386 case json::type::False:
387 case json::type::Null:
388 stack.emplace_back(&nullContext);
390 case json::type::Number:
391 if (is_number_zero(ctx))
392 stack.emplace_back(&nullContext);
394 current = action.pos;
397 current = action.pos;
402 case ActionType::OpenBlock:
404 auto optional_ctx = find_context(tag_name(action), stack);
405 if (!optional_ctx.first)
407 current = action.pos;
411 auto& ctx = optional_ctx.second;
414 case json::type::List:
416 for (
auto it = ctx.l->begin(); it != ctx.l->end(); ++it)
418 stack.push_back(&*it);
419 render_internal(current + 1, action.pos, stack, out, indent);
422 current = action.pos;
424 case json::type::Number:
425 if (is_number_zero(ctx))
426 current = action.pos;
428 stack.push_back(&ctx);
430 case json::type::String:
431 case json::type::Object:
432 case json::type::True:
433 stack.push_back(&ctx);
435 case json::type::False:
436 case json::type::Null:
437 current = action.pos;
440 throw std::runtime_error(
"{{#: not implemented context type: " + utility::lexical_cast<std::string>(
static_cast<int>(ctx.t())));
445 case ActionType::CloseBlock:
449 throw std::runtime_error(
"not implemented " + utility::lexical_cast<std::string>(
static_cast<int>(action.t)));
453 auto& fragment = fragments_[actionEnd];
454 render_fragment(fragment, indent, out);
456 void render_fragment(
const std::pair<int, int> fragment,
int indent, std::string& out)
const
460 for (
int i = fragment.first; i < fragment.second; i++)
463 if (body_[i] ==
'\n' && i + 1 !=
static_cast<int>(body_.size()))
464 out.insert(out.size(), indent,
' ');
468 out.insert(out.size(), body_, fragment.first, fragment.second - fragment.first);
476 std::vector<const context*> stack;
477 stack.emplace_back(&empty_ctx);
480 render_internal(0, fragments_.size() - 1, stack, ret, 0);
487 std::vector<const context*> stack;
488 stack.emplace_back(&ctx);
491 render_internal(0, fragments_.size() - 1, stack, ret, 0);
505 std::vector<const context*> stack;
506 stack.emplace_back(&empty_ctx);
509 render_internal(0, fragments_.size() - 1, stack, ret, 0);
516 std::vector<const context*> stack;
517 stack.emplace_back(&ctx);
520 render_internal(0, fragments_.size() - 1, stack, ret, 0);
527 std::string tag_open =
"{{";
528 std::string tag_close =
"}}";
530 std::vector<int> blockPositions;
535 size_t idx = body_.find(tag_open, current);
536 if (idx == body_.npos)
538 fragments_.emplace_back(
static_cast<int>(current),
static_cast<int>(body_.size()));
539 actions_.emplace_back(
'!', ActionType::Ignore, 0, 0);
542 fragments_.emplace_back(
static_cast<int>(current),
static_cast<int>(idx));
544 idx += tag_open.size();
545 size_t endIdx = body_.find(tag_close, idx);
548 throw invalid_template_exception(
"empty tag is not allowed");
550 if (endIdx == body_.npos)
553 throw invalid_template_exception(
"not matched opening tag");
555 current = endIdx + tag_close.size();
556 char tag_char = body_[idx];
561 while (body_[idx] ==
' ')
563 while (body_[endIdx - 1] ==
' ')
565 blockPositions.emplace_back(
static_cast<int>(actions_.size()));
566 actions_.emplace_back(tag_char, ActionType::OpenBlock, idx, endIdx);
570 while (body_[idx] ==
' ')
572 while (body_[endIdx - 1] ==
' ')
575 if (blockPositions.empty())
577 throw invalid_template_exception(
578 std::string(
"unexpected closing tag: ")
579 + body_.substr(idx, endIdx - idx)
582 auto& matched = actions_[blockPositions.back()];
583 if (body_.compare(idx, endIdx - idx,
584 body_, matched.start, matched.end - matched.start) != 0)
586 throw invalid_template_exception(
587 std::string(
"not matched {{")
590 + body_.substr(matched.start, matched.end - matched.start) +
", "
591 + body_.substr(idx, endIdx - idx)
594 matched.pos =
static_cast<int>(actions_.size());
595 matched.has_end_match =
true;
597 actions_.emplace_back(tag_char, ActionType::CloseBlock, idx, endIdx, blockPositions.back());
598 blockPositions.pop_back();
602 while (body_[idx] ==
' ')
604 while (body_[endIdx - 1] ==
' ')
606 blockPositions.emplace_back(
static_cast<int>(actions_.size()));
607 actions_.emplace_back(tag_char, ActionType::ElseBlock, idx, endIdx);
611 actions_.emplace_back(tag_char, ActionType::Ignore, idx + 1, endIdx);
615 while (body_[idx] ==
' ')
617 while (body_[endIdx - 1] ==
' ')
619 actions_.emplace_back(tag_char, ActionType::Partial, idx, endIdx);
622 if (tag_open !=
"{{" || tag_close !=
"}}")
623 throw invalid_template_exception(
"cannot use triple mustache when delimiter changed");
626 if (body_[endIdx + 2] !=
'}')
628 throw invalid_template_exception(
"{{{: }}} not matched");
630 while (body_[idx] ==
' ')
632 while (body_[endIdx - 1] ==
' ')
634 actions_.emplace_back(tag_char, ActionType::UnescapeTag, idx, endIdx);
639 while (body_[idx] ==
' ')
641 while (body_[endIdx - 1] ==
' ')
643 actions_.emplace_back(tag_char, ActionType::UnescapeTag, idx, endIdx);
648 actions_.emplace_back(tag_char, ActionType::Ignore, idx, endIdx);
650 if (body_[endIdx] !=
'=')
651 throw invalid_template_exception(
"{{=: not matching = tag: " + body_.substr(idx, endIdx - idx));
653 while (body_[idx] ==
' ')
655 while (body_[endIdx] ==
' ')
659 bool succeeded =
false;
660 for (
size_t i = idx; i < endIdx; i++)
664 tag_open = body_.substr(idx, i - idx);
665 while (body_[i] ==
' ')
667 tag_close = body_.substr(i, endIdx - i);
668 if (tag_open.empty())
669 throw invalid_template_exception(
"{{=: empty open tag");
670 if (tag_close.empty())
671 throw invalid_template_exception(
"{{=: empty close tag");
673 if (tag_close.find(
" ") != tag_close.npos)
674 throw invalid_template_exception(
"{{=: invalid open/close tag: " + tag_open +
" " + tag_close);
680 throw invalid_template_exception(
"{{=: cannot find space between new open/close tags");
685 while (body_[idx] ==
' ')
687 while (body_[endIdx - 1] ==
' ')
689 actions_.emplace_back(tag_char, ActionType::Tag, idx, endIdx);
695 for (
int i = 0; i < static_cast<int>(actions_.size()); i++)
697 if (actions_[i].missing_end_pair())
699 throw invalid_template_exception(
700 std::string(
"open tag has no matching end tag {{")
701 + actions_[i].tag_char
703 + body_.substr(actions_[i].start, actions_[i].end - actions_[i].start)
709 for (
int i =
static_cast<int>(actions_.size()) - 2; i >= 0; i--)
711 if (actions_[i].t == ActionType::Tag || actions_[i].t == ActionType::UnescapeTag)
713 auto& fragment_before = fragments_[i];
714 auto& fragment_after = fragments_[i + 1];
715 bool is_last_action = i ==
static_cast<int>(actions_.size()) - 2;
716 bool all_space_before =
true;
718 for (j = fragment_before.second - 1; j >= fragment_before.first; j--)
722 all_space_before =
false;
726 if (all_space_before && i > 0)
728 if (!all_space_before && body_[j] !=
'\n')
730 bool all_space_after =
true;
731 for (k = fragment_after.first; k <
static_cast<int>(body_.size()) && k < fragment_after.second; k++)
735 all_space_after =
false;
739 if (all_space_after && !is_last_action)
741 if (!all_space_after &&
745 k + 1 <
static_cast<int>(body_.size()) &&
746 body_[k + 1] ==
'\n')))
748 if (actions_[i].t == ActionType::Partial)
750 actions_[i].pos = fragment_before.second - j - 1;
752 fragment_before.second = j + 1;
753 if (!all_space_after)
755 if (body_[k] ==
'\n')
759 fragment_after.first = k;
764 std::vector<std::pair<int, int>> fragments_;
765 std::vector<Action> actions_;