Class: Thinreports::BasicReport::Layout::LegacySchema

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/thinreports/basic_report/layout/legacy_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank_value?, #call_block_in, #deep_copy, included

Constructor Details

#initialize(legacy_schema) ⇒ LegacySchema

Returns a new instance of LegacySchema.



12
13
14
15
16
17
18
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 12

def initialize(legacy_schema)
  @legacy_schema = legacy_schema
  @legacy_svg = legacy_schema['svg'].dup
  @legacy_item_schemas = extract_legacy_item_schemas(legacy_svg)

  @legacy_svg = cleanup_svg(@legacy_svg)
end

Instance Attribute Details

#legacy_item_schemasObject (readonly)

Returns the value of attribute legacy_item_schemas.



43
44
45
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 43

def legacy_item_schemas
  @legacy_item_schemas
end

#legacy_schemaObject (readonly)

Returns the value of attribute legacy_schema.



43
44
45
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 43

def legacy_schema
  @legacy_schema
end

#legacy_svgObject (readonly)

Returns the value of attribute legacy_svg.



43
44
45
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 43

def legacy_svg
  @legacy_svg
end

Instance Method Details

#build_item_schemas_from_svg(svg_elements) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 50

def build_item_schemas_from_svg(svg_elements)
  return [] unless svg_elements

  items = []

  svg_elements.each do |item_element|
    item_attributes = item_element.attributes

    items <<
      case item_element.attributes['class']
      when 's-text' then text_item_schema(item_attributes, extract_texts_from(item_element))
      when 's-image' then image_item_schema(item_attributes)
      when 's-rect' then rect_item_schema(item_attributes)
      when 's-ellipse' then ellipse_item_schema(item_attributes)
      when 's-line' then line_item_schema(item_attributes)
      when 's-tblock' then text_block_item_schema(item_attributes)
      when 's-iblock' then image_block_item_schema(item_attributes)
      when 's-pageno' then page_number_item_schema(item_attributes)
      when 's-list' then list_item_schema(item_element)
      else raise 'Unknown item type'
      end
  end

  items
end

#cleanup_svg(svg) ⇒ Object



376
377
378
379
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 376

def cleanup_svg(svg)
  cleaned_svg = svg.gsub(/<!--SHAPE.*?SHAPE-->/, '')
  cleaned_svg.gsub(/<!--LAYOUT(.*?)LAYOUT-->/) { $1 }
end

#display(legacy_display) ⇒ Object



323
324
325
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 323

def display(legacy_display)
  legacy_display == 'true'
end

#ellipse_item_schema(attributes) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 135

def ellipse_item_schema(attributes)
  {
    'id' => attributes['x-id'],
    'type' => 'ellipse',
    'cx' => attributes['cx'].to_f,
    'cy' => attributes['cy'].to_f,
    'rx' => attributes['rx'].to_f,
    'ry' => attributes['ry'].to_f,
    'display' => display(attributes['x-display']),
    'style' => {
      'border-width' => attributes['stroke-width'].to_f,
      'border-color' => attributes['stroke'],
      'border-style' => attributes['x-stroke-type'],
      'fill-color' => attributes['fill']
    }
  }
end

#extract_legacy_item_schemas(svg) ⇒ Object



367
368
369
370
371
372
373
374
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 367

def extract_legacy_item_schemas(svg)
  items = {}
  svg.scan(/<!--SHAPE(.*?)SHAPE-->/) do |(item_schema_json)|
    item_schema = JSON.parse(item_schema_json)
    items[item_schema['id']] = item_schema
  end
  items
end

#extract_texts_from(text_item_element) ⇒ Object



309
310
311
312
313
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 309

def extract_texts_from(text_item_element)
  [].tap do |texts|
    text_item_element.each_element('text') { |e| texts << e.text }
  end
end

#font_style(attributes) ⇒ Object



327
328
329
330
331
332
333
334
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 327

def font_style(attributes)
  style = []
  style << 'bold' if attributes['font-weight'] == 'bold'
  style << 'italic' if attributes['font-style'] == 'italic'
  style << 'underline' if attributes['text-decoration'].include?('underline')
  style << 'linethrough' if attributes['text-decoration'].include?('line-through')
  style
end

#image_block_item_schema(attributes) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 193

def image_block_item_schema(attributes)
  {
    'id' => attributes['x-id'],
    'type' => Core::Shape::ImageBlock::TYPE_NAME,
    'x' => attributes['x-left'].to_f,
    'y' => attributes['x-top'].to_f,
    'width' => attributes['x-width'].to_f,
    'height' => attributes['x-height'].to_f,
    'display' => display(attributes['x-display']),
    'style' => {
      'position-x' => attributes['x-position-x'],
      'position-y' => image_position_y(attributes['x-position-y'])
    }
  }
