Class: HtmlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/piggy-core/htmlgen.rb

Overview

Generates static html pages. (Some RSS 2.0 is also included.) The interface is similar to ruby module cgi. It’s very basic and might later be replaced by WebGen or another generating tool. Due to some problems with CGI.pretty we’ll do some default indentation while writing.

Direct Known Subclasses

ThumbnailPageGenerator

Instance Method Summary collapse

Instance Method Details

#a(params, &block) ⇒ Object



181
182
183
# File 'lib/piggy-core/htmlgen.rb', line 181

def a(params, &block)
  tag 'a', params, &block
end

#add(string) ⇒ Object



245
246
247
# File 'lib/piggy-core/htmlgen.rb', line 245

def add(string)
  @page += string
end

#add_html(html_string) ⇒ Object



241
242
243
# File 'lib/piggy-core/htmlgen.rb', line 241

def add_html(html_string)
  add(escape_html(html_string))
end

#add_item(title_string, link_string, desc_string) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/piggy-core/htmlgen.rb', line 64

def add_item(title_string, link_string, desc_string)
  tag('item', {}) {
    title { add_html(title_string) }
    tag('link', {}) { add_html(link_string) }
    description { add_html(desc_string) }
  }
end

#begin_tag(tagname, parameters) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/piggy-core/htmlgen.rb', line 88

def begin_tag(tagname, parameters)
  cr
  @page += '<' + format_tag(tagname)
  parameters.each_pair { |key, value|
    @page += ' ' + key + '=' + quote(value)
  }
  @page += '>'   
end

#body(params, &block) ⇒ Object



149
150
151
# File 'lib/piggy-core/htmlgen.rb', line 149

def body(params, &block)
  tag 'body', params, &block
end

#brObject



237
238
239
# File 'lib/piggy-core/htmlgen.rb', line 237

def br
  begin_tag 'br', Hash.new
end

#center(&block) ⇒ Object



165
166
167
# File 'lib/piggy-core/htmlgen.rb', line 165

def center(&block)
  tag 'center', {}, &block
end

#channel(&block) ⇒ Object



153
154
155
# File 'lib/piggy-core/htmlgen.rb', line 153

def channel(&block)
  tag 'channel', {}, &block
end

#crObject



84
85
86
# File 'lib/piggy-core/htmlgen.rb', line 84

def cr
  @page += @depth
end

#dd(params, &block) ⇒ Object



209
210
211
# File 'lib/piggy-core/htmlgen.rb', line 209

def dd(params, &block)
  tag 'dd', params, &block
end

#dec_depthObject



24
25
26
# File 'lib/piggy-core/htmlgen.rb', line 24

def dec_depth
  @depth = @depth[0..-3]
end

#description(&block) ⇒ Object



157
158
159
# File 'lib/piggy-core/htmlgen.rb', line 157

def description(&block)
  tag 'description', {}, &block
end

#div(params = {}, &block) ⇒ Object



189
190
191
# File 'lib/piggy-core/htmlgen.rb', line 189

def div(params = {}, &block)
  tag 'div', params, &block
end

#dl(params, &block) ⇒ Object



201
202
203
# File 'lib/piggy-core/htmlgen.rb', line 201

def dl(params, &block)
  tag 'dl', params, &block
end

#dt(params, &block) ⇒ Object



205
206
207
# File 'lib/piggy-core/htmlgen.rb', line 205

def dt(params, &block)
  tag 'dt', params, &block
end

#end_tag(aTag) ⇒ Object



97
98
99
100
# File 'lib/piggy-core/htmlgen.rb', line 97

def end_tag(aTag)
  cr
  @page += '</' + format_tag(aTag) + '>'
end

#escape_html(text) ⇒ Object



72
73
74
# File 'lib/piggy-core/htmlgen.rb', line 72

def escape_html(text)
  return string_as_utf8(CGI.escapeHTML(text))
end

#format_tag(tag) ⇒ Object



80
81
82
# File 'lib/piggy-core/htmlgen.rb', line 80

def format_tag(tag)
  return tag.downcase
end

#frame(params) ⇒ Object



229
230
231
# File 'lib/piggy-core/htmlgen.rb', line 229

def frame(params)
  begin_tag 'frame', params
end

#frameset(params, &block) ⇒ Object



225
226
227
# File 'lib/piggy-core/htmlgen.rb', line 225

def frameset(params, &block)
  tag 'frameset', params, &block
end

#get_pageObject



249
250
251
# File 'lib/piggy-core/htmlgen.rb', line 249

def get_page
  return @page # CGI.pretty(@page)
end

#h1(params, &block) ⇒ Object



169
170
171
# File 'lib/piggy-core/htmlgen.rb', line 169

def h1(params, &block)
  tag 'h1', params, &block
end

#h2(params, &block) ⇒ Object



173
174
175
# File 'lib/piggy-core/htmlgen.rb', line 173

def h2(params, &block)
  tag 'h2', params, &block
end

#h3(params, &block) ⇒ Object



177
178
179
# File 'lib/piggy-core/htmlgen.rb', line 177

