Added alias() template function for resolving PlantUML aliases

This commit is contained in:
Bartek Kryza
2022-03-23 22:56:59 +01:00
parent eed9fcaf66
commit e22849eb32
3 changed files with 27 additions and 8 deletions

View File

@@ -168,16 +168,19 @@ void generator<C, D>::generate_plantuml_directives(
{
using common::model::namespace_;
for (const auto &b : directives) {
std::string note{b};
for (const auto &d : directives) {
// Render the directive with template engine first
std::string directive{env().render(std::string_view{d}, context())};
// Now search for alias @A() directives in the text
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(note, alias_match)) {
while (util::find_element_alias(directive, alias_match)) {
auto alias = m_model.to_alias(
m_config.using_namespace().relative(std::get<0>(alias_match)));
note.replace(
directive.replace(
std::get<1>(alias_match), std::get<2>(alias_match), alias);
}
ostr << note << '\n';
ostr << directive << '\n';
}
}
@@ -268,7 +271,9 @@ template <typename C, typename D> void generator<C, D>::init_context()
template <typename C, typename D> void generator<C, D>::init_env()
{
//
// Add basic string functions to inja environment
//
m_env.add_callback("empty", 1, [](inja::Arguments &args) {
return args.at(0)->get<std::string>().empty();
});
@@ -300,6 +305,20 @@ template <typename C, typename D> void generator<C, D>::init_env()
return util::split(
args[0]->get<std::string>(), args[1]->get<std::string>());
});
//
// Add PlantUML specific functions
//
// Convert C++ entity to PlantUML alias, e.g.
// "note left of {{ alias("ClassA") }}: This is a note"
// is equivalent to the old syntax:
// "note left of @A(ClassA): This is a note"
m_env.add_callback("alias", 1, [this](inja::Arguments &args) {
auto alias_match = args[0]->get<std::string>();
return m_model.to_alias(
m_config.using_namespace().relative(alias_match));
});
}
}

View File

@@ -14,5 +14,5 @@ diagrams:
after:
- '@A(ProductA1) <.. @A(Factory1)'
- '@A(ProductB1) <.. @A(Factory1)'
- '@A(ProductA2) <.. @A(Factory2)'
- '@A(ProductB2) <.. @A(Factory2)'
- '{{ alias("ProductA2") }} <.. {{ alias("Factory2") }}'
- '{{ alias("ProductB2") }} <.. {{ alias("Factory2") }}'

View File

@@ -17,4 +17,4 @@ diagrams:
before:
- "' t30001 test package diagram"
after:
- "note right of @A(A::AA::AAA): A AAA note..."
- 'note right of {{ alias("A::AA::AAA") }}: A AAA note...'