end

#image_item_schema(attributes) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 153

def image_item_schema(attributes)
  _, image_type, image_data = attributes['xlink:href'].match(%r{^data:(image/[a-z]+?);base64,(.+)}).to_a

  {
    'id' => attributes['x-id'],
    'type' => 'image',
    'x' => attributes['x'].to_f,
    'y' => attributes['y'].to_f,
    'width' => attributes['width'].to_f,
    'height' => attributes['height'].to_f,
    'display' => display(attributes['x-display']),
    'data' => {
      'mime-type' => image_type,
      'base64' => image_data
    }
  }
end

#image_position_y(legacy_position_y) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 315

def image_position_y(legacy_position_y)
  case legacy_position_y
  when 'top' then 'top'
  when 'center' then 'middle'
  when 'bottom' then 'bottom'
  end
end

#item_schemasObject



45
46
47
48
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 45

def item_schemas
  svg = REXML::Document.new(legacy_svg)
  build_item_schemas_from_svg(svg.elements['/svg/g'])
end

#letter_spacing(legacy_letter_spacing) ⇒ Object



360
361
362
363
364
365
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 360

def letter_spacing(legacy_letter_spacing)
  case legacy_letter_spacing
  when 'auto', '' then ''
  else legacy_letter_spacing.to_f
  end
end

#line_height(legacy_line_height) ⇒ Object



356
357
358
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 356

def line_height(legacy_line_height)
  blank_value?(legacy_line_height) ? '' : legacy_line_height.to_f
end

#line_item_schema(attributes) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 118

def line_item_schema(attributes)
  {
    'id' => attributes['x-id'],
    'type' => 'line',
    'x1' => attributes['x1'].to_f,
    'y1' => attributes['y1'].to_f,
    'x2' => attributes['x2'].to_f,
    'y2' => attributes['y2'].to_f,
    'display' => display(attributes['x-display']),
    'style' => {
      'border-width' => attributes['stroke-width'].to_f,
      'border-color' => attributes['stroke'],
      'border-style' => attributes['x-stroke-type']
    }
  }
end

#list_item_schema(legacy_element) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 259

def list_item_schema(legacy_element)
  legacy_schema = legacy_item_schemas[legacy_element.attributes['x-id']]

  header = list_section_schema('header', legacy_element, legacy_schema)
  detail = list_section_schema('detail', legacy_element, legacy_schema)
  page_footer = list_section_schema('page-footer', legacy_element, legacy_schema)
  footer = list_section_schema('footer', legacy_element, legacy_schema)

  schema = {
    'id' => legacy_schema['id'],
    'type' => Core::Shape::List::TYPE_NAME,
    'content-height' => legacy_schema['content-height'].to_f,
    'auto-page-break' => legacy_schema['page-break'] == 'true',
    'display' => display(legacy_schema['display']),
    'header' => header,
    'detail' => detail,
    'page-footer' => page_footer,
    'footer' => footer
  }

  page_footer['translate']['y'] += detail['height'] if page_footer['enabled']

  if footer['enabled']
    footer['translate']['y'] += detail['height']
    footer['translate']['y'] += page_footer['height'] if page_footer['enabled']
  end
  schema
end

#list_section_schema(section_name, legacy_list_element, legacy_list_schema) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 288

def list_section_schema(section_name, legacy_list_element, legacy_list_schema)
  legacy_section_schema = legacy_list_schema[section_name]
  return {} if legacy_section_schema.empty?

  section_item_elements = legacy_list_element.elements["g[@class='s-list-#{section_name}']"]

  section_schema = {
    'height' => legacy_section_schema['height'].to_f,
    'translate' => {
      'x' => legacy_section_schema['translate']['x'].to_f,
      'y' => legacy_section_schema['translate']['y'].to_f
    },
    'items' => build_item_schemas_from_svg(section_item_elements)
  }

  unless section_name == 'detail'
    section_schema['enabled'] = legacy_list_schema["#{section_name}-enabled"] == 'true'
  end
  section_schema
end

#page_number_item_schema(attributes) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 171

def page_number_item_schema(attributes)
  {
    'id' => attributes['x-id'],
    'type' => Core::Shape::PageNumber::TYPE_NAME,
    'x' => attributes['x-left'].to_f,
    'y' => attributes['x-top'].to_f,
    'width' => attributes['x-width'].to_f,
    'height' => attributes['x-height'].to_f,
    'format' => attributes['x-format'],
    'target' => attributes['x-target'],
    'display' => display(attributes['x-display']),
    'style' => {
      'font-family' => [attributes['font-family']],
      'font-size' => attributes['font-size'].to_f,
      'color' => attributes['fill'],
      'font-style' => font_style(attributes),
      'text-align' => text_align(attributes['text-anchor']),
      'overflow' => attributes['x-overflow']
    }
  }
