drawio_dsl 0.9.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,33 +4,18 @@
4
4
  module DrawioDsl
5
5
  # Configuration container for the DrawIO DSL
6
6
  class Configuration
7
- include DrawioDsl::ConfigurationShapes
8
- include DrawioDsl::ConfigurationThemes
7
+ extend Forwardable
9
8
 
10
9
  include KLog::Logging
11
10
 
12
- BaseStyle = Struct.new(:white_space, :html, :rounded, :shadow, :sketch, :glass, keyword_init: true)
13
- ElementConfig = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
14
- LineConfig = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
15
- TextConfig = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
11
+ BaseStyle = Struct.new(:white_space, :html, :rounded, :shadow, :sketch, :glass, keyword_init: true)
16
12
 
17
13
  attr_accessor :base_style
18
14
 
19
- attr_accessor :shapes
15
+ def_delegators :shape, :element, :line, :text
20
16
 
21
17
  def initialize
22
18
  @base_style = BaseStyle.new(white_space: :wrap, html: 1, rounded: nil, shadow: nil, sketch: nil, glass: nil)
23
-
24
- add_shapes
25
- add_themes
26
- end
27
-
28
- def palette(theme)
29
- themes[theme]
30
- end
31
-
32
- def random_theme
33
- themes.keys.sample
34
19
  end
35
20
 
36
21
  def stroke(type)
@@ -60,82 +45,153 @@ module DrawioDsl
60
45
  end
61
46
  end
62
47
 
63
- def element(type)
64
- elements[type]
48
+ def shape
49
+ @shape ||= Shape.new(source_config['shape'])
65
50
  end
66
51
 
67
- def elements
68
- return @elements if defined? @elements
69
-
70
- @elements = {}
71
- source_config['shapes'].select { |shape| shape['category'] == 'element' }.each do |element|
72
- @elements[element['type'].to_sym] = ElementConfig.new(
73
- type: element['type'].to_sym,
74
- x: element['x'].to_i,
75
- y: element['y'].to_i,
76
- w: element['w'].to_i,
77
- h: element['h'].to_i,
78
- style_modifiers: element['style_modifiers']
79
- )
80
- end
52
+ def connector
53
+ @connector ||= Connector.new(source_config['connector'])
54
+ end
81
55
 
82
- @elements
56
+ def theme
57
+ @theme ||= Theme.new(source_config['theme'])
83
58
  end
84
59
 
85
- def line(type)
86
- lines[type]
60
+ def source_config
61
+ return @source_config if defined? @source_config
62
+
63
+ @source_config = begin
64
+ file = File.join(DrawioDsl::ROOT_PATH, 'config/configuration.json')
65
+ JSON.parse(File.read(file))
66
+ end
87
67
  end
88
68
 
