Class: Cornucopia::Util::PrettyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/util/pretty_formatter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unknown_string) ⇒ PrettyFormatter

Returns a new instance of PrettyFormatter.



12
13
14
15
# File 'lib/cornucopia/util/pretty_formatter.rb', line 12

def initialize(unknown_string)
  @unknown_string  = unknown_string
  @print_html_safe = @unknown_string.html_safe?
end

Class Method Details

.format_string(unknown_string) ⇒ Object



7
8
9
# File 'lib/cornucopia/util/pretty_formatter.rb', line 7

def format_string(unknown_string)
  PrettyFormatter.new(unknown_string).pretty_print
end

Instance Method Details

#end_grouping(safe_end, options = {}) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/cornucopia/util/pretty_formatter.rb', line 475

def end_grouping(safe_end, options = {})
  @indent_level -= 1

  if options[:group_end_same_line]
    @formatted_string.strip!
    @formatted_string << @unknown_string[@start_pos..@current_pos + safe_end.length - 1]
  else
    output_line(@unknown_string[@start_pos..@current_pos + safe_end.length - 1])

    if options[:end_append_text]
      @formatted_string << options[:end_append_text]
    end
  end

  @indent_level -= 1
  if @state_stack[-1] == :bottom_level ||
      (@state_stack[-1] == :group_implied && @state_stack[-2] == :bottom_level)
    if @state_stack[-1] == :bottom_level
      @state_stack.pop
    else
      @state_stack.pop
      @state_stack.pop
      @state_stack << :group_implied
    end

    @indent_level += 1
  end

  @current_pos   += safe_end.length - 1
  @start_pos     = @current_pos + 1
  @current_state = :value_end
end

#group_separator(separator_value, next_state, group_end_char, options = {}) ⇒ Object



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/cornucopia/util/pretty_formatter.rb', line 449

def group_separator(separator_value, next_state, group_end_char, options = {})
  if position_matches_string(@current_pos, separator_value)
    safe_separator = translate_value(separator_value)

    @formatted_string << @unknown_string[@start_pos..(@current_pos + safe_separator.length - 1)]
    @formatted_string.rstrip!

    if options[:append_text]
      @formatted_string << options[:append_text]
    end

    @start_pos   = @current_pos + safe_separator.length
    @current_pos += safe_separator.length - 1
    @state_stack << next_state
    @state_stack << :value
    @current_state = :whitespace

  elsif position_matches_string(@current_pos, group_end_char)
    end_grouping translate_value(group_end_char), options

  else
    # Some kind of error, exit out
    output_end
  end
end

#group_value_end(next_state, group_end_char, options = {}) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/cornucopia/util/pretty_formatter.rb', line 420

def group_value_end(next_state, group_end_char, options = {})
  @state_stack.pop

  if position_matches_string @current_pos, group_end_char
    output_line(@unknown_string[@start_pos..@current_pos - 1], options.merge(strip_output: true))
    @start_pos = @current_pos

    safe_end = translate_value(group_end_char)
    end_grouping safe_end, options
    @current_pos += 1
  else
    output_line(@unknown_string[@start_pos..@current_pos - 1], options.merge(strip_output: true))

    if options[:append_text]
      @formatted_string << options[:append_text]
    end

    @state_stack << next_state
    @state_stack << :value if options[:add_value]
    @start_pos     = @current_pos
    @current_state = :whitespace
  end
end

#output_endObject



541
542
543
544
545
546
547
548
549
# File 'lib/cornucopia/util/pretty_formatter.rb', line 541

def output_end
  unless @start_pos >= @unknown_string_len
    @formatted_string << "\n" unless @formatted_string[-1] == "\n"
    @formatted_string << @unknown_string[@start_pos..-1]
  end

  @current_pos = @unknown_string_len
  @start_pos   = @unknown_string_len
end

#output_line(text, options = {}) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/cornucopia/util/pretty_formatter.rb', line 569

def output_line(text, options = {})
  unless text.blank?
    unless options[:same_line]
      if (@formatted_string[-1] == " ")
        @formatted_string.rstrip!
      end

      @formatted_string << "\n" unless @formatted_string.length == 0 || @formatted_string[-1] == "\n"
      @formatted_string << " " * (2 * @indent_level)
    end

    @formatted_string << text

    if options[:strip_output]
      @formatted_string.rstrip!
    end
  end
