Refactored inja context evaluation and link rendering
This commit is contained in:
@@ -38,16 +38,24 @@ void generator::generate_link(
|
|||||||
|
|
||||||
auto context = element_context(e);
|
auto context = element_context(e);
|
||||||
|
|
||||||
if (!config().generate_links().link.empty()) {
|
auto maybe_link_pattern = get_link_pattern(e);
|
||||||
|
if (maybe_link_pattern) {
|
||||||
|
const auto &[link_prefix, link_pattern] = *maybe_link_pattern;
|
||||||
|
auto ec = element_context(e);
|
||||||
|
common::generators::make_context_source_relative(ec, link_prefix);
|
||||||
|
|
||||||
ostr << " [[[";
|
ostr << " [[[";
|
||||||
ostr << env().render(
|
ostr << env().render(std::string_view{link_pattern}, context);
|
||||||
std::string_view{config().generate_links().link}, context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config().generate_links().tooltip.empty()) {
|
auto maybe_tooltip_pattern = get_tooltip_pattern(e);
|
||||||
|
|
||||||
|
if (maybe_tooltip_pattern) {
|
||||||
|
const auto &[tooltip_prefix, tooltip_pattern] = *maybe_tooltip_pattern;
|
||||||
|
auto ec = element_context(e);
|
||||||
|
common::generators::make_context_source_relative(ec, tooltip_prefix);
|
||||||
ostr << "{";
|
ostr << "{";
|
||||||
ostr << env().render(
|
ostr << env().render(std::string_view{tooltip_pattern}, ec);
|
||||||
std::string_view{config().generate_links().tooltip}, context);
|
|
||||||
ostr << "}";
|
ostr << "}";
|
||||||
}
|
}
|
||||||
ostr << "]]]";
|
ostr << "]]]";
|
||||||
|
|||||||
@@ -17,10 +17,22 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "common/model/source_location.h"
|
||||||
|
#include "util/error.h"
|
||||||
|
#include "util/util.h"
|
||||||
|
|
||||||
|
#include <inja/inja.hpp>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <regex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace clanguml::common::generators {
|
namespace clanguml::common::generators {
|
||||||
|
|
||||||
|
void make_context_source_relative(
|
||||||
|
inja::json &context, const std::string &prefix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Common diagram generator interface
|
* @brief Common diagram generator interface
|
||||||
*
|
*
|
||||||
@@ -38,6 +50,8 @@ public:
|
|||||||
: config_{config}
|
: config_{config}
|
||||||
, model_{model}
|
, model_{model}
|
||||||
{
|
{
|
||||||
|
init_context();
|
||||||
|
init_env();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~generator() = default;
|
virtual ~generator() = default;
|
||||||
@@ -68,9 +82,240 @@ public:
|
|||||||
*/
|
*/
|
||||||
const DiagramType &model() const { return model_; }
|
const DiagramType &model() const { return model_; }
|
||||||
|
|
||||||
|
template <typename E> inja::json element_context(const E &e) const;
|
||||||
|
|
||||||
|
std::optional<std::pair<std::string, std::string>> get_link_pattern(
|
||||||
|
const common::model::source_location &sl) const
|
||||||
|
{
|
||||||
|
if (sl.file_relative().empty()) {
|
||||||
|
return config().generate_links().get_link_pattern(sl.file());
|
||||||
|
}
|
||||||
|
|
||||||
|
return config().generate_links().get_link_pattern(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::pair<std::string, std::string>> get_tooltip_pattern(
|
||||||
|
const common::model::source_location &sl) const
|
||||||
|
{
|
||||||
|
if (sl.file_relative().empty()) {
|
||||||
|
return config().generate_links().get_tooltip_pattern(sl.file());
|
||||||
|
}
|
||||||
|
|
||||||
|
return config().generate_links().get_tooltip_pattern(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_context();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
|
||||||
|
void init_env();
|
||||||
|
|
||||||
|
const inja::json &context() const;
|
||||||
|
|
||||||
|
inja::Environment &env() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
mutable inja::json m_context;
|
||||||
|
mutable inja::Environment m_env;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ConfigType &config_;
|
ConfigType &config_;
|
||||||
DiagramType &model_;
|
DiagramType &model_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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"] = model().context();
|
||||||
|
}
|
||||||
|
|
||||||
|
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> 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename C, typename D>
|
||||||
|
template <typename E>
|
||||||
|
inja::json generator<C, D>::element_context(const E &e) const
|
||||||
|
{
|
||||||
|
const auto &diagram_context = context();
|
||||||
|
|
||||||
|
inja::json ctx;
|
||||||
|
ctx["element"] = e.context();
|
||||||
|
#if _MSC_VER
|
||||||
|
if (ctx.contains("git")) {
|
||||||
|
#else
|
||||||
|
if (diagram_context.template contains("git")) {
|
||||||
|
#endif
|
||||||
|
ctx["git"] = diagram_context["git"];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!e.file().empty()) {
|
||||||
|
std::filesystem::path file{e.file()};
|
||||||
|
std::string git_relative_path = file.string();
|
||||||
|
if (!e.file_relative().empty()) {
|
||||||
|
#if _MSC_VER
|
||||||
|
if (file.is_absolute() && ctx.contains("git")) {
|
||||||
|
#else
|
||||||
|
if (file.is_absolute() &&
|
||||||
|
diagram_context.template contains("git")) {
|
||||||
|
#endif
|
||||||
|
git_relative_path = std::filesystem::relative(
|
||||||
|
file, diagram_context["git"]["toplevel"])
|
||||||
|
.string();
|
||||||
|
ctx["element"]["source"]["path"] =
|
||||||
|
util::path_to_url(git_relative_path);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ctx["element"]["source"]["path"] = e.file();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
git_relative_path = "";
|
||||||
|
ctx["element"]["source"]["path"] = e.file();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
} // namespace clanguml::common::generators
|
} // namespace clanguml::common::generators
|
||||||
@@ -21,6 +21,28 @@
|
|||||||
#include "progress_indicator.h"
|
#include "progress_indicator.h"
|
||||||
|
|
||||||
namespace clanguml::common::generators {
|
namespace clanguml::common::generators {
|
||||||
|
void make_context_source_relative(
|
||||||
|
inja::json &context, const std::string &prefix)
|
||||||
|
{
|
||||||
|
if (!context.contains("element"))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!context["element"].contains("source"))
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto &source = context["element"]["source"];
|
||||||
|
|
||||||
|
if (source.at("path").empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto path = std::filesystem::path(source.at("path"));
|
||||||
|
auto prefix_path = std::filesystem::path(prefix);
|
||||||
|
if (path.is_absolute() && util::is_relative_to(path, prefix_path)) {
|
||||||
|
source["path"] = relative(path, prefix_path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void find_translation_units_for_diagrams(
|
void find_translation_units_for_diagrams(
|
||||||
const std::vector<std::string> &diagram_names,
|
const std::vector<std::string> &diagram_names,
|
||||||
clanguml::config::config &config,
|
clanguml::config::config &config,
|
||||||
|
|||||||
@@ -67,8 +67,6 @@ public:
|
|||||||
: clanguml::common::generators::generator<ConfigType, DiagramType>{
|
: clanguml::common::generators::generator<ConfigType, DiagramType>{
|
||||||
config, model}
|
config, model}
|
||||||
{
|
{
|
||||||
init_context();
|
|
||||||
init_env();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~generator() override = default;
|
~generator() override = default;
|
||||||
@@ -169,84 +167,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
void print_debug(
|
void print_debug(
|
||||||
const common::model::source_location &e, std::ostream &ostr) const;
|
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:
|
protected:
|
||||||
mutable std::set<std::string> m_generated_aliases;
|
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 git_relative_path = file.string();
|
|
||||||
if (!e.file_relative().empty()) {
|
|
||||||
#if _MSC_VER
|
|
||||||
if (file.is_absolute() && ctx.contains("git"))
|
|
||||||
#else
|
|
||||||
if (file.is_absolute() && ctx.template contains("git"))
|
|
||||||
#endif
|
|
||||||
git_relative_path =
|
|
||||||
std::filesystem::relative(file, ctx["git"]["toplevel"])
|
|
||||||
.string();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
git_relative_path = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx["element"]["source"]["path"] = util::path_to_url(git_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>
|
template <typename C, typename D>
|
||||||
void generator<C, D>::generate(std::ostream &ostr) const
|
void generator<C, D>::generate(std::ostream &ostr) const
|
||||||
{
|
{
|
||||||
@@ -259,7 +184,7 @@ void generator<C, D>::generate(std::ostream &ostr) const
|
|||||||
"Diagram configuration resulted in empty diagram."};
|
"Diagram configuration resulted in empty diagram."};
|
||||||
}
|
}
|
||||||
|
|
||||||
update_context();
|
generators::generator<C, D>::update_context();
|
||||||
|
|
||||||
generate_title(ostr);
|
generate_title(ostr);
|
||||||
|
|
||||||
@@ -278,55 +203,66 @@ template <typename C, typename D>
|
|||||||
template <typename E>
|
template <typename E>
|
||||||
void generator<C, D>::generate_link(std::ostream &ostr, const E &e) const
|
void generator<C, D>::generate_link(std::ostream &ostr, const E &e) const
|
||||||
{
|
{
|
||||||
const auto &config = generators::generator<C, D>::config();
|
if (e.file().empty() && e.file_relative().empty())
|
||||||
|
|
||||||
if (e.file().empty())
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (config.generate_links().link.empty() &&
|
auto maybe_link_pattern = generators::generator<C, D>::get_link_pattern(e);
|
||||||
config.generate_links().tooltip.empty())
|
|
||||||
|
if (!maybe_link_pattern)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
const auto &[link_prefix, link_pattern] = *maybe_link_pattern;
|
||||||
|
|
||||||
ostr << indent(1) << "click " << e.alias() << " href \"";
|
ostr << indent(1) << "click " << e.alias() << " href \"";
|
||||||
try {
|
try {
|
||||||
|
auto ec = generators::generator<C, D>::element_context(e);
|
||||||
|
common::generators::make_context_source_relative(ec, link_prefix);
|
||||||
std::string link{};
|
std::string link{};
|
||||||
if (!config.generate_links().link.empty()) {
|
if (!link_pattern.empty()) {
|
||||||
link = env().render(std::string_view{config.generate_links().link},
|
link = generators::generator<C, D>::env().render(
|
||||||
element_context(e));
|
std::string_view{link_pattern}, ec);
|
||||||
}
|
}
|
||||||
if (link.empty())
|
if (link.empty())
|
||||||
link = " ";
|
link = " ";
|
||||||
ostr << link;
|
ostr << link;
|
||||||
}
|
}
|
||||||
catch (const inja::json::parse_error &e) {
|
catch (const inja::json::parse_error &e) {
|
||||||
LOG_ERROR(
|
LOG_ERROR("Failed to parse Jinja template: {}", link_pattern);
|
||||||
"Failed to parse Jinja template: {}", config.generate_links().link);
|
|
||||||
ostr << " ";
|
ostr << " ";
|
||||||
}
|
}
|
||||||
catch (const inja::json::exception &e) {
|
catch (const inja::json::exception &e) {
|
||||||
LOG_ERROR("Failed to render comment directive: \n{}\n due to: {}",
|
LOG_ERROR("Failed to render comment directive: \n{}\n due to: {}",
|
||||||
config.generate_links().link, e.what());
|
link_pattern, e.what());
|
||||||
ostr << " ";
|
ostr << " ";
|
||||||
}
|
}
|
||||||
ostr << "\"";
|
ostr << "\"";
|
||||||
|
|
||||||
if (!config.generate_links().tooltip.empty()) {
|
auto maybe_tooltip_pattern =
|
||||||
|
generators::generator<C, D>::get_tooltip_pattern(e);
|
||||||
|
|
||||||
|
if (!maybe_tooltip_pattern)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto &[tooltip_prefix, tooltip_pattern] = *maybe_tooltip_pattern;
|
||||||
|
|
||||||
|
if (!tooltip_pattern.empty()) {
|
||||||
ostr << " \"";
|
ostr << " \"";
|
||||||
try {
|
try {
|
||||||
auto tooltip_text =
|
auto ec = generators::generator<C, D>::element_context(e);
|
||||||
env().render(std::string_view{config.generate_links().tooltip},
|
common::generators::make_context_source_relative(
|
||||||
element_context(e));
|
ec, tooltip_prefix);
|
||||||
|
auto tooltip_text = generators::generator<C, D>::env().render(
|
||||||
|
std::string_view{tooltip_pattern}, ec);
|
||||||
util::replace_all(tooltip_text, "\"", "„");
|
util::replace_all(tooltip_text, "\"", "„");
|
||||||
ostr << tooltip_text;
|
ostr << tooltip_text;
|
||||||
}
|
}
|
||||||
catch (const inja::json::parse_error &e) {
|
catch (const inja::json::parse_error &e) {
|
||||||
LOG_ERROR("Failed to parse Jinja template: {}",
|
LOG_ERROR("Failed to parse Jinja template: {}", tooltip_pattern);
|
||||||
config.generate_links().link);
|
|
||||||
ostr << " ";
|
ostr << " ";
|
||||||
}
|
}
|
||||||
catch (const inja::json::exception &e) {
|
catch (const inja::json::exception &e) {
|
||||||
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
|
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
|
||||||
config.generate_links().link, e.what());
|
tooltip_pattern, e.what());
|
||||||
ostr << " ";
|
ostr << " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,7 +284,8 @@ void generator<C, D>::generate_mermaid_directives(
|
|||||||
for (const auto &d : directives) {
|
for (const auto &d : directives) {
|
||||||
try {
|
try {
|
||||||
// Render the directive with template engine first
|
// Render the directive with template engine first
|
||||||
std::string directive{env().render(std::string_view{d}, context())};
|
std::string directive{generators::generator<C, D>::env().render(
|
||||||
|
std::string_view{d}, generators::generator<C, D>::context())};
|
||||||
|
|
||||||
// Now search for alias `@A()` directives in the text
|
// Now search for alias `@A()` directives in the text
|
||||||
// (this is deprecated)
|
// (this is deprecated)
|
||||||
@@ -453,127 +390,4 @@ std::ostream &operator<<(
|
|||||||
g.generate(os);
|
g.generate(os);
|
||||||
return 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 MermaidJS 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 MermaidJS 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
|
} // namespace clanguml::common::generators::mermaid
|
||||||
@@ -21,7 +21,6 @@
|
|||||||
#include "common/model/filters/diagram_filter.h"
|
#include "common/model/filters/diagram_filter.h"
|
||||||
#include "common/model/relationship.h"
|
#include "common/model/relationship.h"
|
||||||
#include "config/config.h"
|
#include "config/config.h"
|
||||||
#include "util/error.h"
|
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
@@ -63,8 +62,6 @@ public:
|
|||||||
: clanguml::common::generators::generator<ConfigType, DiagramType>{
|
: clanguml::common::generators::generator<ConfigType, DiagramType>{
|
||||||
config, model}
|
config, model}
|
||||||
{
|
{
|
||||||
init_context();
|
|
||||||
init_env();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~generator() override = default;
|
~generator() override = default;
|
||||||
@@ -177,21 +174,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
void print_debug(
|
void print_debug(
|
||||||
const common::model::source_location &e, std::ostream &ostr) const;
|
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:
|
private:
|
||||||
void generate_row_column_hints(std::ostream &ostr,
|
void generate_row_column_hints(std::ostream &ostr,
|
||||||
@@ -200,74 +182,17 @@ private:
|
|||||||
void generate_position_hints(std::ostream &ostr,
|
void generate_position_hints(std::ostream &ostr,
|
||||||
const std::string &entity_name, const config::layout_hint &hint) const;
|
const std::string &entity_name, const config::layout_hint &hint) const;
|
||||||
|
|
||||||
void init_context();
|
|
||||||
|
|
||||||
void init_env();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mutable std::set<std::string> m_generated_aliases;
|
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 git_relative_path = file.string();
|
|
||||||
if (!e.file_relative().empty()) {
|
|
||||||
#if _MSC_VER
|
|
||||||
if (file.is_absolute() && ctx.contains("git"))
|
|
||||||
#else
|
|
||||||
if (file.is_absolute() && ctx.template contains("git"))
|
|
||||||
#endif
|
|
||||||
git_relative_path =
|
|
||||||
std::filesystem::relative(file, ctx["git"]["toplevel"])
|
|
||||||
.string();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
git_relative_path = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx["element"]["source"]["path"] = util::path_to_url(git_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>
|
template <typename C, typename D>
|
||||||
void generator<C, D>::generate(std::ostream &ostr) const
|
void generator<C, D>::generate(std::ostream &ostr) const
|
||||||
{
|
{
|
||||||
const auto &config = generators::generator<C, D>::config();
|
const auto &config = generators::generator<C, D>::config();
|
||||||
const auto &model = generators::generator<C, D>::model();
|
const auto &model = generators::generator<C, D>::model();
|
||||||
|
|
||||||
update_context();
|
generators::generator<C, D>::update_context();
|
||||||
|
|
||||||
if (!config.allow_empty_diagrams() && model.is_empty() &&
|
if (!config.allow_empty_diagrams() && model.is_empty() &&
|
||||||
config.puml().before.empty() && config.puml().after.empty()) {
|
config.puml().before.empty() && config.puml().after.empty()) {
|
||||||
@@ -413,7 +338,8 @@ void generator<C, D>::generate_plantuml_directives(
|
|||||||
for (const auto &d : directives) {
|
for (const auto &d : directives) {
|
||||||
try {
|
try {
|
||||||
// Render the directive with template engine first
|
// Render the directive with template engine first
|
||||||
std::string directive{env().render(std::string_view{d}, context())};
|
std::string directive{generators::generator<C, D>::env().render(
|
||||||
|
std::string_view{d}, generators::generator<C, D>::context())};
|
||||||
|
|
||||||
// Now search for alias `@A()` directives in the text
|
// Now search for alias `@A()` directives in the text
|
||||||
// (this is deprecated)
|
// (this is deprecated)
|
||||||
@@ -519,42 +445,56 @@ template <typename C, typename D>
|
|||||||
template <typename E>
|
template <typename E>
|
||||||
void generator<C, D>::generate_link(std::ostream &ostr, const E &e) const
|
void generator<C, D>::generate_link(std::ostream &ostr, const E &e) const
|
||||||
{
|
{
|
||||||
const auto &config = generators::generator<C, D>::config();
|
if (e.file().empty() && e.file_relative().empty())
|
||||||
|
|
||||||
if (e.file_relative().empty())
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
auto maybe_link_pattern = generators::generator<C, D>::get_link_pattern(e);
|
||||||
|
|
||||||
|
if (!maybe_link_pattern)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto &[link_prefix, link_pattern] = *maybe_link_pattern;
|
||||||
|
|
||||||
ostr << " [[";
|
ostr << " [[";
|
||||||
try {
|
try {
|
||||||
if (!config.generate_links().link.empty()) {
|
if (!link_pattern.empty()) {
|
||||||
ostr << env().render(std::string_view{config.generate_links().link},
|
auto ec = generators::generator<C, D>::element_context(e);
|
||||||
element_context(e));
|
common::generators::make_context_source_relative(ec, link_prefix);
|
||||||
|
ostr << generators::generator<C, D>::env().render(
|
||||||
|
std::string_view{link_pattern}, ec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const inja::json::parse_error &e) {
|
catch (const inja::json::parse_error &e) {
|
||||||
LOG_ERROR(
|
LOG_ERROR("Failed to parse Jinja template: {}", link_pattern);
|
||||||
"Failed to parse Jinja template: {}", config.generate_links().link);
|
|
||||||
}
|
}
|
||||||
catch (const inja::json::exception &e) {
|
catch (const inja::json::exception &e) {
|
||||||
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
|
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
|
||||||
config.generate_links().link, e.what());
|
link_pattern, e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto maybe_tooltip_pattern =
|
||||||
|
generators::generator<C, D>::get_tooltip_pattern(e);
|
||||||
|
|
||||||
|
if (!maybe_tooltip_pattern)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto &[tooltip_prefix, tooltip_pattern] = *maybe_tooltip_pattern;
|
||||||
|
|
||||||
ostr << "{";
|
ostr << "{";
|
||||||
try {
|
try {
|
||||||
if (!config.generate_links().tooltip.empty()) {
|
auto ec = generators::generator<C, D>::element_context(e);
|
||||||
ostr << env().render(
|
common::generators::make_context_source_relative(ec, tooltip_prefix);
|
||||||
std::string_view{config.generate_links().tooltip},
|
if (!tooltip_pattern.empty()) {
|
||||||
element_context(e));
|
ostr << generators::generator<C, D>::env().render(
|
||||||
|
std::string_view{tooltip_pattern}, ec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const inja::json::parse_error &e) {
|
catch (const inja::json::parse_error &e) {
|
||||||
LOG_ERROR(
|
LOG_ERROR("Failed to parse Jinja template: {}", tooltip_pattern);
|
||||||
"Failed to parse Jinja template: {}", config.generate_links().link);
|
|
||||||
}
|
}
|
||||||
catch (const inja::json::exception &e) {
|
catch (const inja::json::exception &e) {
|
||||||
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
|
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
|
||||||
config.generate_links().link, e.what());
|
tooltip_pattern, e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
ostr << "}";
|
ostr << "}";
|
||||||
@@ -579,126 +519,4 @@ std::ostream &operator<<(
|
|||||||
return 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::plantuml
|
} // namespace clanguml::common::generators::plantuml
|
||||||
@@ -455,8 +455,44 @@ struct layout_hint {
|
|||||||
using layout_hints = std::map<std::string, std::vector<layout_hint>>;
|
using layout_hints = std::map<std::string, std::vector<layout_hint>>;
|
||||||
|
|
||||||
struct generate_links_config {
|
struct generate_links_config {
|
||||||
std::string link;
|
std::map</* path */ std::string, /* pattern */ std::string> link;
|
||||||
std::string tooltip;
|
std::map</* path */ std::string, /* pattern */ std::string> tooltip;
|
||||||
|
|
||||||
|
std::optional<std::pair<std::string, std::string>> get_link_pattern(
|
||||||
|
const std::string &path) const
|
||||||
|
{
|
||||||
|
if (link.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if ((path.empty() || path == ".") && link.count(".") > 0) {
|
||||||
|
return {{".", link.at(".")}};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &[key, pattern] : link) {
|
||||||
|
if (util::starts_with(path, key))
|
||||||
|
return {{key, pattern}};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::pair<std::string, std::string>> get_tooltip_pattern(
|
||||||
|
const std::string &path) const
|
||||||
|
{
|
||||||
|
if (tooltip.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if ((path.empty() || path == ".") && tooltip.count(".") > 0) {
|
||||||
|
return {{".", tooltip.at(".")}};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &[key, pattern] : tooltip) {
|
||||||
|
if (util::starts_with(path, key))
|
||||||
|
return {{key, pattern}};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct git_config {
|
struct git_config {
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ types:
|
|||||||
- abbreviated
|
- abbreviated
|
||||||
- none
|
- none
|
||||||
generate_links_t:
|
generate_links_t:
|
||||||
link: string
|
link: !optional [string, map_t<string;string>]
|
||||||
tooltip: string
|
tooltip: !optional [string, map_t<string;string>]
|
||||||
git_t:
|
git_t:
|
||||||
branch: string
|
branch: string
|
||||||
revision: [string, int]
|
revision: [string, int]
|
||||||
|
|||||||
@@ -616,11 +616,19 @@ template <> struct convert<filter> {
|
|||||||
template <> struct convert<generate_links_config> {
|
template <> struct convert<generate_links_config> {
|
||||||
static bool decode(const Node &node, generate_links_config &rhs)
|
static bool decode(const Node &node, generate_links_config &rhs)
|
||||||
{
|
{
|
||||||
if (node["link"])
|
if (node["link"]) {
|
||||||
rhs.link = node["link"].as<decltype(rhs.link)>();
|
if (node["link"].IsMap())
|
||||||
|
rhs.link = node["link"].as<decltype(rhs.link)>();
|
||||||
|
else
|
||||||
|
rhs.link.emplace(".", node["link"].as<std::string>());
|
||||||
|
}
|
||||||
|
|
||||||
if (node["tooltip"])
|
if (node["tooltip"]) {
|
||||||
rhs.tooltip = node["tooltip"].as<decltype(rhs.tooltip)>();
|
if (node["tooltip"].IsMap())
|
||||||
|
rhs.tooltip = node["tooltip"].as<decltype(rhs.tooltip)>();
|
||||||
|
else
|
||||||
|
rhs.tooltip.emplace(".", node["tooltip"].as<std::string>());
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,11 +30,14 @@
|
|||||||
void inject_diagram_options(std::shared_ptr<clanguml::config::diagram> diagram)
|
void inject_diagram_options(std::shared_ptr<clanguml::config::diagram> diagram)
|
||||||
{
|
{
|
||||||
// Inject links config to all test cases
|
// Inject links config to all test cases
|
||||||
clanguml::config::generate_links_config links_config{
|
clanguml::config::generate_links_config links_config;
|
||||||
R"(https://github.com/bkryza/clang-uml/blob/{{ git.commit }}/{{ element.source.path }}#L{{ element.source.line }})",
|
|
||||||
R"({% if existsIn(element, "comment") and existsIn(element.comment, "brief") %}{{ abbrv(trim(replace(element.comment.brief.0, "\n+", " ")), 256) }}{% else %}{{ element.name }}{% endif %})"};
|
|
||||||
|
|
||||||
diagram->generate_links.set(links_config);
|
links_config.link.emplace(".",
|
||||||
|
R"(https://github.com/bkryza/clang-uml/blob/{{ git.commit }}/{{ element.source.path }}#L{{ element.source.line }})");
|
||||||
|
links_config.tooltip.emplace(".",
|
||||||
|
R"({% if existsIn(element, "comment") and existsIn(element.comment, "brief") %}{{ abbrv(trim(replace(element.comment.brief.0, "\n+", " ")), 256) }}{% else %}{{ element.name }}{% endif %})");
|
||||||
|
|
||||||
|
diagram->generate_links.set(std::move(links_config));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<clanguml::config::config_ptr,
|
std::pair<clanguml::config::config_ptr,
|
||||||
@@ -249,7 +252,8 @@ void try_run_test_case(const diagram_source_storage &diagrams, TC &&tc)
|
|||||||
tc(diagrams.get<T>());
|
tc(diagrams.get<T>());
|
||||||
}
|
}
|
||||||
catch (doctest::TestFailureException &e) {
|
catch (doctest::TestFailureException &e) {
|
||||||
std::cout << "-----------------------------------------------------"
|
std::cout << "---------------------------------------------"
|
||||||
|
"--------"
|
||||||
"--------------------------\n";
|
"--------------------------\n";
|
||||||
std::cout << "Test case failed for diagram type "
|
std::cout << "Test case failed for diagram type "
|
||||||
<< T::diagram_type_name << ": "
|
<< T::diagram_type_name << ": "
|
||||||
|
|||||||
@@ -43,10 +43,10 @@ TEST_CASE("Test config simple")
|
|||||||
CHECK(diagram.generate_packages() == true);
|
CHECK(diagram.generate_packages() == true);
|
||||||
CHECK(diagram.generate_template_argument_dependencies() == false);
|
CHECK(diagram.generate_template_argument_dependencies() == false);
|
||||||
CHECK(diagram.generate_links == true);
|
CHECK(diagram.generate_links == true);
|
||||||
CHECK(diagram.generate_links().link ==
|
CHECK(diagram.generate_links().link.at(".") ==
|
||||||
"https://github.com/bkryza/clang-uml/blob/{{ git.branch }}/{{ "
|
"https://github.com/bkryza/clang-uml/blob/{{ git.branch }}/{{ "
|
||||||
"element.source.file }}#L{{ element.source.line }}");
|
"element.source.file }}#L{{ element.source.line }}");
|
||||||
CHECK(diagram.generate_links().tooltip == "{{ element.comment }}");
|
CHECK(diagram.generate_links().tooltip.at(".") == "{{ element.comment }}");
|
||||||
|
|
||||||
CHECK(
|
CHECK(
|
||||||
diagram.comment_parser() == clanguml::config::comment_parser_t::clang);
|
diagram.comment_parser() == clanguml::config::comment_parser_t::clang);
|
||||||
|
|||||||
Reference in New Issue
Block a user