html_gen 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/html_gen.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{html_gen}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-08-11}
12
+ s.date = %q{2012-08-24}
13
13
  s.description = %q{A small framework for generating HTML.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
@@ -81,7 +81,14 @@ class Html_gen::Element
81
81
  # another_ele.str = "Hello world!"
82
82
  # element.html #=> "<a>\n\t<b>\n\t\tHello world!\n\t</b>\n</a>\n"
83
83
  def add_ele(name, args = {})
84
- ele = Html_gen::Element.new(name, args)
84
+ ele = Html_gen::Element.new(name, args.merge(:nl => @nl, :inden => @inden))
85
+ @eles << ele
86
+ return ele
87
+ end
88
+
89
+ #Add a text-element to the element.
90
+ def add_str(str)
91
+ ele = Html_gen::Text_ele.new(:str => str, :inden => @inden, :nl => @nl)
85
92
  @eles << ele
86
93
  return ele
87
94
  end
@@ -103,7 +110,7 @@ class Html_gen::Element
103
110
  end
104
111
 
105
112
  #Used for keeping 'pretty'-value and correct indentation according to parent elements.
106
- pass_args = {:level => (level + 1), :pretty => pretty}
113
+ pass_args = {:level => (level + 1), :pretty => pretty, :inden => @inden}
107
114
 
108
115
  #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).
109
116
  attr = @attr.clone
@@ -100,6 +100,7 @@ class Html_gen::Parser
100
100
  end
101
101
  end
102
102
 
103
+ #Parses all attributes of the current tag.
103
104
  def parse_attr_of_tag(ele, tag_name)
104
105
  loop do
105
106
  if match = search(/\A\s*(\S+)=(\"|'|)/)
@@ -127,6 +128,7 @@ class Html_gen::Parser
127
128
  end
128
129
  end
129
130
 
131
+ #Parses an attribute-value until a given quote-char is reached.
130
132
  def parse_attr_until_quote(quote_char, quote_val)
131
133
  val = ""
132
134
 
@@ -3,10 +3,34 @@ class Html_gen::Text_ele
3
3
 
4
4
  def initialize(args)
5
5
  @str = args[:str]
6
+ @inden = args[:inden]
7
+ @nl = args[:nl]
6
8
  end
7
9
 
8
10
  #Returns the text that this element holds.
9
11
  def str
10
- return @args[:str]
12
+ return @str
13
+ end
14
+
15
+ #Returns the text HTML-escaped.
16
+ def html(args)
17
+ if args[:level]
18
+ level = args[:level]
19
+ else
20
+ level = 0
21
+ end
22
+
23
+ if !args.key?(:pretty) or args[:pretty]
24
+ pretty = true
25
+ else
26
+ pretty = false
27
+ end
28
+
29
+ str = ""
30
+ str << @inden * (level + 1) if pretty
31
+ str << Html_gen.escape_html(@str)
32
+ str << @nl if pretty
33
+
34
+ return str
11
35
  end
12
36
  end
@@ -23,5 +23,12 @@ describe "HtmlGen" do
23
23
 
24
24
  html = Html_gen::Element.new(:b, :str_html => "<b>Test</b>").html(:pretty => false)
25
25
  raise "Expected escape HTML: '#{html}'." if html != "<b><b>Test</b></b>"
26
+
27
+ div_ele = Html_gen::Element.new(:div)
28
+ div_ele.add_ele(:br)
29
+ div_ele.add_str("This is a test")
30
+
31
+ html = div_ele.html(:pretty => false)
32
+ raise "Expected HTML: '#{html}'." if html != "<div><br />This is a test</div>"
26
33
  end
27
34
  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.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-08-11 00:00:00 +02:00
13
+ date: 2012-08-24 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  requirements:
98
98
  - - ">="
99
99
  - !ruby/object:Gem::Version
100
- hash: 4461972116095674458
100
+ hash: 4125427218880571716
101
101
  segments:
102
102
  - 0
103
103
  version: "0"