Module: WWW_App::HTML

Included in:
WWW_App
Defined in:
lib/www_app/HTML.rb

Constant Summary collapse

SELF_CLOSING_TAGS =
[:br, :input, :link, :meta, :hr, :img]
NO_NEW_LINES =
[:p, :code, :span].freeze
TAGS =
%w[
  title
  body   div    span

  h1 h2 h3 h4 h5 h6

  img
  b      em     i  strong  u  a 
  abbr   blockquote  cite
  br     cite   code 
  ul     ol     li  p  pre  q 
  sup    sub 
  form   input  button

  link

  label

  script
].map(&:to_sym)
TAGS_TO_ATTRIBUTES =
{
  :all         => [:id, :class],
  :a           => [:href, :rel],
  :form        => [:action, :method, :accept_charset],
  :input       => [:type, :name, :value],
  :style       => [:type],
  :label       => [:for],
  :script      => [:type, :src, :language],
  :link        => [:rel, :type, :sizes, :href, :title],
  :meta        => [:name, :http_equiv, :property, :content, :charset],
  :img         => [:src], # :width, :height will be used by CSS only.
  :html        => [:lang]
}
ATTRIBUTES_TO_TAGS =
TAGS_TO_ATTRIBUTES.inject({}) { |memo, (tag, attrs)|
  attrs.each { |a|
    memo[a] ||= []
    memo[a] << tag
  }
  memo
}
ATTRIBUTES =
ATTRIBUTES_TO_TAGS.keys

Instance Method Summary collapse

Instance Method Details

#^(*names) ⇒ Object

Example:

div.^(:alert, :red_hot) { 'my content' }


174
175
176
177
178
179
180
181
182
183
# File 'lib/www_app/HTML.rb', line 174

def ^ *names
  @tag[:class] ||= []
  @tag[:class].concat(names)

  if block_given?
    close { yield }
  else
    self
  end
end

#alter_attribute(name, val) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/www_app/HTML.rb', line 75

def alter_attribute name, val
  allowed = @tag &&
    ATTRIBUTES_TO_TAGS[name] &&
    ATTRIBUTES_TO_TAGS[name].include?(@tag[:tag_name])

  fail "#{name.inspect} not allowed to be set here." unless allowed

  @tag[name] = val

  block_given? ?
    close { yield } :
    self
end

#id(new_id) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/www_app/HTML.rb', line 106

def id new_id
  if !@tag
    fail "No HTML tag found. Try using _.id(#{new_id.inspect})"
  end

  if !ancestor?(:group)
    old_id = @tag[:id]
    if old_id && old_id != new_id
      fail("Id already set: #{old_id} new: #{new_id}")
    end

    if @html_ids[new_id]
      fail(HTML_ID_Duplicate, "Id already used: #{new_id.inspect}, tag index: #{@html_ids[new_id]}")
    end
    @html_ids[ new_id ] = new_id
  end

  @tag[:id] = new_id

  if block_given?
    close { yield }
  else
    self
  end
end

#input(*args) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/www_app/HTML.rb', line 201

def input *args
  case
  when args.size === 3
    create(:input, :type=>args[0].to_s, :name=>args[1].to_s, :value=>args[2], :closed=>true)
    go_up
  else
    super
  end
end

#is_doc?Boolean



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/www_app/HTML.rb', line 136

def is_doc?
  @is_doc ||= begin
                found = false
                tags = @tags.dup
                while !found && !tags.empty?
                  t = tags.shift
                  found = begin
                            (t[:tag_name] == :body && (t[:id] || t[:css]) ) ||
                              t[:tag_name] == :style                        ||
                              t[:tag_name] == :script                       ||
                              t[:tag_name] == :meta                         ||
                              t[:css]                                       ||
                              (t[:tag_name] == :title && t[:parent] && t[:parent][:tag_name] == :body)
                          end
                  if !found && t[:children]
                    tags = t[:children].concat(tags)
                  end
                end

                found
              end
end

#is_fragment?Boolean

def id



132
133
134
# File 'lib/www_app/HTML.rb', line 132

def is_fragment?
  !is_doc?
end

#lang(name) ⇒ Object

def is_doc?



159
160
161
162
163
164
165
166
167
168
# File 'lib/www_app/HTML.rb', line 159

def lang name
  fail "Tag has to be placed tomost of the page." if parent
  fail "Block not allowed here." if block_given?
  create :html_tag_attr do
    @tag[:lang] = name.to_s.downcase.gsub(/[^a-z0-9\_\-]+/, ''.freeze)
    @tag[:lang] = 'en' if @tag[:lang].empty?
  end

  self
end

#meta(*args) ⇒ Object



89
90
91
92
93
# File 'lib/www_app/HTML.rb', line 89

def meta *args
  fail "No block allowed." if block_given?
  fail "Not allowed here." if parent
  create(:meta, *args)
end

#render_if(name) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/www_app/HTML.rb', line 185

def render_if name
  fail ::ArgumentError, "Not a symbol: #{name.inspect}" unless name.is_a?(Symbol)
  raw_text %^{{#coll.#{name}}}^
  yield
  raw_text %^{{/coll.#{name}}}^
  nil
end

#render_unless(name) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/www_app/HTML.rb', line 193

def render_unless name
  fail ::ArgumentError, "Not a symbol: #{name.inspect}" unless name.is_a?(Symbol)
  raw_text %!{{^coll.#{name}}}!
  yield
  raw_text %!{{/coll.#{name}}}!
  nil
end

#script(src = nil) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/www_app/HTML.rb', line 211

def script src = nil

  if src.is_a?(String) && src['.js'.freeze]
    return create(:script, :src=>src) { }
  end

  attrs = {
    :type => src || "text/mustache"
  }

  create :script, attrs
  close { yield } if block_given?
  self
end

#title(str = :none) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/www_app/HTML.rb', line 95

def title str = :none
  fail ":title not allowed here" if parent
  if !block_given? && str != :none
    create(:title) { text str }
  else
    create :title do
      yield
    end
  end
end