Class: Metanorma::Utils::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log.



8
9
10
11
12
# File 'lib/utils/log.rb', line 8

def initialize
  @log = {}
  @c = HTMLEntities.new
  @mapid = {}
end

Instance Attribute Details

#xml=(value) ⇒ Object (writeonly)

Sets the attribute xml

Parameters:

  • value

    the value to set the attribute xml to.



6
7
8
# File 'lib/utils/log.rb', line 6

def xml=(value)
  @xml = value
end

Instance Method Details

#abort_messagesObject



25
26
27
28
29
30
31
# File 'lib/utils/log.rb', line 25

def abort_messages
  @log.values.each_with_object([]) do |v, m|
    v.each do |e|
      e[:severity].zero? and m << e[:message]
    end
  end
end

#add(category, loc, msg, severity: 2) ⇒ Object

severity: 0: abort; 1: serious; 2: not serious; 3: info only



15
16
17
18
19
20
21
22
23
# File 'lib/utils/log.rb', line 15

def add(category, loc, msg, severity: 2)
  @novalid and return
  @log[category] ||= []
  item = create_entry(loc, msg, severity)
  @log[category] << item
  loc = loc.nil? ? "" : "(#{current_location(loc)}): "
  suppress_display?(category, loc, msg) or
    warn "#{category}: #{loc}#{msg}"
end

#break_up_long_str(str, threshold, punct) ⇒ Object



180
181
182
# File 'lib/utils/log.rb', line 180

def break_up_long_str(str, threshold, punct)
  Metanorma::Utils.break_up_long_str(str, threshold, punct)
end

#context(node) ⇒ Object



91
92
93
94
95
96
# File 'lib/utils/log.rb', line 91

def context(node)
  node.is_a? String and return nil
  node.respond_to?(:to_xml) and return human_readable_xml(node)
  node.respond_to?(:to_s) and return node.to_s
  nil
end

#context_render(entry) ⇒ Object



157
158
159
160
161
# File 'lib/utils/log.rb', line 157

def context_render(entry)
  entry[:context] or return nil
  entry[:context].split("\n").first(5)
    .join("\n").gsub("><", "> <")
end

#create_entry(loc, msg, severity) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/utils/log.rb', line 37

def create_entry(loc, msg, severity)
  msg = msg.encode("UTF-8", invalid: :replace, undef: :replace)
  item = { location: current_location(loc), severity: severity,
           message: msg, context: context(loc), line: line(loc, msg) }
  if item[:message].include?(" :: ")
    a = item[:message].split(" :: ", 2)
    item[:context] = a[1]
    item[:message] = a[0]
  end
  item
end

#current_location(node) ⇒ Object



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/utils/log.rb', line 49

def current_location(node)
  if node.nil? then ""
  elsif node.respond_to?(:id) && !node.id.nil? then "ID #{node.id}"
  elsif node.respond_to?(:id) && node.id.nil? && node.respond_to?(:parent)
    while !node.nil? && node.id.nil?
      node = node.parent
    end
    node.nil? ? "" : "ID #{node.id}"
  elsif node.respond_to?(:to_xml) && node.respond_to?(:parent)
    while !node.nil? && node["id"].nil? && node.respond_to?(:parent)
      node = node.parent
    end
    node.respond_to?(:parent) ? "ID #{node['id']}" : ""
  elsif node.is_a? String then node
  elsif node.respond_to?(:lineno) && !node.lineno.nil? &&
      !node.lineno.empty?
    "Asciidoctor Line #{'%06d' % node.lineno}"
  elsif node.respond_to?(:line) && !node.line.nil?
    "XML Line #{'%06d' % node.line}"
  elsif node.respond_to?(:parent)
    while !node.nil? &&
        (!node.respond_to?(:level) || node.level.positive?) &&
        (!node.respond_to?(:context) || node.context != :section)
      node = node.parent
      return "Section: #{node.title}" if node.respond_to?(:context) &&
        node&.context == :section
    end
    "??"
  else "??"
  end
end

#human_readable_xml(node) ⇒ Object

try to approximate input, at least for maths



