Class: Hermeneutics::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/html.rb,
lib/hermeneutics/cgi.rb

Overview

Example

require “hermeneutics/color” require “hermeneutics/html”

class MyHtml < Hermeneutics::Html

def build
  html {
    head {
      title { "Example" }
      comment "created as an example, #{Time.now}"
    }
    body bgcolor: Hermeneutics::Color.from_s( "ffffef") do
      h1 {
        pcdata "Ruby "
        a href: "www.w3.org" do "Html" end
        _ { " example" }
      }
      p { "Some text.\nBye." }
      p {
        self << "link"
        br
        a href: "www.w3.org" do "Html" end
      }
    end
  }
end

end

Hermeneutics::Html.document

Direct Known Subclasses

XHtml

Defined Under Namespace

Classes: Generator

Constant Summary collapse

CONTENT_TYPE =
"text/html"
NBSP =
TAGS =
{
  a:0, abbr:0, address:1, article:3, aside:3, audio:3, b:0, bdi:0, bdo:2,
  blockquote:3, body:2, button:3, canvas:1, caption:1, cite:0, code:0,
  colgroup:3, data:0, datalist:3, dd:1, del:0, details:3, dfn:0, dialog:0,
  div:3, dl:3, dt:1, em:0, fieldset:3, figcaption:1, figure:3, footer:3,
  form:3, h1:1, h2:1, h3:1, h4:1, h5:1, h6:1, head:3, header:3, html:2,
  i:0, iframe:3, ins:0, kbd:0, label:0, legend:1, li:1, main:3, map:3,
  mark:0, meter:0, nav:3, noscript:3, object:3, ol:3, optgroup:3,
  option:1, output:0, p:2, picture:3, pre:1, progress:0, q:0, rp:0, rt:0,
  ruby:2, s:0, samp:0, section:3, select:3, small:0, span:0, strong:0,
  sub:0, summary:1, sup:0, svg:3, table:3, tbody:3, td:1, template:3,
  textarea:1, tfoot:3, th:1, thead:3, time:0, title:1, tr:3, u:0, ul:3,
  var:0, video:3,

  # tags containing foreign code blocks
  script:4, style:4,

  # void tags
  area:0x11, base:0x11, br:0x11, col:0x11, embed:0x11, hr:0x11, img:0x10,
  input:0x10, keygen:0x11, link:0x11, meta:0x11, param:0x11, source:0x11,
  track:0x11, wbr:0x10,
}
Field =

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cgi) ⇒ Html

Returns a new instance of Html.



18
19
20
# File 'lib/hermeneutics/cgi.rb', line 18

def initialize cgi
  @cgi = cgi
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



322
323
324
325
326
327
328
329
330
331
# File 'lib/hermeneutics/html.rb', line 322

def method_missing name, *args, &block
  t = tag? name
  t or super
  if String === args.last then
    b = args.pop
    @generator.tag name, t, *args do b end
  else
    @generator.tag name, t, *args, &block
  end
end

Class Attribute Details

.mainObject

Returns the value of attribute main.



45
46
47
# File 'lib/hermeneutics/html.rb', line 45

def main
  @main
end

Instance Attribute Details

#cgiObject (readonly)

Returns the value of attribute cgi.



16
17
18
# File 'lib/hermeneutics/cgi.rb', line 16

def cgi
  @cgi
end

Class Method Details

.document(*args, **kwargs, &block) ⇒ Object



55
56
57
58
59
# File 'lib/hermeneutics/html.rb', line 55

def document *args, **kwargs, &block
  open do |i|
    i.document *args, **kwargs, &block
  end
end

.inherited(cls) ⇒ Object



46
47
48
# File 'lib/hermeneutics/html.rb', line 46

def inherited cls
  Html.main = cls
end

