Class: ReportBuilder::Builder::Html

Inherits:
ReportBuilder::Builder show all
Defined in:
lib/reportbuilder/builder/html.rb

Instance Attribute Summary collapse

Attributes inherited from ReportBuilder::Builder

#options, #parse_level, #toc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReportBuilder::Builder

#graph, #graph_entry, #image, #image_blob, #image_filename, inherited, inherited_classes, #parse, #parse_cycle, #parse_element, #save, #section, #table, #table_entry, #toc_entry

Constructor Details

#initialize(builder, options) ⇒ Html

Returns a new instance of Html.



6
7
8
9
10
11
12
# File 'lib/reportbuilder/builder/html.rb', line 6

def initialize(builder, options)
  super
  @directory        = @options.delete :directory
  @body=""
  @headers=[]
  @footers=[]
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



5
6
7
# File 'lib/reportbuilder/builder/html.rb', line 5

def directory
  @directory
end

Class Method Details

.codeObject



14
15
16
# File 'lib/reportbuilder/builder/html.rb', line 14

def self.code
  %w{html htm}
end

Instance Method Details

#basic_cssObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reportbuilder/builder/html.rb', line 21

def basic_css
  <<-HERE
  <style>
  body {
    margin:0;
    padding:1em;
  }
  table {
    border-collapse: collapse;

  }
  table td {
    border: 1px solid black;
  }
  .section {
    margin:0.5em;
  }
  </style>
  HERE
end

#css(css) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/reportbuilder/builder/html.rb', line 98

def css(css)
  if(File.exists? css)
    if(!File.exists? @directory+"/css") 
      FileUtils.mkdir @directory+"/css"
    end        
    if(!File.exists? @directory+"/css/"+File.basename(css))
      FileUtils.cp css, @directory+"/css/"+File.basename(css)
    end
    @headers.push("<link rel='stylesheet' type='text/css' href='css/#{File.basename(css)}' />")
  end
end

#default_optionsObject



18
19
20
# File 'lib/reportbuilder/builder/html.rb', line 18

def default_options
  {:directory => Dir.pwd}
end

#html(t) ⇒ Object



148
149
150
151
# File 'lib/reportbuilder/builder/html.rb', line 148

def html(t)
  ws=(" "*parse_level*2)
  @body << ws << t << "\n"
end

#js(js) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reportbuilder/builder/html.rb', line 86

def js(js)
  if(File.exists? js)
    if(!File.exists? @directory+"/js") 
      FileUtils.mkdir @directory+"/js"
    end
    if(!File.exists? @directory+"/js/"+File.basename(js))
      FileUtils.cp js, @directory+"/js/"+File.basename(js)
    end
    @headers.push("<script type='text/javascript' src='js/#{File.basename(js)}'></script>")
  end
end

#new_pageObject



141
142
143
# File 'lib/reportbuilder/builder/html.rb', line 141

def new_page
  @body << "<hr />\n"
end

#outObject



41
42
43
44
45
46
47
48
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
80
81
82
83
84
85
# File 'lib/reportbuilder/builder/html.rb', line 41

def out
  out= <<-HERE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>#{@builder.name}</title>
#{basic_css}
  HERE
  out << @headers.join("\n")
  out << "</head><body>\n"
  out << "<h1>#{@builder.name}</h1>"
  if(@toc.size>0)                      
  out << "<div id='toc'><div class='title'>List of contents</div>\n"
    actual_level=0
    
    @toc.each do |anchor,name,level|
      
      if actual_level!=level
        if actual_level > level
          (actual_level-level).times { out << "</ul>\n"}
        else
          (level-actual_level).times { out << "<ul>\n"}
        end
      end
      
      out << "<li><a href='##{anchor}'>#{name}</a></li>\n"
      actual_level=level
    end
    actual_level.times { out << "</ul>\n"}
    out << "</div>\n"
  end
  if (@list_tables.size>0)
    out << "<div class='tot'><div class='title'>List of tables</div><ul>"
    @list_tables.each {|anchor,name|
      out << "<li><a href='#"+anchor+"'>#{name}</a></li>"
    }
    out << "</ul></div>\n"
  end

  out << @body
  out << @footers.join("\n")
  out << "</body></html>"
  out
end

#parse_js(d, level = 0) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/reportbuilder/builder/html.rb', line 110

def parse_js(d, level=0)
  ei=5
  indent=" "*(level*2)
  case d
    when String
      "\"#{d}\""
    when TrueClass
      'true'
    when FalseClass
      'false'
    when Numeric
      d.to_s
    when Array
      if d.size>ei
        sep,pre=",\n"+indent, "\n"+indent
      else
        sep, pre=", ", ""
      end
      pre+"["+d.map {|i| parse_js(i, level+1)}.join(sep)+pre+"]"
    when Hash
      if d.size>ei
        sep,  pre=",\n"+indent, "\n"+indent
      else
        sep, pre=", ", ""
      end
      pre+"{ "+d.map {|k,v| parse_js(k)+" : "+parse_js(v, level+1)}.join(sep)+pre+"}"
      
    else
      d.to_s
  end
end

#preformatted(t) ⇒ Object



152
153
154
155
156
# File 'lib/reportbuilder/builder/html.rb', line 152

def preformatted(t)
  ws=(" "*parse_level*2)
  @body << ws << "<pre>#{t}</pre>\n"

end

#text(t) ⇒ Object



144
145
146
147
# File 'lib/reportbuilder/builder/html.rb', line 144

def text(t)
  ws=(" "*parse_level*2)
  @body << ws << "<p>#{t}</p>\n"
end