Initial mermaid class diagram support

This commit is contained in:
Bartek Kryza
2023-09-05 00:04:05 +02:00
parent e8235805f8
commit 6822930a12
13 changed files with 1747 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,269 @@
/**
* @file src/class_diagram/generators/mermaid/class_diagram_generator.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "class_diagram/model/class.h"
#include "class_diagram/model/concept.h"
#include "class_diagram/model/diagram.h"
#include "class_diagram/model/enum.h"
#include "class_diagram/visitor/translation_unit_visitor.h"
#include "common/generators/mermaid/generator.h"
#include "common/generators/nested_element_stack.h"
#include "common/model/relationship.h"
#include "config/config.h"
#include "util/util.h"
#include <glob/glob.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
namespace clanguml {
namespace class_diagram {
namespace generators {
namespace mermaid {
using diagram_config = clanguml::config::class_diagram;
using diagram_model = clanguml::class_diagram::model::diagram;
template <typename C, typename D>
using common_generator = clanguml::common::generators::mermaid::generator<C, D>;
using clanguml::class_diagram::model::class_;
using clanguml::class_diagram::model::class_element;
using clanguml::class_diagram::model::class_member;
using clanguml::class_diagram::model::class_method;
using clanguml::class_diagram::model::concept_;
using clanguml::class_diagram::model::enum_;
using clanguml::common::model::access_t;
using clanguml::common::model::package;
using clanguml::common::model::relationship;
using clanguml::common::model::relationship_t;
using namespace clanguml::util;
/**
* @brief Class diagram MermaidJS generator
*/
class generator : public common_generator<diagram_config, diagram_model> {
using method_groups_t = std::map<std::string, std::vector<class_method>>;
public:
generator(diagram_config &config, diagram_model &model);
using common_generator<diagram_config, diagram_model>::generate;
/**
* @brief Main generator method.
*
* This method is called first and coordinates the entire diagram
* generation.
*
* @param ostr Output stream.
*/
void generate_diagram(std::ostream &ostr) const override;
/**
* @brief In a nested diagram, generate the top level elements.
*
* This method iterates over the top level elements. In case the diagram
* is nested (i.e. includes packages), for each package it recursively
* call generation of elements contained in each package.
*
* @param parent JSON node
*/
void generate_top_level_elements(std::ostream &ostr) const;
/**
* @brief Generate PlantUML alias for a class element.
*
* @param c Class element
* @param ostr Output stream
*/
void generate_alias(
const common::model::element &e, std::ostream &ostr) const;
/**
* @brief Render class element to PlantUML
*
* @param c Class element
* @param ostr Output stream
*/
void generate(const class_ &c, std::ostream &ostr) const;
/**
* @brief Render class methods to PlantUML
*
* @param methods List of class methods
* @param ostr Output stream
*/
void generate_methods(
const std::vector<class_method> &methods, std::ostream &ostr) const;
/**
* @brief Render class methods to PlantUML in groups
*
* @param methods Methods grouped by method type
* @param ostr Output stream
*/
void generate_methods(
const method_groups_t &methods, std::ostream &ostr) const;
/**
* @brief Render class method to PlantUML
*
* @param m Class method
* @param ostr Output stream
*/
void generate_method(const class_method &m, std::ostream &ostr) const;
/**
* @brief Render class member to PlantUML
*
* @param m Class member
* @param ostr Output stream
*/
void generate_member(const class_member &m, std::ostream &ostr) const;
/**
* @brief Render all relationships in the diagram to PlantUML
*
* @param ostr Output stream
*/
void generate_relationships(std::ostream &ostr) const;
/**
* @brief Render all relationships originating from class element.
*
* @param c Class element
* @param ostr Output stream
*/
void generate_relationships(const class_ &c, std::ostream &ostr) const;
/**
* @brief Render a specific relationship to PlantUML.
*
* @param r Relationship model
* @param rendered_relations Set of already rendered relationships, to
* ensure that there are no duplicate
* relationships
*/
void generate_relationship(
const relationship &r, std::set<std::string> &rendered_relations) const;
/**
* @brief Render enum element to PlantUML
*
* @param e Enum element
* @param ostr Output stream
*/
void generate(const enum_ &e, std::ostream &ostr) const;
/**
* @brief Render all relationships originating from enum element.
*
* @param c Enum element
* @param ostr Output stream
*/
void generate_relationships(const enum_ &c, std::ostream &ostr) const;
/**
* @brief Render concept element to PlantUML
*
* @param c Concept element
* @param ostr Output stream
*/
void generate(const concept_ &c, std::ostream &ostr) const;
/**
* @brief Render all relationships originating from concept element.
*
* @param c Concept element
* @param ostr Output stream
*/
void generate_relationships(const concept_ &c, std::ostream &ostr) const;
/**
* @brief Render package element to PlantUML
*
* @param p Package element
* @param ostr Output stream
*/
void generate(const package &p, std::ostream &ostr) const;
/**
* @brief Render all relationships originating from package element.
*
* @param p Package element
* @param ostr Output stream
*/
void generate_relationships(const package &p, std::ostream &ostr) const;
/**
* @brief Generate any notes attached specifically to some class element.
*
* @param ostream Output stream
* @param member Class element (member or method)
* @param alias PlantUML class alias
*/
void generate_member_notes(std::ostream &ostream,
const class_element &member, const std::string &alias) const;
/**
* @brief Generate elements grouped together in `together` groups.
*
* @param ostr Output stream
*/
void generate_groups(std::ostream &ostr) const;
/**
* @brief Group class methods based on method type.
*
* @param methods List of class methods.
*
* @return Map of method groups.
*/
method_groups_t group_methods(
const std::vector<class_method> &methods) const;
private:
const std::vector<std::string> method_groups_{
"constructors", "assignment", "operators", "other"};
std::string render_name(std::string name) const;
template <typename T>
void sort_class_elements(std::vector<T> &elements) const
{
if (config().member_order() == config::member_order_t::lexical) {
std::sort(elements.begin(), elements.end(),
[](const auto &m1, const auto &m2) {
return m1.name() < m2.name();
});
}
}
mutable common::generators::nested_element_stack<common::model::element>
together_group_stack_;
};
} // namespace mermaid
} // namespace generators
} // namespace class_diagram
} // namespace clanguml

