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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dom.rb', line 67

def ansi2html
  ansi = StringScanner.new(self)
  html = StringIO.new
  until ansi.eos?
    if ansi.scan(/\e\[0?m/)
      html.print(%{</span>}.freeze)
    elsif ansi.scan(/\e\[0?(\d+)m/)
      html.print(%{<span class="#{AnsiColor[ansi[1]]}">})
    else
      html.print(ansi.scan(/./m))
    end
  end
  html.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 CGI.escapeHTML(self) end
end

#escapedObject



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

def escaped; DomString.new(self) end

#jsonml(tag, attr = nil) ⇒ Object



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

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