lutaml-lml 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lutaml-lml.rb +5 -0
  3. data/lib/lutaml_lml/version.rb +6 -0
  4. metadata +12 -164
  5. data/exe/lutaml-lml +0 -6
  6. data/exe/lutaml-lml-lsp +0 -11
  7. data/lib/lutaml/lml/association_label_resolver.rb +0 -52
  8. data/lib/lutaml/lml/cli.rb +0 -489
  9. data/lib/lutaml/lml/data_processor/attribute_processing.rb +0 -133
  10. data/lib/lutaml/lml/data_processor/collection_processing.rb +0 -37
  11. data/lib/lutaml/lml/data_processor/instance_processing.rb +0 -77
  12. data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +0 -70
  13. data/lib/lutaml/lml/data_processor/value_processing.rb +0 -103
  14. data/lib/lutaml/lml/data_processor/view_processing.rb +0 -26
  15. data/lib/lutaml/lml/data_processor.rb +0 -59
  16. data/lib/lutaml/lml/diagnostic.rb +0 -149
  17. data/lib/lutaml/lml/document_builder.rb +0 -481
  18. data/lib/lutaml/lml/entity_types.rb +0 -30
  19. data/lib/lutaml/lml/executor/adapter_helpers.rb +0 -45
  20. data/lib/lutaml/lml/executor/condition_evaluator.rb +0 -169
  21. data/lib/lutaml/lml/executor/csv_adapter.rb +0 -88
  22. data/lib/lutaml/lml/executor/format_adapter.rb +0 -54
  23. data/lib/lutaml/lml/executor/xml_adapter.rb +0 -102
  24. data/lib/lutaml/lml/executor.rb +0 -96
  25. data/lib/lutaml/lml/format/adapter/document.rb +0 -11
  26. data/lib/lutaml/lml/format/adapter/mapping.rb +0 -31
  27. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +0 -162
  28. data/lib/lutaml/lml/format/adapter/transform.rb +0 -11
  29. data/lib/lutaml/lml/format.rb +0 -29
  30. data/lib/lutaml/lml/formatter/base.rb +0 -79
  31. data/lib/lutaml/lml/formatter/canonical.rb +0 -736
  32. data/lib/lutaml/lml/formatter/elk.rb +0 -45
  33. data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +0 -89
  34. data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +0 -72
  35. data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +0 -89
  36. data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +0 -130
  37. data/lib/lutaml/lml/formatter/graphviz.rb +0 -90
  38. data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +0 -134
  39. data/lib/lutaml/lml/formatter/svg_pdf.rb +0 -285
  40. data/lib/lutaml/lml/formatter.rb +0 -12
  41. data/lib/lutaml/lml/grammar/lml.artifact.json +0 -14993
  42. data/lib/lutaml/lml/grammar/lml.parg +0 -389
  43. data/lib/lutaml/lml/grammar.rb +0 -60
  44. data/lib/lutaml/lml/has_attributes.rb +0 -14
  45. data/lib/lutaml/lml/import_resolver.rb +0 -96
  46. data/lib/lutaml/lml/layout/elk_engine.rb +0 -15
  47. data/lib/lutaml/lml/layout/elk_graph_builder.rb +0 -129
  48. data/lib/lutaml/lml/layout/elk_svg_markers.rb +0 -45
  49. data/lib/lutaml/lml/layout/elk_svg_renderer.rb +0 -119
  50. data/lib/lutaml/lml/layout/engine.rb +0 -17
  51. data/lib/lutaml/lml/layout/graph_viz_engine.rb +0 -19
  52. data/lib/lutaml/lml/layout.rb +0 -12
  53. data/lib/lutaml/lml/literal_value.rb +0 -32
  54. data/lib/lutaml/lml/lsp/conversions.rb +0 -78
  55. data/lib/lutaml/lml/lsp/keywords.rb +0 -43
  56. data/lib/lutaml/lml/lsp/server.rb +0 -378
  57. data/lib/lutaml/lml/lsp/store.rb +0 -68
  58. data/lib/lutaml/lml/lsp/transport.rb +0 -53
  59. data/lib/lutaml/lml/lsp.rb +0 -16
  60. data/lib/lutaml/lml/model_compiler/enums.rb +0 -172
  61. data/lib/lutaml/lml/model_compiler/hydration.rb +0 -123
  62. data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +0 -188
  63. data/lib/lutaml/lml/model_compiler/string_formats.rb +0 -42
  64. data/lib/lutaml/lml/model_compiler/validation.rb +0 -65
  65. data/lib/lutaml/lml/model_compiler.rb +0 -323
  66. data/lib/lutaml/lml/models/action.rb +0 -10
  67. data/lib/lutaml/lml/models/association.rb +0 -29
  68. data/lib/lutaml/lml/models/cardinality.rb +0 -10
  69. data/lib/lutaml/lml/models/collection.rb +0 -11
  70. data/lib/lutaml/lml/models/constraint.rb +0 -23
  71. data/lib/lutaml/lml/models/data_type.rb +0 -41
  72. data/lib/lutaml/lml/models/derived_field.rb +0 -14
  73. data/lib/lutaml/lml/models/diagram.rb +0 -20
  74. data/lib/lutaml/lml/models/document.rb +0 -84
  75. data/lib/lutaml/lml/models/enum.rb +0 -38
  76. data/lib/lutaml/lml/models/fidelity.rb +0 -10
  77. data/lib/lutaml/lml/models/group.rb +0 -11
  78. data/lib/lutaml/lml/models/instance.rb +0 -26
  79. data/lib/lutaml/lml/models/instance_collection.rb +0 -16
  80. data/lib/lutaml/lml/models/instances_export.rb +0 -10
  81. data/lib/lutaml/lml/models/instances_import.rb +0 -11
  82. data/lib/lutaml/lml/models/mapping.rb +0 -15
  83. data/lib/lutaml/lml/models/mapping_rule.rb +0 -16
  84. data/lib/lutaml/lml/models/member_field.rb +0 -14
  85. data/lib/lutaml/lml/models/namespace.rb +0 -16
  86. data/lib/lutaml/lml/models/operation.rb +0 -26
  87. data/lib/lutaml/lml/models/operation_parameter.rb +0 -12
  88. data/lib/lutaml/lml/models/package.rb +0 -26
  89. data/lib/lutaml/lml/models/primitive_type.rb +0 -33
  90. data/lib/lutaml/lml/models/reference.rb +0 -23
  91. data/lib/lutaml/lml/models/serialization_mapping.rb +0 -19
  92. data/lib/lutaml/lml/models/string_format.rb +0 -16
  93. data/lib/lutaml/lml/models/top_element_attribute.rb +0 -116
  94. data/lib/lutaml/lml/models/uml_class.rb +0 -85
  95. data/lib/lutaml/lml/models/value.rb +0 -15
  96. data/lib/lutaml/lml/models/view_filter.rb +0 -11
  97. data/lib/lutaml/lml/models/view_import.rb +0 -11
  98. data/lib/lutaml/lml/parser.rb +0 -19
  99. data/lib/lutaml/lml/pipeline.rb +0 -73
  100. data/lib/lutaml/lml/positioned.rb +0 -13
  101. data/lib/lutaml/lml/preprocessor.rb +0 -130
  102. data/lib/lutaml/lml/quoting.rb +0 -60
  103. data/lib/lutaml/lml/source.rb +0 -49
  104. data/lib/lutaml/lml/source_position.rb +0 -20
  105. data/lib/lutaml/lml/symbol_index.rb +0 -200
  106. data/lib/lutaml/lml/transform.rb +0 -119
  107. data/lib/lutaml/lml/types/text_type.rb +0 -28
  108. data/lib/lutaml/lml/types.rb +0 -10
  109. data/lib/lutaml/lml/validator.rb +0 -458
  110. data/lib/lutaml/lml/version.rb +0 -7
  111. data/lib/lutaml/lml/view_resolution.rb +0 -32
  112. data/lib/lutaml/lml/view_resolver.rb +0 -48
  113. data/lib/lutaml/lml/yaml_parser.rb +0 -17
  114. data/lib/lutaml/lml.rb +0 -130
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Formatter
5
- # ELK-layout diagram formatter: builds an Elkrb::Graph from the
6
- # document, lays it out in-process (no external tools) and emits SVG,
7
- # PS/EPS/EMF through vectory, or PDF through the pdfrb-backed
8
- # SvgPdf renderer.
9
- class Elk
10
- VALID_TYPES = %i[svg ps eps emf pdf].freeze
11
-
12
- attr_reader :type
13
-
14
- def initialize(_attributes = {})
15
- @type = :svg
16
- end
17
-
18
- def type=(value)
19
- @type = value.to_sym
20
- return if VALID_TYPES.include?(@type)
21
-
22
- raise ArgumentError,
23
- "unsupported output type #{@type.inspect} (valid: #{VALID_TYPES.join(', ')})"
24
- end
25
-
26
- def format(document)
27
- graph = Lutaml::Layout::ElkGraphBuilder.new.build(document)
28
- svg = Lutaml::Layout::ElkEngine.new(input: graph).render(:svg)
29
- @type == :svg ? svg : convert(svg)
30
- end
31
-
32
- private
33
-
34
- def convert(svg)
35
- return SvgPdf.new(svg).render if @type == :pdf
36
-
37
- require 'vectory'
38
- Vectory::Svg.new(svg).public_send(:"to_#{@type}").content
39
- rescue LoadError
40
- raise Error, 'The vectory gem is required for PS/EPS/EMF output: ' \
41
- 'add `gem "vectory"` to your Gemfile'
42
- end
43
- end
44
- end
45
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Formatter
5
- class Graphviz < Base
6
- module DocumentFormatter
7
- def format_document(node)
8
- @fontname = node.fontname || DEFAULT_CLASS_FONT
9
- @node['fontname'] = "#{@fontname}-bold"
10
-
11
- hide_members, hide_other_classes = extract_fidelity_options(node)
12
- classes = format_all_classes(node, hide_members)
13
- associations = build_associations(node, hide_other_classes)
14
-
15
- build_digraph(classes, associations)
16
- end
17
-
18
- def extract_fidelity_options(node)
19
- if node.fidelity
20
- [node.fidelity.hideMembers, node.fidelity.hideOtherClasses]
21
- else
22
- [nil, nil]
23
- end
24
- end
25
-
26
- def format_all_classes(node, hide_members)
27
- node.all_classes.map do |class_node|
28
- graph_node_name = generate_graph_name(class_node.name)
29
- <<~HEREDOC
30
- #{graph_node_name} [
31
- shape="plain"
32
- fontname="#{@fontname || DEFAULT_CLASS_FONT}"
33
- label=<#{format_class(class_node, hide_members)}>]
34
- HEREDOC
35
- end.join("\n")
36
- end
37
-
38
- def build_associations(node, hide_other_classes)
39
- associations = collect_all_associations(node)
40
- associations = sort_by_document_grouping(node.groups, associations) if node.groups
41
- classes_names = node.classes.map(&:name)
42
- format_filtered_associations(associations, classes_names, hide_other_classes)
43
- end
44
-
45
- def collect_all_associations(node)
46
- class_level = node.classifiable_classes
47
- seen = Set.new
48
- all = class_level.filter_map(&:associations).flatten + node.associations
49
- all.uniq { |a| association_key(a) }
50
- end
51
-
52
- def format_filtered_associations(associations, classes_names, hide_other_classes)
53
- associations.filter_map do |assoc_node|
54
- next if hide_other_classes && !classes_names.include?(assoc_node.member_end)
55
-
56
- format_relationship(assoc_node)
57
- end.join("\n")
58
- end
59
-
60
- def build_digraph(classes, associations)
61
- indented_classes = indent_lines(classes)
62
- indented_assocs = indent_lines(associations)
63
-
64
- <<~HEREDOC
65
- digraph G {
66
- graph [#{@graph}]
67
- edge [#{@edge}]
68
- node [#{@node}]
69
-
70
- #{indented_classes}
71
-
72
- #{indented_assocs}
73
- }
74
- HEREDOC
75
- end
76
-
77
- def indent_lines(text)
78
- text.lines.map { |line| " #{line}" }.join.chomp
79
- end
80
-
81
- private
82
-
83
- def association_key(assoc)
84
- [assoc.owner_end.to_s, assoc.member_end.to_s, assoc.name.to_s]
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Formatter
5
- class Graphviz < Base
6
- module HtmlBuilder
7
- EMPTY_MEMBER_TABLE = <<~HEREDOC.chomp
8
- <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
9
- <TR><TD ALIGN="LEFT"></TD></TR>
10
- </TABLE>
11
- HEREDOC
12
-
13
- def escape_html_chars(text)
14
- text
15
- .gsub('&', '&amp;')
16
- .gsub('<', '&#60;')
17
- .gsub('>', '&#62;')
18
- .gsub('[', '&#91;')
19
- .gsub(']', '&#93;')
20
- end
21
-
22
- def format_member_rows(members, hide_members, &formatter)
23
- return EMPTY_MEMBER_TABLE if hide_members || !members&.any?
24
-
25
- formatter ||= method(:dispatch_format)
26
- field_rows = members.map do |member|
27
- %(<TR><TD ALIGN="LEFT">#{formatter.call(member)}</TD></TR>)
28
- end
29
- build_member_table(field_rows)
30
- end
31
-
32
- def format_enum_member(member)
33
- enum_literal?(member) ? format_enum_literal(member) : dispatch_format(member)
34
- end
35
-
36
- def enum_literal?(node)
37
- node.is_a?(Lml::TopElementAttribute) &&
38
- node.type.nil? && node.cardinality.nil?
39
- end
40
-
41
- def build_member_table(field_rows)
42
- indented = field_rows.map { |row| (' ' * 10) + row }.join("\n")
43
- <<~HEREDOC.chomp
44
- <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
45
- #{indented}
46
- </TABLE>
47
- HEREDOC
48
- .concat("\n")
49
- .concat(' ' * 6)
50
- end
51
-
52
- def build_name_table(name_parts)
53
- <<~HEREDOC
54
- <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
55
- #{name_parts.map { |n| %(<TR><TD ALIGN="CENTER">#{n}</TD></TR>) }.join("\n")}
56
- </TABLE>
57
- HEREDOC
58
- end
59
-
60
- def build_table_body(name_html, field_table, method_table)
61
- [name_html, field_table, method_table].compact.filter_map do |type|
62
- <<~TEXT
63
- <TR>
64
- <TD>#{type}</TD>
65
- </TR>
66
- TEXT
67
- end.join("\n")
68
- end
69
- end
70
- end
71
- end
72
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Formatter
5
- class Graphviz < Base
6
- module NodeFormatter
7
- ACCESS_SYMBOLS = {
8
- 'public' => '+',
9
- 'protected' => '#',
10
- 'private' => '-',
11
- 'package' => '~'
12
- }.freeze
13
-
14
- def format_attribute(node)
15
- symbol = ACCESS_SYMBOLS[node.visibility]
16
- result = "#{symbol}#{node.name}"
17
- if node.type
18
- stereotype = node.stereotype.empty? ? '' : node.stereotype.map { |s| "«#{s}»" }.join
19
- result += " : #{stereotype}#{node.type}"
20
- end
21
- result += format_cardinality_bounds(node.cardinality) if node.cardinality
22
- result = escape_html_chars(result)
23
- result = "<U>#{result}</U>" if node.static
24
- result
25
- end
26
-
27
- def format_cardinality_bounds(cardinality)
28
- min = cardinality.min || '*'
29
- max = cardinality.max || '*'
30
- "[#{min}..#{max}]"
31
- end
32
-
33
- def format_operation(node)
34
- symbol = ACCESS_SYMBOLS[node.visibility]
35
- params = format_operation_params(node.owned_parameter)
36
- result = "#{symbol} #{node.name}(#{params})"
37
- result << " : #{node.return_type}" if node.return_type
38
- result = escape_html_chars(result)
39
- result = "<U>#{result}</U>" if node.is_static
40
- result = "<I>#{result}</I>" if node.is_abstract
41
- result
42
- end
43
-
44
- def format_operation_params(params)
45
- return '' unless params&.any?
46
-
47
- params.map do |param|
48
- param.type ? "#{param.name} : #{param.type}" : param.name.to_s
49
- end.join(', ')
50
- end
51
-
52
- # Kind labels rendered above the name: guillemet stereotypes first,
53
- # then the entity-kind label (enums/data types/primitives carry no
54
- # user stereotypes of their own in the grammar).
55
- KIND_LABELS = {
56
- enums: "enumeration",
57
- data_types: "dataType",
58
- primitives: "primitive"
59
- }.freeze
60
-
61
- def format_class(node, hide_members = nil)
62
- name = ["<B>#{escape_html_chars(node.name)}</B>"]
63
- node.stereotype.each do |stereotype|
64
- name.unshift("«#{escape_html_chars(stereotype)}»")
65
- end
66
- if (kind = KIND_LABELS[node.class.entity_type])
67
- name.unshift("«#{kind}»")
68
- end
69
- name_html = build_name_table(name)
70
-
71
- member_formatter = node.is_a?(Lml::Enum) ? method(:format_enum_member) : nil
72
- field_table = format_member_rows(node.attributes, hide_members, &member_formatter)
73
- method_table = format_member_rows(node.operations, hide_members) if node.operations&.any?
74
- table_body = build_table_body(name_html, field_table, method_table)
75
-
76
- <<~HEREDOC.chomp
77
- <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="10">
78
- #{table_body}
79
- </TABLE>
80
- HEREDOC
81
- end
82
-
83
- def format_enum_literal(node)
84
- "<I>#{escape_html_chars(node.name.to_s)}</I>"
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,130 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Formatter
5
- class Graphviz < Base
6
- module RelationshipFormatter
7
- ARROW_TYPES = {
8
- 'composition' => 'diamond',
9
- 'aggregation' => 'odiamond',
10
- 'direct' => 'vee'
11
- }.freeze
12
-
13
- DASHED_TYPES = %w[dependency realizes].freeze
14
-
15
- DIRECTION_LABELS = {
16
- 'target' => '%s ▶',
17
- 'source' => '◀ %s'
18
- }.freeze
19
-
20
- def format_relationship(node)
21
- graph_parent_name = generate_graph_name(node.owner_end)
22
- graph_node_name = generate_graph_name(node.member_end)
23
- attributes = build_edge_attributes(node)
24
- graph_attributes = " [#{attributes}]" unless attributes.empty?
25
-
26
- "#{graph_parent_name} -> #{graph_node_name}#{graph_attributes}"
27
- end
28
-
29
- def build_edge_attributes(node)
30
- attrs = Attributes.new
31
-
32
- apply_edge_style(attrs, node)
33
- apply_edge_direction(attrs, node)
34
- apply_edge_labels(attrs, node)
35
- apply_arrow_types(attrs, node)
36
- maybe_swap_labels(attrs)
37
-
38
- attrs
39
- end
40
-
41
- def format_label(name, cardinality = nil)
42
- res = "+#{name}"
43
- return res if cardinality.nil? || cardinality.min.nil? || cardinality.max.nil?
44
-
45
- "#{res} #{cardinality.min}..#{cardinality.max}"
46
- end
47
-
48
- private
49
-
50
- def sort_by_document_grouping(groups, associations)
51
- result = []
52
- seen = Set.new
53
-
54
- groups.each do |group|
55
- group.values.each do |group_name|
56
- matches = associations.select { |a| a.owner_end == group_name }
57
- matches.each { |a| add_unseen(a, result, seen) }
58
- end
59
- end
60
- associations.each { |a| add_unseen(a, result, seen) }
61
- result
62
- end
63
-
64
- def add_unseen(association, result, seen)
65
- return if seen.include?(association)
66
-
67
- result.push(association)
68
- seen.add(association)
69
- end
70
-
71
- def apply_edge_style(attrs, node)
72
- attrs['style'] = 'dashed' if DASHED_TYPES.include?(node.member_end_type)
73
- end
74
-
75
- def apply_edge_direction(attrs, node)
76
- attrs['dir'] = if node.owner_end_type && node.member_end_type
77
- 'both'
78
- elsif node.owner_end_type
79
- 'back'
80
- else
81
- 'direct'
82
- end
83
- end
84
-
85
- def apply_edge_labels(attrs, node)
86
- apply_verb_label(attrs, node)
87
- apply_direction_label(attrs, node)
88
- apply_endpoint_labels(attrs, node)
89
- end
90
-
91
- def apply_verb_label(attrs, node)
92
- attrs['label'] = node.action.verb if node&.action&.verb
93
- end
94
-
95
- def apply_direction_label(attrs, node)
96
- return unless node&.action&.direction
97
-
98
- template = DIRECTION_LABELS[node.action.direction]
99
- attrs['label'] = template % attrs['label'] if template
100
- end
101
-
102
- def apply_endpoint_labels(attrs, node)
103
- if node.owner_end_attribute_name
104
- attrs['headlabel'] = format_label(
105
- node.owner_end_attribute_name, node.owner_end_cardinality
106
- )
107
- end
108
-
109
- return unless node.member_end_attribute_name
110
-
111
- attrs['taillabel'] = format_label(
112
- node.member_end_attribute_name, node.member_end_cardinality
113
- )
114
- end
115
-
116
- def apply_arrow_types(attrs, node)
117
- attrs['arrowtail'] = ARROW_TYPES.fetch(node.owner_end_type, 'onormal')
118
- attrs['arrowhead'] = ARROW_TYPES.fetch(node.member_end_type, 'onormal')
119
- end
120
-
121
- def maybe_swap_labels(attrs)
122
- return unless attrs['dir'] == 'back' && attrs['arrowtail'] != 'vee'
123
-
124
- attrs['arrowhead'], attrs['arrowtail'] = [attrs['arrowtail'], attrs['arrowhead']]
125
- attrs['headlabel'], attrs['taillabel'] = [attrs['taillabel'], attrs['headlabel']]
126
- end
127
- end
128
- end
129
- end
130
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'open3'
4
- require 'set'
5
-
6
- module Lutaml
7
- module Formatter
8
- class Graphviz < Base
9
- autoload :HtmlBuilder, "lutaml/lml/formatter/graphviz/html_builder"
10
- autoload :NodeFormatter, "lutaml/lml/formatter/graphviz/node_formatter"
11
- autoload :RelationshipFormatter, "lutaml/lml/formatter/graphviz/relationship_formatter"
12
- autoload :DocumentFormatter, "lutaml/lml/formatter/graphviz/document_formatter"
13
-
14
- include HtmlBuilder
15
- include NodeFormatter
16
- include RelationshipFormatter
17
- include DocumentFormatter
18
-
19
- class Attributes < Hash
20
- def to_s
21
- to_a
22
- .reject { |(_k, val)| val.nil? }
23
- .map { |(a, b)| "#{a}=#{b.inspect}" }
24
- .join(' ')
25
- end
26
- end
27
-
28
- DEFAULT_CLASS_FONT = 'Helvetica'
29
-
30
- VALID_TYPES = %i[
31
- dot xdot ps pdf svg svgz fig png gif jpg jpeg json imap cmapx
32
- ].freeze
33
-
34
- def initialize(attributes = {})
35
- super
36
- setup_default_attributes
37
- @type = :dot
38
- end
39
-
40
- attr_reader :graph, :edge, :node
41
-
42
- def type=(value)
43
- super
44
- @type = :dot unless VALID_TYPES.include?(@type)
45
- end
46
-
47
- def format(node)
48
- dot = super.lines.map(&:rstrip).join("\n")
49
- generate_from_dot(dot)
50
- end
51
-
52
- def setup_default_attributes
53
- @graph = build_graph_attrs
54
- @edge = build_edge_attrs
55
- @node = build_node_attrs
56
- end
57
-
58
- def build_graph_attrs
59
- Attributes.new.tap do |g|
60
- g['splines'] = 'ortho'
61
- g['pad'] = '0.5'
62
- g['ranksep'] = '1.2'
63
- g['nodesep'] = '1.2'
64
- g['rankdir'] = 'BT'
65
- end
66
- end
67
-
68
- def build_edge_attrs
69
- Attributes.new.tap { |e| e['color'] = 'gray50' }
70
- end
71
-
72
- def build_node_attrs
73
- Attributes.new.tap { |n| n['shape'] = 'box' }
74
- end
75
-
76
- private :setup_default_attributes, :build_graph_attrs,
77
- :build_edge_attrs, :build_node_attrs
78
-
79
- protected
80
-
81
- def generate_from_dot(input)
82
- Lutaml::Layout::GraphVizEngine.new(input: input).render(@type)
83
- end
84
-
85
- def generate_graph_name(name)
86
- name.gsub(/[^0-9a-zA-Z]/i, '')
87
- end
88
- end
89
- end
90
- end
@@ -1,134 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Formatter
5
- class SvgPdf
6
- # Minimal SVG path data parser: absolute and relative M/m, L/l,
7
- # H/h, V/v, C/c, Z/z — enough for ELK geometry and arrow shapes.
8
- class PathParser
9
- Point = Struct.new(:x, :y)
10
- Line = Struct.new(:point) do
11
- def last_point
12
- point
13
- end
14
- end
15
- Curve = Struct.new(:c1, :c2, :point) do
16
- def last_point
17
- point
18
- end
19
- end
20
- Close = Struct.new(:last_point)
21
-
22
- attr_reader :subpaths
23
-
24
- def initialize(d, dx, dy)
25
- @dx = dx
26
- @dy = dy
27
- parse(d)
28
- end
29
-
30
- private
31
-
32
- def parse(d)
33
- tokens = d.scan(/[MmLlHhVvCcZz]|-?\d*\.?\d+(?:[eE][-+]?\d+)?/)
34
- current = Point.new(0.0, 0.0)
35
- start = current
36
- @subpaths = [[]]
37
- i = 0
38
- while i < tokens.size
39
- cmd = tokens[i]
40
- i += 1
41
- case cmd
42
- when 'Z', 'z'
43
- @subpaths.last << Close.new(start)
44
- current = start.dup
45
- when 'M', 'm'
46
- current, start = move_to(tokens, i, current, cmd == 'm')
47
- i += 2
48
- @subpaths << [] if cmd == 'm'
49
- when 'L', 'l'
50
- current = line_to(tokens, i, current, cmd == 'l')
51
- i += 2
52
- when 'H', 'h'
53
- current = h_line(tokens, i, current, cmd == 'h')
54
- i += 1
55
- when 'V', 'v'
56
- current = v_line(tokens, i, current, cmd == 'v')
57
- i += 1
58
- when 'C', 'c'
59
- current = curve_to(tokens, i, current, cmd == 'c')
60
- i += 6
61
- end
62
- end
63
- finish
64
- end
65
-
66
- def move_to(tokens, i, current, relative)
67
- x = num(tokens, i)
68
- y = num(tokens, i + 1)
69
- point = relative ? Point.new(current.x + x, current.y + y) : Point.new(x, y)
70
- [point, point.dup]
71
- end
72
-
73
- def line_to(tokens, i, current, relative)
74
- x = num(tokens, i)
75
- y = num(tokens, i + 1)
76
- point = relative ? Point.new(current.x + x, current.y + y) : Point.new(x, y)
77
- @subpaths.last << Line.new(point.dup)
78
- point
79
- end
80
-
81
- def h_line(tokens, i, current, relative)
82
- x = num(tokens, i) + (relative ? current.x : 0)
83
- point = Point.new(x, current.y)
84
- @subpaths.last << Line.new(point.dup)
85
- point
86
- end
87
-
88
- def v_line(tokens, i, current, relative)
89
- y = num(tokens, i) + (relative ? current.y : 0)
90
- point = Point.new(current.x, y)
91
- @subpaths.last << Line.new(point.dup)
92
- point
93
- end
94
-
95
- def curve_to(tokens, i, current, relative)
96
- c1 = Point.new(num(tokens, i), num(tokens, i + 1))
97
- c2 = Point.new(num(tokens, i + 2), num(tokens, i + 3))
98
- to = Point.new(num(tokens, i + 4), num(tokens, i + 5))
99
- if relative
100
- c1 = Point.new(c1.x + current.x, c1.y + current.y)
101
- c2 = Point.new(c2.x + current.x, c2.y + current.y)
102
- to = Point.new(to.x + current.x, to.y + current.y)
103
- end
104
- @subpaths.last << Curve.new(c1, c2, to)
105
- to
106
- end
107
-
108
- def num(tokens, i)
109
- tokens[i].to_f
110
- end
111
-
112
- def finish
113
- @subpaths.map { |segments| segments.map { |s| offset(s) } }
114
- .reject(&:empty?)
115
- end
116
-
117
- def offset(segment)
118
- case segment
119
- when Line
120
- Line.new(shift(segment.point))
121
- when Curve
122
- Curve.new(shift(segment.c1), shift(segment.c2), shift(segment.point))
123
- else
124
- segment
125
- end
126
- end
127
-
128
- def shift(point)
129
- Point.new(point.x + @dx, point.y + @dy)
130
- end
131
- end
132
- end
133
- end
134
- end