0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
clanguml::common::generators::mermaid::generator< ConfigType, DiagramType > Class Template Referenceabstract

Base class for diagram generators. More...

Detailed Description

template<typename ConfigType, typename DiagramType>
class clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >

Base class for diagram generators.

Template Parameters
ConfigTypeConfiguration type
DiagramTypeDiagram model type

Definition at line 57 of file generator.h.

#include <generator.h>

Public Member Functions

 generator (ConfigType &config, DiagramType &model)
 Constructor.
 
 ~generator () override=default
 
void generate (std::ostream &ostr) const override
 Generate diagram.
 
virtual void generate_diagram (std::ostream &ostr) const =0
 Generate diagram specific part.
 
void generate_mermaid_directives (std::ostream &ostr, const std::vector< std::string > &directives) const
 Generate MermaidJS directives from config file.
 
virtual void generate_diagram_type (std::ostream &ostr) const =0
 Generate the diagram type.
 
virtual void generate_notes (std::ostream &ostr, const model::diagram_element &element) const
 Generate diagram notes.
 
void generate_metadata (std::ostream &ostr) const
 Generate comment with diagram metadata.
 
void generate_title (std::ostream &ostr) const
 Generate diagram title.
 
template<typename E >
void generate_link (std::ostream &ostr, const E &e) const
 Generate hyper link to element.
 
void print_debug (const common::model::source_location &e, std::ostream &ostr) const
 Print debug information in diagram comments.
 
- Public Member Functions inherited from clanguml::common::generators::generator< ConfigType, DiagramType >
 generator (ConfigType &config, DiagramType &model)
 Constructor.
 
virtual ~generator ()=default
 
virtual void generate (std::ostream &ostr) const =0
 Generate diagram.
 
const ConfigType & config () const
 Get reference to diagram config.
 
const DiagramType & model () const
 Get reference to diagram 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
 
std::optional< std::pair< std::string, std::string > > get_tooltip_pattern (const common::model::source_location &sl) const
 
void init_context ()
 Initialize diagram Jinja context.
 
void update_context () const
 Update diagram Jinja context.
 
void init_env ()
 
const inja::json & context () const
 
inja::Environment & env () const
 

Protected Attributes

std::set< std::string > m_generated_aliases
 
- Protected Attributes inherited from clanguml::common::generators::generator< ConfigType, DiagramType >
inja::json m_context
 
inja::Environment m_env
 

Constructor & Destructor Documentation

◆ generator()

template<typename ConfigType , typename DiagramType >
clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >::generator ( ConfigType &  config,
DiagramType &  model 
)
inline

Constructor.

Parameters
configReference to instance of model Reference to instance of clanguml::model::diagram

Definition at line 66 of file generator.h.

◆ ~generator()

template<typename ConfigType , typename DiagramType >
clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >::~generator ( )
overridevirtualdefault

Member Function Documentation

◆ generate()

template<typename C , typename D >
void clanguml::common::generators::mermaid::generator< C, D >::generate ( std::ostream &  ostr) const
overridevirtual

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.

Parameters
ostrOutput stream

Implements clanguml::common::generators::generator< ConfigType, DiagramType >.

Definition at line 176 of file generator.h.

177{
180
181 if (!config.allow_empty_diagrams() && model.is_empty() &&
182 config.mermaid().before.empty() && config.mermaid().after.empty()) {
184 "Diagram configuration resulted in empty diagram."};
185 }
186
188
189 generate_title(ostr);
190
192
193 generate_mermaid_directives(ostr, config.mermaid().before);
194
195 generate_diagram(ostr);
196
197 generate_mermaid_directives(ostr, config.mermaid().after);
198
199 generate_metadata(ostr);
200}

◆ generate_diagram()

template<typename ConfigType , typename DiagramType >
virtual void clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >::generate_diagram ( std::ostream &  ostr) const
pure virtual

Generate diagram specific part.

This method must be implemented in subclasses for specific diagram types.

Parameters
ostrOutput stream

