html-builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/html-builder.rb +121 -0
  2. metadata +46 -0
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/ruby
2
+
3
+ class HtmlBuilder
4
+ def initialize name, attrs, text
5
+ @name = name
6
+ @attrs = attrs
7
+ @text = text
8
+ @children = []
9
+ @refed = false
10
+ end
11
+ attr_accessor :refed
12
+ def escape str
13
+ # TODO
14
+ str.to_s
15
+ end
16
+ def quote str
17
+ '"' + escape(str) + '"'
18
+ end
19
+ def self.html *args, &block
20
+ attrs, text = HtmlBuilder.parse_arg *args
21
+ h = HtmlBuilder.new "html", attrs, text
22
+ h.instance_eval &block if block
23
+ h.generate
24
+ end
25
+ def format_node
26
+ str = escape @name
27
+ if @attrs
28
+ @attrs.each do |k, v|
29
+ str += " #{escape k}=#{quote v}"
30
+ end
31
+ end
32
+ str
33
+ end
34
+ def generate_internel lines, dep, inl
35
+ if @name == nil
36
+ @text.each_line do |l|
37
+ lines << " " * dep + l.chomp
38
+ end
39
+ return
40
+ end
41
+ dep = 0 if inl
42
+ cs = @children.keep_if {|c| !c.refed}
43
+ raise "Cannot own block and text both." if @text and not cs.empty?
44
+
45
+ if not cs.empty?
46
+ lines << " " * dep + "<" + format_node + ">"
47
+ cs.each do |c|
48
+ c.generate_internel lines, dep + 1, inl
49
+ end
50
+ lines << " " * dep + "</#{escape @name}>"
51
+ elsif @text
52
+ lines << " " * dep + "<#{format_node}>#{escape @text}</#{escape @name}>"
53
+ else
54
+ lines << " " * dep + "<#{format_node}/>"
55
+ end
56
+ end
57
+ def generate dep = 0, inl = false
58
+ lines = []
59
+ generate_internel lines, dep, inl
60
+ if inl
61
+ return lines.join
62
+ else
63
+ return lines.join "\n"
64
+ end
65
+ end
66
+ # do not def to_s for this class!!!
67
+ def self.parse_arg *args
68
+ if args.last.class == Hash
69
+ attrs = args.pop
70
+ else
71
+ attrs = {}
72
+ end
73
+ text = ""
74
+ args.each do |arg|
75
+ if arg.instance_of? HtmlBuilder
76
+ text += arg.generate(0, true)
77
+ else
78
+ text += arg.to_s
79
+ end
80
+ end
81
+ text = nil if text == ""
82
+ return attrs, text
83
+ end
84
+ def sub_node name, *args, &block
85
+ attrs, text = HtmlBuilder.parse_arg *args
86
+ args.each do |a|
87
+ @children.delete a if a.instance_of? HtmlBuilder
88
+ end
89
+ @children << HtmlBuilder.new(name, attrs, text)
90
+ if block
91
+ @children.last.instance_eval &block
92
+ end
93
+ @children.last
94
+ end
95
+ def self.def_node name
96
+ define_method name do |*args, &block|
97
+ sub_node name, *args, &block
98
+ end
99
+ end
100
+ def text *args, &block
101
+ sub_node nil, *args, &block
102
+ end
103
+ def_node :head
104
+ def_node :title
105
+ def_node :meta
106
+ def_node :link
107
+ def_node :script
108
+ def_node :body
109
+ def_node :a
110
+ def_node :h1
111
+ def_node :div
112
+ def_node :img
113
+ def_node :span
114
+ def javascript code
115
+ script(:type => "text/javascript"){
116
+ text code
117
+ }
118
+ end
119
+ end
120
+
121
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chizhong Jin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A powerful html builder
15
+ email: jinchizhong@kingsoft.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/html-builder.rb
21
+ homepage: http://github.com/jinchizhong/html-builder
22
+ licenses:
23
+ - BSD
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Html builder
46
+ test_files: []