89
- def lines
90
- return @lines if defined? @lines
91
-
92
- @lines = {}
93
- source_config['shapes'].select { |shape| shape['category'] == 'line' }.each do |line|
94
- @lines[line['type'].to_sym] = LineConfig.new(
95
- type: line['type'].to_sym,
96
- x: line['x'].to_i,
97
- y: line['y'].to_i,
98
- w: line['w'].to_i,
99
- h: line['h'].to_i,
100
- style_modifiers: line['style_modifiers']
69
+ class Shape
70
+ attr_reader :source_config
71
+
72
+ ElementShapeConfig = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
73
+ LineShapeConfig = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
74
+ TextShapeConfig = Struct.new(:type, :x, :y, :w, :h, :style_modifiers, keyword_init: true)
75
+
76
+ def initialize(source_config)
77
+ @source_config = source_config
78
+ end
79
+
80
+ # Elements
81
+
82
+ def element(type)
83
+ elements[type] || ElementShapeConfig.new(
84
+ type: :square,
85
+ x: 0,
86
+ y: 0,
87
+ w: 160,
88
+ h: 160,
89
+ style_modifiers: ''
101
90
  )
102
91
  end
103
92
 
104
- @lines
105
- end
93
+ def elements
94
+ return @elements if defined? @elements
95
+
96
+ @elements = {}
97
+ source_config['elements'].each do |element|
98
+ @elements[element['type'].to_sym] = ElementShapeConfig.new(
99
+ type: element['type'].to_sym,
100
+ x: element['x'].to_i,
101
+ y: element['y'].to_i,
102
+ w: element['w'].to_i,
103
+ h: element['h'].to_i,
104
+ style_modifiers: element['style_modifiers']
105
+ )
106
+ end
106
107
 
107
- def text(type)
108
- texts[type]
109
- end
108
+ @elements
109
+ end
110
+
111
+ def element_types
112
+ elements.keys
113
+ end
114
+
115
+ def random_element_type
116
+ elements.values.sample.type
117
+ end
118
+
119
+ # Lines
110
120
 
111
- def texts
112
- return @texts if defined? @texts
113
-
114
- @texts = {}
115
- source_config['shapes'].select { |shape| shape['category'] == 'text' }.each do |text|
116
- @texts[text['type'].to_sym] = TextConfig.new(
117
- type: text['type'].to_sym,
118
- x: text['x'].to_i,
119
- y: text['y'].to_i,
120
- w: text['w'].to_i,
121
- h: text['h'].to_i,
122
- style_modifiers: text['style_modifiers']
121
+ def line(type)
122
+ lines[type] || LineShapeConfig.new(
123
+ type: :solid,
124
+ x: 0,
125
+ y: 0,
126
+ w: 50,
127
+ h: 50,
128
+ style_modifiers: 'edgeStyle=none;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0'
123
129
  )
124
130
  end
125
131
 
126
- @texts
127
- end
132
+ def lines
133
+ return @lines if defined? @lines
134
+
135
+ @lines = {}
136
+ source_config['lines'].each do |line|
137
+ @lines[line['type'].to_sym] = LineShapeConfig.new(
138
+ type: line['type'].to_sym,
139
+ x: line['x'].to_i,
140
+ y: line['y'].to_i,
141
+ w: line['w'].to_i,
142
+ h: line['h'].to_i,
143
+ style_modifiers: line['style_modifiers']
144
+ )
145
+ end
128
146
 
129
- def connector
130
- @connector ||= Connector.new(source_config['connector'])
131
- end
147
+ @lines
148
+ end
132
149
 
133
- def source_config
134
- return @source_config if defined? @source_config
150
+ def line_types
151
+ lines.keys
152
+ end
135
153
 
136
- @source_config = begin
137
- file = File.join(DrawioDsl::ROOT_PATH, 'config/configuration.json')
138
- JSON.parse(File.read(file))
154
+ def random_line_type
155
+ lines.values.sample.type
156
+ end
157
+
158
+ def text(type)
159
+ texts[type] || TextShapeConfig.new(
160
+ type: :p,
161
+ x: 0,
162
+ y: 0,
163
+ w: 100,
164
+ h: 50,
165
+ style_modifiers: 'text;fontSize=16;fontStyle=1;fillColor=none'
166
+ )
167
+ end
168
+
169
+ # Texts
170
+
171
+ def texts
172
+ return @texts if defined? @texts
173
+
174
+ @texts = {}
175
+ source_config['texts'].each do |text|
176
+ @texts[text['type'].to_sym] = TextShapeConfig.new(
177
+ type: text['type'].to_sym,
178
+ x: text['x'].to_i,
179
+ y: text['y'].to_i,
180
+ w: text['w'].to_i,
181
+ h: text['h'].to_i,
182
+ style_modifiers: text['style_modifiers']
183
+ )
184
+ end
185
+
186
+ @texts
187
+ end
188
+
189
+ def text_types
190
+ texts.keys
191
+ end
192
+
193
+ def random_text_type
194
+ texts.values.sample.type
139
195
  end
140
196
  end
141
197
 
@@ -209,6 +265,89 @@ module DrawioDsl
209
265
  @designs
210
266
  end
211
267
  end
268
+
269
+ class Theme
270
+ attr_reader :source_config
271
+
272
+ BackgroundThemeConfig = Struct.new(:type, :bg_color, :font_color, :favourite, keyword_init: true)
273
+ ElementThemeConfig = Struct.new(:type, :fill_color, :stroke_color, :font_color, :gradient, keyword_init: true)
274
+
275
+ def initialize(source_config)
276
+ @source_config = source_config
277
+ end
278
+
279
+ def background(type)
280
+ backgrounds[type] || BackgroundThemeConfig.new(
281
+ type: type,
282
+ bg_color: '#000000',
283
+ font_color: '#FFFFFF',
284
+ favourite: false
285
+ )
286
+ end
287
+
288
+ def backgrounds
289
+ return @backgrounds if defined? @backgrounds
290
+
291
+ @backgrounds = {}
292
+ source_config['backgrounds'].each do |background|
293
+ @backgrounds[background['type'].to_sym] = BackgroundThemeConfig.new(
294
+ type: background['type'].to_sym,
295
+ bg_color: background['bg_color'],
296
+ font_color: background['font_color'],
297
+ favourite: background['favourite'] == 1
298
+ )
299
+ end
300
+
301
+ @backgrounds
302
+ end
303
+
304
+ def background_types
305
+ backgrounds.keys
306
+ end
307
+
308
+ def favourite_background_types
309
+ backgrounds.values.select(&:favourite).map(&:type)
310
+ end
311
+
312
+ def random_background_type
313
+ backgrounds.values.sample.type
314
+ end
315
+
316
+ def element(type)
317
+ elements[type] || ElementThemeConfig.new(
318
+ type: type,
319
+ fill_color: '#ffffff',
320
+ stroke_color: '#000000',
321
+ font_color: '#000000',
322
+ gradient: nil
323
+ )
324
+ end
325
+
326
+ def elements
327
+ return @elements if defined? @elements
328
+
329
+ @elements = {}
330
+ source_config['elements'].each do |element|
331
+ @elements[element['type'].to_sym] = ElementThemeConfig.new(
332
+ type: element['type'].to_sym,
333
+ fill_color: element['fill_color'],
334
+ stroke_color: element['stroke_color'],
335
+ font_color: element['font_color'],
336
+ gradient: element['gradient']
337
+ )
338
+ end
339
+
340
+ @elements
341
+ end
342
+
343
+ def element_types
344
+ elements.keys
345
+ end
346
+
347
+ def random_element_type
348
+ elements.values.sample.type
349
+ end
350
+ end
212
351
  end
213
352
  end
214
353
 
@@ -6,16 +6,17 @@ module DrawioDsl
6
6
  class Diagram
7
7
  attr_accessor :host
8
8
  attr_accessor :theme
9
+ attr_accessor :bg_theme
9
10
  attr_accessor :style
10
11
  attr_accessor :palette
11
12
  attr_accessor :pages
12
13
 
13
14
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
14
15
  def initialize(**args)
15
- @host = args[:host] || SecureRandom.alphanumeric(3)
16
-
17
- # Apply a random theme to the diagram if none is specified.
18
- @theme = args[:theme] || KConfig.configuration.drawio.random_theme
16
+ @host = args[:host] || SecureRandom.alphanumeric(3)
17
+ # TODO: assess and resolve this inconsistency
18
+ @theme = args[:theme] || KConfig.configuration.drawio.theme.random_background_type
19
+ @bg_theme = args[:bg_theme] || :not_set
19
20
 
20
21
  @style = DrawioDsl::Schema::CommonStyle.new(**args) do
21
22
  default_style = KConfig.configuration.drawio.base_style
@@ -30,19 +31,24 @@ module DrawioDsl
30
31
  end
31
32
 
32
33
  @palette = DrawioDsl::Schema::DefaultPalette.new(self, **args) do |diagram|
33
- theme_palette = KConfig.configuration.drawio.palette(diagram.theme)
34
-
35
- # Inherit from theme when specific palette options are not specified.
36
- @fill_color ||= theme_palette.fill_color
37
- @stroke_color ||= theme_palette.stroke_color
38
- @font_color ||= theme_palette.element_font_color
39
- @gradient ||= theme_palette.gradient
34
+ @fill_color ||= diagram.theme_palette.fill_color
35
+ @stroke_color ||= diagram.theme_palette.stroke_color
36
+ @font_color ||= diagram.theme_palette.font_color
37
+ @gradient ||= diagram.theme_palette.gradient
40
38
  end
41
39
 
42
40
  @pages = args[:pages] || []
43
41
  end
44
42
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
45
43
 
44
+ def theme_palette
45
+ @theme_palette ||= KConfig.configuration.drawio.theme.element(theme)
46
+ end
47
+
48
+ def bg_theme_palette
49
+ @bg_theme_palette ||= KConfig.configuration.drawio.theme.background(bg_theme)
50
+ end
51
+
46
52
  def to_h
47
53
  {
48
54
  host: host,
@@ -16,7 +16,7 @@ module DrawioDsl
16
16
  @fill_color = args[:fill_color] || theme_palette.fill_color
17
17
  @stroke_color = args[:stroke_color] || theme_palette.stroke_color
18
18
  @gradient = args[:gradient] || theme_palette.gradient
19
- @font_color = args[:font_color] || theme_palette.element_font_color
19
+ @font_color = args[:font_color] || theme_palette.font_color
20
20
  end
21
21
  end
22
22
  end
@@ -14,6 +14,7 @@ module DrawioDsl
14
14
  attr_accessor :active
15
15
  attr_reader :name
16
16
  attr_reader :theme
17
+ attr_reader :bg_theme
17
18
  attr_reader :style
18
19
  attr_reader :palette
19
20
  attr_reader :margin_left
@@ -43,6 +44,7 @@ module DrawioDsl
43
44
  @active = args[:active].nil? ? true : !args[:active].nil?
44
45
  @name = args[:name]
45
46
  @theme = args[:theme] || diagram.theme
47
+ @bg_theme = args[:bg_theme] || diagram.bg_theme
46
48
 
47
49
  # cursor positioning is used by the layout engine
48
50
  @position_x = 0
@@ -64,7 +66,7 @@ module DrawioDsl
64
66
  @page_scale = args[:page_scale] || 1
65
67
  @page_width = args[:page_width] || 1169 # A4
66
68
  @page_height = args[:page_height] || 827 # A4
67
- @background = args[:background] || '#FFFACD'
69
+ @background = args[:background] || bg_theme_palette.bg_color
68
70
  @page_shadow = args[:page_shadow] || 0
69
71
  @math = args[:math] || 0
70
72
 
@@ -82,7 +84,7 @@ module DrawioDsl
82
84
  # Inherit from theme when specific palette options are not specified.
83
85
  @fill_color ||= page.theme_palette.fill_color
84
86
  @stroke_color ||= page.theme_palette.stroke_color
85
- @font_color ||= page.theme_palette.element_font_color
87
+ @font_color ||= page.theme_palette.font_color
86
88
  @gradient ||= page.theme_palette.gradient
87
89
  end
88
90
 
@@ -100,7 +102,11 @@ module DrawioDsl
100
102
  end
101
103
 
102
104
  def theme_palette
103
- @theme_palette ||= KConfig.configuration.drawio.palette(theme)
105
+ @theme_palette ||= KConfig.configuration.drawio.theme.element(theme)
106
+ end
107
+
108
+ def bg_theme_palette
109
+ @bg_theme_palette ||= KConfig.configuration.drawio.theme.background(bg_theme)
104
110
  end
105
111
 
106
112
  def as_xml(xml)
@@ -142,7 +142,12 @@ module DrawioDsl
142
142
  end
143
143
 
144
144
  def theme_palette
145
- @theme_palette ||= KConfig.configuration.drawio.palette(theme)
145
+ @theme_palette ||= KConfig.configuration.drawio.theme.element(theme)
146
+ end
147
+
148
+ # TODO: test
149
+ def bg_theme_palette
150
+ @bg_theme_palette ||= KConfig.configuration.drawio.theme.background(bg_theme)
146
151
  end
147
152
 
148
153
  # :nocov:
@@ -16,7 +16,7 @@ module DrawioDsl
16
16
  @fill_color = args[:fill_color]
17
17
  @stroke_color = args[:stroke_color]
18
18
  @gradient = args[:gradient]
19
- @font_color = args[:font_color] || theme_palette.text_font_color
19
+ @font_color = args[:font_color] || page.bg_theme_palette.font_color
20
20
  end
21
21
  end
22
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DrawioDsl
4
- VERSION = '0.9.0'
4
+ VERSION = '0.11.0'
5
5
  end
data/lib/drawio_dsl.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'securerandom'
4
4
  require 'nokogiri'
5
-
5
+ require 'forwardable'
6
6
  require 'k_config'
7
7
  require 'k_log'
8
8
  require 'k_director'
@@ -15,8 +15,6 @@ module DrawioDsl
15
15
  end
16
16
 
17
17
  require_relative 'drawio_dsl/configuration_extension'
18
- require_relative 'drawio_dsl/configuration_shapes'
19
- require_relative 'drawio_dsl/configuration_themes'
20
18
  require_relative 'drawio_dsl/configuration'
21
19
  require_relative 'drawio_dsl/version'
22
20
  require_relative 'drawio_dsl/formatters/_'
data/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "drawio_dsl",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "drawio_dsl",
9
- "version": "0.9.0",
9
+ "version": "0.11.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.9.0",
3
+ "version": "0.11.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.9.0
4
+ version: 0.11.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-23 00:00:00.000000000 Z
11
+ date: 2022-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: k_config
@@ -255,7 +255,8 @@ files:
255
255
  - docs/printspeak-architecture-controllers-fat.svg
256
256
  - docs/printspeak-architecture-controllers-thin.svg
257
257
  - docs/printspeak-architecture-controllers.drawio
258
- - docs/printspeak-architecture-generator.drawio
258
+ - docs/printspeak-architecture-generator-custom.drawio
259
+ - docs/printspeak-architecture-generator-custom.png
259
260
  - docs/printspeak-architecture-generator.svg
260
261
  - docs/project-plan.md
261
262
  - docs/project-plan/project.drawio
@@ -281,8 +282,6 @@ files:
281
282
  - lib/drawio_dsl.rb
282
283
  - lib/drawio_dsl/configuration.rb
283
284
  - lib/drawio_dsl/configuration_extension.rb
284
- - lib/drawio_dsl/configuration_shapes.rb
285
- - lib/drawio_dsl/configuration_themes.rb
286
285
  - lib/drawio_dsl/dom_builder.rb
287
286
  - lib/drawio_dsl/dom_builder_shapes.rb
288
287
  - lib/drawio_dsl/drawio.rb