bakkdoor-srxml 0.0.4 → 0.1.0

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/README CHANGED
@@ -6,6 +6,7 @@ Created by Christopher Bertels (bakkdoor@flasht.de / http://github.com/bakkdoor/
6
6
  SRXML is a very (super, if you will!) lightweight xml generator for Ruby. No big magic here, it simply uses method_missing to create the tags. There is some plan to make it more useful. For Example being able to parse existing files etc. but there are probably better libraries to do that.
7
7
  If you simply want to create an xml file without the need for extra fancy formatting, SRXML could just be the deal for you!
8
8
 
9
+ Oh, by the way. It also supports html-output now.
9
10
 
10
11
  =========================
11
12
  LICENSE:
@@ -19,8 +20,9 @@ Play with it as much as you like, fork it and put the changes here back on githu
19
20
  ==================================
20
21
  Short Example (from examble.rb):
21
22
  ==================================
22
-
23
- require "sr_xml"
23
+
24
+ require "rubygems"
25
+ require "srxml"
24
26
 
25
27
  xml = SRXML::XML.new
26
28
 
@@ -48,6 +50,32 @@ Which will then give you something like this:
48
50
  <something_else>yo!</something_else>
49
51
  </project>
50
52
  </projects>
53
+
54
+ ==================================
55
+ Simple HTML generation:
56
+ ==================================
57
+
58
+ SRXML now also supports html-output:
59
+
60
+ require "rubygems"
61
+ require "srxml"
62
+
63
+ html = SRXML::XML.new :xml => false, :html => true # this will automatically set SRXML into 'html-mode',
64
+ # knowing for example, that certain tags are single (no end tag)
65
+ html.html{
66
+ html.head{
67
+ html.title "my title"
68
+ }
69
+ html.body{
70
+ html.div(:style => "padding:10em; font-size: 140%"){
71
+ "hello, world!"
72
+ }
73
+ }
74
+ }
75
+
76
+ puts html.to_s
77
+
78
+ Will output the expected html-output.
51
79
 
52
80
  For more examples take a look in the example-files.
53
81
 
@@ -1,4 +1,4 @@
1
- require "srxml"
1
+ require "../lib/srxml"
2
2
 
3
3
  @projects = []
4
4
 
@@ -1,4 +1,4 @@
1
- require "srxml"
1
+ require "../lib/srxml"
2
2
 
3
3
  @projects = []
4
4
 
@@ -1,55 +1,55 @@
1
1
  module SRXML
2
2
 
3
- =begin rdoc
4
- BlankSlate class. Has nearly no methods, except for anything among the regex
5
- (__*, instance_eval, inspect).
6
- Is used by the XML class, since we want to use method_missing to create the xml-output.
7
- Having fewer methods predefined is good, since we want as few nameclashes as possible.
8
- =end
3
+
4
+ # <tt>BlankSlate</tt> class. Has nearly no methods, except for anything among the regex (__*, instance_eval, inspect).
5
+ # Is used by the XML class, since we want to use method_missing to create the xml-output.
6
+ # Having fewer methods predefined is good, since we want as few nameclashes as possible.
9
7
  class BlankSlate
10
8
  instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_eval|inspect)/ }
11
9
  end
12
10
 
13
11
 
14
- =begin rdoc
15
- Main class. Use it to create the xml-output.
16
- For example:
17
- xml = SRXML::XML.new :xml => false
18
- xml.people{
19
- xml.person{
20
- xml.name "Todd"
21
- }
22
- xml.person{
23
- xml.name "Mary"
24
- }
25
- }
26
-
27
- puts xml.to_s :formatted
28
-
29
- Will give you:
30
- <people>
31
- <person>
32
- <name>Todd</name>
33
- </person>
34
- <person>
35
- <name>Mary</name>
36
- </person
37
- </people>
38
- =end
12
+ # Main class. Use it to create the xml-output.
13
+ # For example:
14
+ # xml = SRXML::XML.new :xml => false
15
+ # xml.people{
16
+ # xml.person{
17
+ # xml.name "Todd"
18
+ # }
19
+ # xml.person{
20
+ # xml.name "Mary"
21
+ # }
22
+ # }
23
+ #
24
+ # puts xml.to_s :formatted
25
+ #
26
+ # Will give you:
27
+ # <people>
28
+ # <person>
29
+ # <name>Todd</name>
30
+ # </person>
31
+ # <person>
32
+ # <name>Mary</name>
33
+ # </person
34
+ # </people>
39
35
  class XML < BlankSlate
40
36
 
41
- attr_accessor :singles # defines tags, which don't have a closing-tag (e.g. <br/>)
42
- attr_reader :xml_tag # indicates, if xml-tag is used (should be false for html-mode, for example)
43
- attr_reader :sep # holds a seperator-string, indicating, where a newline (for formatted-output) should be placed
44
- # if not specified, its default value is '<>'
37
+ @@html_singles = [:br, :hr, :img, :input, :meta, :param, :link, :base, :embed, :dd, :dt]
38
+
39
+ attr_accessor :singles # defines tags, which don't have a closing-tag (e.g. <br/>)
40
+ attr_reader :xml_tag # indicates, if xml-tag is used (should be false for html-mode, for example)
41
+ attr_reader :sep # holds a seperator-string, indicating, where a newline (for formatted-output) should be placed
42
+ # if not specified, its default value is '<>'
43
+
44
+ def self.html_singles
45
+ @@html_singles
46
+ end
45
47
 
