Class: AtlassianDocumentFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/jirametrics/atlassian_document_format.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(users:, timezone_offset:) ⇒ AtlassianDocumentFormat

Returns a new instance of AtlassianDocumentFormat.



6
7
8
9
# File 'lib/jirametrics/atlassian_document_format.rb', line 6

def initialize users:, timezone_offset:
  @users = users
  @timezone_offset = timezone_offset
end

Instance Attribute Details

#usersObject (readonly)

Returns the value of attribute users.



4
5
6
# File 'lib/jirametrics/atlassian_document_format.rb', line 4

def users
  @users
end

Instance Method Details

#adf_marks_to_html(list) ⇒ Object



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
# File 'lib/jirametrics/atlassian_document_format.rb', line 117

def adf_marks_to_html list
  return [] if list.nil?

  mappings = [
    ['strong', '<b>', '</b>'],
    ['code', '<code>', '</code>'],
    ['em', '<em>', '</em>'],
    ['strike', '<s>', '</s>'],
    ['underline', '<u>', '</u>']
  ]

  list.filter_map do |mark|
    type = mark['type']
    if type == 'textColor'
      color = mark['attrs']['color']
      ["<span style='color: #{color}'>", '</span>']
    elsif type == 'link'
      href = mark['attrs']['href']
      title = mark['attrs']['title']
      ["<a href='#{href}' title='#{title}'>", '</a>']
    else
      line = mappings.find { |key, _open, _close| key == type }
      [line[1], line[2]] if line
    end
  end
end

#adf_node_to_html(node) ⇒ Object



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
# File 'lib/jirametrics/atlassian_document_format.rb', line 38

def adf_node_to_html node # rubocop:disable Metrics/CyclomaticComplexity
  adf_node_render(node) do |n|
    node_attrs = n['attrs']
    case n['type']
    when 'blockquote'                then ['<blockquote>', '</blockquote>']
    when 'bulletList'                then ['<ul>', '</ul>']
    when 'codeBlock'                 then ['<code>', '</code>']
    when 'date'
      [Time.at(node_attrs['timestamp'].to_i / 1000, in: @timezone_offset).to_date.to_s, nil]
    when 'decisionItem', 'listItem'  then ['<li>', '</li>']
    when 'decisionList'              then ['<div>Decisions<ul>', '</ul></div>']
    when 'emoji', 'status'           then [node_attrs['text'], nil]
    when 'expand'                    then ["<div>#{node_attrs['title']}</div>", nil]
    when 'hardBreak'                 then ['<br />', nil]
    when 'heading'
      level = node_attrs['level']
      ["<h#{level}>", "</h#{level}>"]
    when 'inlineCard'
      url = node_attrs['url']
      ["[Inline card]: <a href='#{url}'>#{url}</a>", nil]
    when 'media'
      text = node_attrs['alt'] || node_attrs['id']
      ["Media: #{text}", nil]
    when 'mediaSingle', 'mediaGroup' then ['<div>', '</div>']
    when 'mention'                   then ["<b>#{node_attrs['text']}</b>", nil]
    when 'orderedList'               then ['<ol>', '</ol>']
    when 'panel'                     then ["<div>#{node_attrs['panelType'].upcase}</div>", nil]
    when 'paragraph'                 then ['<p>', '</p>']
    when 'rule'                      then ['<hr />', nil]
    when 'table'                     then ['<table>', '</table>']
    when 'tableCell'                 then ['<td>', '</td>']
    when 'tableHeader'               then ['<th>', '</th>']
    when 'tableRow'                  then ['<tr>', '</tr>']
    when 'text'
      marks = adf_marks_to_html(n['marks'])
      [marks.collect(&:first).join + n['text'], marks.collect(&:last).join]
    when 'taskItem'
      state = node_attrs['state'] == 'TODO' ? '' : ''
      ["<li>#{state} ", '</li>']
    when 'taskList'                  then ["<ul class='taskList'>", '</ul>']
    else
      ["<p>Unparseable section: #{n['type']}</p>", nil]
    end
  end
end

#adf_node_to_text(node) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



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
# File 'lib/jirametrics/atlassian_document_format.rb', line 84

def adf_node_to_text node # rubocop:disable Metrics/CyclomaticComplexity
  adf_node_render(node) do |n|
    node_attrs = n['attrs']
    case n['type']
    when 'blockquote', 'bulletList', 'codeBlock',
         'mediaSingle', 'mediaGroup',
         'orderedList', 'table', 'taskList'  then ['', nil]
    when 'date'
      [Time.at(node_attrs['timestamp'].to_i / 1000, in: @timezone_offset).to_date.to_s, nil]
    when 'decisionItem'              then ['- ', "\n"]
    when 'decisionList'              then ["Decisions:\n", nil]
    when 'emoji', 'mention', 'status' then [node_attrs['text'], nil]
    when 'expand'                    then ["#{node_attrs['title']}\n", nil]
    when 'hardBreak'                 then ["\n", nil]
    when 'heading', 'paragraph', 'tableRow' then ['', "\n"]
    when 'inlineCard'                then [node_attrs['url'], nil]
    when 'listItem'                  then ['- ', nil]
    when 'media'
      text = node_attrs['alt'] || node_attrs['id']
      ["Media: #{text}", nil]
    when 'panel'                     then ["#{node_attrs['panelType'].upcase}\n", nil]
    when 'rule'                      then ["---\n", nil]
    when 'tableCell', 'tableHeader'  then ['', "\t"]
    when 'text'                      then [n['text'], nil]
    when 'taskItem'
      state = node_attrs['state'] == 'TODO' ? '' : ''
      ["#{state} ", "\n"]
    else
      ["[Unparseable: #{n['type']}]\n", nil]
    end
  end
end

#expand_account_id(account_id) ⇒ Object



144
145
146
147
148
149
# File 'lib/jirametrics/atlassian_document_format.rb', line 144

def  
  user = @users.find { |u| u. ==  }
  text = 
  text = "@#{user.display_name}" if user
  "<span class='account_id'>#{text}</span>"
end

#to_html(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jirametrics/atlassian_document_format.rb', line 11

def to_html input
  if input.is_a? String
    input
      .gsub(/{color:(#\w{6})}([^{]+){color}/, '<span style="color: \1">\2</span>') # Colours
      .gsub(/\[~accountid:([^\]]+)\]/) {  $1 } # Tagged people
      .gsub(/\[([^|]+)\|(https?[^\]]+)\]/, '<a href="\2">\1</a>') # URLs
      .gsub("\n", '<br />')
  elsif input&.[]('content')
    input['content'].collect { |element| adf_node_to_html element }.join("\n")
  else
    # We have an actual ADF document with no content.
    ''
  end
end

#to_text(input) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/jirametrics/atlassian_document_format.rb', line 26

def to_text input
  if input.is_a? String
    input
  elsif input&.[]('content')
    input['content'].collect { |element| adf_node_to_text element }.join
  else
    ''
  end
end