def h3(params, &block)
  tag 'h3', params, &block
end

#head(&block) ⇒ Object



114
115
116
# File 'lib/piggy-core/htmlgen.rb', line 114

def head(&block)
  tag 'head', {}, &block
end

#html(&block) ⇒ Object



110
111
112
# File 'lib/piggy-core/htmlgen.rb', line 110

def html(&block)
  tag 'html', {}, &block
end

#img(src_path, alt_name) ⇒ Object



197
198
199
# File 'lib/piggy-core/htmlgen.rb', line 197

def img(src_path, alt_name)
  begin_tag('img', { 'src'=>src_path, 'alt'=>alt_name })
end

#inc_depthObject



20
21
22
# File 'lib/piggy-core/htmlgen.rb', line 20

def inc_depth
  @depth += '  '
end

#language(value = 'de') ⇒ Object



161
162
163
# File 'lib/piggy-core/htmlgen.rb', line 161

def language(value = 'de')
  tag('language', {}) { add value }
end


134
135
136
# File 'lib/piggy-core/htmlgen.rb', line 134

def link(params)
  begin_tag 'link', params
end

#meta(params) ⇒ Object



118
119
120
# File 'lib/piggy-core/htmlgen.rb', line 118

def meta(params)
  begin_tag 'meta', params
end

#new_frameObject

Start a frameset



29
30
31
32
# File 'lib/piggy-core/htmlgen.rb', line 29

def new_frame
  reset
  @page = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">'
end

#new_rss(title_string, desc_string, &block) ⇒ Object

Start a RSS 2.0 feed



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/piggy-core/htmlgen.rb', line 41

def new_rss(title_string, desc_string, &block)
  new_xml do
      @page += '<rss version="2.0">'
      cr
      channel {
          title { add_html(title_string) }
          description { add_html(desc_string) }
          language      
          block.call
          }
      cr
      @page += '</rss>'
      cr
  end
end

#new_transitionalObject

Start a ‘normal’ HTML-page



35
36
37
38
# File 'lib/piggy-core/htmlgen.rb', line 35

def new_transitional
  reset
  @page = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
end

#new_xml(encoding = "utf-8", &block) ⇒ Object



57
58
59
60
61
62
# File 'lib/piggy-core/htmlgen.rb', line 57

def new_xml(encoding="utf-8", &block)
  reset
  @page = "<?xml version=\"1.0\" encoding=\"#{encoding}\"?>"
  cr
  block.call
end

#noframes(&block) ⇒ Object



233
234
235
# File 'lib/piggy-core/htmlgen.rb', line 233

def noframes(&block)
  tag 'noframes', {}, &block
end

#norobotsObject



128
129
130
131
132
# File 'lib/piggy-core/htmlgen.rb', line 128

def norobots()
  meta({
      "name" => "robots",
      "content" =>"noindex,nofollow"})
end

#p(params = {}, &block) ⇒ Object



185
186
187
# File 'lib/piggy-core/htmlgen.rb', line 185

def p(params = {}, &block)
  tag 'p', params, &block
end

#quote(string) ⇒ Object



76
77
78
# File 'lib/piggy-core/htmlgen.rb', line 76

def quote(string)
  return '"' + string + '"'
end

#resetObject



15
16
17
18
# File 'lib/piggy-core/htmlgen.rb', line 15

def reset
  @page = ''
  @depth = "\n"
end

#span(params = {}, &block) ⇒ Object



193
194
195
# File 'lib/piggy-core/htmlgen.rb', line 193

def span(params = {}, &block)
  tag 'span', params, &block
end

#stylesheet(url) ⇒ Object



138
139
140
141
142
143
# File 'lib/piggy-core/htmlgen.rb', line 138

def stylesheet(url)
  link({
      "rel" => "stylesheet",
      "type"=>"text/css",
      "href"=>"#{url}"})
end

#table(params, &block) ⇒ Object



213
214
215
# File 'lib/piggy-core/htmlgen.rb', line 213

def table(params, &block)
  tag 'table', params, &block
end

#tag(tagname, parameters, &block) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/piggy-core/htmlgen.rb', line 102

def tag(tagname, parameters, &block)
  begin_tag(tagname, parameters)
  inc_depth
  block.call # assume block modifies @page, too
  dec_depth
  end_tag(tagname)
end

#td(params, &block) ⇒ Object



221
222
223
# File 'lib/piggy-core/htmlgen.rb', line 221

def td(params, &block)
  tag 'td', params, &block
end

#title(&block) ⇒ Object



145
146
147
# File 'lib/piggy-core/htmlgen.rb', line 145

def title(&block)
  tag 'title', {}, &block
end

#tr(params, &block) ⇒ Object



217
218
219
# File 'lib/piggy-core/htmlgen.rb', line 217

def tr(params, &block)
  tag 'tr', params, &block
end

#utf8Object



122
123
124
125
126
# File 'lib/piggy-core/htmlgen.rb', line 122

def utf8()
  meta({
      "http-equiv" => "content-type",
      "content" => "text/html;charset=utf-8"})
end