Class: Weby::HTML
- Inherits:
-
Object
- Object
- Weby::HTML
- Defined in:
- lib/weby/html.rb
Constant Summary collapse
- @@prefix =
'wby'- @@evaluation_instance =
nil- @@evaluation_binding =
TOPLEVEL_BINDING- @@use_cache =
true- @@cache =
{}
- @@include_path =
nil
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#node ⇒ Object
Returns the value of attribute node.
-
#nodeset ⇒ Object
Returns the value of attribute nodeset.
Class Method Summary collapse
- .evaluation_binding ⇒ Object
- .evaluation_binding=(b) ⇒ Object
- .evaluation_instance ⇒ Object
- .evaluation_instance=(obj) ⇒ Object
- .include_path ⇒ Object
- .include_path=(path) ⇒ Object
- .load(path, opts = {}) ⇒ Object
- .load_doc(path) ⇒ Object
- .parse(text, opts = nil) ⇒ Object
- .parse_doc(text, opts = {}) ⇒ Object
- .prefix ⇒ Object
- .prefix=(prfx) ⇒ Object
Instance Method Summary collapse
- #[](attr) ⇒ Object
- #[]=(attr, val) ⇒ Object
- #add_class(classname) ⇒ Object
- #append(_node = nil, &block) ⇒ Object
- #append_to(_node) ⇒ Object
- #as_template(obj, opts = nil) ⇒ Object
- #builder ⇒ Object
- #children ⇒ Object
- #clone ⇒ Object
- #data(*args) ⇒ Object
- #each(&block) ⇒ Object
- #each_node(&block) ⇒ Object
- #evaluate(opts = {}) ⇒ Object
- #exec(mode = :append, src = nil, &block) ⇒ Object
- #find(selector) ⇒ Object
- #hide ⇒ Object
-
#initialize(obj, opts = {}, &block) ⇒ HTML
constructor
A new instance of HTML.
- #inner_html ⇒ Object
- #inner_html=(html) ⇒ Object
- #next ⇒ Object
- #parent ⇒ Object
- #prepend(_node = nil, &block) ⇒ Object
- #prepend_to(_node) ⇒ Object
- #remove ⇒ Object
- #remove_class(classname) ⇒ Object
- #style(*args) ⇒ Object
- #text ⇒ Object (also: #content)
- #text=(txt) ⇒ Object (also: #content=)
- #to_html ⇒ Object
- #to_s ⇒ Object
- #to_xhtml ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(obj, opts = {}, &block) ⇒ HTML
Returns a new instance of HTML.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/weby/html.rb', line 26 def initialize(obj, opts = {}, &block) @source = opts[:source] if obj.is_a? Nokogiri::XML::Node @node = obj @is_doc = obj.is_a? Nokogiri::XML::Document @is_fragm = obj.is_a? Nokogiri::XML::DocumentFragment @document = @node if @is_doc || @is_fragm @document ||= @node.document elsif obj.is_a? Nokogiri::XML::NodeSet @nodeset = obj @node = obj[0] elsif obj.is_a? Symbol @document = opts[:_doc] raise_err ':_doc option is missing' if !@document @document = @document.document if @document.is_a?(HTML) if !@document.is_a?(Nokogiri::XML::DocumentFragment) && !@document.is_a?(Nokogiri::XML::Document) raise_err ':_doc must be Nokogiri::XML::Document[Fragment]' end @node = Nokogiri::XML::Element.new obj.to_s, @document opts.delete :_doc opts.each{|attr, val| next if val.nil? @node[attr] = val } self.exec(&block) if block_given? elsif obj.is_a? String if @source && @source[/\.rb$/] @node = Nokogiri::HTML::DocumentFragment.parse '' self.exec :append, obj else @node = Nokogiri::HTML::DocumentFragment.parse obj end @document = @node @is_fragm = true end @document ||= (@nodeset || @node).document if @@use_cache && @source @@cache[@source] = self if !@@cache.key?(@source) end end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
8 9 10 |
# File 'lib/weby/html.rb', line 8 def document @document end |
#node ⇒ Object
Returns the value of attribute node.
8 9 10 |
# File 'lib/weby/html.rb', line 8 def node @node end |
#nodeset ⇒ Object
Returns the value of attribute nodeset.
8 9 10 |
# File 'lib/weby/html.rb', line 8 def nodeset @nodeset end |
Class Method Details
.evaluation_binding ⇒ Object
444 445 446 |
# File 'lib/weby/html.rb', line 444 def HTML::evaluation_binding @@evaluation_binding end |
.evaluation_binding=(b) ⇒ Object
448 449 450 |
# File 'lib/weby/html.rb', line 448 def HTML::evaluation_binding=(b) @@evaluation_binding = b end |
.evaluation_instance ⇒ Object
436 437 438 |
# File 'lib/weby/html.rb', line 436 def HTML::evaluation_instance @@evaluation_instance end |
.evaluation_instance=(obj) ⇒ Object
440 441 442 |
# File 'lib/weby/html.rb', line 440 def HTML::evaluation_instance=(obj) @@evaluation_instance = obj end |
.include_path ⇒ Object
452 453 454 |
# File 'lib/weby/html.rb', line 452 def HTML::include_path @@include_path end |
.include_path=(path) ⇒ Object
456 457 458 |
# File 'lib/weby/html.rb', line 456 def HTML::include_path=(path) @@include_path = path end |
.load(path, opts = {}) ⇒ Object
414 415 416 417 418 419 420 421 422 |
# File 'lib/weby/html.rb', line 414 def HTML::load(path, opts = {}) if @@use_cache cached = @@cache[path] return cached.clone if cached end opts[:source] = path text = File.read path HTML::parse text, opts end |
.load_doc(path) ⇒ Object
424 425 426 |
# File 'lib/weby/html.rb', line 424 def HTML::load_doc(path) HTML::load path, is_document: true, source: path end |
.parse(text, opts = nil) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/weby/html.rb', line 395 def HTML::parse(text, opts = nil) text ||= '' opts ||= {} opts = {auto: true}.merge(opts) if opts[:auto] opts[:is_document] = !text.strip[/^<!doctype /i].nil? end if opts[:is_document] HTML.new(Nokogiri::HTML::Document.parse(text)) else HTML.new(text) end end |
.parse_doc(text, opts = {}) ⇒ Object
409 410 411 412 |
# File 'lib/weby/html.rb', line 409 def HTML::parse_doc(text, opts = {}) opts[:is_document] = true HTML::parse text, opts end |
.prefix ⇒ Object
428 429 430 |
# File 'lib/weby/html.rb', line 428 def HTML::prefix @@prefix end |
.prefix=(prfx) ⇒ Object
432 433 434 |
# File 'lib/weby/html.rb', line 432 def HTML::prefix=(prfx) @@prefix = prfx end |
Instance Method Details
#[](attr) ⇒ Object
182 183 184 |
# File 'lib/weby/html.rb', line 182 def [](attr) (@node || {})[attr] end |
#[]=(attr, val) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/weby/html.rb', line 186 def []=(attr, val) if @node @node[attr] = val elsif @nodeset @nodeset.each{|_node| _node[attr] = val } end end |
#add_class(classname) ⇒ Object
222 223 224 225 |
# File 'lib/weby/html.rb', line 222 def add_class(classname) (@nodeset || [@node]).each{|n| add_class_to(n, classname)} self end |
#append(_node = nil, &block) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/weby/html.rb', line 121 def append(_node = nil, &block) _node = _node.node if _node.is_a? HTML @node.add_child _node if _node self.exec(&block) if block_given? self end |
#append_to(_node) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/weby/html.rb', line 128 def append_to(_node) if @node _node = _node.node if _node.is_a? HTML _node.add_child(@node) end self end |
#as_template(obj, opts = nil) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/weby/html.rb', line 316 def as_template(obj, opts = nil) opts ||= {} nl = opts[:new_line] res = self if @node if obj.is_a? String @node.content = obj elsif hashlike?(obj) obj.each{|attr, v| attr_s = attr.to_s if v.nil? && !@node[attr_s].nil? @node.remove_attribute attr_s elsif attr == :content v = '' if v.nil? @node.content = v.to_s elsif attr == :data && hashlike?(v) v.each{|data_name, data_val| @node["data-#{data_name}"] = data_val } elsif attr == :select && hashlike?(v) v.each{|sel, o| e = self.find sel.to_s e.as_template(o) } else @node[attr_s] = v end } elsif obj.is_a? Array last = @node obj.each{|o| _node = @node.clone e = HTML.new(_node) if block_given? yield e, o else e.as_template(o) end last.after("\n") if nl last.after(e.node) last = e.node } @node.remove end end res end |
#builder ⇒ Object
68 69 70 |
# File 'lib/weby/html.rb', line 68 def builder @builder ||= HTMLBuilder.new(self) end |
#children ⇒ Object
155 156 157 158 |
# File 'lib/weby/html.rb', line 155 def children return [] if !@node HTML.new(@node.children) end |
#clone ⇒ Object
385 386 387 388 389 390 391 392 393 |
# File 'lib/weby/html.rb', line 385 def clone _clone = super _clone.instance_eval{ @node = @node.clone if @node @nodeset = @nodeset.clone if @nodeset @document = @document.clone if @document } _clone end |
#data(*args) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/weby/html.rb', line 282 def data(*args) argl = args.length if argl.zero? return {} if !@node attrs = @node.attributes res = {} attrs.each{|a,v| res[a] = v if a[/^data-/] } res elsif argl == 1 arg = args[0] if hashlike?(arg) (@nodeset || [@node]).each{|n| next if !n arg.each{|name, val| n["data-#{name}"] = val } } self else return nil if !@node @node["data-#{arg}"] end else name, val = args (@nodeset || [@node]).each{|n| next if !n n["data-#{name}"] = val } self end end |
#each(&block) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/weby/html.rb', line 160 def each(&block) (@nodeset || [@node]).each{|_node| next if !_node element = self.class.new _node yield element } end |
#each_node(&block) ⇒ Object
168 169 170 |
# File 'lib/weby/html.rb', line 168 def each_node(&block) (@nodeset || [@node]).each &block end |
#evaluate(opts = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/weby/html.rb', line 82 def evaluate(opts = {}) return if !@node _self = opts[:self] || @@evaluation_instance _binding = opts[:binding] || @@evaluation_binding conditional_attr = "#{@@prefix}-if" conditionals = @node.css "*[#{conditional_attr}]" conditionals.each{|n| condition = n[conditional_attr] args = [condition] args += [@source, n.line] if @source if _self ok = _self.instance_eval *args else ok = _binding.eval *args end if !ok n.remove else n.remove_attribute conditional_attr end } imports = @node.css "#{@@prefix}-include" imports.each{|n| path = n['path'] if path path = resolvepath path if !File.exists? path line = (@source ? n.line : nil) raise_err "File not found: #{path}", line end fragm = HTML::load path next if !fragm.node fragm.evaluate self: _self, binding: _binding n.after fragm.node n.remove end } end |
#exec(mode = :append, src = nil, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/weby/html.rb', line 72 def exec(mode = :append, src = nil, &block) if src text = builder.mode(mode).instance_eval(src) else text = builder.mode(mode).instance_eval(&block) end @node.add_child text if text.is_a? String text end |
#find(selector) ⇒ Object
151 152 153 |
# File 'lib/weby/html.rb', line 151 def find(selector) self.class.new((@nodeset || @node).css(selector)) end |
#hide ⇒ Object
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/weby/html.rb', line 271 def hide (@nodeset || [@node]).each{|n| next if !n css = n['style'] || '' hash = parse_css css hash['display'] = 'none' n['style'] = hash_to_css(hash) } self end |
#inner_html ⇒ Object
208 209 210 211 |
# File 'lib/weby/html.rb', line 208 def inner_html return '' if !@node @node.inner_html end |
#inner_html=(html) ⇒ Object
213 214 215 |
# File 'lib/weby/html.rb', line 213 def inner_html=(html) @node.inner_html = html if @node end |
#next ⇒ Object
176 177 178 179 180 |
# File 'lib/weby/html.rb', line 176 def next if @node && (nxt = @node.next) self.class.new(nxt) end end |
#parent ⇒ Object
172 173 174 |
# File 'lib/weby/html.rb', line 172 def parent self.class.new(@node.parent) if @node end |
#prepend(_node = nil, &block) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/weby/html.rb', line 136 def prepend(_node = nil, &block) _node = _node.node if _node.is_a? HTML @node.prepend_child _node if _node self.exec(:prepend, &block) if block_given? self end |
#prepend_to(_node) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/weby/html.rb', line 143 def prepend_to(_node) if @node _node = _node.node if _node.is_a? HTML _node.prepend_child(@node) end self end |
#remove ⇒ Object
217 218 219 220 |
# File 'lib/weby/html.rb', line 217 def remove (@nodeset || @node).remove self end |
#remove_class(classname) ⇒ Object
227 228 229 230 |
# File 'lib/weby/html.rb', line 227 def remove_class(classname) (@nodeset || [@node]).each{|n| remove_class_from(n, classname)} self end |
#style(*args) ⇒ Object
232 233 234 235 236 237 238 239 240 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 |
# File 'lib/weby/html.rb', line 232 def style(*args) argl = args.length _node = @node || {} css = (_node['style']) || '' if argl.zero? hash = parse_css css return hash end if argl == 1 arg = args[0] if hashlike? arg return if !@node (@nodeset || [@node]).each{|n| next if !n css = (n['style']) || '' hash = parse_css css arg.each{|prop, val| hash[prop] = val } n['style'] = hash_to_css(hash) } return self else hash = parse_css css return hash[arg.to_s] end else prop, val = args (@nodeset || [@node]).each{|n| next if !n css = (n['style']) || '' hash = parse_css css hash[prop] = val n['style'] = hash_to_css(hash) } return self end end |
#text ⇒ Object Also known as: content
196 197 198 199 |
# File 'lib/weby/html.rb', line 196 def text return '' if !@node node.content end |
#text=(txt) ⇒ Object Also known as: content=
201 202 203 |
# File 'lib/weby/html.rb', line 201 def text=(txt) @node.content = txt if @node end |
#to_html ⇒ Object
364 365 366 |
# File 'lib/weby/html.rb', line 364 def to_html @node.to_html end |
#to_s ⇒ Object
376 377 378 379 380 381 382 383 |
# File 'lib/weby/html.rb', line 376 def to_s super if !@node if @node.xml? to_xml else to_html end end |
#to_xhtml ⇒ Object
368 369 370 |
# File 'lib/weby/html.rb', line 368 def to_xhtml @node.to_xhtml end |
#to_xml ⇒ Object
372 373 374 |
# File 'lib/weby/html.rb', line 372 def to_xml @node.to_xml end |