end

#rect_item_schema(attributes) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 99

def rect_item_schema(attributes)
  {
    'id' => attributes['x-id'],
    'type' => 'rect',
    'x' => attributes['x'].to_f,
    'y' => attributes['y'].to_f,
    'width' => attributes['width'].to_f,
    'height' => attributes['height'].to_f,
    'display' => display(attributes['x-display']),
    'border-radius' => attributes['rx'].to_i,
    'style' => {
      'border-width' => attributes['stroke-width'].to_f,
      'border-color' => attributes['stroke'],
      'border-style' => attributes['x-stroke-type'],
      'fill-color' => attributes['fill']
    }
  }
end

#text_align(legacy_text_align) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 336

def text_align(legacy_text_align)
  case legacy_text_align
  when 'start' then 'left'
  when 'middle' then 'center'
  when 'end' then 'right'
  else 'left'
  end
end

#text_block_item_schema(attributes) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 209

def text_block_item_schema(attributes)
  text_format = {
    'base' => attributes['x-format-base'],
    'type' => attributes['x-format-type']
  }
  case text_format['type']
  when 'datetime'
    text_format['datetime'] = {
      'format' => attributes['x-format-datetime-format']
    }
  when 'number'
    text_format['number'] = {
      'delimiter' => attributes['x-format-number-delimiter'],
      'precision' => attributes['x-format-number-precision'].to_i
    }
  when 'padding'
    text_format['padding'] = {
      'length' => attributes['x-format-padding-length'].to_i,
      'char' => attributes['x-format-padding-char'],
      'direction' => attributes['x-format-padding-direction']
    }
  end

  {
    'id' => attributes['x-id'],
    'type' => Core::Shape::TextBlock::TYPE_NAME,
    'x' => attributes['x-left'].to_f,
    'y' => attributes['x-top'].to_f,
    'width' => attributes['x-width'].to_f,
    'height' => attributes['x-height'].to_f,
    'display' => display(attributes['x-display']),
    'value' => attributes['x-value'],
    'multiple-line' => attributes['x-multiple'] == 'true',
    'format' => text_format,
    'reference-id' => attributes['x-ref-id'],
    'style' => {
      'font-family' => [attributes['font-family']],
      'font-size' => attributes['font-size'].to_f,
      'color' => attributes['fill'],
      'font-style' => font_style(attributes),
      'text-align' => text_align(attributes['text-anchor']),
      'vertical-align' => vertical_align(attributes['x-valign']),
      'line-height' => line_height(attributes['x-line-height']),
      'letter-spacing' => letter_spacing(attributes['kerning']),
      'overflow' => attributes['x-overflow'],
      'word-wrap' => attributes['x-word-wrap'] || ''
    }
  }
end

#text_item_schema(attributes, texts) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 76

def text_item_schema(attributes, texts)
  {
    'id' => attributes['x-id'],
    'type' => Core::Shape::Text::TYPE_NAME,
    'x' => attributes['x-left'].to_f,
    'y' => attributes['x-top'].to_f,
    'width' => attributes['x-width'].to_f,
    'height' => attributes['x-height'].to_f,
    'display' => display(attributes['x-display']),
    'texts' => texts,
    'style' => {
      'font-family' => [attributes['font-family']],
      'font-size' => attributes['font-size'].to_f,
      'color' => attributes['fill'],
      'font-style' => font_style(attributes),
      'text-align' => text_align(attributes['text-anchor']),
      'vertical-align' => vertical_align(attributes['x-valign']),
      'line-height' => line_height(attributes['x-line-height']),
      'letter-spacing' => letter_spacing(attributes['kerning'])
    }
  }
end

#upgradeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 20

def upgrade
  config = legacy_schema['config']
  page_config = config['page']

  {
    'version' => legacy_schema['version'],
    'title' => legacy_schema['config']['title'],
    'report' => {
      'paper-type' => page_config['paper-type'],
      'width' => page_config['width'].to_f,
      'height' => page_config['height'].to_f,
      'orientation' => page_config['orientation'],
      'margin' => page_config.values_at(
        'margin-top',
        'margin-right',
        'margin-bottom',
        'margin-left'
      ).map(&:to_f)
    },
    'items' => item_schemas
  }
end

#vertical_align(legacy_vertical_align) ⇒ Object



345
346
347
348
349
350
351
352
353
354
# File 'lib/thinreports/basic_report/layout/legacy_schema.rb', line 345

def vertical_align(legacy_vertical_align)
  return '' unless legacy_vertical_align

  case legacy_vertical_align
  when 'top' then 'top'
  when 'center' then 'middle'
  when 'bottom' then 'bottom'
  else 'top'
  end
end