99
100
101
102
103
104
105
106
107
# File 'lib/utils/log.rb', line 99

def human_readable_xml(node)
  ret = node.dup
  ret.xpath(".//*[local-name() = 'stem']").each do |s|
    sub = s.at("./*[local-name() = 'asciimath'] | " \
               "./*[local-name() = 'latexmath']")
    sub and s.replace(sub)
  end
  ret.to_xml
end

#line(node, msg) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/utils/log.rb', line 81

def line(node, msg)
  if node.respond_to?(:line) && !node.line.nil?
    "#{'%06d' % node.line}"
  elsif /^XML Line /.match?(msg)
    msg.sub(/^XML Line /, "").sub(/:.*$/, "")
  else
    "000000"
  end
end


167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/utils/log.rb', line 167

def loc_link(entry)
  loc = entry[:location]
  loc.nil? || loc.empty? and loc = "--"
  if /^ID /.match?(loc)
    loc.sub!(/^ID /, "")
    loc = @mapid[loc] while @mapid[loc]
    url = "#{@filename}##{loc}"
  end
  loc &&= break_up_long_str(loc, 10, 2)
  url and loc = "<a href='#{url}'>#{loc}</a>"
  loc
end

#log_hdr(file) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/utils/log.rb', line 109

def log_hdr(file)
  "    <html><head><title>\#{file} errors</title>\n    <meta charset=\"UTF-8\"/>\n    <style> pre { white-space: pre-wrap; }\n    thead th { font-weight: bold; background-color: aqua; }\n    .severity0 { font-weight: bold; background-color: lightpink }\n    .severity1 { font-weight: bold; }\n    .severity2 { }\n    .severity3 { font-style: italic; color: grey; }\n    </style>\n    </head><body><h1>\#{file} errors</h1>\n  HTML\nend\n"

#mapid(old, new) ⇒ Object



163
164
165
# File 'lib/utils/log.rb', line 163

def mapid(old, new)
  @mapid[old] = new
end

#render_preproc_entry(entry) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/utils/log.rb', line 147

def render_preproc_entry(entry)
  ret = entry.dup
  ret[:line] = nil if ret[:line] == "000000"
  ret[:location] = loc_link(entry)
  ret[:message] = break_up_long_str(entry[:message], 10, 2)
    .gsub(/`([^`]+)`/, "<code>\\1</code>")
  ret[:context] = context_render(entry)
  ret.compact
end

#suppress_display?(category, _loc, _msg) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/utils/log.rb', line 33

def suppress_display?(category, _loc, _msg)
  ["Metanorma XML Syntax"].include?(category)
end

#write(file) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/utils/log.rb', line 124

def write(file)
  @filename = file.sub(".err.html", ".html")
  File.open(file, "w:UTF-8") do |f|
    f.puts log_hdr(file)
    @log.each_key { |key| write_key(f, key) }
    f.puts "</body></html>\n"
  end
end

#write_entry(file, entry) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/utils/log.rb', line 184

def write_entry(file, entry)
  entry[:context] &&= @c.encode(break_up_long_str(entry[:context], 40, 2))
  file.print "    <tr class=\"severity\#{entry[:severity]}\">\n    <td>\#{entry[:line]}</td><th><code>\#{entry[:location]}</code></th>\n    <td>\#{entry[:message]}</td><td><pre>\#{entry[:context]}</pre></td><td>\#{entry[:severity]}</td></tr>\n  HTML\nend\n"

#write_key(file, key) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/utils/log.rb', line 133

def write_key(file, key)
  file.puts "    <h2>\#{key}</h2>\\n<table border=\"1\">\n    <thead><th width=\"5%\">Line</th><th width=\"20%\">ID</th>\n    <th width=\"30%\">Message</th><th width=\"40%\">Context</th><th width=\"5%\">Severity</th></thead>\n    <tbody>\n  HTML\n  @log[key].sort_by { |a| [a[:line], a[:location], a[:message]] }\n    .each do |n|\n    write_entry(file, render_preproc_entry(n))\n  end\n  file.puts \"</tbody></table>\\n\"\nend\n"