Class: Q

Inherits:
Object
  • Object
show all
Defined in:
lib/q.rb,
lib/q/version.rb

Constant Summary collapse

VALID_TAGS =
%w(a abbr acronym address area b base bdo big blockquote body br button caption cite code col colgroup dd del dfn div dl dt em fieldset form h1 h2 h3 h4 h5 h6 head html hr i img input ins kbd label legend li link map meta noscript object ol optgroup option p param pre q samp script select small span strong style sub sup table tbody td textarea tfoot th thead title tr tt ul var article aside audio canvas command datalist details embed figcaption figure footer header hgroup keygen mark meter nav output progress rp rt ruby section source summary time video)
VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil, &block) ⇒ Q

Returns a new instance of Q.



13
14
15
16
17
18
19
20
# File 'lib/q.rb', line 13

def initialize(opts = nil, &block)
  @__xml_compat = opts && opts.key?(:xml) ? opts[:xml] : false
  @__indent = opts && opts.key?(:indent) ? opts[:indent] : false
  @__default_doctype = opts && opts.key?(:doctype) ? opts[:doctype] : (@__xml_compat ? :xhtml_11 : :html5)
  @__out = ''
  @__level = 0
  instance_eval(&block)
end

Instance Method Details

#_(tagname, value = nil, opts = nil, &blk) ⇒ Object



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
# File 'lib/q.rb', line 54

def _(tagname, value = nil, opts = nil, &blk)
  if @__indent and !@__level.zero?
    @__out << "\n"
    @__level.times {__(@__indent)}
  end
  if value.is_a?(Hash)
    opts = value
    value = nil
  end
  @__out << "<#{tagname}"
  if opts and !opts.empty?
    opts.each do |k,v|
      @__out << " #{EscapeUtils.escape_html(k.to_s)}=\"#{EscapeUtils.escape_html(v.to_s)}\""
    end
  end
  if blk or value
    @__out << '>'
    __(value) if value
    @__level += 1
    instance_eval(&blk) if blk
    @__level -= 1
    if @__indent && blk
      @__out << "\n"
      @__level.times {__(@__indent)}
    end
    @__out << "</#{tagname}>"
  else
    @__out << (@__xml_compat ? '/>' : '>')
  end
  self
end

#__(text) ⇒ Object



91
92
93
94
# File 'lib/q.rb', line 91

def __(text)
  __no_escape EscapeUtils.escape_html(text)
  self
end

#__no_escape(text) ⇒ Object



86
87
88
89
# File 'lib/q.rb', line 86

def __no_escape(text)
  @__out << text
  self
end

#_doctype(type = @__default_doctype) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/q.rb', line 40

def _doctype(type = @__default_doctype)
  @__out << case type
  when :html_401_strict       then '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
  when :html_401_transitional then '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
  when :html_401_frameset     then '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
  when :xhtml_10_strict       then '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
  when :xhtml_10_transitional then '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
  when :xhtml_10_frameset     then '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
  when :xhtml_11              then '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
  when :xhtml_11_basic        then '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">'
  when :html5                 then '<!DOCTYPE HTML>'
  end
end

#div(class_or_id, value = nil, opts = nil, &blk) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/q.rb', line 30

def div(class_or_id, value = nil, opts = nil, &blk)
  attr_name, attr_val = case class_or_id[0]
  when ?.
    ['class', class_or_id[1, class_or_id.size - 1]]
  when ?#
    ['class', class_or_id[1, class_or_id.size - 1]]
  end
  _('div', value, opts ? {attr_name => attr_val}.merge(opts) : {attr_name => attr_val}, &blk)
end

#to_sObject



96
97
98
# File 'lib/q.rb', line 96

def to_s
  @__out
end