Class: XhtmlReportGenerator::Generator
- Inherits:
-
Object
- Object
- XhtmlReportGenerator::Generator
- Defined in:
- lib/xhtml_report_generator.rb
Overview
This is the main generator class. It can be instanced with custom javascript, css, and ruby files to allow generation of arbitrary reports.
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#file ⇒ Object
Returns the value of attribute file.
Class Method Summary collapse
-
.create_xhtml_document(title) ⇒ Object
Creates a minimal valid xhtml document including header title and body elements.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Generator
constructor
A new instance of Generator.
-
#to_s(indent = 0) ⇒ Object
returns the string representation of the xml document.
-
#write(file = @file, mode = 'w') ⇒ Object
Saves the xml document to a file.
Constructor Details
#initialize(opts = {}) ⇒ Generator
Returns a new instance of Generator.
20 21 22 23 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 |
# File 'lib/xhtml_report_generator.rb', line 20 def initialize(opts = {}) # define the default values path = File.("../xhtml_report_generator", __FILE__) symbols = { :jquery => File.("jquery.js",path), :toc => File.("toc.js",path), :css => File.("style_template.css",path), :css_print => File.("print_template.css",path), :custom_rb => File.("custom.rb",path) } # either use the default files provided with the gem, or those provided by the caller symbols = symbols.merge(opts) for key in symbols.keys do # read the contents into the symbols hash symbols[key] = File.read(symbols[key]) end # load the custom module and extend it, use instance_eval otherwise the module will affect # all existing Generator classes instance_eval symbols[:custom_rb] @document = Generator.create_xhtml_document("Title") head = @document.elements["//head"] # insert the custom css, and javascript files style = head.add_element("style", {"type" => "text/css"}) # remove all newlines style.add_text(REXML::CData.new("\n"+symbols[:css].gsub(/\n/, "")+"\n")) style = head.add_element("style", {"type" => "text/css", "media"=>"print"}) style.add_text(REXML::CData.new("\n"+symbols[:css_print].gsub(/\n/, "")+"\n")) script = head.add_element("script", {"type" => "text/javascript"}) script.add_text(REXML::CData.new("\n"+symbols[:jquery]+"\n")) script = head.add_element("script", {"type" => "text/javascript"}) script.add_text(REXML::CData.new("\n"+symbols[:toc]+"\n")) end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
12 13 14 |
# File 'lib/xhtml_report_generator.rb', line 12 def document @document end |
#file ⇒ Object
Returns the value of attribute file.
12 13 14 |
# File 'lib/xhtml_report_generator.rb', line 12 def file @file end |
Class Method Details
.create_xhtml_document(title) ⇒ Object
Creates a minimal valid xhtml document including header title and body elements
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/xhtml_report_generator.rb', line 59 def self.create_xhtml_document(title) header = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' header += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' doc = REXML::Document.new(header) html = doc.add_element("html", {"xmlns" => "http://www.w3.org/1999/xhtml"}) # create header head = html.add_element("head") t = head.add_element("title") t.text = title html.add_element("body") return doc end |
Instance Method Details
#to_s(indent = 0) ⇒ Object
returns the string representation of the xml document
75 76 77 78 79 80 81 82 83 |
# File 'lib/xhtml_report_generator.rb', line 75 def to_s(indent = 0) output = "" # note : transitive is needed to preserve newlines in <pre> tags # note2: the hash options syntax is supported only from ruby version >= 2.0.0 we need the old style # for compatibility with 1.9.3 #@document.write({:output=>output, :indent=>indent, :transitive=>true}) @document.write(output, indent, true) return output end |
#write(file = @file, mode = 'w') ⇒ Object
Saves the xml document to a file. If no file is given, the file which was used most recently for this Generator object will be overwritten.
89 90 91 92 93 94 95 96 |
# File 'lib/xhtml_report_generator.rb', line 89 def write(file=@file, mode='w') # instance variables are nil if they were never initialized if file == nil raise "no valid file given" end @file = file File.open(file, "#{mode}:UTF-8") {|f| f.write(self.to_s.force_encoding(Encoding::UTF_8))} end |