View File

@@ -19,6 +19,7 @@
#include "class_diagram/generators/json/class_diagram_generator.h"
#include "class_diagram/generators/plantuml/class_diagram_generator.h"
#include "class_diagram/generators/mermaid/class_diagram_generator.h"
#include "cli/cli_handler.h"
#include "common/compilation_database.h"
#include "common/generators/generators.h"
@@ -105,6 +106,9 @@ struct plantuml_generator_tag {
struct json_generator_tag {
inline static const std::string extension = "json";
};
struct mermaid_generator_tag {
inline static const std::string extension = "mmd";
};
/** @} */
/** @defgroup diagram_generator_t Diagram generator selector
@@ -114,6 +118,7 @@ struct json_generator_tag {
*
* @{
*/
// plantuml
template <typename DiagramConfig, typename GeneratorType>
struct diagram_generator_t;
template <>
@@ -136,6 +141,7 @@ struct diagram_generator_t<clanguml::config::include_diagram,
plantuml_generator_tag> {
using type = clanguml::include_diagram::generators::plantuml::generator;
};
// json
template <>
struct diagram_generator_t<clanguml::config::class_diagram,
json_generator_tag> {
@@ -156,6 +162,27 @@ struct diagram_generator_t<clanguml::config::include_diagram,
json_generator_tag> {
using type = clanguml::include_diagram::generators::json::generator;
};
// mermaid
template <>
struct diagram_generator_t<clanguml::config::class_diagram,
mermaid_generator_tag> {
using type = clanguml::class_diagram::generators::mermaid::generator;
};
//template <>
//struct diagram_generator_t<clanguml::config::sequence_diagram,
// mermaid_generator_tag> {
// using type = clanguml::sequence_diagram::generators::mermaid::generator;
//};
//template <>
//struct diagram_generator_t<clanguml::config::package_diagram,
// mermaid_generator_tag> {
// using type = clanguml::package_diagram::generators::mermaid::generator;
//};
//template <>
//struct diagram_generator_t<clanguml::config::include_diagram,
// mermaid_generator_tag> {
// using type = clanguml::include_diagram::generators::mermaid::generator;
//};
/** @} */
/**

View File

@@ -0,0 +1,75 @@
/**
* @file src/common/generators/mermaid/generator.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "generator.h"
namespace clanguml::common::generators::mermaid {
std::string to_mermaid(relationship_t r, const std::string &style)
{
switch (r) {
case relationship_t::kOwnership:
case relationship_t::kComposition:
return style.empty() ? "*--" : fmt::format("*-[{}]-", style);
case relationship_t::kAggregation:
return style.empty() ? "o--" : fmt::format("o-[{}]-", style);
case relationship_t::kContainment:
return style.empty() ? "--" : fmt::format("-[{}]-", style);
case relationship_t::kAssociation:
return style.empty() ? "-->" : fmt::format("-[{}]->", style);
case relationship_t::kInstantiation:
return style.empty() ? "..|>" : fmt::format(".[{}].|>", style);
case relationship_t::kFriendship:
return style.empty() ? "<.." : fmt::format("<.[{}].", style);
case relationship_t::kDependency:
return style.empty() ? "..>" : fmt::format(".[{}].>", style);
case relationship_t::kConstraint:
return style.empty() ? "..>" : fmt::format(".[{}].>", style);
case relationship_t::kAlias:
return style.empty() ? ".." : fmt::format(".[{}].", style);
default:
return "";
}
}
std::string to_mermaid(access_t scope)
{
switch (scope) {
case access_t::kPublic:
return "+";
case access_t::kProtected:
return "#";
case access_t::kPrivate:
return "-";
default:
return "";
}
}
std::string to_mermaid(message_t r)
{
switch (r) {
case message_t::kCall:
return "->";
case message_t::kReturn:
return "-->";
default:
return "";
}
}
} // namespace clanguml::common::generators::mermaid

View File

@@ -0,0 +1,454 @@
/**
* @file src/common/generators/mermaid/generator.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/generators/generator.h"
#include "common/model/diagram_filter.h"
#include "config/config.h"
#include "util/error.h"
#include "util/util.h"
#include "version.h"
#include <clang/Basic/Version.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Tooling/CompilationDatabase.h>
#include <clang/Tooling/Tooling.h>
#include <glob/glob.hpp>
#include <inja/inja.hpp>
namespace clanguml::common::generators::mermaid {
using clanguml::common::model::access_t;
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(access_t scope);
std::string to_mermaid(message_t r);
/**
* @brief Base class for diagram generators
*
* @tparam ConfigType Configuration type
* @tparam DiagramType Diagram model type
*/
template <typename ConfigType, typename DiagramType>
class generator
: public clanguml::common::generators::generator<ConfigType, DiagramType> {
public:
/**
* @brief Constructor
*
* @param config Reference to instance of @link clanguml::config::diagram
* @param model Reference to instance of @link clanguml::model::diagram
*/
generator(ConfigType &config, DiagramType &model)
: clanguml::common::generators::generator<ConfigType, DiagramType>{
config, model}
{
init_context();
init_env();
}
~generator() override = default;
/**
* @brief Generate diagram
*
* This is the main diagram generation entrypoint. It is responsible for
* calling other methods in appropriate order to generate the diagram into
* the output stream. It generates diagram elements, that are common
* to all types of diagrams in a given generator.
*
* @param ostr Output stream
*/
void generate(std::ostream &ostr) const override;
/**
* @brief Generate diagram specific part
*
* This method must be implemented in subclasses for specific diagram
* types.
*
* @param ostr Output stream
*/
virtual void generate_diagram(std::ostream &ostr) const = 0;
/**
* @brief Generate diagram layout hints
*
* This method adds to the diagram any layout hints that were provided
* in the configuration file.
*
* @param ostr Output stream
*/
void generate_config_layout_hints(std::ostream &ostr) const;
/**
* @brief Generate MermaidJS directives from config file.
*
* This method renders the MermaidJS directives provided in the configuration
* file, including resolving any element aliases and Jinja templates.
*
* @param ostr Output stream
* @param directives List of directives from the configuration file
*/
void generate_mermaid_directives(
std::ostream &ostr, const std::vector<std::string> &directives) const;
/**
* @brief Generate diagram notes
*
* This method adds any notes in the diagram, which were declared in the
* code using inline directives
*
* @param ostr Output stream
* @param element Element to which the note should be attached
*/
void generate_notes(
std::ostream &ostr, const model::element &element) const;
/**
* @brief Generate comment with diagram metadata
*
* @param ostr Output stream
*/
void generate_metadata(std::ostream &ostr) const;
/**
* @brief Generate hyper link to element
*
* This method renders links to URL's based on templates provided
* in the configuration file (e.g. Git browser with specific line and
* column offset)
*
* @param ostr Output stream
* @param e Reference to diagram element
* @tparam E Diagram element type
*/
template <typename E>
void generate_link(std::ostream &ostr, const E &e) const;
/**
* @brief Print debug information in diagram comments
*
* @param m Diagram element to describe
* @param ostr Output stream
*/
void print_debug(
const common::model::source_location &e, std::ostream &ostr) const;
/**
* @brief Update diagram Jinja context
*
* This method updates the diagram context with models properties
* which can be used to render Jinja templates in the diagram (e.g.
* in notes or links)
*/
void update_context() const;
protected:
const inja::json &context() const;
inja::Environment &env() const;
template <typename E> inja::json element_context(const E &e) const;
private:
void init_context();
void init_env();
protected:
mutable std::set<std::string> m_generated_aliases;
mutable inja::json m_context;
mutable inja::Environment m_env;
};
template <typename C, typename D>
const inja::json &generator<C, D>::context() const
{
return m_context;
}
template <typename C, typename D>
inja::Environment &generator<C, D>::env() const
{
return m_env;
}
template <typename C, typename D>
template <typename E>
inja::json generator<C, D>::element_context(const E &e) const
{
auto ctx = context();
ctx["element"] = e.context();
if (!e.file().empty()) {
std::filesystem::path file{e.file()};
std::string relative_path = file.string();
#if _MSC_VER
if (file.is_absolute() && ctx.contains("git"))
#else
if (file.is_absolute() && ctx.template contains("git"))
#endif
relative_path =
std::filesystem::relative(file, ctx["git"]["toplevel"])
.string();
ctx["element"]["source"]["path"] = util::path_to_url(relative_path);
ctx["element"]["source"]["full_path"] = file.string();
ctx["element"]["source"]["name"] = file.filename().string();
ctx["element"]["source"]["line"] = e.line();
}
const auto maybe_comment = e.comment();
if (maybe_comment) {
ctx["element"]["comment"] = maybe_comment.value();
}
return ctx;
}
template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
update_context();
// generate_mermaid_diagram_type(ostr, config);
generate_mermaid_directives(ostr, config.puml().before);
generate_diagram(ostr);
generate_mermaid_directives(ostr, config.puml().after);
generate_metadata(ostr);
}
template <typename C, typename D>
void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
{
using namespace clanguml::util;
const auto &config = generators::generator<C, D>::config();
// Generate layout hints
for (const auto &[entity_name, hints] : config.layout()) {
for (const auto &hint : hints) {
try {
if (hint.hint == config::hint_t::together) {
// 'together' layout hint is handled separately
}
else if (hint.hint == config::hint_t::row ||
hint.hint == config::hint_t::column) {
generate_row_column_hints(ostr, entity_name, hint);
}
else {
generate_position_hints(ostr, entity_name, hint);
}
}
catch (clanguml::error::uml_alias_missing &e) {
LOG_DBG("=== Skipping layout hint '{}' from {} due "
"to: {}",
to_string(hint.hint), entity_name, e.what());
}
}
}
}
template <typename C, typename D>
void generator<C, D>::generate_mermaid_directives(
std::ostream &ostr, const std::vector<std::string> &directives) const
{
}
template <typename C, typename D>
void generator<C, D>::generate_notes(
std::ostream &ostr, const model::element &e) const
{
// const auto &config = generators::generator<C, D>::config();
//
// for (const auto &decorator : e.decorators()) {
// auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
// if (note && note->applies_to_diagram(config.name)) {
// ostr << "note " << note->position << " of " << e.alias() << '\n'
// << note->text << '\n'
// << "end note\n";
// }
// }
}
template <typename C, typename D>
void generator<C, D>::generate_metadata(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
if (config.generate_metadata()) {
ostr << '\n'
<< " %% Generated with clang-uml, version "
<< clanguml::version::CLANG_UML_VERSION << '\n'
<< " %% LLVM version " << clang::getClangFullVersion() << '\n';
}
}
template <typename C, typename D>
void generator<C, D>::print_debug(
const common::model::source_location &e, std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
if (config.debug_mode())
ostr << " %% " << e.file() << ":" << e.line() << '\n';
}
template <typename DiagramModel, typename DiagramConfig>
std::ostream &operator<<(
std::ostream &os, const generator<DiagramModel, DiagramConfig> &g)
{
g.generate(os);
return os;
}
template <typename C, typename D> void generator<C, D>::init_context()
{
const auto &config = generators::generator<C, D>::config();
if (config.git) {
m_context["git"]["branch"] = config.git().branch;
m_context["git"]["revision"] = config.git().revision;
m_context["git"]["commit"] = config.git().commit;
m_context["git"]["toplevel"] = config.git().toplevel;
}
}
template <typename C, typename D> void generator<C, D>::update_context() const
{
m_context["diagram"] = generators::generator<C, D>::model().context();
}
template <typename C, typename D> void generator<C, D>::init_env()
{
const auto &model = generators::generator<C, D>::model();
const auto &config = generators::generator<C, D>::config();
//
// Add basic string functions to inja environment
//
// Check if string is empty
m_env.add_callback("empty", 1, [](inja::Arguments &args) {
return args.at(0)->get<std::string>().empty();
});
// Remove spaces from the left of a string
m_env.add_callback("ltrim", 1, [](inja::Arguments &args) {
return util::ltrim(args.at(0)->get<std::string>());
});
// Remove trailing spaces from a string
m_env.add_callback("rtrim", 1, [](inja::Arguments &args) {
return util::rtrim(args.at(0)->get<std::string>());
});
// Remove spaces before and after a string
m_env.add_callback("trim", 1, [](inja::Arguments &args) {
return util::trim(args.at(0)->get<std::string>());
});
// Make a string shorted with a limit to
m_env.add_callback("abbrv", 2, [](inja::Arguments &args) {
return util::abbreviate(
args.at(0)->get<std::string>(), args.at(1)->get<unsigned>());
});
m_env.add_callback("replace", 3, [](inja::Arguments &args) {
std::string result = args[0]->get<std::string>();
std::regex pattern(args[1]->get<std::string>());
return std::regex_replace(result, pattern, args[2]->get<std::string>());
});
m_env.add_callback("split", 2, [](inja::Arguments &args) {
return util::split(
args[0]->get<std::string>(), args[1]->get<std::string>());
});
//
// Add PlantUML specific functions
//
// Return the entire element JSON context based on element name
// e.g.:
// {{ element("clanguml::t00050::A").comment }}
//
m_env.add_callback("element", 1, [&model, &config](inja::Arguments &args) {
inja::json res{};
auto element_opt = model.get_with_namespace(
args[0]->get<std::string>(), config.using_namespace());
if (element_opt.has_value())
res = element_opt.value().context();
return res;
});
// Convert C++ entity to PlantUML alias, e.g.
// "note left of {{ alias("A") }}: This is a note"
// Shortcut to:
// {{ element("A").alias }}
//
m_env.add_callback("alias", 1, [&model, &config](inja::Arguments &args) {
auto element_opt = model.get_with_namespace(
args[0]->get<std::string>(), config.using_namespace());
if (!element_opt.has_value())
throw clanguml::error::uml_alias_missing(
args[0]->get<std::string>());
return element_opt.value().alias();
});
// Get elements' comment:
// "note left of {{ alias("A") }}: {{ comment("A") }}"
// Shortcut to:
// {{ element("A").comment }}
//
m_env.add_callback("comment", 1, [&model, &config](inja::Arguments &args) {
inja::json res{};
auto element_opt = model.get_with_namespace(
args[0]->get<std::string>(), config.using_namespace());
if (!element_opt.has_value())
throw clanguml::error::uml_alias_missing(
args[0]->get<std::string>());
auto comment = element_opt.value().comment();
if (comment.has_value()) {
assert(comment.value().is_object());
res = comment.value();
}
return res;
});
}
} // namespace clanguml::common::generators::mermaid

