Module: Custom
- Defined in:
- lib/xhtml_report_generator/custom.rb
Overview
The module needs to be called ‘Custom’
Instance Method Summary collapse
- #code(text) ⇒ Object
- #content(text, attrs = {}) ⇒ Object
-
#contentAfter(locaiton, text) ⇒ Object
TODO.
-
#contentBefore(locaiton, text) ⇒ Object
TODO.
-
#createLayout ⇒ Object
puts Module.nesting css classes mapped to toc creates the basic page layout and sets the current Element to //body/div.
-
#heading(type, text, toc = :ltoc) ⇒ Object
Appends a new heading element to body.
-
#highlight(regex, color = "y", el = @current) ⇒ Object
puts a <span> </span> tag around all matches of regex.
-
#highlightCaptures(regex, color = "y", el = @current) ⇒ Object
puts a <span> </span> tag around all captures of the regex NOTE: nested captures are not supported and don’t make sense in this context!!.
- #html(text, attrs = {}) ⇒ Object
- #replaceTextWithElements(parent, element, tagname, attribs, index_length_array) ⇒ Object
-
#setPosition!(xpath) ⇒ Object
set the current element the first element matched by the xpath expression.
- #setTitle(title) ⇒ Object
-
#table(table_data) ⇒ Object
creates a table from csv data.
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 |
#createLayout ⇒ Object
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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/xhtml_report_generator/custom.rb', line 143 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
puts a <span> </span> tag around all matches of regex
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/xhtml_report_generator/custom.rb', line 106 def highlight(regex, color="y", el = @current) # get all children of the current node arr = el.to_a() #puts arr.inspect # depth first recursion into grand-children for i in arr do # detach from current i.parent = nil #puts i.class.to_s() 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)] } replaceTextWithElements(el, i, "span", {"class" => color}, positions) else # for non-text nodes we recurse into it and finally reattach to our parent to preserve ordering # puts "recurse" highlight(regex, color, i) el.add(i) end # if i.class.to_s() == "REXML::Text" end # for i in arr do end |
#highlightCaptures(regex, color = "y", el = @current) ⇒ Object
puts a <span> </span> tag around all captures of the regex NOTE: nested captures are not supported and don’t make sense in this context!!
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 |
# File 'lib/xhtml_report_generator/custom.rb', line 70 def highlightCaptures(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 SUB-matches positions = i.value().enum_for(:scan, regex).flat_map { # Regexp.last_match is a MatchData object, the index 0 is the entire match and # indices 1..n are the captures (sub expressions) 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 } replaceTextWithElements(el, i, "span", {"class" => color}, positions) else # for non-text nodes we recurse into it and finally reattach 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 |
#replaceTextWithElements(parent, element, tagname, attribs, index_length_array) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/xhtml_report_generator/custom.rb', line 166 def replaceTextWithElements(parent, element, tagname, attribs, index_length_array) last_end = 0 index = 0 #puts index_length_array.inspect #puts element.inspect for j in index_length_array do # reattach normal (unmatched) text if j[0] > last_end text = REXML::Text.new(element.value()[ last_end, j[0] - last_end ]) parent.add_text(text) end #create the tag node with attributes and add the text to it tag = parent.add_element(REXML::Element.new(tagname), attribs) tag.add_text(element.value()[ j[0], j[1] ]) last_end = j[0]+j[1] # in the last round check for any remaining text if index == index_length_array.length - 1 if last_end < element.value().length text = REXML::Text.new(element.value()[ last_end, element.value().length - last_end ]) parent.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 parent.add(element) end 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
135 136 |
# File 'lib/xhtml_report_generator/custom.rb', line 135 def table (table_data) end |