Class: BlockHTML::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/block-html.rb

Constant Summary collapse

ESC =
{
  '&' => '&',
  '"' => '"',
  "'" => ''',
  '>' => '>',
  '<' => '&lt;',
  #' ' => '&nbsp;'
}
ESC_STR =
ESC.inject('') do |ret, (key, val)|
  ret << key
  ret
end

Instance Method Summary collapse

Constructor Details

#initialize(html, indent) ⇒ Renderer

Returns a new instance of Renderer.



186
187
188
189
190
191
192
# File 'lib/block-html.rb', line 186

def initialize(html, indent)
  @html   = html 
  @indent = indent.to_i
  @count  = 0
  @text   = 0 < @indent
  @buf    = ''
end

Instance Method Details

#doctype(attrs) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/block-html.rb', line 209

def doctype(attrs)
  case attrs[:format]
  when 'html5'
    @buf << '<!DOCTYPE html>'
  when 'html4'
    case attrs[:type]
    when 'strict'
      @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
    when 'frameset'
      @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
    else
      @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
    end
  when 'xhtml'
    if attrs[:version] == '1.1'
      @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
    else
      case attrs[:type]
      when 'strict'
        @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
      when 'frameset'
        @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
      else
        @buf << '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
      end
    end
  end

  @buf << "\n" if 0 < @indent
  @text = true
end

#escape(text) ⇒ Object



194
195
196
197
198
# File 'lib/block-html.rb', line 194

def escape(text)
  text.to_s.gsub(/[#{ESC_STR}]/) do |val|
    ESC[val]
  end
end

#escaped_text(text) ⇒ Object



281
282
283
284
# File 'lib/block-html.rb', line 281

def escaped_text(text)
  @buf << text
  @text = true
end

#tag(node, tag, attrs) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/block-html.rb', line 241

def tag(node, tag, attrs)
  space = ' ' * @count * @indent

  if node.empty?
    if 0 < @indent && !@text
      @buf << "\n%s<%s%s />" % [space, tag, attrs.to_s]
    else
      @buf << '<%s%s />' % [tag, attrs.to_s]
    end
  else
    if 0 < @indent && !@text
      @buf << "\n"
      @buf << "%s<%s%s>" % [space, tag, attrs.to_s]
    else
      @buf << '<%s%s>' % [tag, attrs.to_s]
    end
    @text = false

    @count += 1
    node.each do |obj|
      obj.render(self)
    end
    @count -= 1

    if 0 < @indent && !@text
      @buf << "\n"
      @buf << "%s</%s>" % [space, tag]
    else
      @buf << '</%s>' % [tag]
    end
  end

  @text = false
end

#text(text) ⇒ Object



276
277
278
279
# File 'lib/block-html.rb', line 276

def text(text)
  @buf << escape(text)
  @text = true
end

#to_sObject



286
287
288
289
290
# File 'lib/block-html.rb', line 286

def to_s
  @buf = ''
  @html.render(self)
  @buf
end

#xml(attrs) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/block-html.rb', line 200

def xml(attrs)
  @buf << '<?xml version="%s" encoding="%s"?>' % [
    attrs[:version],
    attrs[:encoding]
  ]
  @buf << "\n" if 0 < @indent
  @text = true
end