0.6.0
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
clanguml::common::jinja Namespace Reference

Classes

class  jinja_context
 Jinja diagram element context wrapper. More...
 

Typedefs

template<typename T >
using element_context = jinja_context< T, element_context_tag >
 
template<typename T >
using diagram_context = jinja_context< T, diagram_context_tag >
 

Functions

void to_json (inja::json &ctx, const element_context< class_diagram::model::class_element > &d)
 
void to_json (inja::json &ctx, const diagram_context< class_diagram::model::diagram > &d)
 
void to_json (inja::json &ctx, const element_context< common::model::decorated_element > &jc)
 
void to_json (inja::json &ctx, const element_context< diagram_element > &jc)
 
void to_json (inja::json &ctx, const element_context< element > &jc)
 
void to_json (inja::json &ctx, const element_context< common::model::relationship > &jc)
 
void to_json (inja::json &ctx, const element_context< source_file > &jc)
 
void to_json (inja::json &ctx, const diagram_context< source_location > &jc)
 
void to_json (inja::json &ctx, const element_context< source_location > &jc)
 
void to_json (inja::json &ctx, const diagram_context< common::model::decorated_element > &jc)
 
void to_json (inja::json &ctx, const diagram_context< common::model::diagram_element > &jc)
 
void to_json (inja::json &ctx, const diagram_context< common::model::element > &jc)
 
void to_json (inja::json &ctx, const diagram_context< source_file > &jc)
 
std::optional< std::string > render_template (inja::Environment &env, const inja::json &context, const std::string &jinja_template)
 
std::optional< std::string > render_template (inja::Environment &env, const std::string &jinja_template)
 
void to_json (inja::json &ctx, const element_context< common::model::diagram_element > &jc)
 
void to_json (inja::json &ctx, const element_context< common::model::element > &jc)
 
void to_json (inja::json &ctx, const element_context< common::model::source_file > &jc)
 
void to_json (inja::json &ctx, const element_context< common::model::source_location > &jc)
 
void to_json (inja::json &ctx, const diagram_context< common::model::source_file > &jc)
 
void to_json (inja::json &ctx, const diagram_context< common::model::source_location > &jc)
 
void to_json (inja::json &ctx, const diagram_context< include_diagram::model::diagram > &d)
 
void to_json (inja::json &ctx, const diagram_context< package_diagram::model::diagram > &d)
 
void to_json (inja::json &ctx, const diagram_context< sequence_diagram::model::diagram > &d)
 

Typedef Documentation

◆ diagram_context

template<typename T >
using clanguml::common::jinja::diagram_context = typedef jinja_context<T, diagram_context_tag>

Definition at line 75 of file jinja_context.h.

◆ element_context

template<typename T >
using clanguml::common::jinja::element_context = typedef jinja_context<T, element_context_tag>

Definition at line 73 of file jinja_context.h.

Function Documentation

◆ render_template() [1/2]

std::optional< std::string > clanguml::common::jinja::render_template ( inja::Environment &  env,
const inja::json &  context,
const std::string &  jinja_template 
)

Definition at line 209 of file jinja_context.cc.

211{
212 std::optional<std::string> result;
213
214 if (jinja_template.empty())
215 return result;
216
217 try {
218 // Render the directive with template engine first
219 auto rendered_template =
220 env.render(std::string_view{jinja_template}, context);
221
222 result = std::move(rendered_template);
223 }
224 catch (const clanguml::error::uml_alias_missing &e) {
225 LOG_WARN("Failed to render Jinja template '{}' due to unresolvable "
226 "alias: {}",
227 jinja_template, e.what());
228 }
229 catch (const inja::json::parse_error &e) {
230 LOG_WARN("Failed to parse Jinja template: {}", jinja_template);
231 }
232 catch (const inja::json::exception &e) {
233 LOG_WARN("Failed to render Jinja template: \n{}\n due to: {}",
234 jinja_template, e.what());
235 }
236 catch (const std::regex_error &e) {
237 LOG_WARN("Failed to render Jinja template: \n{}\n due to "
238 "std::regex_error: {}",
239 jinja_template, e.what());
240 }
241 catch (const std::exception &e) {
242 LOG_WARN("Failed to render Jinja template: \n{}\n due to: {}",
243 jinja_template, e.what());
244 }
245
246 return result;
247}

◆ render_template() [2/2]

std::optional< std::string > clanguml::common::jinja::render_template ( inja::Environment &  env,
const std::string &  jinja_template 
)

Definition at line 249 of file jinja_context.cc.

251{
252 inja::json empty;
253 return render_template(env, empty, jinja_template);
254}

◆ to_json() [1/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< class_diagram::model::diagram > &  d 
)

Definition at line 49 of file jinja_context.cc.

