24#include <inja/inja.hpp>
34 inja::json &context,
const std::string &prefix);
41template <
typename ConfigType,
typename DiagramType>
class generator {
69 virtual void generate(std::ostream &ostr)
const = 0;
109 const inja::json &
context()
const;
111 inja::Environment &
env()
const;
127 m_context[
"git"][
"branch"] = config.git().branch;
128 m_context[
"git"][
"revision"] = config.git().revision;
129 m_context[
"git"][
"commit"] = config.git().commit;
130 m_context[
"git"][
"toplevel"] = config.git().toplevel;
136 m_context[
"diagram"] = model().context();
139template <
typename C,
typename D>
145template <
typename C,
typename D>
161 m_env.add_callback(
"empty", 1, [](inja::Arguments &args) {
162 return args.at(0)->get<std::string>().empty();
166 m_env.add_callback(
"ltrim", 1, [](inja::Arguments &args) {
167 return util::ltrim(args.at(0)->get<std::string>());
171 m_env.add_callback(
"rtrim", 1, [](inja::Arguments &args) {
172 return util::rtrim(args.at(0)->get<std::string>());
176 m_env.add_callback(
"trim", 1, [](inja::Arguments &args) {
177 return util::trim(args.at(0)->get<std::string>());
181 m_env.add_callback(
"abbrv", 2, [](inja::Arguments &args) {
183 args.at(0)->get<std::string>(), args.at(1)->get<
unsigned>());
186 m_env.add_callback(
"replace", 3, [](inja::Arguments &args) {
187 std::string result = args[0]->get<std::string>();
188 std::regex pattern(args[1]->get<std::string>());
189 return std::regex_replace(result, pattern, args[2]->get<std::string>());
192 m_env.add_callback(
"split", 2, [](inja::Arguments &args) {
194 args[0]->get<std::string>(), args[1]->get<std::string>());
205 m_env.add_callback(
"element", 1, [&model, &config](inja::Arguments &args) {
207 auto element_opt = model.get_with_namespace(
208 args[0]->get<std::string>(), config.using_namespace());
210 if (element_opt.has_value())
211 res = element_opt.value().context();
221 m_env.add_callback(
"alias", 1, [&model, &config](inja::Arguments &args) {
222 auto element_opt = model.get_with_namespace(
223 args[0]->get<std::string>(), config.using_namespace());
225 if (!element_opt.has_value())
227 args[0]->get<std::string>());
229 return element_opt.value().alias();
237 m_env.add_callback(
"comment", 1, [&model, &config](inja::Arguments &args) {
239 auto element_opt = model.get_with_namespace(
240 args[0]->get<std::string>(), config.using_namespace());
242 if (!element_opt.has_value())
244 args[0]->get<std::string>());
246 auto comment = element_opt.value().comment();
248 if (comment.has_value()) {
249 assert(comment.value().is_object());
250 res = comment.value();
257template <
typename C,
typename D>
261 const auto &diagram_context = context();
264 ctx[
"element"] = e.context();
266 if (ctx.contains(
"git")) {
268 if (diagram_context.template contains(
"git")) {
270 ctx[
"git"] = diagram_context[
"git"];
273 if (!e.file().empty()) {
274 std::filesystem::path file{e.file()};
275 std::string git_relative_path = file.string();
276 if (!e.file_relative().empty()) {
278 if (file.is_absolute() && ctx.contains(
"git")) {
280 if (file.is_absolute() &&
281 diagram_context.template contains(
"git")) {
283 git_relative_path = std::filesystem::relative(
284 file, diagram_context[
"git"][
"toplevel"])
286 ctx[
"element"][
"source"][
"path"] =
290 ctx[
"element"][
"source"][
"path"] = e.file();
294 git_relative_path =
"";
295 ctx[
"element"][
"source"][
"path"] = e.file();
298 ctx[
"element"][
"source"][
"full_path"] = file.string();
299 ctx[
"element"][
"source"][
"name"] = file.filename().string();
300 ctx[
"element"][
"source"][
"line"] = e.line();
303 const auto &maybe_comment = e.comment();
305 ctx[
"element"][
"comment"] = maybe_comment.value();
311template <
typename C,
typename D>
312std::optional<std::pair<std::string, std::string>>
317 return config().generate_links().get_link_pattern(sl.
file());
320 return config().generate_links().get_link_pattern(sl.
file_relative());
323template <
typename C,
typename D>
324std::optional<std::pair<std::string, std::string>>
329 return config().generate_links().get_tooltip_pattern(sl.
file());
332 return config().generate_links().get_tooltip_pattern(sl.
file_relative());