html_gen 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,8 +3,6 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
- gem "knjrbfw"
7
-
8
6
  # Add dependencies to develop your gem here.
9
7
  # Include everything needed to run rake, tests, features, etc.
10
8
  group :development do
data/Gemfile.lock CHANGED
@@ -1,25 +1,14 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- datet (0.0.14)
5
4
  diff-lcs (1.1.3)
6
5
  git (1.2.5)
7
- http2 (0.0.5)
8
6
  jeweler (1.8.4)
9
7
  bundler (~> 1.0)
10
8
  git (>= 1.2.5)
11
9
  rake
12
10
  rdoc
13
11
  json (1.7.4)
14
- knjrbfw (0.0.78)
15
- datet
16
- http2
17
- php4r
18
- tsafe
19
- wref
20
- php4r (0.0.2)
21
- datet
22
- http2
23
12
  rake (0.9.2.2)
24
13
  rdoc (3.12)
25
14
  json (~> 1.4)
@@ -31,8 +20,6 @@ GEM
31
20
  rspec-expectations (2.8.0)
32
21
  diff-lcs (~> 1.1.2)
33
22
  rspec-mocks (2.8.0)
34
- tsafe (0.0.11)
35
- wref (0.0.5)
36
23
 
37
24
  PLATFORMS
38
25
  ruby
@@ -40,6 +27,5 @@ PLATFORMS
40
27
  DEPENDENCIES
41
28
  bundler (>= 1.0.0)
42
29
  jeweler (~> 1.8.4)
43
- knjrbfw
44
30
  rdoc (~> 3.12)
