Array of hashes for custom attributes on specific cells (
only) of the table
Returns:
-
(REXML::Element)
—
the Element which was just added
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
# File 'lib/xhtml_report_generator.rb', line 604
def custom_table(table_data, opts = {})
defaults = {
headers: 0,
data_is_xhtml: false,
table_attrs: {},
th_attrs: {},
tr_attrs: {},
td_attrs: {},
special: [],
}
o = defaults.merge(opts)
temp = REXML::Element.new("table")
temp.add_attributes(o[:table_attrs])
row_titles = table_data.collect{|row| row[0].to_s}
col_titles = table_data[0].collect{|title| title.to_s}
for i in 0..table_data.length-1 do
row = temp.add_element("tr", o[:tr_attrs])
for j in 0..table_data[i].length-1 do
if (i == 0 && (0x1 & o[:headers])==0x1)
col = row.add_element("th", o[:th_attrs])
elsif (j == 0 && (0x2 & o[:headers])==0x2)
col = row.add_element("th", o[:th_attrs])
elsif ((i == 0 || j ==0) && (0x3 & o[:headers])==0x3)
col = row.add_element("th", o[:th_attrs])
else
_td_attrs = Marshal.load(Marshal.dump(o[:td_attrs]))
o[:special].each do |h|
if !h[:col_index].nil?
if h[:col_index].is_a?(Range)
next if (!h[:col_index].include?(j))
elsif h[:col_index].is_a?(Integer)
next if (h[:col_index] != j)
end
elsif !h[:col_title].nil?
next if !col_titles[j].match(h[:col_title])
end
if !h[:row_index].nil?
if h[:row_index].is_a?(Range)
next if (!h[:row_index].include?(i))
elsif h[:row_index].is_a?(Integer)
next if (h[:row_index] != i)
end
elsif !h[:row_title].nil?
next if !row_titles[i].match(h[:row_title])
end
if h[:condition].call(table_data[i][j])
h[:attributes].each { |attr, val|
if !_td_attrs[attr].nil?
_td_attrs[attr] << val
else
_td_attrs[attr] = val
end
}
end
end
col = row.add_element("td", _td_attrs)
end
if o[:data_is_xhtml]
doc = REXML::Document.new("<root>" + table_data[i][j].to_s + "</root>")
for elem in doc.root.to_a do
if elem.is_a?(REXML::Text)
col.add_text(elem)
else
col.add_element(elem)
end
end
else
col.add_text(table_data[i][j].to_s)
end
end
end
@div_middle.insert_after(@current, temp)
@current = temp
document_changed()
return @current
end
|
#document_changed ⇒ Object
This method should be called after every change to the document.
Here we ensure the report is written to disk after each change
if #sync is true. If #sync is false this method does nothing
182
183
184
185
186
187
188
189
|
# File 'lib/xhtml_report_generator.rb', line 182
def document_changed()
if @sync
if @file.nil?
raise "You must call #write at least once before you can enable synced mode"
end
write()
end
end
|
#element_to_string(elem) ⇒ String
289
290
291
292
293
294
|
# File 'lib/xhtml_report_generator.rb', line 289
def element_to_string(elem)
f = REXML::Formatters::Transitive.new(0)
out = ""
f.write(elem, out)
return out
end
|
#encoding_fixer(str) ⇒ String
Check if the give string is a valid UTF-8 byte sequence. If it is not valid UTF-8, then
all invalid bytes are replaced by "\u2e2e" (\xe2\xb8\xae) ('REVERSED QUESTION MARK') because the default
replacement character "\uFFFD" ('QUESTION MARK IN DIAMOND BOX') is two slots wide and might
destroy mono spaced formatting
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/xhtml_report_generator.rb', line 120
def encoding_fixer(str)
str = str.to_s
tmp = str.force_encoding('UTF-8').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => "\u2e2e")
tmp.force_encoding('binary').gsub!(/[\x00-\x07\x0C-\x1F]|\xef\xbf\xbe|\xef\xbf\xbf/n, "\xe2\xb8\xae".force_encoding('binary'))
return tmp.force_encoding('UTF-8')
end
|
#get_code_html(attrs = {}, &block) ⇒ String
Instead of adding content to the report, this method returns the produced html code as a string.
This can be used to insert code into #custom_table (with the option data_is_xhtml: true)
300
301
302
303
304
305
306
307
|
# File 'lib/xhtml_report_generator.rb', line 300
def get_code_html(attrs={}, &block)
temp = REXML::Element.new("pre")
temp.add_attributes(attrs)
raise "Block argument is mandatory" unless block_given?
text = encoding_fixer(block.call())
temp.add_text(text)
element_to_string(temp)
end
|
#get_content_html(attrs = {}, &block) ⇒ String
Instead of adding content to the report, this method returns the produced html code as a string.
This can be used to insert code into #custom_table (with the option data_is_xhtml: true)
351
352
353
354
355
356
357
358
|
# File 'lib/xhtml_report_generator.rb', line 351
def get_content_html(attrs={}, &block)
temp = REXML::Element.new("p")
temp.add_attributes(attrs)
raise "Block argument is mandatory" unless block_given?
text = encoding_fixer(block.call())
temp.add_text(text)
element_to_string(temp)
end
|
#get_current ⇒ REXML::Element
returns the current xml element
265
266
267
|
# File 'lib/xhtml_report_generator.rb', line 265
def get_current()
return @current
end
|
#get_element_text(el = @current, recursive = true) ⇒ String
returns the plain text without any xml tags of the specified element and all its children
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/xhtml_report_generator.rb', line 273
def get_element_text(el = @current, recursive = true)
out = ""
el.to_a.each { |child|
if child.is_a?(REXML::Text)
out << child.value()
else
if recursive
out << get_element_text(child, true)
end
end
}
return out
end
|
#get_image_html(path, attributes = {}) ⇒ String
Instead of adding content to the report, this method returns the produced html code as a string.
This can be used to insert code into #custom_table (with the option data_is_xhtml: true)
428
429
430
431
432
433
434
435
436
437
438
|
# File 'lib/xhtml_report_generator.rb', line 428
def get_image_html(path, attributes = {})
binary_data = Base64.strict_encode64(IO.binread(path))
type = File.extname(path).gsub('.', '')
temp = REXML::Element.new("img")
temp.add_attribute("src","data:image/#{type};base64,#{binary_data}")
temp.add_attributes(attributes)
element_to_string(temp)
end
|
#get_link_html(href, attrs = {}, &block) ⇒ String
Instead of adding content to the report, this method returns the produced html code as a string.
This can be used to insert code into #custom_table (with the option data_is_xhtml: true)
396
397
398
399
400
401
402
403
404
|
# File 'lib/xhtml_report_generator.rb', line 396
def get_link_html(href, attrs={}, &block)
temp = REXML::Element.new("a")
attrs.merge!({"href" => href})
temp.add_attributes(attrs)
raise "Block argument is mandatory" unless block_given?
text = encoding_fixer(block.call())
temp.add_text(text)
element_to_string(temp)
end
|
#get_title ⇒ String
returns the title text of the report
245
246
247
248
|
# File 'lib/xhtml_report_generator.rb', line 245
def get_title()
pagetitle = @document.elements["//head/title"]
return pagetitle.text
end
|
#heading(tag_type = "h1", attrs = {}, &block) ⇒ REXML::Element
Appends a new heading element to body, and sets current to this new heading
711
712
713
714
715
716
717
718
719
720
721
722
|
# File 'lib/xhtml_report_generator.rb', line 711
def heading(tag_type="h1", attrs={}, &block)
temp = REXML::Element.new(tag_type)
temp.add_attributes(attrs)
@div_middle.insert_after(@current, temp)
@current = temp
raise "Block argument is mandatory" unless block_given?
text = encoding_fixer(block.call())
@current.text = text
document_changed()
return @current
end
|
#heading_top(tag_type = "h1", attrs = {}, &block) ⇒ REXML::Element
Inserts a new heading element at the very beginning of the middle div section, and points @current to this heading
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
|
# File 'lib/xhtml_report_generator.rb', line 731
def heading_top(tag_type="h1", attrs={}, &block)
temp = REXML::Element.new(tag_type)
temp.add_attributes(attrs)
if @div_middle.has_elements?()
@div_middle.insert_before("//div[@id='middle']/*[1]", temp)
else
@div_middle.insert_after(@current, temp)
end
@current = temp
raise "Block argument is mandatory" unless block_given?
text = encoding_fixer(block.call())
@current.text = text
document_changed()
return @current
end
|
#highlight(regex, color = "y", el = @current) ⇒ Fixnum
Scans all REXML::Text children of an REXML::Element for any occurrences of regex.
The text will be matched as one, not line by line as you might think.
If you want to write a regexp matching multiple lines keep in mind that the dot "." by default doesn't
match newline characters. Consider using the "m" option (e.g. /regex/m ) which makes dot match newlines
or match newlines explicitly.
highlight then puts a tag around all matches of regex
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
# File 'lib/xhtml_report_generator.rb', line 523
def highlight(regex, color="y", el = @current)
arr = el.to_a()
num_matches = 0
arr.each {|i| i.remove() }
for i in arr do
if i.is_a?(REXML::Text)
positions = i.value().enum_for(:scan, regex).map {
[Regexp.last_match.begin(0),
Regexp.last_match.end(0)-Regexp.last_match.begin(0)]
}
num_matches += positions.length
if ["y","r","g","b"].include?(color)
attr = {"class" => color}
elsif color.match(/^#[A-Fa-f0-9]{6}$/)
attr = {"style" => "background: #{color};"}
else
raise "invalid color: #{color}"
end
replace_text_with_elements(el, i, "span", attr, positions)
else
num_matches += highlight(regex, color, i)
el.add(i)
end
end
document_changed()
return num_matches
end
|
#highlight_captures(regex, color = "y", el = @current) ⇒ Fixnum
Scans all REXML::Text children of an REXML::Element for any occurrences of regex.
The text will be matched as one, not line by line as you might think.
If you want to write a regexp matching multiple lines keep in mind that the dot "." by default doesn't
match newline characters. Consider using the "m" option (e.g. /regex/m ) which makes dot match newlines
or match newlines explicitly.
highlight_captures then puts a tag around all captures of the regex
NOTE: nested captures are not supported and don't make sense in this context!!
471
472
473
474
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
507
508
509
510
511
|
# File 'lib/xhtml_report_generator.rb', line 471
def highlight_captures(regex, color="y", el = @current)
arr = el.to_a()
num_matches = 0
arr.each {|i| i.remove() }
for i in arr do
if i.is_a?(REXML::Text)
positions = i.value().enum_for(:scan, regex).flat_map {
array = Array.new
for k in 1..Regexp.last_match.length - 1 do
array.push([Regexp.last_match.begin(k),
Regexp.last_match.end(k)-Regexp.last_match.begin(k)])
end
array
}
num_matches += positions.length
if ["y","r","g","b"].include?(color)
attr = {"class" => color}
elsif color.match(/^#[A-Fa-f0-9]{6}$/)
attr = {"style" => "background: #{color};"}
else
raise "invalid color: #{color}"
end
replace_text_with_elements(el, i, "span", attr, positions)
else
num_matches += highlight_captures(regex, color, i)
el.add(i)
end
end
document_changed()
return num_matches
end
|
#html(text) ⇒ REXML::Element
insert arbitrary xml code after the @current element in the content pane (div middle)
379
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/xhtml_report_generator.rb', line 379
def html(text)
doc = REXML::Document.new("<root>"+text.to_s+"</root>")
for i in doc.root.to_a do
@div_middle.insert_after(@current, i)
@current = i
end
document_changed()
return @current
end
|
#image(path, attributes = {}) ⇒ Object
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
# File 'lib/xhtml_report_generator.rb', line 444
def image(path, attributes = {})
binary_data = Base64.strict_encode64(IO.binread(path))
type = File.extname(path).gsub('.', '')
temp = REXML::Element.new("img")
temp.add_attribute("src","data:image/#{type};base64,#{binary_data}")
temp.add_attributes(attributes)
@div_middle.insert_after(@current, temp)
@current = temp
document_changed()
return @current
end
|
#javascript(attrs = {}, &block) ⇒ REXML::Element
Appends a
Appends a <a href = > node after the @current nodes
411
412
413
414
415
416
417
418
419
420
421
422
|
# File 'lib/xhtml_report_generator.rb', line 411
def link(href, attrs={}, &block)
temp = REXML::Element.new("a")
attrs.merge!({"href" => href})
temp.add_attributes(attrs)
@div_middle.insert_after(@current, temp)
@current = temp
raise "Block argument is mandatory" unless block_given?
text = encoding_fixer(block.call())
@current.add_text(text)
document_changed()
return @current
end
|
#replace_text_with_elements(parent, element, tagname, attribs, index_length_array) ⇒ Object
Helper Method for the highlight methods. it will introduce specific xhtml tags around parts of a text child of an xml element.
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
# File 'lib/xhtml_report_generator.rb', line 771
def replace_text_with_elements(parent, element, tagname, attribs, index_length_array)
last_end = 0
index = 0
for j in index_length_array do
if j[0] > last_end
parent.add_text( element.value()[ last_end, j[0] - last_end ] )
end
tag = parent.add_element(REXML::Element.new(tagname), attribs)
tag.add_text(element.value()[ j[0], j[1] ])
last_end = j[0]+j[1]
if index == index_length_array.length - 1
if last_end < element.value().length
parent.add_text( element.value()[ last_end, element.value().length - last_end ] )
end
end
index += 1
end
if index == 0
parent.add(element)
end
end
|
#set_current!(xpath) ⇒ Object
set the current element to the element or first element matched by the xpath expression.
The current element is the one which can be modified through highlighting.
253
254
255
256
257
258
259
260
261
|
# File 'lib/xhtml_report_generator.rb', line 253
def set_current!(xpath)
if xpath.is_a?(REXML::Element)
@current = xpath
elsif xpath.is_a?(String)
@current = @document.elements[xpath]
else
raise "xpath is neither a String nor a REXML::Element"
end
end
|
#set_title(title) ⇒ Object
sets the title of the document in the section as well as in the layout header div
create_layout must be called before!
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/xhtml_report_generator.rb', line 232
def set_title(title)
if !@layout
raise "call create_layout first"
end
pagetitle = @document.elements["//head/title"]
pagetitle.text = title
div = @document.elements["//body/div[@id='head']"]
div.text = title
document_changed()
end
|
#table(table_data, headers = 0, table_attrs = {}, tr_attrs = {}, th_attrs = {}, td_attrs = {}) ⇒ REXML::Element
creates a html table from two dimensional array of the form Array [row] [col]
|