Added option style to plantuml config section

This commit is contained in:
Bartek Kryza
2024-03-03 17:01:31 +01:00
parent c4ec8bef8a
commit 76fa811869
34 changed files with 363 additions and 128 deletions

View File

@@ -19,7 +19,8 @@
namespace clanguml::common::generators::mermaid {
std::string to_mermaid(relationship_t r, const std::string & /*style*/)
std::string to_mermaid(
relationship_t r, const std::optional<std::string> & /*style*/)
{
switch (r) {
case relationship_t::kOwnership:

View File

@@ -38,7 +38,8 @@ using clanguml::common::model::element;
using clanguml::common::model::message_t;
using clanguml::common::model::relationship_t;
std::string to_mermaid(relationship_t r, const std::string &style);
std::string to_mermaid(
relationship_t r, const std::optional<std::string> &style);
std::string to_mermaid(access_t scope);
std::string to_mermaid(message_t r);

View File

@@ -19,28 +19,48 @@
namespace clanguml::common::generators::plantuml {
std::string to_plantuml(relationship_t r, const std::string &style)
std::string to_plantuml(const relationship &r, const config::diagram &cfg)
{
switch (r) {
using common::model::relationship_t;
std::string style;
const auto &inline_style = r.style();
if (inline_style && !inline_style.value().empty()) {
if (inline_style && inline_style.value().back() == ']')
style = *inline_style;
else
style = fmt::format("[{}]", inline_style.value());
}
if (style.empty() && cfg.puml) {
if (auto config_style = cfg.puml().get_style(r.type());
config_style.has_value()) {
style = config_style.value();
}
}
switch (r.type()) {
case relationship_t::kOwnership:
case relationship_t::kComposition:
return style.empty() ? "*--" : fmt::format("*-[{}]-", style);
return style.empty() ? "*--" : fmt::format("*-{}-", style);
case relationship_t::kAggregation:
return style.empty() ? "o--" : fmt::format("o-[{}]-", style);
return style.empty() ? "o--" : fmt::format("o-{}-", style);
case relationship_t::kContainment:
return style.empty() ? "--+" : fmt::format("-[{}]-+", style);
return style.empty() ? "--+" : fmt::format("-{}-+", style);
case relationship_t::kAssociation:
return style.empty() ? "-->" : fmt::format("-[{}]->", style);
return style.empty() ? "-->" : fmt::format("-{}->", style);
case relationship_t::kInstantiation:
return style.empty() ? "..|>" : fmt::format(".[{}].|>", style);
return style.empty() ? "..|>" : fmt::format(".{}.|>", style);
case relationship_t::kFriendship:
return style.empty() ? "<.." : fmt::format("<.[{}].", style);
return style.empty() ? "<.." : fmt::format("<.{}.", style);
case relationship_t::kDependency:
return style.empty() ? "..>" : fmt::format(".[{}].>", style);
return style.empty() ? "..>" : fmt::format(".{}.>", style);
case relationship_t::kConstraint:
return style.empty() ? "..>" : fmt::format(".[{}].>", style);
return style.empty() ? "..>" : fmt::format(".{}.>", style);
case relationship_t::kAlias:
return style.empty() ? ".." : fmt::format(".[{}].", style);
return style.empty() ? ".." : fmt::format(".{}.", style);
default:
return "";
}

View File

@@ -19,6 +19,7 @@
#include "common/generators/generator.h"
#include "common/model/diagram_filter.h"
#include "common/model/relationship.h"
#include "config/config.h"
#include "util/error.h"
#include "util/util.h"
@@ -36,9 +37,9 @@ namespace clanguml::common::generators::plantuml {
using clanguml::common::model::access_t;
using clanguml::common::model::element;
using clanguml::common::model::message_t;
using clanguml::common::model::relationship_t;
using clanguml::common::model::relationship;
std::string to_plantuml(relationship_t r, const std::string &style);
std::string to_plantuml(const relationship &r, const config::diagram &cfg);
std::string to_plantuml(access_t scope);
std::string to_plantuml(message_t r);
@@ -124,6 +125,19 @@ public:
void generate_notes(
std::ostream &ostr, const model::element &element) const;
/**
* @brief Generate diagram element PlantUML style
*
* This method renders a style for a specific `el` element if specified
* in the config file or inline comment directive.
*
* @param ostr Output stream
* @param element_type Name of the element type (e.g. "class")
* @param el Reference to a stylable diagram element
*/
void generate_style(std::ostream &ostr, const std::string &element_type,
const model::stylable_element &el) const;
/**
* @brief Generate comment with diagram metadata
*
@@ -437,6 +451,22 @@ void generator<C, D>::generate_plantuml_directives(
}
}
template <typename C, typename D>
void generator<C, D>::generate_style(std::ostream &ostr,
const std::string &element_type, const model::stylable_element &el) const
{
const auto &config = generators::generator<C, D>::config();
if (el.style() && !el.style().value().empty())
ostr << " " << *el.style();
else if (config.puml) {
if (const auto config_style = config.puml().get_style(element_type);
config_style) {
ostr << " " << *config_style;
}
}
}
template <typename C, typename D>
void generator<C, D>::generate_notes(
std::ostream &ostr, const model::element &e) const

View File

@@ -93,16 +93,16 @@ public:
void set_name(const std::string &name) { name_ = name; }
/**
* Return diagram's name.
* Return diagram element name.
*
* @return Diagram's name.
* @return Diagram element name.
*/
std::string name() const { return name_; }
/**
* Return the type name of the diagram.
* Return the type name of the diagram element.
*
* @return Diagrams type name.
* @return Diagrams element type name.
*/
virtual std::string type_name() const { return "__undefined__"; };

View File

@@ -22,6 +22,6 @@ namespace clanguml::common::model {
void stylable_element::set_style(const std::string &style) { style_ = style; }
std::string stylable_element::style() const { return style_; }
std::optional<std::string> stylable_element::style() const { return style_; }
}

View File

@@ -17,6 +17,7 @@
*/
#pragma once
#include <optional>
#include <string>
namespace clanguml::common::model {
@@ -40,10 +41,10 @@ public:
*
* @return Style specification
*/
std::string style() const;
std::optional<std::string> style() const;
private:
std::string style_;
std::optional<std::string> style_;
};
} // namespace clanguml::common::model