Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/dom.rb

Direct Known Subclasses

DomString

Constant Summary collapse

AnsiColor =
{
	"1" => "bold",
	"4" => "underline",
	"30" => "black",
	"31" => "red",
	"32" => "green",
	"33" => "yellow",
	"34" => "blue",
	"35" => "magenta",
	"36" => "cyan",
	"37" => "white",
	"40" => "bg-black",
	"41" => "bg-red",
	"42" => "bg-green",
	"43" => "bg-yellow",
	"44" => "bg-blue",
	"45" => "bg-magenta",
	"46" => "bg-cyan",
	"47" => "bg-white",
}

Instance Method Summary collapse

Instance Method Details

#ansi2htmlObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/dom.rb', line 76

def ansi2html
	sc = StringScanner.new(self)
	io = StringIO.new
	io.print(
		if sc.scan(/\e\[0?m/o) then '</span>'.freeze
		elsif sc.scan(/\e\[0?(\d+)m/o) then '<span class="%s">'.freeze % AnsiColor[sc[1]]
		end ||
	sc.scan(/./mo)) until sc.eos?
	io.string
end

#dom(tag, mounted: nil, **attr) ⇒ Object



44
45
46
47
48
# File 'lib/dom.rb', line 44

def dom tag, mounted: nil, **attr
	_tag = Dom.hyphenize(tag)
	("<%s%s>%s</%s>".freeze % [_tag, Dom.attr(attr), escape_html(tag).ansi2html, _tag])
	.dom_escaped.mounted_set(mounted)
end

#dom_escapedObject



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

def dom_escaped; DomString.new(self) end

#escape_html(tag = nil) ⇒ Object



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

def escape_html tag = nil
	case tag; when :style, :script then self else Dom::Coder.encode(self) end
end

#jsonml(tag, attr = nil) ⇒ Object



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

def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr), self] end

#mountedObject



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

def mounted; nil end

#unescape_htmlObject



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

def unescape_html; Dom::Coder.decode(self) end