Implemented in clanguml::class_diagram::generators::mermaid::generator, clanguml::include_diagram::generators::mermaid::generator, clanguml::package_diagram::generators::mermaid::generator, and clanguml::sequence_diagram::generators::mermaid::generator.

◆ generate_diagram_type()

template<typename ConfigType , typename DiagramType >
virtual void clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >::generate_diagram_type ( std::ostream &  ostr) const
pure virtual

Generate the diagram type.

This method must be overriden for each diagram type (e.g. it renders a single line classDiagram for Mermaid class diagrams.

Parameters
ostrOutput stream

Implemented in clanguml::class_diagram::generators::mermaid::generator, clanguml::include_diagram::generators::mermaid::generator, clanguml::package_diagram::generators::mermaid::generator, and clanguml::sequence_diagram::generators::mermaid::generator.

◆ generate_link()

template<typename C , typename D >
template<typename E >
void clanguml::common::generators::mermaid::generator< C, D >::generate_link ( std::ostream &  ostr,
const E &  e 
) const

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)

Parameters
ostrOutput stream
eReference to diagram element
Template Parameters
EDiagram element type

Definition at line 204 of file generator.h.

205{
206 if (e.file().empty() && e.file_relative().empty())
207 return;
208
209 auto maybe_link_pattern = generators::generator<C, D>::get_link_pattern(e);
210
211 if (!maybe_link_pattern)
212 return;
213
214 const auto &[link_prefix, link_pattern] = *maybe_link_pattern;
215
216 ostr << indent(1) << "click " << e.alias() << " href \"";
217 try {
220 std::string link{};
221 if (!link_pattern.empty()) {
222 link = generators::generator<C, D>::env().render(
223 std::string_view{link_pattern}, ec);
224 }
225 if (link.empty())
226 link = " ";
227 ostr << link;
228 }
229 catch (const inja::json::parse_error &e) {
230 LOG_ERROR("Failed to parse Jinja template: {}", link_pattern);
231 ostr << " ";
232 }
233 catch (const inja::json::exception &e) {
234 LOG_ERROR("Failed to render comment directive: \n{}\n due to: {}",
235 link_pattern, e.what());
236 ostr << " ";
237 }
238 ostr << "\"";
239
240 auto maybe_tooltip_pattern =
242
243 if (maybe_tooltip_pattern) {
244 const auto &[tooltip_prefix, tooltip_pattern] = *maybe_tooltip_pattern;
245
246 if (!tooltip_pattern.empty()) {
247 ostr << " \"";
248 try {
251 ec, tooltip_prefix);
252 auto tooltip_text = generators::generator<C, D>::env().render(
253 std::string_view{tooltip_pattern}, ec);
254 util::replace_all(tooltip_text, "\"", "&bdquo;");
255 ostr << tooltip_text;
256 }
257 catch (const inja::json::parse_error &e) {
258 LOG_ERROR(
259 "Failed to parse Jinja template: {}", tooltip_pattern);
260 ostr << " ";
261 }
262 catch (const inja::json::exception &e) {
263 LOG_ERROR(
264 "Failed to render PlantUML directive: \n{}\n due to: {}",
265 tooltip_pattern, e.what());
266 ostr << " ";
267 }
268
269 ostr << "\"";
270 }
271 }
272 ostr << "\n";
273}

◆ generate_mermaid_directives()

template<typename C , typename D >
void clanguml::common::generators::mermaid::generator< C, D >::generate_mermaid_directives ( std::ostream &  ostr,
const std::vector< std::string > &  directives 
) const

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.

Parameters
ostrOutput stream
directivesList of directives from the configuration file

Definition at line 276 of file generator.h.