View File

@@ -36,7 +36,8 @@ using id_t = int64_t;
*/
enum class generator_type_t {
plantuml, /*!< Diagrams will be gnerated in PlantUML format */
json /*!< Diagrams will be generated in JSON format */
json, /*!< Diagrams will be generated in JSON format */
mermaid /*!< Diagrams will be generated in MermaidJS format */
};
std::string to_string(const std::string &s);

View File

@@ -172,6 +172,12 @@ void plantuml::append(const plantuml &r)
after.insert(after.end(), r.after.begin(), r.after.end());
}
void mermaid::append(const mermaid &r)
{
before.insert(before.end(), r.before.begin(), r.before.end());
after.insert(after.end(), r.after.begin(), r.after.end());
}
void inheritable_diagram_options::inherit(
const inheritable_diagram_options &parent)
{

View File

@@ -125,6 +125,22 @@ struct plantuml {
void append(const plantuml &r);
};
/**
* @brief MermaidJS diagram config section
*
* This configuration option can be used to add any MermaidJS directives
* before or after the generated diagram, such as diagram name, or custom
* notes.
*/
struct mermaid {
/*! List of directives to add before diagram */
std::vector<std::string> before;
/*! List of directives to add before diagram */
std::vector<std::string> after;
void append(const mermaid &r);
};
/**
* @brief Definition of diagram template
*/
@@ -430,6 +446,7 @@ struct inheritable_diagram_options {
option<filter> include{"include"};
option<filter> exclude{"exclude"};
option<plantuml> puml{"plantuml", option_inherit_mode::kAppend};
option<struct mermaid> mermaid{"mermaid", option_inherit_mode::kAppend};
option<method_arguments> generate_method_arguments{
"generate_method_arguments", method_arguments::full};
option<bool> group_methods{"group_methods", true};

View File

@@ -107,4 +107,10 @@ TEST_CASE("t00002", "[test-case][class]")
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
{
auto mmd = generate_class_mermaid(diagram, *model);
save_puml(
config.output_directory() + "/" + diagram->name + ".mmd", mmd);
}
}

View File

@@ -95,4 +95,10 @@ TEST_CASE("t00003", "[test-case][class]")
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
{
auto mmd = generate_class_mermaid(diagram, *model);
save_puml(
config.output_directory() + "/" + diagram->name + ".mmd", mmd);
}
}

