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,
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



328
329
330
331
332
333
334
335
336
337
# File 'lib/hermeneutics/html.rb', line 328

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

Returns the value of attribute cgi.



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

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



353
354
355
356
# File 'lib/hermeneutics/html.rb', line 353

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

#_(*strs) ⇒ Object



358
359
360
361
362
363
364
365
# File 'lib/hermeneutics/html.rb', line 358

def _ *strs
  if strs.notempty? then
    pcdata *strs
  else
    @generator.plain yield
    nil
  end
end

#buildObject



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

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

#comment(str) ⇒ Object



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

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

#file_pathObject



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

def file_path ; @generator.file_path ; end

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



392
393
394
395
396
# File 'lib/hermeneutics/html.rb', line 392

def form **attrs, &block
  attrs[ :method] ||=
          attrs[ :enctype] == "multipart/form-data" ? "post" : "get"
  method_missing :form, **attrs, &block
end

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



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

def form! **attrs, &block
  attrs[ :action] = @cgi.fullname 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

#h(i, **attrs, &block) ⇒ Object



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

def h i, **attrs, &block
  method_missing :"h#{i.to_i}", **attrs, &block
end

#head(**attrs) ⇒ Object



381
382
383
384
385
386
# File 'lib/hermeneutics/html.rb', line 381

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

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



24
25
26
27
28
# File 'lib/hermeneutics/cgi.rb', line 24

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

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



30
31
32
33
# File 'lib/hermeneutics/cgi.rb', line 30

def href! dest, params = nil, anchor = nil
  dest = @cgi.fullpath dest
  href dest, params, anchor
end

#html(**attrs) ⇒ Object



376
377
378
379
# File 'lib/hermeneutics/html.rb', line 376

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

#input(**attrs, &block) ⇒ Object



398
399
400
401
# File 'lib/hermeneutics/html.rb', line 398

def input **attrs, &block
  attrs[ :id] ||= attrs[ :name]
  method_missing :input, **attrs, &block
end

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



371
372
373
374
# File 'lib/hermeneutics/html.rb', line 371

def javascript str = nil, &block
  mime = { type: "text/javascript" }
  script mime, str, &block
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

#merge(str) ⇒ Object



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

def merge str
  @generator.merge str
  nil
end

#pcdata(*strs) ⇒ Object



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

def pcdata *strs
  strs.each { |s|
    @generator.plain s if s.notempty?
  }
  nil
end

#tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/hermeneutics/html.rb', line 322

def tag? name
  TAGS[ name]
end