Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/dom.rb
Direct Known Subclasses
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
- #ansi2html ⇒ Object
- #dom(tag, attr = {}) ⇒ Object
- #escape_html(tag = nil) ⇒ Object
- #escaped ⇒ Object
- #jsonml(tag, attr = nil) ⇒ Object
- #unescape_html ⇒ Object
Instance Method Details
#ansi2html ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dom.rb', line 68 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]] elsif sc.scan(/./mo) then scanner.matched end ) until sc.eos? io.string end |
#dom(tag, attr = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/dom.rb', line 37 def dom tag, attr = {} "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)}>"\ "#{escape_html(tag).ansi2html}"\ "</#{Dom.hyphenize(tag)}>".escaped end |
#escape_html(tag = nil) ⇒ Object
42 43 44 |
# File 'lib/dom.rb', line 42 def escape_html tag = nil case tag; when :style, :script then self else Dom::Coder.encode(self) end end |
#escaped ⇒ Object
46 |
# File 'lib/dom.rb', line 46 def escaped; DomString.new(self) end |
#jsonml(tag, attr = nil) ⇒ Object
47 |
# File 'lib/dom.rb', line 47 def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr), self] end |
#unescape_html ⇒ Object
45 |
# File 'lib/dom.rb', line 45 def unescape_html; Dom::Coder.decode(self) end |