46
- =begin rdoc
47
- Constructor. Takes a options-hash.
48
- Valid options are:
49
- :xml => true/false # indicates if xml-tag should be used at top of xml-output
50
- :sep => "<>" # custom specified seperator-string; '<>' if not set
51
- :singles => [] # custom specified list of single-tags (no closing tag), e.g. '<br/>' in html
52
- =end
48
+ # Constructor. Takes a options-hash.
49
+ # Valid options are:
50
+ # :xml => true/false # indicates if xml-tag should be used at top of xml-output
51
+ # :sep => "<>" # custom specified seperator-string; '<>' if not set
52
+ # :singles => [] # custom specified list of single-tags (no closing tag), e.g. '<br/>' in html
53
53
  def initialize(options = {})
54
54
  @xml_tag = options[:xml].nil? ? true : options[:xml]
55
55
  @sep = options[:sep] || "<>"
@@ -59,17 +59,17 @@ module SRXML
59
59
  if @xml_tag
60
60
  @output = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>#{@sep}"]
61
61
  else
62
+ if options[:html]
63
+ @singles += @@html_singles
64
+ end
62
65
  @output = []
63
66
  end
64
67
  end
65
68
 
66
-
67
- =begin rdoc
68
- This method is used to create the xml-ouput based on the called methods on the XML-object.
69
- =end
69
+ # This method is used to create the xml-ouput based on the called methods on the XML-object.
70
70
  def method_missing(method_name, *args)
71
71
  @output << "<#{method_name}"
72
-
72
+
73
73
  attributes = []
74
74
  value = ""
75
75
 
@@ -102,20 +102,19 @@ module SRXML
102
102
  end
103
103
 
104
104
 
105
- =begin rdoc
106
- Returns the xml-output string created by using the xml-object.
107
- Default-output is non_formatted. Optional output-styles are:
108
- :formatted # will put newlines in the correct places
109
- :keep_sep # will leave seperator-string in place (probably non-valid xml then - mainly for debug purposes)
110
- =end
105
+ # Returns the xml-output string created by using the xml-object.
106
+ # Default-output is non_formatted. Optional output-styles are:
107
+ # :formatted # will put newlines in the correct places
108
+ # :keep_sep # will leave seperator-string in place (probably non-valid xml then - mainly for debug purposes)
111
109
  def to_s(option = :non_formatted)
110
+ @output = @output.select{|x| x.class == String}
112
111
  if option == :formatted
113
112
  # format here with newline etc.
114
- @output.select{|x| x.class == String}.join("").gsub(@sep, "\n")
113
+ @output.join("").gsub(@sep, "\n")
115
114
  elsif option == :keep_sep
116
115
  @output.join("")
117
116
  else
118
- @output.select{|x| x.class == String}.join("").gsub(@sep, "")
117
+ @output.join("").gsub(@sep, "")
119
118
  end
120
119
  end
121
120
 
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><friends><friend><name>Thomas</name><age>20</age></friend><friend><name>Jessie</name><age>21</age></friend><friend><name>Joe</name><age>22</age></friend><friend><name>Sandra</name><age>19</age></friend><friend><name>Chris</name><age>25</age></friend></friends>
@@ -0,0 +1 @@
1
+ <html><head><title>Title</title></head><body><div>OkiDoki</div></body></html>
@@ -1,5 +1,7 @@
1
1
  module TestHelper
2
- def fixture(name)
3
- File.open("test/fixtures/#{name}.xml", "r").read
2
+ def fixture(name, opts = {})
3
+ opts[:xml] = opts[:xml].nil? ? true : opts[:xml]
4
+ ending = opts[:xml] ? ".xml" : ""
5
+ File.open("test/fixtures/#{name}#{ending}", "r").read
4
6
  end
5
7
  end
@@ -64,4 +64,33 @@ class TestSRXML < Test::Unit::TestCase
64
64
 
65
65
  assert_equal(fixture("inner_string.html"), xml.to_s)
66
66
  end
67
+
68
+ def test_html_singles
69
+ html = SRXML::XML.new :xml => false, :html => true
70
+ assert_equal(SRXML::XML.html_singles, html.singles)
71
+ end
72
+
73
+
74
+ def test_html_output
75
+ html = SRXML::XML.new :xml => false, :html => true
76
+
77
+ html.html{
78
+ html.head{
79
+ html.title "hello, world!"
80
+ html.link :rel => "bla", :href => "http://localhost/whatever", :title => "something here!"
81
+ }
82
+ html.body{
83
+ html.div(:style => "padding: 10em; font-size: 150%"){
84
+ html.p "hey, whats up"
85
+ html.br
86
+ html.p{
87
+ "nothing much, i guess!"
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ assert_equal(fixture("test_html_output.html", :xml => false), html.to_s)
94
+ end
95
+
67
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bakkdoor-srxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Bertels
@@ -29,12 +29,15 @@ files:
29
29
  - examples/example.xml
30
30
  - examples/example2.rb
31
31
  - examples/example2.xml
32
+ - test/fixtures/friends.xml
32
33
  - test/fixtures/friends_formatted.xml
34
+ - test/fixtures/inner_string.html.xml
33
35
  - test/fixtures/projects.xml
34
36
  - test/fixtures/projects_formatted.xml
37
+ - test/fixtures/test_output_html
35
38
  - test/test_helper.rb
36
39
  - test/test_srxml.rb
37
- has_rdoc: false
40
+ has_rdoc: true
38
41
  homepage: http://github.com/bakkdoor/srxml
39
42
  post_install_message:
40
43
  rdoc_options:
@@ -64,3 +67,9 @@ summary: Simple Ruby XML Generator
64
67
  test_files:
65
68
  - test/test_helper.rb
66
69
  - test/test_srxml.rb
70
+ - test/fixtures/friends.xml
71
+ - test/fixtures/friends_formatted.xml
72
+ - test/fixtures/inner_string.html.xml
73
+ - test/fixtures/projects.xml
74
+ - test/fixtures/projects_formatted.xml
75
+ - test/fixtures/test_output_html