.open(out = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/hermeneutics/html.rb', line 49

def open out = nil
  i = (@main||self).new
  i.generate out do
    yield i
  end
end

.write_file(name = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hermeneutics/html.rb', line 60

def write_file name = nil
  name ||= (File.basename $0, ".rb") + ".html"
  File.open name, "w" do |f|
    open f do |i|
      if block_given? then
        yield i
      else
        i.document
      end
    end
  end
end

Instance Method Details

#<<(str) ⇒ Object



341
342
343
344
# File 'lib/hermeneutics/html.rb', line 341

def << str
  @generator.plain str if str
  self
end

#_(str = nil) ⇒ Object



346
347
348
349
# File 'lib/hermeneutics/html.rb', line 346

def _ str = nil
  @generator.plain str||yield
  nil
end

#buildObject



93
94
95
# File 'lib/hermeneutics/html.rb', line 93

def build
  html { body { h1 { "It works." } } }
end

#comment(str) ⇒ Object



351
352
353
# File 'lib/hermeneutics/html.rb', line 351

def comment str
  @generator.comment str
end

#doctype_headerObject



89
90
91
# File 'lib/hermeneutics/html.rb', line 89

def doctype_header
  @generator.doctype "html"
end

#document(*args, **kwargs, &block) ⇒ Object



84
85
86
87
# File 'lib/hermeneutics/html.rb', line 84

def document *args, **kwargs, &block
  doctype_header
  build *args, **kwargs, &block
end

#field(type, attrs) ⇒ Object



386
387
388
389
390
391
# File 'lib/hermeneutics/html.rb', line 386

def field type, attrs
  attrs[ :id] ||= attrs[ :name]
  @tabindex += 1
  attrs[ :tabindex] ||= @tabindex
  Field[ type, attrs]
end

#file_pathObject



286
# File 'lib/hermeneutics/html.rb', line 286

def file_path ; @generator.file_path ; end

#form(attrs, &block) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
# File 'lib/hermeneutics/html.rb', line 372

def form attrs, &block
  attrs[ :method] ||= if attrs[ :enctype] == "multipart/form-data" then
    "post"
  else
    "get"
  end
  @tabindex = 0
  method_missing :form, attrs, &block
ensure
  @tabindex = nil
end

#form!(**attrs, &block) ⇒ Object



22
23
24
25
# File 'lib/hermeneutics/cgi.rb', line 22

def form! **attrs, &block
  attrs[ :action] = @cgi.fullpath attrs[ :action]
  form **attrs, &block
end

#generate(out = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/hermeneutics/html.rb', line 74

def generate out = nil
  g = @generator
  begin
    @generator = Generator.new out||$stdout
    yield
  ensure
    @generator = g
  end
end

#head(attrs = nil) ⇒ Object



365
366
367
368
369
370
# File 'lib/hermeneutics/html.rb', line 365

def head attrs = nil
  method_missing :head, attrs do
    meta charset: @generator.encoding
    yield
  end
end

#href(dest, params = nil, anchor = nil) ⇒ Object



27
28
29
30
31
# File 'lib/hermeneutics/cgi.rb', line 27

def href dest, params = nil, anchor = nil
  @utx ||= URLText.new
  dest = @cgi.fullpath dest
  @utx.mkurl dest, params, anchor
end

#href!(params = nil, anchor = nil) ⇒ Object



33
34
35
# File 'lib/hermeneutics/cgi.rb', line 33

def href! params = nil, anchor = nil
  href nil, params, anchor
end

#html(**attrs) ⇒ Object



360
361
362
363
# File 'lib/hermeneutics/html.rb', line 360

def html **attrs
  attrs[ :"lang"] ||= language
  method_missing :html, **attrs do yield end
end

#input(arg, &block) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/hermeneutics/html.rb', line 406

def input arg, &block
  if Field === arg then
    case arg.type
      when /select/i   then
        method_missing :select, arg.attrs, &block
      when /textarea/i then
        block and
          raise ArgumentError, "Field textarea: use the value attribute."
        v = arg.attrs.delete :value
        method_missing :textarea, arg.attrs do v end
      else
        arg.attrs[ :type] ||= arg.type
        method_missing :input, arg.attrs, &block
    end
  else
    method_missing :input, arg, &block
  end
end

#javascript(str = nil, &block) ⇒ Object



355
356
357
358
# File 'lib/hermeneutics/html.rb', line 355

def javascript str = nil, &block
  mime = { type: "text/javascript" }
  script mime, str, &block
end

#label(field = nil, attrs = nil, &block) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/hermeneutics/html.rb', line 393

def label field = nil, attrs = nil, &block
  if String === attrs then
    label field do attrs end
  else
    if Field === field or attrs then
      attrs = attrs.merge for: field.attrs[ :id]
    else
      attrs = field
    end
    method_missing :label, attrs, &block
  end
end

#languageObject



98
99
100
101
102
103
104
# File 'lib/hermeneutics/html.rb', line 98

def language
  if ENV[ "LANG"] =~ /\A\w{2,}/ then
    r = $&
    r.gsub! /_/, "-"
    r
  end
end

#pcdata(*strs) ⇒ Object



333
334
335
336
337
338
339
# File 'lib/hermeneutics/html.rb', line 333

def pcdata *strs
  strs.each { |s|
    s or next
    @generator.plain s
  }
  nil
end

#tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


318
319
320
# File 'lib/hermeneutics/html.rb', line 318

def tag? name
  TAGS[ name]
end