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:) ⇒ AtlassianDocumentFormat

Returns a new instance of AtlassianDocumentFormat.



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

def initialize users:
  @users = users
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



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

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



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

def adf_node_to_html node
  closing_tag = nil
  result = +''
  case node['type']
  when 'paragraph'
    result << '<p>'
    closing_tag = '</p>'
  when 'text'
    marks = adf_marks_to_html node['marks']
    result << marks.collect(&:first).join
    result << node['text']
    result << marks.collect(&:last).join
  when 'bulletList'
    result << '<ul>'
    closing_tag = '</ul>'
  when 'orderedList'
    result << '<ol>'
    closing_tag = '</ol>'
  when 'listItem'
    result << '<li>'
    closing_tag = '</li>'
  when 'table'
    result << '<table>'
    closing_tag = '</table>'
  when 'tableRow'
    result << '<tr>'
    closing_tag = '</tr>'
  when 'tableCell'
    result << '<td>'
    closing_tag = '</td>'
  when 'tableHeader'
    result << '<th>'
    closing_tag = '</th>'
  when 'mention'
    user = node['attrs']['text']
    result << "<b>#{user}</b>"
  when 'taskList'
    result << "<ul class='taskList'>"
    closing_tag = '</ul>'
  when 'taskItem'
    state = node['attrs']['state'] == 'TODO' ? '' : ''
    result << "<li>#{state} "
    closing_tag = '</li>'
  when 'emoji'
    result << node['attrs']['text']
  else
    result << "<p>Unparseable section: #{node['type']}</p>"
  end

  node['content']&.each do |child|
    result << adf_node_to_html(child)
  end

  result << closing_tag if closing_tag
  result
end

#expand_account_id(account_id) ⇒ Object



108
109
110
111
112
113
# File 'lib/jirametrics/atlassian_document_format.rb', line 108

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

#to_html(input) ⇒ Object



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

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 />')
  else
    input['content'].collect { |element| adf_node_to_html element }.join("\n")
  end
end