278{
279
282
284
285 for (const auto &d : directives) {
286 try {
287 // Render the directive with template engine first
288 std::string directive{generators::generator<C, D>::env().render(
289 std::string_view{d}, generators::generator<C, D>::context())};
290
291 // Now search for alias `@A()` directives in the text
292 // (this is deprecated)
293 std::tuple<std::string, size_t, size_t> alias_match;
294 while (util::find_element_alias(directive, alias_match)) {
295 const auto full_name =
296 config.using_namespace() | std::get<0>(alias_match);
297 auto element_opt = model.get(full_name.to_string());
298
299 if (element_opt)
300 directive.replace(std::get<1>(alias_match),
301 std::get<2>(alias_match), element_opt.value().alias());
302 else {
303 LOG_ERROR("Cannot find clang-uml alias for element {}",
304 full_name.to_string());
305 directive.replace(std::get<1>(alias_match),
306 std::get<2>(alias_match), "UNKNOWN_ALIAS");
307 }
308 }
309
310 ostr << indent(1) << directive << '\n';
311 }
312 catch (const clanguml::error::uml_alias_missing &e) {
313 LOG_ERROR(
314 "Failed to render MermaidJS directive due to unresolvable "
315 "alias: {}",
316 e.what());
317 }
318 catch (const inja::json::parse_error &e) {
319 LOG_ERROR("Failed to parse Jinja template: {}", d);
320 }
321 catch (const inja::json::exception &e) {
322 LOG_ERROR("Failed to render MermaidJS directive: \n{}\n due to: {}",
323 d, e.what());
324 }
325 catch (const std::regex_error &e) {
326 LOG_ERROR("Failed to render MermaidJS directive: \n{}\n due to "
327 "std::regex_error: {}",
328 d, e.what());
329 }
330 catch (const std::exception &e) {
331 LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
332 d, e.what());
333 }
334 }
335}

◆ generate_metadata()

template<typename C , typename D >
void clanguml::common::generators::mermaid::generator< C, D >::generate_metadata ( std::ostream &  ostr) const

Generate comment with diagram metadata.

Parameters
ostrOutput stream

Definition at line 353 of file generator.h.

354{
356
357 if (config.generate_metadata()) {
358 ostr << '\n'
359 << "%% Generated with clang-uml, version "
360 << clanguml::version::CLANG_UML_VERSION << '\n'
361 << "%% LLVM version " << clang::getClangFullVersion() << '\n';
362 }
363}

◆ generate_notes()

template<typename C , typename D >
void clanguml::common::generators::mermaid::generator< C, D >::generate_notes ( std::ostream &  ostr,
const model::diagram_element element 
) const
virtual

Generate diagram notes.

This method adds any notes in the diagram, which were declared in the code using inline directives

Parameters
ostrOutput stream
elementElement to which the note should be attached

Reimplemented in clanguml::package_diagram::generators::mermaid::generator.

Definition at line 338 of file generator.h.

340{
342
343 for (const auto &decorator : e.decorators()) {
344 auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
345 if (note && note->applies_to_diagram(config.name)) {
346 ostr << indent(1) << "note for " << e.alias() << " \"" << note->text
347 << "\"" << '\n';
348 }
349 }
350}

◆ generate_title()

template<typename C , typename D >
void clanguml::common::generators::mermaid::generator< C, D >::generate_title ( std::ostream &  ostr) const

Generate diagram title.

Generates a MermaidJS diagram title directive if diagram title is provided in the diagram configuration.

Parameters
ostrOutput stream

Definition at line 366 of file generator.h.

367{
369
370 if (config.title) {
371 ostr << "---\n";
372 ostr << "title: " << config.title() << '\n';
373 ostr << "---\n";
374 }
375}

◆ print_debug()

template<typename C , typename D >
void clanguml::common::generators::mermaid::generator< C, D >::print_debug ( const common::model::source_location e,
std::ostream &  ostr 
) const

Print debug information in diagram comments.

Parameters
mDiagram element to describe
ostrOutput stream

Definition at line 378 of file generator.h.

380{
382
383 if (config.debug_mode())
384 ostr << "%% " << e.file() << ":" << e.line() << '\n';
385}

Member Data Documentation

◆ m_generated_aliases

template<typename ConfigType , typename DiagramType >
std::set<std::string> clanguml::common::generators::mermaid::generator< ConfigType, DiagramType >::m_generated_aliases
mutableprotected

Definition at line 172 of file generator.h.


The documentation for this class was generated from the following file: