drawio_dsl 0.6.0 → 0.7.0

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.
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Work with HTML formatters to DrawIO shapes
4
+ module DrawioDsl
5
+ # HTML formatters can be used on shapes to render HTML using a DSL
6
+ module Formatters
7
+ # Create an instance of a HTML formatter on the shape
8
+ class HtmlBuilder
9
+ attr_reader :element_style_defaults
10
+
11
+ def initialize(element_style_defaults = {})
12
+ @element_style_defaults = element_style_defaults
13
+ end
14
+
15
+ def default_for(tag)
16
+ element_style_defaults[tag] || {}
17
+ end
18
+
19
+ def style_for(tag, **opts)
20
+ defaults = default_for(tag)
21
+ DrawioDsl::Formatters::StyleBuilder.new(**defaults).customize(**opts).style_attribute
22
+ end
23
+
24
+ def empty?
25
+ build_lines.empty?
26
+ end
27
+
28
+ def exist?
29
+ !empty?
30
+ end
31
+
32
+ def as_html(new_line: false)
33
+ new_line ? build_lines.join("\n") : build_lines.join
34
+ end
35
+
36
+ def hr(size: 1)
37
+ add_line("<hr size=\"#{size}\"/>")
38
+ end
39
+
40
+ def b(content, **opts)
41
+ add_line("<b#{style_for(:b, **opts)}>#{content}</b>")
42
+ end
43
+
44
+ def p(content, **opts)
45
+ # style_parts = %w[margin:0px margin-top:4px]
46
+ # style_parts << "text-align:#{opts[:text_align]}" if opts[:text_align]
47
+ # style=\"#{style_parts.join(';')}\"
48
+ add_line("<p#{style_for(:p, **opts)}>#{content}</p>")
49
+ end
50
+
51
+ def add_line(line)
52
+ lines << line
53
+ end
54
+
55
+ def add_placeholder(group_key)
56
+ lines << group_key
57
+ end
58
+
59
+ def group(key)
60
+ groups[key] ||= DrawioDsl::Formatters::HtmlBuilder.new(element_style_defaults)
61
+ end
62
+
63
+ def build_lines
64
+ lines.flat_map do |line|
65
+ if line.is_a?(Symbol)
66
+ group(line).build_lines
67
+ else
68
+ line
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def lines
76
+ @lines ||= []
77
+ end
78
+
79
+ def groups
80
+ @groups ||= {}
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Work with HTML formatters to DrawIO shapes
4
+ module DrawioDsl
5
+ # HTML formatters can be used on shapes to render HTML using a DSL
6
+ module Formatters
7
+ # Create an instance of a HTML formatter on the shape
8
+ class InterfaceFormatter < BaseFormatter
9
+ def initialize
10
+ super({ p: { margin: '0px', margin_left: '4px', margin_top: '4px' } })
11
+ end
12
+
13
+ def header(value)
14
+ html.p('<i>Interface</i>', text_align: :center)
15
+ html.p("<b>#{value}</b>", text_align: :center)
16
+ html.hr
17
+
18
+ self
19
+ end
20
+
21
+ def field(name, type: nil)
22
+ value = if type
23
+ "#{name}: #{type}"
24
+ else
25
+ name
26
+ end
27
+ html.group(:fields).p(value)
28
+
29
+ self
30
+ end
31
+
32
+ def method(name, type: nil)
33
+ value = if type
34
+ "#{name}() : #{type}"
35
+ else
36
+ "#{name}()"
37
+ end
38
+ html.group(:methods).p(value)
39
+
40
+ self
41
+ end
42
+
43
+ def as_html(new_line: false)
44
+ html.add_placeholder(:fields)
45
+ html.add_placeholder(:methods)
46
+
47
+ html.group(:fields).hr if html.group(:fields).exist? && html.group(:methods).exist?
48
+
49
+ html.as_html(new_line: new_line)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Work with HTML formatters to DrawIO shapes
4
+ module DrawioDsl
5
+ # HTML formatters can be used on shapes to render HTML using a DSL
6
+ module Formatters
7
+ # Create an instance of a HTML formatter on the shape
8
+ class KlassFormatter < BaseFormatter
9
+ def initialize
10
+ super({ p: { margin: '0px', margin_left: '4px', margin_top: '4px' } })
11
+ end
12
+
13
+ def header(value)
14
+ html.p("<b>#{value}</b>", text_align: :center)
15
+ html.hr
16
+
17
+ self
18
+ end
19
+
20
+ def field(name, type: nil)
21
+ value = if type
22
+ "#{name}: #{type}"
23
+ else
24
+ name
25
+ end
26
+ html.group(:fields).p(value)
27
+
28
+ self
29
+ end
30
+
31
+ def method(name, type: nil)
32
+ value = if type
33
+ "#{name}() : #{type}"
34
+ else
35
+ "#{name}()"
36
+ end
37
+ html.group(:methods).p(value)
38
+
39
+ self
40
+ end
41
+
42
+ def as_html(new_line: false)
43
+ html.add_placeholder(:fields)
44
+ html.add_placeholder(:methods)
45
+
46
+ html.group(:fields).hr if html.group(:fields).exist? && html.group(:methods).exist?
47
+
48
+ html.as_html(new_line: new_line)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Work with HTML formatters to DrawIO shapes
4
+ module DrawioDsl
5
+ # HTML formatters can be used on shapes to render HTML using a DSL
6
+ module Formatters
7
+ # Create an instance of a HTML formatter on the shape
8
+ class StyleBuilder
9
+ MAPPINGS = {
10
+ margin: 'margin',
11
+ margin_top: 'margin-top',
12
+ margin_bottom: 'margin-bottom',
13
+ margin_left: 'margin-left',
14
+ margin_right: 'margin-right',
15
+ padding: 'padding',
16
+ padding_top: 'padding-top',
17
+ padding_bottom: 'padding-bottom',
18
+ padding_left: 'padding-left',
19
+ padding_right: 'padding-right',
20
+ text_align: 'text-align',
21
+ font_size: 'font-size',
22
+ font_weight: 'font-weight',
23
+ font_style: 'font-style',
24
+ font_family: 'font-family',
25
+ color: 'color'
26
+ }.freeze
27
+
28
+ attr_reader :defaults
29
+ attr_reader :custom
30
+
31
+ def initialize(**opts)
32
+ @style_parts = []
33
+ @defaults = opts
34
+ @custom = {}
35
+ end
36
+
37
+ def customize(**opts)
38
+ @custom = opts
39
+
40
+ self
41
+ end
42
+
43
+ def style
44
+ build
45
+ @style_parts.join(';')
46
+ end
47
+
48
+ def style_attribute
49
+ style_value = style
50
+ style_value.empty? ? nil : " style=\"#{style_value}\""
51
+ end
52
+
53
+ private
54
+
55
+ def build
56
+ defaults.merge(custom).each do |key, value|
57
+ style_key = MAPPINGS[key]
58
+ @style_parts << "#{style_key}:#{value}" if style_key
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -27,16 +27,18 @@ require_relative 'shapes/callout3'
27
27
  require_relative 'shapes/callout4'
28
28
  require_relative 'shapes/circle'
29
29
  require_relative 'shapes/cloud'
30
+ require_relative 'shapes/container'
31
+ require_relative 'shapes/container2'
32
+ require_relative 'shapes/container3'
33
+ require_relative 'shapes/container4'
30
34
  require_relative 'shapes/cross'
31
35
  require_relative 'shapes/envelop'
32
36
  require_relative 'shapes/diamond'
33
37
  require_relative 'shapes/document'
34
38
  require_relative 'shapes/ellipse'
35
39
  require_relative 'shapes/hexagon'
36
- require_relative 'shapes/container'
37
- require_relative 'shapes/container2'
38
- require_relative 'shapes/container3'
39
- require_relative 'shapes/container4'
40
+ require_relative 'shapes/interface'
41
+ require_relative 'shapes/klass'
40
42
  require_relative 'shapes/note'
41
43
  require_relative 'shapes/process'
42
44
  require_relative 'shapes/rectangle'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DrawioDsl
4
+ module Schema
5
+ class Interface < Shape
6
+ configure_shape(:interface)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DrawioDsl
4
+ module Schema
5
+ class Klass < Shape
6
+ configure_shape(:klass)
7
+ end
8
+ end
9
+ end
@@ -4,6 +4,8 @@ module DrawioDsl
4
4
  module Schema
5
5
  # Shape is a graphical element, it can be a shape, a text, or a group (todo)
6
6
  class Shape < Node
7
+ include DrawioDsl::Formatters::Factory
8
+
7
9
  class << self
8
10
  attr_reader :shape_defaults
9
11
 
@@ -23,6 +25,7 @@ module DrawioDsl
23
25
 
24
26
  attr_accessor :theme
25
27
  attr_accessor :title
28
+ attr_accessor :value
26
29
 
27
30
  # The style of the element, these will derive from the page style if not provided
28
31
  attr_accessor :white_space
@@ -44,11 +47,15 @@ module DrawioDsl
44
47
  attr_accessor :h
45
48
  attr_accessor :style_modifiers
46
49
 
47
- def initialize(page, **args)
50
+ def initialize(page, **args, &block)
48
51
  args[:classification] = :shape
49
52
  super(page, **args)
50
53
 
51
54
  apply_defaults(args)
55
+
56
+ instance_eval(&block) if block_given?
57
+
58
+ @value = formatter.empty? ? title : formatter.as_html
52
59
  end
53
60
 
54
61
  def shape_defaults
@@ -101,7 +108,7 @@ module DrawioDsl
101
108
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
102
109
 
103
110
  def as_xml(xml)
104
- xml.mxCell(id: id, value: title, style: style, vertex: 1, parent: parent&.id) do
111
+ xml.mxCell(id: id, value: value, style: style, vertex: 1, parent: parent&.id) do
105
112
  xml.mxGeometry(x: x, y: y, width: w, height: h, as: 'geometry')
106
113
  end
107
114
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DrawioDsl
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
data/lib/drawio_dsl.rb CHANGED
@@ -12,6 +12,7 @@ require_relative 'drawio_dsl/configuration_shapes'
12
12
  require_relative 'drawio_dsl/configuration_themes'
13
13
  require_relative 'drawio_dsl/configuration'
14
14
  require_relative 'drawio_dsl/version'
15
+ require_relative 'drawio_dsl/formatters/_'
15
16
  require_relative 'drawio_dsl/schema/_'
16
17
  require_relative 'drawio_dsl/dom_builder_shapes'
17
18
  require_relative 'drawio_dsl/dom_builder'
data/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "drawio_dsl",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "drawio_dsl",
9
- "version": "0.6.0",
9
+ "version": "0.7.0",
10
10
  "devDependencies": {
11
11
  "@klueless-js/semantic-release-rubygem": "github:klueless-js/semantic-release-rubygem",
12
12
  "@semantic-release/changelog": "^6.0.1",
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drawio_dsl",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "DrawIO DSL can build DrawIO diagrams using a Domain Specific Language",
5
5
  "scripts": {
6
6
  "release": "semantic-release"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drawio_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-13 00:00:00.000000000 Z
11
+ date: 2022-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: k_config
@@ -96,6 +96,7 @@ files:
96
96
  - ".builders/generators/sample_diagrams/16-grid-alignment.rb"
97
97
  - ".builders/generators/sample_diagrams/20-styles.rb"
98
98
  - ".builders/generators/sample_diagrams/25-themes.rb"
99
+ - ".builders/generators/sample_diagrams/30-html-shapes.rb"
99
100
  - ".builders/generators/sample_diagrams/50-willoughby-council.rb"
100
101
  - ".releaserc.json"
101
102
  - ".rspec"
@@ -295,6 +296,13 @@ files:
295
296
  - lib/drawio_dsl/drawio_extensions.rb
296
297
  - lib/drawio_dsl/drawio_extensions_active.rb
297
298
  - lib/drawio_dsl/drawio_shapes.rb
299
+ - lib/drawio_dsl/formatters/_.rb
300
+ - lib/drawio_dsl/formatters/base_formatter.rb
301
+ - lib/drawio_dsl/formatters/factory.rb
302
+ - lib/drawio_dsl/formatters/html_builder.rb
303
+ - lib/drawio_dsl/formatters/interface_formatter.rb
304
+ - lib/drawio_dsl/formatters/klass_formatter.rb
305
+ - lib/drawio_dsl/formatters/style_builder.rb
298
306
  - lib/drawio_dsl/layout_engine.rb
299
307
  - lib/drawio_dsl/schema/_.rb
300
308
  - lib/drawio_dsl/schema/common_style.rb
@@ -334,6 +342,8 @@ files:
334
342
  - lib/drawio_dsl/schema/shapes/h5.rb
335
343
  - lib/drawio_dsl/schema/shapes/h6.rb
336
344
  - lib/drawio_dsl/schema/shapes/hexagon.rb
345
+ - lib/drawio_dsl/schema/shapes/interface.rb
346
+ - lib/drawio_dsl/schema/shapes/klass.rb
337
347
  - lib/drawio_dsl/schema/shapes/note.rb
338
348
  - lib/drawio_dsl/schema/shapes/p.rb
339
349
  - lib/drawio_dsl/schema/shapes/process.rb