51{
52 using namespace class_diagram::model;
53
54 ctx["name"] = d.get().name();
55 ctx["type"] = "class";
56
57 inja::json::array_t elements{};
58
59 d.get().view<class_>().for_each([&](auto &&e) mutable {
60 elements.emplace_back(diagram_context<element>(e));
61 });
62
63 d.get().view<enum_>().for_each([&](auto &&e) mutable {
64 elements.emplace_back(diagram_context<element>(e));
65 });
66
67 d.get().view<concept_>().for_each([&](auto &&e) mutable {
68 elements.emplace_back(diagram_context<element>(e));
69 });
70
71 d.get().view<objc_interface>().for_each([&](auto &&e) mutable {
72 elements.emplace_back(diagram_context<element>(e));
73 });
74
75 ctx["elements"] = elements;
76}

◆ to_json() [2/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< common::model::decorated_element > &  jc 
)

Definition at line 163 of file jinja_context.cc.

165{
166 if (const auto maybe_comment = jc.get().comment();
167 maybe_comment.has_value()) {
168 ctx["comment"] = maybe_comment.value();
169 }
170}

◆ to_json() [3/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< common::model::diagram_element > &  jc 
)

Definition at line 172 of file jinja_context.cc.

174{
175 to_json(ctx, jc.as<decorated_element>());
176
177 ctx["name"] = display_name_adapter(jc.get()).name();
178 ctx["type"] = jc.get().type_name();
179 ctx["alias"] = jc.get().alias();
180 ctx["full_name"] = display_name_adapter(jc.get()).full_name(false);
181 auto maybe_doxygen_link = jc.get().doxygen_link();
182 if (maybe_doxygen_link)
183 ctx["doxygen_link"] = maybe_doxygen_link.value();
184
185 to_json(ctx, jc.as<source_location>());
186}

◆ to_json() [4/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< common::model::element > &  jc 
)

Definition at line 188 of file jinja_context.cc.

189{
190 to_json(ctx, jc.as<diagram_element>());
191
192 ctx["using_namespace"] = jc.get().using_namespace().to_string();
193 ctx["namespace"] = jc.get().get_namespace().to_string();
194 if (const auto maybe_comment = jc.get().comment();
195 maybe_comment.has_value()) {
196 ctx["comment"] = maybe_comment.value();
197 }
198}

◆ to_json() [5/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< common::model::source_file > &  jc 
)

◆ to_json() [6/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< common::model::source_location > &  jc 
)

◆ to_json() [7/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< include_diagram::model::diagram > &  d 
)

Definition at line 23 of file jinja_context.cc.

25{
26 ctx["name"] = d.get().name();
27 ctx["type"] = "include";
28
29 inja::json::array_t elements{};
30
31 d.get().view<common::model::source_file>().for_each(
32 [&](auto &&element) mutable {
33 elements.emplace_back(
34 diagram_context<common::model::source_file>(element));
35 });
36
37 ctx["elements"] = elements;
38}

◆ to_json() [8/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< package_diagram::model::diagram > &  d 
)

Definition at line 25 of file jinja_context.cc.

27{
28 ctx["name"] = d.get().name();
29 ctx["type"] = "package";
30
31 inja::json::array_t elements{};
32
33 d.get().view<package>().for_each([&](auto &&e) mutable {
34 elements.emplace_back(diagram_context<element>(e));
35 });
36
37 ctx["elements"] = elements;
38}

◆ to_json() [9/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< sequence_diagram::model::diagram > &  d 
)

Definition at line 25 of file jinja_context.cc.

27{
28 ctx["name"] = d.get().name();
29 ctx["type"] = "sequence";
30
31 inja::json::array_t elements{};
32
33 for (const auto &[id, p] : d.get().participants()) {
34 elements.emplace_back(diagram_context<element>(*p));
35 }
36
37 ctx["elements"] = elements;
38}

◆ to_json() [10/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< source_file > &  jc 
)

Definition at line 200 of file jinja_context.cc.

201{
202 to_json(ctx, jc.as<diagram_element>());
203
204 std::filesystem::path fullNamePath{ctx["full_name"].get<std::string>()};
205 fullNamePath.make_preferred();
206 ctx["full_name"] = fullNamePath.string();
207}

◆ to_json() [11/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const diagram_context< source_location > &  jc 
)

Definition at line 96 of file jinja_context.cc.

