detail.rb 2.45 KB
Newer Older
1
module Reporter
2
3
4
5
  module Docx
    class Detail
      attr_reader :obj, :digit, :last_id
      def initialize(args)
6
7
        @spl_settings = args[:spl_settings]
        @rxn_settings = args[:rxn_settings]
8
        @configs = args[:configs] || { whole_diagram: true }
9
10
11
12
13
14
15
16
17
18
19
20
        @last_id = args[:last_id]
        @digit = args.fetch(:digit, 3)
        @img_format = args[:img_format]
      end

      private

      def id
        obj.id
      end

      def collection_label
jasonych99's avatar
jasonych99 committed
21
        obj.collections.map { |c| c[:label] if c[:label] != "All" }.compact.join(", ")
22
      end
23
24
25

      def merge_items(init, items)
        items.reduce(init) do |sum, i|
jasonych99's avatar
jasonych99 committed
26
          ops = parse_ops(i)
27
28
29
30
          sum + ops
        end
      end

jasonych99's avatar
jasonych99 committed
31
      def merge_items_symbols(init, items, symbol)
32
        items.reduce(init) do |sum, i|
jasonych99's avatar
jasonych99 committed
33
34
35
          ops = parse_ops(i)
          ops = rm_head_tail_space(ops)
          return sum if ops.blank?
36

jasonych99's avatar
jasonych99 committed
37
          sum + ops_tail_with_symbol(ops, symbol)
38
39
40
        end
      end

jasonych99's avatar
jasonych99 committed
41
      def ops_tail_with_symbol(ops, symbol)
jasonych99's avatar
jasonych99 committed
42
        ops + [{ 'insert' => symbol }]
jasonych99's avatar
jasonych99 committed
43
44
45
46
47
48
49
50
51
52
      end

      def rm_head_tail_space(ops = [])
        ops = rm_head_space(ops)
        rm_tail_space(ops)
      end

      def rm_head_space(ops = [])
        head = nil
        ops.each do |op|
53
          head = op['insert'].gsub(/^[\u00A0\s]+/, '')
jasonych99's avatar
jasonych99 committed
54
55
56
57
58
59
60
61
62
63
64
          break if head.present?
          ops = ops[1..-1]
        end
        return [] if ops.blank?
        ops[0]['insert'] = head
        ops
      end

      def rm_tail_space(ops = [])
        tail = nil
        ops.reverse.each do |op|
65
          tail = op['insert'].gsub(/[\u00A0\s]*[,.;]*[\u00A0\s]*$/, '')
jasonych99's avatar
jasonych99 committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
          break if tail.present?
          ops = ops[0..-2]
        end
        return [] if ops.blank?
        ops[-1]['insert'] = tail
        ops
      end

      def parse_ops(i)
        if i['extended_metadata'] && i['extended_metadata']['content']
          JSON.parse(i['extended_metadata']['content'])['ops']
        elsif i[:extended_metadata] && i[:extended_metadata][:content]
          JSON.parse(i[:extended_metadata][:content])['ops']
        else
          []
        end
      end

84
      def remove_redundant_space_break(ops) # ensure one line
85
        ops.map.with_index do |op, i|
jasonych99's avatar
jasonych99 committed
86
87
88
89
90
          if op["insert"]
            op["insert"] = op["insert"].gsub(/[\u00A0\s]{2,}/, " ")
            op["insert"] = op["insert"].lstrip if i == 0
            op["insert"] = op["insert"].gsub(/\n/, " ")
          end
91
92
93
          op
        end
      end
94
95
96
    end
  end
end