45
31
  rspec (~> 2.8.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/html_gen.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{html_gen}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kasper Johansen"]
12
+ s.date = %q{2012-08-10}
13
+ s.description = %q{A small framework for generating HTML.}
14
+ s.email = %q{k@spernj.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "html_gen.gemspec",
29
+ "lib/html_gen.rb",
30
+ "lib/html_gen_element.rb",
31
+ "spec/html_gen_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/kaspernj/html_gen}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.6.2}
38
+ s.summary = %q{A small framework for generating HTML.}
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
45
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
46
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
56
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
59
+ end
60
+ end
61
+
data/lib/html_gen.rb CHANGED
@@ -1,3 +1,4 @@
1
+ #This class doesnt hold other methods than for autoloading of subclasses.
1
2
  class Html_gen
2
3
  #Autoloader for subclasses.
3
4
  def self.const_missing(name)
@@ -5,4 +6,9 @@ class Html_gen
5
6
  raise "Still not defined: '#{name}'." if !Html_gen.const_defined?(name)
6
7
  return Html_gen.const_get(name)
7
8
  end
9
+
10
+ #Escapes HTML from the given string. This is to avoid any dependencies and should not be used by other libs.
11
+ def self.escape_html(string)
12
+ return string.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
13
+ end
8
14
  end
@@ -1,34 +1,88 @@
1
- require "knjrbfw"
2
-
1
+ #This class can be used to generate HTML.
2
+ #===Examples
3
+ # ele = Html_gen::Element.new(:a) #=> #<Html_gen::Element:0x00000000e5f650 @attr={}, @name=:a, @classes=[], @str_html="", @str="", @css={}, @eles=[], @nl="\n", @inden="\t">
4
+ # ele.classes << "custom_link"
5
+ # ele.css["font-weight"] = "bold"
6
+ # ele.attr[:href] = "http://www.youtube.com"
7
+ #
8
+ # b = ele.add_ele(:b)
9
+ # b.str = "Title of link"
10
+ #
11
+ # ele.html #=> "<a href=\"http://www.youtube.com\" style=\"font-weight: bold;\" class=\"custom_link\">\n\t<b>\n\t\tTitle of link\n\t</b>\n</a>\n"
3
12
  class Html_gen::Element
4
- attr_accessor :attr, :css, :classes, :str, :html
13
+ #Attributes hash which will be used to generate attributes-elements.
14
+ #===Example
15
+ # element.attr[:href] = "http://www.youtube.com"
16
+ attr_reader :attr
5
17
 
18
+ #CSS-hash which will be used to generate the 'style'-attribute.
19
+ #===Example
20
+ # element.css["font-weight"] = "bold"
21
+ attr_reader :css
22
+
23
+ #Classes-array which will be used to generate the 'class'-attribute.
24
+ #===Example
25
+ # element.classes += ["class1", "class2"]
26
+ # element.html #=> ... class="class1 class2"...
27
+ attr_reader :classes
28
+
29
+ #This string is used to generate the value of an element. It will be HTML-escaped.
30
+ #===Example
31
+ # element = Html_gen::Element.new("b")
32
+ # element.str = "Te<i>s</i>t"
33
+ # element.html(:pretty => false) #=> "<b>Te&lt;i&gt;s&lt;/i&gt;t</b>"
34
+ attr_accessor :str
35
+
36
+ #This string is used to generate the value of an element. It will not be HTML-escaped.
37
+ #===Example
38
+ # element = Html_gen::Element.new("b")
39
+ # element.str_html = "Te<i>s</i>t"
40
+ # element.html #=> "<b>Te<i>s</i>t</b>"
41
+ attr_accessor :str_html
42
+
43
+ #You can give various arguments as shortcuts to calling the methods. You can also decide what should be used for newline and indentation.
44
+ # Html_gen::Element.new(:b, {
45
+ # :css => {"font-weight" => "bold"},
46
+ # :attr => {"href" => "http://www.youtube.com"},
47
+ # :classes => ["class1", "class2"],
48
+ # :str => "A title",
49
+ # :str_html => "Some custom URL as title",
50
+ # :nl => "\n",
51
+ # :inden => "\t",
52
+ # :eles => [Html_gen::Element.new("i", :str => "Hello world!")
53
+ # })
6
54
  def initialize(name, args = {})
7
- if args[:attr]
8
- @attr = args[:attr]
9
- else
10
- @attr = {}
11
- end
12
-
13
55
  raise "'name' should be a string or a symbol but was a '#{name.class.name}'."if !name.is_a?(String) and !name.is_a?(Symbol)
14
-
15
56
  @name = name
16
57
 
17
- {:classes => [], :html => "", :str => "", :css => {}, :eles => [], :nl => "\n", :inden => "\t"}.each do |arg, default_val|
58
+ {:attr => {}, :classes => [], :str_html => "", :str => "", :css => {}, :eles => [], :nl => "\n", :inden => "\t"}.each do |arg, default_val|
18
59
  if args[arg]
19
60
  instance_variable_set("@#{arg}", args[arg])
20
61
  else
21
62
  instance_variable_set("@#{arg}", default_val)
22
63
  end
64
+
65
+ args.delete(arg)
23
66
  end
67
+
68
+ raise "Unknown arguments: '#{args.keys.join(",")}'." if !args.empty?
24
69
  end
25
70
 
71
+ #Adds a sub-element to the element.
72
+ #===Examples
73
+ # element = Html_gen::Element.new("a")
74
+ # another_ele = element.add_ele("b")
75
+ # another_ele.str = "Hello world!"
76
+ # element.html #=> "<a>\n\t<b>\n\t\tHello world!\n\t</b>\n</a>\n"
26
77
  def add_ele(name, args = {})
27
78
  ele = Html_gen::Element.new(name, args)
28
79
  @eles << ele
29
80
  return ele
30
81
  end
31
82
 
83
+ # Returns the HTML for the element.
84
+ # To avoid indentation and newlines you can use the 'pretty'-argument:
85
+ # element.html(:pretty => false)
32
86
  def html(args = {})
33
87
  if args[:level]
34
88
  level = args[:level]
@@ -42,15 +96,23 @@ class Html_gen::Element
42
96
  pretty = true
43
97
  end
44
98
 
99
+ #Used for keeping 'pretty'-value and correct indentation according to parent elements.
45
100
  pass_args = {:level => (level + 1), :pretty => pretty}
46
101
 
102
+ #Clone the attributes-hash since we are going to add stuff to it, and it shouldnt be reflected (if 'html' is called multiple times, it will bug unless we clone).
47
103
  attr = @attr.clone
48
- classes = @classes.clone
49
104
 
50
- str = ""
105
+ #Start generating the string with HTML (possible go give a custom 'str'-variable where the content should be pushed to).
106
+ if args[:str]
107
+ str = args[:str]
108
+ else
109
+ str = ""
110
+ end
111
+
51
112
  str << @inden * level if pretty and level > 0
52
113
  str << "<#{@name}"
53
114
 
115
+ #Add content from the 'css'-hash to the 'style'-attribute in the right format.
54
116
  if !@css.empty?
55
117
  style = ""
56
118
  @css.each do |key, val|
@@ -66,36 +128,40 @@ class Html_gen::Element
66
128
  end
67
129
  end
68
130
 
131
+ #Add content from the 'classes'-array to the 'class'-attribute in the right format.
69
132
  if !@classes.empty?
70
133
  class_str = @classes.join(" ")
71
134
 
72
135
  if @attr[:class] and !@attr[:class].empty?
73
- @attr[:class] << " #{class_str}"
136
+ attr[:class] << " #{class_str}"
74
137
  else
75
- @attr[:class] = class_str
138
+ attr[:class] = class_str
76
139
  end
77
140
  end
78
141
 
142
+ #Write out the attributes to the string.
79
143
  attr.each do |key, val|
80
- str << " #{key}=\"#{Knj::Web.html(val)}\""
144
+ str << " #{key}=\"#{Html_gen.escape_html(val)}\""
81
145
  end
82
146
 
83
- if @eles.empty? and @str.empty? and @html.empty?
147
+ if @eles.empty? and @str.empty? and @str_html.empty?
148
+ #If no sub-string, sub-HTML or sub-elements are given, we should end the HTML with " />".
84
149
  str << " />"
85
150
  str << @nl if pretty
86
151
  else
152
+ #Write end-of-element and then all sub-elements.
87
153
  str << ">"
88
154
  str << @nl if pretty
89
155
 
90
156
  if !@str.empty?
91
157
  str << @inden * (level + 1) if pretty
92
- str << Knj::Web.html(@str)
158
+ str << Html_gen.escape_html(@str)
93
159
  str << @nl if pretty
94
160
  end
95
161
 
96
- if !@html.empty?
162
+ if !@str_html.empty?
97
163
  str << @inden * (level + 1) if pretty
98
- str << @html
164
+ str << @str_html
99
165
  str << @nl if pretty
100
166
  end
101
167
 
@@ -108,6 +174,7 @@ class Html_gen::Element
108
174
  str << @nl if pretty
109
175
  end
110
176
 
177
+ #Returns the string.
111
178
  return str
112
179
  end
113
180
  end
@@ -2,13 +2,26 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "HtmlGen" do
4
4
  it "should be able to generate html" do
5
- html = Html_gen::Element.new(:td, :classes => [:test]).html
5
+ html = Html_gen::Element.new(:td, :classes => [:test]).html(:pretty => false)
6
6
  raise "Expected valid HTML." if html != "<td class=\"test\" />"
7
7
 
8
- html = Html_gen::Element.new(:td, :attr => {:colspan => 2}).html
8
+ html = Html_gen::Element.new(:td, :attr => {:colspan => 2}).html(:pretty => false)
9
9
  raise "Expected valid HTML." if html != "<td colspan=\"2\" />"
10
10
 
11
- html = Html_gen::Element.new(:td, :css => {:width => "80px"}).html
11
+ html = Html_gen::Element.new(:td, :css => {:width => "80px"}).html(:pretty => false)
12
12
  raise "Expected valid HTML: '#{html}'." if html != "<td style=\"width: 80px;\" />"
13
+
14
+ a = Html_gen::Element.new(:a)
15
+ b = a.add_ele(:b)
16
+ b.str = "Test"
17
+
18
+ html = a.html(:pretty => false)
19
+ raise "Expected something else." if html != "<a><b>Test</b></a>"
20
+
21
+ html = Html_gen::Element.new(:b, :str => "<b>Test</b>").html(:pretty => false)
22
+ raise "Expected escape HTML: '#{html}'." if html != "<b>&lt;b&gt;Test&lt;/b&gt;</b>"
23
+
24
+ html = Html_gen::Element.new(:b, :str_html => "<b>Test</b>").html(:pretty => false)
25
+ raise "Expected escape HTML: '#{html}'." if html != "<b><b>Test</b></b>"
13
26
  end
14
27
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: html_gen
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.0
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -13,20 +13,9 @@ cert_chain: []
13
13
  date: 2012-08-10 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: knjrbfw
18
- requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- type: :runtime
25
- prerelease: false
26
- version_requirements: *id001
27
16
  - !ruby/object:Gem::Dependency
28
17
  name: rspec
29
- requirement: &id002 !ruby/object:Gem::Requirement
18
+ requirement: &id001 !ruby/object:Gem::Requirement
30
19
  none: false
31
20
  requirements:
32
21
  - - ~>
@@ -34,10 +23,10 @@ dependencies:
34
23
  version: 2.8.0
35
24
  type: :development
36
25
  prerelease: false
37
- version_requirements: *id002
26
+ version_requirements: *id001
38
27
  - !ruby/object:Gem::Dependency
39
28
  name: rdoc
40
- requirement: &id003 !ruby/object:Gem::Requirement
29
+ requirement: &id002 !ruby/object:Gem::Requirement
41
30
  none: false
42
31
  requirements:
43
32
  - - ~>
@@ -45,10 +34,10 @@ dependencies:
45
34
  version: "3.12"
46
35
  type: :development
47
36
  prerelease: false
48
- version_requirements: *id003
37
+ version_requirements: *id002
49
38
  - !ruby/object:Gem::Dependency
50
39
  name: bundler
51
- requirement: &id004 !ruby/object:Gem::Requirement
40
+ requirement: &id003 !ruby/object:Gem::Requirement
52
41
  none: false
53
42
  requirements:
54
43
  - - ">="
@@ -56,10 +45,10 @@ dependencies:
56
45
  version: 1.0.0
57
46
  type: :development
58
47
  prerelease: false
59
- version_requirements: *id004
48
+ version_requirements: *id003
60
49
  - !ruby/object:Gem::Dependency
61
50
  name: jeweler
62
- requirement: &id005 !ruby/object:Gem::Requirement
51
+ requirement: &id004 !ruby/object:Gem::Requirement
63
52
  none: false
64
53
  requirements:
65
54
  - - ~>
@@ -67,7 +56,7 @@ dependencies:
67
56
  version: 1.8.4
68
57
  type: :development
69
58
  prerelease: false
70
- version_requirements: *id005
59
+ version_requirements: *id004
71
60
  description: A small framework for generating HTML.
72
61
  email: k@spernj.org
73
62
  executables: []
@@ -86,6 +75,7 @@ files:
86
75
  - README.rdoc
87
76
  - Rakefile
88
77
  - VERSION
78
+ - html_gen.gemspec
89
79
  - lib/html_gen.rb
90
80
  - lib/html_gen_element.rb
91
81
  - spec/html_gen_spec.rb
@@ -104,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
94
  requirements:
105
95
  - - ">="
106
96
  - !ruby/object:Gem::Version
107
- hash: -1933339893132629672
97
+ hash: 3676011512365822361
108
98
  segments:
109
99
  - 0
110
100
  version: "0"