97{
98 const auto &e = jc.get();
99
100 if (!e.file().empty()) {
101 const std::filesystem::path file{e.file()};
102 std::string git_relative_path = file.string();
103 if (!e.file_relative().empty()) {
104#if _MSC_VER
105 if (file.is_absolute() && ctx.contains("git")) {
106#else
107 if (file.is_absolute() && ctx.contains("git")) {
108#endif
109 git_relative_path =
110 std::filesystem::relative(file, ctx["git"]["toplevel"])
111 .string();
112 ctx["source"]["path"] = util::path_to_url(git_relative_path);
113 }
114 else {
115 ctx["source"]["path"] = e.file();
116 }
117 }
118 else {
119 git_relative_path = "";
120 ctx["source"]["path"] = e.file();
121 }
122
123 ctx["source"]["full_path"] = file.string();
124 ctx["source"]["name"] = file.filename().string();
125 ctx["source"]["line"] = e.line();
126 }
127}

◆ to_json() [12/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< class_diagram::model::class_element > &  d 
)

Definition at line 29 of file jinja_context.cc.

31{
32 to_json(ctx, d.as<decorated_element>());
33
34 ctx["element"]["name"] = display_name_adapter(d.get()).name();
35 ctx["element"]["type"] = display_name_adapter(d.get()).type();
36 ctx["element"]["access"] = to_string(d.get().access());
37
38 if (d.diagram_context().contains("git")) {
39 ctx["git"] = d.diagram_context()["git"];
40 }
41
42 if (d.diagram_context().contains("user_data")) {
43 ctx["user_data"] = d.diagram_context()["user_data"];
44 }
45
46 to_json(ctx, d.as<source_location>());
47}

◆ to_json() [13/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< common::model::decorated_element > &  jc 
)

Definition at line 29 of file jinja_context.cc.

31{
32 if (const auto maybe_comment = jc.get().comment();
33 maybe_comment.has_value()) {
34 ctx["element"]["comment"] = maybe_comment.value();
35 }
36
37 if (jc.diagram_context().contains("git")) {
38 ctx["git"] = jc.diagram_context()["git"];
39 }
40
41 if (jc.diagram_context().contains("user_data")) {
42 ctx["user_data"] = jc.diagram_context()["user_data"];
43 }
44}

◆ to_json() [14/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< common::model::diagram_element > &  jc 
)

◆ to_json() [15/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< common::model::element > &  jc 
)

◆ to_json() [16/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< common::model::relationship > &  jc 
)

Definition at line 70 of file jinja_context.cc.

72{
73 to_json(ctx, jc.as<decorated_element>());
74
75 ctx["element"]["type"] = "relationship";
76 ctx["element"]["name"] = jc.get().label();
77 ctx["element"]["relationship_type"] = to_string(jc.get().type());
78 ctx["element"]["multiplicity"]["source"] = jc.get().multiplicity_source();
79 ctx["element"]["multiplicity"]["destination"] =
80 jc.get().multiplicity_destination();
81 ctx["element"]["access"] = to_string(jc.get().access());
82
83 to_json(ctx, jc.as<source_location>());
84}

◆ to_json() [17/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< common::model::source_file > &  jc 
)

◆ to_json() [18/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< common::model::source_location > &  jc 
)

◆ to_json() [19/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< diagram_element > &  jc 
)

Definition at line 46 of file jinja_context.cc.

47{
48 to_json(ctx, jc.as<decorated_element>());
49
50 ctx["element"]["name"] = display_name_adapter(jc.get()).name();
51 ctx["element"]["type"] = jc.get().type_name();
52 ctx["element"]["alias"] = jc.get().alias();
53 ctx["element"]["full_name"] =
55 auto maybe_doxygen_link = jc.get().doxygen_link();
56 if (maybe_doxygen_link)
57 ctx["element"]["doxygen_link"] = maybe_doxygen_link.value();
58
59 to_json(ctx, jc.as<source_location>());
60}

◆ to_json() [20/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< element > &  jc 
)

Definition at line 62 of file jinja_context.cc.

63{
64 to_json(ctx, jc.as<diagram_element>());
65
66 ctx["element"]["using_namespace"] = jc.get().using_namespace().to_string();
67 ctx["element"]["namespace"] = jc.get().get_namespace().to_string();
68}

◆ to_json() [21/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< source_file > &  jc 
)

Definition at line 86 of file jinja_context.cc.

87{
88 to_json(ctx, jc.as<diagram_element>());
89
90 std::filesystem::path fullNamePath{
91 ctx["element"]["full_name"].get<std::string>()};
92 fullNamePath.make_preferred();
93 ctx["element"]["full_name"] = fullNamePath.string();
94}

◆ to_json() [22/22]

void clanguml::common::jinja::to_json ( inja::json &  ctx,
const element_context< source_location > &  jc 
)

Definition at line 129 of file jinja_context.cc.

130{
131 const auto &e = jc.get();
132
133 if (!e.file().empty()) {
134 const std::filesystem::path file{e.file()};
135 std::string git_relative_path = file.string();
136 if (!e.file_relative().empty()) {
137#if _MSC_VER
138 if (file.is_absolute() && ctx.contains("git")) {
139#else
140 if (file.is_absolute() && ctx.contains("git")) {
141#endif
142 git_relative_path =
143 std::filesystem::relative(file, ctx["git"]["toplevel"])
144 .string();
145 ctx["element"]["source"]["path"] =
146 util::path_to_url(git_relative_path);
147 }
148 else {
149 ctx["element"]["source"]["path"] = e.file();
150 }
151 }
152 else {
153 git_relative_path = "";
154 ctx["element"]["source"]["path"] = e.file();
155 }
156
157 ctx["element"]["source"]["full_path"] = file.string();
158 ctx["element"]["source"]["name"] = file.filename().string();
159 ctx["element"]["source"]["line"] = e.line();
160 }
161}