Module: Custom

Defined in:
lib/xhtml_report_generator/custom.rb

Overview

The module needs to be called ‘Custom’

Instance Method Summary collapse

Instance Method Details

#code(text) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/xhtml_report_generator/custom.rb', line 32

def code(text)
  pre = REXML::Element.new("pre")
  parent = @div_middle.insert_after(@current, pre)
  @current = pre
  @current.add_text(text)

end

#content(text, attrs = {}) ⇒ Object



40
41
42
43
44
# File 'lib/xhtml_report_generator/custom.rb', line 40

def content(text, attrs={})
  @current = @div_middle.add_element("p", attrs)
  @current.add_text(text)
  return @current
end

#contentAfter(locaiton, text) ⇒ Object

TODO



58
59
# File 'lib/xhtml_report_generator/custom.rb', line 58

def contentAfter(locaiton, text)
end

#contentBefore(locaiton, text) ⇒ Object

TODO



62
63
# File 'lib/xhtml_report_generator/custom.rb', line 62

def contentBefore(locaiton, text)
end

#createLayoutObject

puts Module.nesting css classes mapped to toc creates the basic page layout and sets the current Element to //body/div



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/xhtml_report_generator/custom.rb', line 7

def createLayout
  @body = @document.elements["//body"]

  if !@layout
    @body.add_element("div", {"class" => "head"})
    @body.add_element("div", {"class" => "lefttoc", "id" => "ltoc"})
    @body.add_element("div", {"class" => "righttoc", "id" => "rtoc"})
    @div_middle = @body.add_element("div", {"class" => "middle"})
    @layout = true
  end
  @current = @document.elements["//body/div[@class='middle']"]
end

#heading(type, text, toc = :ltoc) ⇒ Object

Appends a new heading element to body

Parameters:

  • type (String)

    specifiy “h1”, “h2”, “h3” for the heading

  • text (String)

    the heading text

  • toc (symbol) (defaults to: :ltoc)

    one of :ltoc, :rtoc, :btoc depending on in which toc you want to display the heading

Returns:

  • the added element



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/xhtml_report_generator/custom.rb', line 125

def heading(type, text, toc=:ltoc)
  case toc
  when :rtoc
    opts = {"class" => "onlyrtoc"}
  when :btoc
    opts = {"class" => "bothtoc"}
  else
    opts = {}
  end

  @current = @div_middle.add_element(type, opts)
  @current.text = text

  return @current
end

#highlight(regex, color = "y", el = @current) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
107
108
109
110
111
112
113
114
# File 'lib/xhtml_report_generator/custom.rb', line 65

def highlight(regex, color="y", el = @current)
  # get all children of the current node
  arr = el.to_a()

  # depth first recursion into grand-children
  for i in arr do
    # detach from current
    i.parent = nil
    if i.class.to_s()  == "REXML::Text"
      # in general a text looks as follows:
      # .*(matchstring|.*)*
      # We get an array of [[start,length], [start,length], ...] for all our regex matches
      positions = i.value().enum_for(:scan, regex).map {
        [Regexp.last_match.begin(0),
          Regexp.last_match.end(0)-Regexp.last_match.begin(0)]
      }

      last_end = 0
      index = 0
      for j in positions do
        # reattach normal (unmatched) text
        if j[0] > last_end
          text = REXML::Text.new(i.value()[ last_end, j[0] - last_end ])
          el.add_text(text)
        end
        #create the span node with color and add the text to it
        span = el.add_element(REXML::Element.new("span"), {"class" => color})
        span.add_text(i.value()[ j[0], j[1] ])
        last_end = j[0]+j[1]
        # in the last round check for any remaining text
        if index == positions.length - 1
          if last_end < i.value().length
            text = REXML::Text.new(i.value()[ last_end, i.value().length - last_end ])
            el.add(text)
          end
        end
        index  += 1
      end # for j in positions do

      # don't forget to reattach the textnode if there are no regex matches at all
      if index == 0
        el.add(i)
      end
    else
      # for non-text nodes we recurse into it and finally reattacht to our parent to preserve ordering
      highlight(regex, color, i)
      el.add(i)
    end # if  i.class.to_s()  == "REXML::Text"
  end # for i in arr do
end

#html(text, attrs = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/xhtml_report_generator/custom.rb', line 46

def html(text, attrs={})
  @current = @div_middle.add_element("p", attrs)
  # we need to create a new document with a pseudo root
  doc = REXML::Document.new("<root>"+text+"</root>")
  # then we move all children of root to the actual <p> </p> element
  for i in doc.root.to_a do
    @current.add(i)
  end
  return @current
end

#setPosition!(xpath) ⇒ Object

set the current element the first element matched by the xpath expression



28
29
30
# File 'lib/xhtml_report_generator/custom.rb', line 28

def setPosition!(xpath)
  @current = @document.elements[xpath]
end

#setTitle(title) ⇒ Object



20
21
22
23
24
25
# File 'lib/xhtml_report_generator/custom.rb', line 20

def setTitle(title)
  pagetitle = @document.elements["//head/title"]
  pagetitle.text = title
  div = @document.elements["//body/div[@class='head']"]
  div.text = title
end

#table(table_data) ⇒ Object

creates a table from csv data



117
118
# File 'lib/xhtml_report_generator/custom.rb', line 117

def table (table_data)
end