View File

@@ -91,4 +91,10 @@ TEST_CASE("t00004", "[test-case][class]")
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
{
auto mmd = generate_class_mermaid(diagram, *model);
save_puml(
config.output_directory() + "/" + diagram->name + ".mmd", mmd);
}
}

View File

@@ -111,6 +111,24 @@ auto generate_diagram_json(
return nlohmann::json::parse(ss.str());
}
template <typename DiagramConfig, typename DiagramModel>
auto generate_diagram_mermaid(
std::shared_ptr<clanguml::config::diagram> config, DiagramModel &model)
{
using diagram_config = DiagramConfig;
using diagram_model = DiagramModel;
using diagram_generator =
typename clanguml::common::generators::diagram_generator_t<
DiagramConfig,
clanguml::common::generators::mermaid_generator_tag>::type;
std::stringstream ss;
ss << diagram_generator(dynamic_cast<diagram_config &>(*config), model);
return ss.str();
}
}
std::unique_ptr<clanguml::class_diagram::model::diagram> generate_class_diagram(
@@ -209,6 +227,14 @@ nlohmann::json generate_include_json(
config, model);
}
std::string generate_class_mermaid(
std::shared_ptr<clanguml::config::diagram> config,
clanguml::class_diagram::model::diagram &model)
{
return detail::generate_diagram_mermaid<clanguml::config::class_diagram>(
config, model);
}
void save_puml(const std::string &path, const std::string &puml)
{
std::filesystem::path p{path};
@@ -229,6 +255,17 @@ void save_json(const std::string &path, const nlohmann::json &j)
ofs.close();
}
void save_mermaid(const std::string &path, const std::string &mmd)
{
std::filesystem::path p{path};
std::filesystem::create_directory(p.parent_path());
std::ofstream ofs;
ofs.open(p, std::ofstream::out | std::ofstream::trunc);
ofs << mmd;
ofs.close();
}
using namespace clanguml::test::matchers;
///

View File

@@ -20,6 +20,7 @@
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include "class_diagram/generators/plantuml/class_diagram_generator.h"
#include "class_diagram/generators/mermaid/class_diagram_generator.h"
#include "class_diagram/model/diagram.h"
#include "class_diagram/visitor/translation_unit_visitor.h"
#include "common/clang_utils.h"