Module: Dom

Defined in:
lib/dom.rb

Constant Summary collapse

Coder =
HTMLEntities.new
Indent =
"  "

Class Method Summary collapse

Class Method Details

.compactObject



13
# File 'lib/dom.rb', line 13

def self.compact; singleton_class.class_eval{alias join join_compact} end

.format(tag, h) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dom.rb', line 29

def self.format tag, h
	tag = hyphenize(tag)
	[
		[
			tag,
			*h.map do
				|k, v|
				v = case v
				when nil then next
				when false then "none"
				when true then ""
				else v
				end
				"%s=\"%s\"" % [hyphenize(k), v]
			end
		].compact.join(" "),
		tag
	]
end

.hyphenize(sym) ⇒ Object



54
# File 'lib/dom.rb', line 54

def self.hyphenize sym; sym.to_s.tr("_", "-") end

.join_compact(a, tag) ⇒ Object



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

def self.join_compact a, tag
	a.map{|e| e.to_s.dom_escape(tag)}.join
end

.join_nested(a, tag) ⇒ Object



19
20
21
22
23
# File 'lib/dom.rb', line 19

def self.join_nested a, tag
	a.map{|e| e.to_s.dom_escape(tag).concat($/)}
	.join.gsub(/^/o, Indent)
	.prepend($/)
end

.join_pre(a, tag) ⇒ Object



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

def self.join_pre a, tag
	a.map{|e| e.to_s.dom_escape(tag).prepend("-->").concat("<!--#$/")}
	.join.gsub(/^/o, Indent)
	.prepend("<!--#$/").concat("-->")
end

.json_format(tag, h) ⇒ Object



48
49
50
51
52
53
# File 'lib/dom.rb', line 48

def self.json_format tag, h
	[
		hyphenize(tag),
		*([h.map{|k, v| [hyphenize(k), v]}.to_h] if attr)
	]
end

.nestedObject



14
# File 'lib/dom.rb', line 14

def self.nested; singleton_class.class_eval{alias join join_nested} end

.preObject



15
# File 'lib/dom.rb', line 15

def self.pre; singleton_class.class_eval{alias join join_pre} end