dom 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dom.rb +82 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d7196bca06ee6efa2dc0be67a4968876af2e6b62
4
+ data.tar.gz: 007e14dc88d60d3ccaad800f4fab9926553595ea
5
+ SHA512:
6
+ metadata.gz: 3dd0e268e64378980f0a5217c03c0978e0ffe99e0b7cbb61d678af4049f3c76e63ed2647aea7c5aedf2b3b605d517b43e9a3e9b6f0e26b75917b3df51fc05568
7
+ data.tar.gz: f2b130320ecce6363c097faae647b50c84883cdc3f68837015170b226f4c259253ab1b8ccde773788c11c609d8ae590f886e6ba1c9a3701a4623a37279d09b80
@@ -0,0 +1,82 @@
1
+ #!ruby
2
+ require "cgi"
3
+ require "stringio"
4
+ require "strscan"
5
+
6
+ module Dom
7
+ def self.attr h; h.map{|k, v| " #{hyphenize(k)}=\"#{v}\""}.join end
8
+ def self.hyphenize sym; sym.to_s.tr("_", "-") end
9
+ def self.compact; singleton_class.class_eval{alias :join :join_compact} end
10
+ def self.nested; singleton_class.class_eval{alias :join :join_nested} end
11
+ def self.pre; singleton_class.class_eval{alias :join :join_pre} end
12
+ def self.join_compact a, tag, s = ""; a.map{|e| e.to_s.escape_html(tag)}.join(s) end
13
+ def self.join_nested a, tag; "#$/#{join_compact(a, tag, $/).gsub(/^/, " ")}#$/" end
14
+ def self.join_pre a, tag
15
+ "<!--#$/"\
16
+ "#{"-->#{join_compact(a, tag, "<!--#$/-->")}<!--".gsub(/^/, " ")}#$/"\
17
+ "-->"
18
+ end
19
+ end
20
+
21
+ module Kernel
22
+ private
23
+ def dom tag, attr = {}
24
+ "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)} />".escaped
25
+ end
26
+ end
27
+
28
+ class String
29
+ def dom tag, attr = {}
30
+ "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)}>"\
31
+ "#{escape_html(tag)}"\
32
+ "</#{Dom.hyphenize(tag)}>".escaped
33
+ end
34
+ def escape_html tag = nil
35
+ case tag; when :style, :script then self else CGI.escapeHTML(self) end
36
+ end
37
+ def escaped; DomString.new(self) end
38
+ AnsiColor = {
39
+ "1" => "bold",
40
+ "30" => "black",
41
+ "31" => "red",
42
+ "32" => "green",
43
+ "33" => "yellow",
44
+ "34" => "blue",
45
+ "35" => "magenta",
46
+ "36" => "cyan",
47
+ "37" => "white",
48
+ "90" => "grey"
49
+ }
50
+ def ansi2html
51
+ ansi = StringScanner.new(escape_html)
52
+ html = StringIO.new
53
+ until ansi.eos?
54
+ if ansi.scan(/\e\[(3[0-7]|90|1)m/)
55
+ html.print(%{<span class="#{AnsiColor[ansi[1]]}">})
56
+ elsif ansi.scan(/\e\[0?m/)
57
+ html.print(%{</span>})
58
+ else
59
+ html.print(ansi.scan(/./m))
60
+ end
61
+ end
62
+ html.string.escaped
63
+ end
64
+ end
65
+
66
+ class DomString < String
67
+ def % v; super(v).escaped end
68
+ def to_s; self end
69
+ def escape_html tag; self end
70
+ # Newline in Javascript terminates the command, causing `Unterminated string literal` error
71
+ def escape_ajax; tr($/, "\\\n").to_json end
72
+ end
73
+
74
+ class Array
75
+ def dom tag, attr = {}
76
+ "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)}>"\
77
+ "#{Dom.join(self, tag)}"\
78
+ "</#{Dom.hyphenize(tag)}>".escaped
79
+ end
80
+ end
81
+
82
+ Dom.nested
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sawa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: []
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/dom.rb
20
+ homepage: http://stackoverflow.com/users/314166/sawa
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.0.5
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Creates HTML DOM strings
43
+ test_files: []