end

#position_matches_string(test_position, test_string) ⇒ Object



562
563
564
565
566
567
# File 'lib/cornucopia/util/pretty_formatter.rb', line 562

def position_matches_string(test_position, test_string)
  safe_string = translate_value(test_string)

  test_position <= @unknown_string_len - safe_string.length &&
      @unknown_string[test_position..(test_position + safe_string.length - 1)] == safe_string
end

#pretty_printObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
258
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/cornucopia/util/pretty_formatter.rb', line 17

def pretty_print
  search_pos = 0

  do_pretty_print     = @unknown_string =~ /Parameters:[ \t]*\{.*?\}/
  formatted_class_pos = class_pos = -1
  while !do_pretty_print && class_pos && class_pos == formatted_class_pos
    search_pos += formatted_class_pos + 1

    if @print_html_safe
      class_pos           = @unknown_string[search_pos..-1] =~ /\#\&lt;[^ \t\n]+/
      formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\&lt;[^ \t\n]+[ \t]*\n/)
    else
      class_pos           = @unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+/
      formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+[ \t]*\n/)
    end

    do_pretty_print ||= (class_pos && class_pos != formatted_class_pos)
  end

  if do_pretty_print
    @loop_pos           = [-1, -1]
    @loop_count         = [0, 0]
    @indent_level       = 0
    @current_state      = :unknown
    @state_stack        = [:unknown]
    @current_pos        = 0
    @start_pos          = 0
    @unknown_string_len = @unknown_string.length
    @formatted_string   = "".dup

    while @current_pos < @unknown_string_len
      break if in_infinite_loop?

      case @current_state
        when :unknown
          search_pos          = @current_pos
          params_pos          = @unknown_string[search_pos..-1] =~ /Parameters:[ \t]*\{.*?\}/
          params_pos          += search_pos if params_pos
          formatted_class_pos = class_pos = -1
          while (class_pos && class_pos == formatted_class_pos)
            search_pos += formatted_class_pos + 1

            if @print_html_safe
              class_pos           = @unknown_string[search_pos..-1] =~ /\#\&lt;[^ \t\n]+/
              formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\&lt;[^ \t\n]+[ \t]*\n/)
            else
              class_pos           = @unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+/
              formatted_class_pos = (@unknown_string[search_pos..-1] =~ /\#\<[^ \t\n]+[ \t]*\n/)
            end
          end
          class_pos           += search_pos if class_pos
          formatted_class_pos += search_pos if formatted_class_pos

          if class_pos &&
              class_pos != formatted_class_pos &&
              (!params_pos || class_pos < params_pos)
            @current_pos = class_pos

            if @start_pos < @current_pos || @start_pos > 0
              output_line(@unknown_string[@start_pos..@current_pos - 1])
            else
              @indent_level -= 1
              @state_stack << :bottom_level
            end

            start_class
          else
            if params_pos
              @current_pos = params_pos
              @state_stack << :parameters

              if @start_pos < @current_pos || @start_pos > 0
                output_line(@unknown_string[@start_pos..@current_pos - 1])
              else
                @state_stack << :bottom_level
                @indent_level -= 1
              end

              @formatted_string.rstrip!
              @state_stack << :parameters_name
              @current_state = :value
              @start_pos     = @current_pos
              @indent_level  += 1
            else
              output_end
            end
          end

        when :value_end
          case @state_stack[-1]
            when :unknown
              @current_state = :unknown

            when :parameters
              if @current_pos + 1 < @unknown_string_len
                @formatted_string << "\n"
              end
              @indent_level -= 1
              @state_stack.pop
              @current_state = :unknown

            when :parameters_name
              group_value_end :parameters_start, "}"

            when :class_name
              group_value_end :variable,
                              ">",
                              end_append_text:     @indent_level == 0 ? "\n" : "",
                              append_text:         " ",
                              group_end_same_line: true,
                              add_value:           true
              @indent_level += 1

            when :variable
              if (position_matches_string(@current_pos - 1, ":") &&
                  ((!@print_html_safe &&
                      0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*({.*?}|\#<.*?>|\[.*?\]|\".*?\"|[^\n,\"]+)[ \t\r\n]*[,\>]/)) ||
                      (@print_html_safe &&
                          0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*({.*?}|\#&lt\;.*?&gt\;|\[.*?\]|&quot\;.*?&quot\;|[^\n,\"]+)[ \t\r\n]*(,|&gt\;)/))
                  ))
                @state_stack.pop
                @state_stack << :group_implied
                @state_stack << :hash_key_symbol_post
              elsif (!position_matches_string(@current_pos - 1, ":") &&
                  ((!@print_html_safe &&
                      0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*=>[ \t\r\n]*({.*?}|\#<.*?>|\[.*?\]|\".*?\"|[^\n,\"]+)[ \t\r\n]*[,\>]/)) ||
                      (@print_html_safe &&
                          0 == (@unknown_string[@current_pos..-1] =~ /^[ \t\r\n]*=&gt\;[ \t\r\n]*({.*?}|\#&lt\;.*?&gt\;|\[.*?\]|&quot\;.*?&quot\;|[^\n,\"&]+)[ \t\r\n]*(,|&gt\;)/))
                  ))
                @state_stack.pop
                @state_stack << :group_implied
                @state_stack << :hash_key_string
              else
                group_value_end :variable_equal,
                                ">",
                                append_text:         " ",
                                group_end_same_line: !@variable_set,
                                end_append_text:     @indent_level == 0 ? "\n" : ""
              end

            when :variable_value
              group_value_end :variable_comma, ">",
                              same_line:       true,
                              end_append_text: @indent_level == 0 ? "\n" : ""

            when :hash_key_symbol_post
              group_value_end :hash_value,
                              @state_stack[-2] == :group_implied ? ">" : "}",
                              append_text: " ",
                              add_value:   true

            when :hash_key, :hash_key_symbol, :hash_key_string
              group_value_end :hash_rocket,
                              @state_stack[-2] == :group_implied ? ">" : "}",
                              append_text: " "

            when :hash_value
              group_value_end :hash_comma,
                              @state_stack[-1] == :group_implied ? ">" : "}",
                              same_line: true

            when :array
              group_value_end :array_comma, "]"

            when :group_implied
              @current_pos += 1
          end

          @current_pos -= 1

        when :hash_rocket
          if @unknown_string[@current_pos] == ","
            group_separator ",",
                            :hash_value,
                            @state_stack[-2] == :group_implied ? ">" : "}",
                            append_text: " "
          else
            group_separator "=>",
                            :hash_value,
                            @state_stack[-2] == :group_implied ? ">" : "}",
                            append_text: " "
          end

        when :variable_equal
          @variable_set = true
          if @unknown_string[@current_pos] == ","
            group_separator ",",
                            :variable_value, ">",
                            end_append_text: @indent_level == 0 ? "\n" : ""
          else
            group_separator "=",
                            :variable_value, ">",
                            end_append_text: @indent_level == 0 ? "\n" : "",
                            append_text:     " "
          end

        when :variable_comma
          group_separator ",",
                          :variable, ">",
                          end_append_text: @indent_level == 0 ? "\n" : ""

        when :array_comma
          group_separator ",", :array, "]"

        when :hash_comma
          group_separator ",",
                          :hash_key,
                          @state_stack[-1] == :group_implied ? ">" : "}"
          if @state_stack[-1] == :group_implied
            @state_stack.pop
          end

        when :parameters_start
          start_grouping :hash_key

        when :whitespace
          case @unknown_string[@current_pos]
            when " ", "\r", "\n", "\t"
              @start_pos = @current_pos + 1

            else
              @start_pos     = @current_pos # probably redundant, but better safe than sorry...
              @current_pos   -= 1
              @current_state = @state_stack.pop
          end

        when :string
          if position_matches_string(@current_pos, "\\")
            @current_pos += 1
          elsif position_matches_string(@current_pos, "\"")
            @current_pos += translate_value("\"").length
            value_end
          end

        when :value
          case @unknown_string[@current_pos]
            when "{"
              if @start_pos == @current_pos
                start_grouping :hash_key
              else
                output_end
              end

            when "["
              if @start_pos == @current_pos
                start_grouping :array
              else
                output_end
              end

            when "\""
              if position_matches_string(@current_pos, "\"")
                start_quote
              end

            when "#"
              if @start_pos == @current_pos &&
                  @current_pos < @unknown_string_len - 2 &&
                  position_matches_string(@current_pos + 1, "<")
                start_class
              end

            when "="
              case @state_stack[-1]
                when :variable,
                    :hash_key,
                    :hash_key_string,
                    :hash_key_symbol,
                    :hash_key_symbol_post
                  value_end
              end

            when ":"
              case @state_stack[-1]
                when :hash_key
                  if @current_pos == @start_pos
                    @state_stack.pop
                    @state_stack << :hash_key_symbol
                  else
                    @state_stack.pop
                    @state_stack << :hash_key_symbol_post

                    @current_pos += 1
                    value_end
                  end

                when :parameters_name
                  @current_pos += 1
                  value_end

                when :variable
                  unless @current_pos == @start_pos
                    @current_pos += 1
                    value_end
                  end
              end

            when "&", ">"
              if position_matches_string(@current_pos, ">")
                case @state_stack[-1]
                  when :class_name,
                      :variable,
                      :variable_equal,
                      :variable_value,
                      :variable_comma
                    value_end
                  when :hash_key,
                      :hash_key_symbol_post,
                      :hash_key_symbol,
                      :hash_key_string,
                      :hash_rocket,
                      :hash_value,
                      :hash_comma
                    if @state_stack[-2] == :group_implied
                      value_end
                    end
                end
              elsif position_matches_string(@current_pos, "\"")
                start_quote
              end

            when "}"
              case @state_stack[-1]
                when :hash_key,
                    :hash_key_symbol_post,
                    :hash_key_symbol,
                    :hash_key_string,
                    :hash_rocket,
                    :hash_value,
                    :hash_comma
                  value_end
              end

            when "]"
              case @state_stack[-1]
                when :array,
                    :array_comma
                  value_end
              end

            when ","
              case @state_stack[-1]
                when :hash_value,
                    :array,
                    :variable_value,
                    #unexpected, probably error states...
                    :variable,
                    :variable_equal,
                    :hash_key,
                    :hash_key_string,
                    :hash_key_symbol,
                    :hash_key_symbol_post,
                    :hash_rocket,
                    :hash_comma,
                    :variable_comma,
                    :array_comma
                  value_end
              end

            when " ", "\r", "\n", "\t"
              case @state_stack[-1]
                when :variable_value,
                    :array,
                    :hash_value
                  @current_pos += 1
                  @current_pos -= 1

                else
                  value_end
              end
          end
      end

      @current_pos += 1
    end

    if @start_pos < @current_pos
      output_end
    end

    @formatted_string.rstrip!
    @formatted_string = @formatted_string.html_safe if @print_html_safe
    @formatted_string
  else
    @unknown_string
  end
end

#start_classObject



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/cornucopia/util/pretty_formatter.rb', line 523

def start_class
  if @state_stack[-1] == :array
    @state_stack << :bottom_level
    @indent_level -= 1
  end
  @indent_level += 1

  @start_pos = @current_pos

  @variable_set = false
  @state_stack << :class_name

  @current_pos   += 1
  @current_state = :value

  @formatted_string.rstrip!
end

#start_grouping(group_type) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/cornucopia/util/pretty_formatter.rb', line 508

def start_grouping group_type
  if @state_stack[-1] == :array
    @state_stack << :bottom_level
  else
    @indent_level += 1
  end

  output_line(@unknown_string[@start_pos..@current_pos])
  @indent_level += 1
  @start_pos    = @current_pos + 1
  @state_stack << group_type
  @state_stack << :value
  @current_state = :whitespace
end

#start_quoteObject



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/cornucopia/util/pretty_formatter.rb', line 405

def start_quote
  if @start_pos == @current_pos
    if @state_stack[-1] == :hash_key && @start_pos == @current_pos
      @state_stack.pop
      @state_stack << :hash_key_string
    end

    @current_state = :string
    @current_pos   += translate_value("\"").length - 1
  else
    # This is almost certainly an error.  End it now...
    output_end
  end
end

#translate_value(value) ⇒ Object



551
552
553
554
555
556
557
558
559
560
# File 'lib/cornucopia/util/pretty_formatter.rb', line 551

def translate_value(value)
  safe_string = value

  if @print_html_safe
    safe_string = "".html_safe
    safe_string << value
  end

  safe_string
end

#value_endObject



444
445
446
447
# File 'lib/cornucopia/util/pretty_formatter.rb', line 444

def value_end
  @current_state = :value_end
  @current_pos   -= 1
end