From e22849eb32e07d77feb98df86907b939d76a196f Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Wed, 23 Mar 2022 22:56:59 +0100 Subject: [PATCH] Added alias() template function for resolving PlantUML aliases --- src/common/generators/plantuml/generator.h | 29 ++++++++++++++++++---- tests/t00020/.clang-uml | 4 +-- tests/t30001/.clang-uml | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/common/generators/plantuml/generator.h b/src/common/generators/plantuml/generator.h index 15507af2..29aebf4e 100644 --- a/src/common/generators/plantuml/generator.h +++ b/src/common/generators/plantuml/generator.h @@ -168,16 +168,19 @@ void generator::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 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 void generator::init_context() template void generator::init_env() { + // // Add basic string functions to inja environment + // m_env.add_callback("empty", 1, [](inja::Arguments &args) { return args.at(0)->get().empty(); }); @@ -300,6 +305,20 @@ template void generator::init_env() return util::split( args[0]->get(), args[1]->get()); }); + + // + // 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(); + return m_model.to_alias( + m_config.using_namespace().relative(alias_match)); + }); } } diff --git a/tests/t00020/.clang-uml b/tests/t00020/.clang-uml index fcd04dcd..b24214be 100644 --- a/tests/t00020/.clang-uml +++ b/tests/t00020/.clang-uml @@ -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") }}' diff --git a/tests/t30001/.clang-uml b/tests/t30001/.clang-uml index 1e5f3625..d519c601 100644 --- a/tests/t30001/.clang-uml +++ b/tests/t30001/.clang-uml @@ -17,4 +17,4 @@ diagrams: before: - "' t30001 test package diagram" after: - - "note right of @A(A::AA::AAA): A AAA note..." \ No newline at end of file + - 'note right of {{ alias("A::AA::AAA") }}: A AAA note...' \ No newline at end of file