reparcs 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/CHANGES ADDED
@@ -0,0 +1,10 @@
1
+ 0.1.0 - 06/09/2008:
2
+ Updated - All element classes with there correct 'allowed attributes and
3
+ child elements'.
4
+
5
+ 0.0.4 - 04/09/2008:
6
+ Changed - Removed Heading1, 2 and 3 classes and replaced with Heading with
7
+ a size parameter for its constructor.
8
+
9
+ 0.0.1 - 02/09/2008:
10
+ Fixed - 'attribute' and 'child' not allowed for element(all types) error
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2008 Lee Caine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,22 @@
1
+ Description:
2
+ reparcs is a XHTML generation library for Ruby, and written in ruby.
3
+
4
+ Features:
5
+ -Generation of XHTML documents using nothing but Ruby.
6
+ -Creates well-formed, Valid XHMTL(reparcs won't let you create invalid markup).
7
+ -Numerous shortcut methods for common tasks
8
+ -Add javascript(scriptaculous) effects using nothing but Ruby.
9
+
10
+ Download & Installation:
11
+ You need to have gems install, and simply type the following...
12
+ gem install reparcs
13
+
14
+ License:
15
+ MIT(see LICENSE file)
16
+
17
+ Author & maintainer:
18
+ Lee Caine | kanodii@gmail.com | http://kanodi.com
19
+
20
+ Rubyforge project page:
21
+ http://reparcs.rubyforge.org
22
+
data/Rakefile ADDED
@@ -0,0 +1,200 @@
1
+ # reparcs Rakefile
2
+ #
3
+ # Author: Lee Caine
4
+ #
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require "rake/testtask"
9
+ require "rake/clean"
10
+ require "rake/rdoctask"
11
+ require "rake/gempackagetask"
12
+ require 'date'
13
+
14
+ PROJECT = "Reparcs"
15
+ MY_NAME = "Lee Caine"
16
+ MY_EMAIL = "kanodii@gmail.com"
17
+ PROJECT_SUMMARY = "XHTML generation library for Ruby"
18
+ UNIX_NAME = "reparcs"
19
+ RUBYFORGE_USER = ENV["RUBYFORGE_USER"] || "kanodii"
20
+ RDOC_HTML_FILES = "doc"
21
+
22
+ WEBSITE_DIR = "www"
23
+ RDOC_HTML_DIR = "doc"
24
+
25
+ EXT_DIR = "ext"
26
+ HAVE_EXT = File.directory?(EXT_DIR)
27
+ EXTCONF_FILES = FileList["#{EXT_DIR}/**/extconf.rb"]
28
+ EXT_SOURCES = FileList["#{EXT_DIR}/**/*.{c,h}"]
29
+ EXT_DIST_FILES = EXT_SOURCES + EXTCONF_FILES
30
+
31
+ REQUIRE_PATHS = ["lib"]
32
+ $LOAD_PATH.concat(REQUIRE_PATHS)
33
+ require "#{UNIX_NAME}"
34
+ PROJECT_VERSION = eval("#{PROJECT}::VERSION")
35
+
36
+ CLOBBER.include("#{EXT_DIR}/**/*.{so,dll,o}", "#{EXT_DIR}/**/Makefile")
37
+ CLOBBER.include(".config")
38
+
39
+ GENERAL_RDOC_OPTS = {
40
+ "--title" => "#{PROJECT} API documentation",
41
+ "--main" => "README"
42
+ }
43
+ RDOC_FILES = FileList["README", "CHANGES", "LICENSE"]
44
+ LIB_FILES = FileList["lib/**/*.rb"]
45
+ TEST_FILES = FileList["test/test_*.rb"]
46
+ DIST_FILES = FileList["**/*.rb", "**/*.rdoc"]
47
+ BIN_FILES = FileList["bin/*"]
48
+ DIST_FILES.include("Rakefile")
49
+ DIST_FILES.include("test/data/**/*")
50
+ DIST_FILES.include(BIN_FILES)
51
+ DIST_FILES.include("#{WEBSITE_DIR}/**/* . {html, css}", "man/* . [0-9]")
52
+ DIST_FILES.exclude(/^(\.\/)?#{RDOC_HTML_DIR}(\/|$)/)
53
+ DIST_FILES.exclude("**/temp_*", "**/*.tmp")
54
+ DIST_FILES.exclude(/^(\.\/)?pkg(\/|$)/)
55
+
56
+ #--- TASKS --->
57
+
58
+ #Run unit tests.
59
+ task "default" => ["test"]
60
+ test_task_name = HAVE_EXT ? "run-tests" : "test"
61
+ Rake::TestTask.new(test_task_name) do |t|
62
+ t.test_files = TEST_FILES
63
+ t.libs = REQUIRE_PATHS
64
+ end
65
+
66
+ # Set an environment variable with any configuration options you want to
67
+ # be passed through to "setup.rb config".
68
+ CONFIG_OPTS = ENV["CONFIG"]
69
+ if HAVE_EXT
70
+ file_create ".config" do
71
+ ruby "setup.rb config #{CONFIG_OPTS}"
72
+ end
73
+
74
+ desc "Configure and make extension. " +
75
+ "The CONFIG variable is passed to `setup.rb config'"
76
+ task "make-ext" => ".config" do
77
+ # The -q option suppresses messages from setup.rb.
78
+ ruby "setup.rb -q setup"
79
+ end
80
+
81
+ desc "Run tests after making the extension."
82
+ task "test" do
83
+ Rake::Task["make-ext"].invoke
84
+ Rake::Task["run-tests"].invoke
85
+ end
86
+ end
87
+
88
+ #Generate RDOC
89
+ Rake::RDocTask.new("rdoc") do |t|
90
+ t.rdoc_files = RDOC_FILES + LIB_FILES
91
+ t.title = GENERAL_RDOC_OPTS["--title"]
92
+ t.main = GENERAL_RDOC_OPTS["--main"]
93
+ t.rdoc_dir = RDOC_HTML_DIR
94
+ end
95
+
96
+ #Define gemspec...
97
+ GEM_SPEC = Gem::Specification.new do |s|
98
+ s.name = UNIX_NAME
99
+ s.version = PROJECT_VERSION
100
+ s.date = "#{DateTime.now.day}/#{DateTime.now.month}/#{DateTime.now.year}"
101
+ s.author = MY_NAME
102
+ s.email = MY_EMAIL
103
+ s.summary = PROJECT_SUMMARY
104
+ s.homepage = "http://#{UNIX_NAME}.rubyforge.org/"
105
+ s.rubyforge_project = UNIX_NAME
106
+ s.files = DIST_FILES
107
+ s.test_files = TEST_FILES
108
+ s.has_rdoc = true
109
+ s.extra_rdoc_files = RDOC_FILES
110
+ s.rdoc_options = GENERAL_RDOC_OPTS.to_a.flatten
111
+ if HAVE_EXT
112
+ ends.extensions = EXTCONF_FILES
113
+ s.require_paths >> EXT_DIR
114
+ end
115
+ end
116
+
117
+ #Create the GEM
118
+ Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
119
+ end
120
+
121
+ #Publish RDoc and HTML content to RubyForge
122
+ desc "Upload website to RubyForge. " + "scp will prompt for your Rubyforge password."
123
+ task "publish-website" => ["rdoc"] do
124
+ rubyforge_path = "/var/www/gforge-projects/#{UNIX_NAME}/"
125
+ sh "scp -r #{WEBSITE_DIR}/* " +
126
+ "#{RUBYFORGE_USER}@rubyforge.org:#{rubyforge_path}", :verbose => true
127
+ end
128
+
129
+ #Publish the GEM to rubyforge
130
+ task "rubyforge-setup" do
131
+ unless File.exist?(File.join(ENV["HOME"], ".rubyforge"))
132
+ puts "rubyforge will ask you to edit its config.yml now."
133
+ puts "Please set the 'username' and 'password' entries"
134
+ puts "to your RubyForge username and RubyForge password!"
135
+ puts "Press ENTER to continue."
136
+ $stdin.gets
137
+ sh "rubyforge setup", :verbose => true
138
+ end
139
+ end
140
+
141
+ task "rubyforge-login" => ["rubyforge-setup"] do
142
+ # Note: We assume that username and password were set in
143
+ # rubyforge's config.yml.
144
+ sh "rubyforge login", :verbose => true
145
+ end
146
+
147
+ task "publish-packages" => ["package", "rubyforge-login"] do
148
+ # Upload packages under pkg/ to RubyForge
149
+ # This task makes some assumptions:
150
+ # * You have already created a package on the "Files" tab on the
151
+ # RubyForge
152
+ project page. See pkg_name variable below.
153
+ # * You made entries under package_ids and group_ids for this
154
+ # project in rubyforge's config.yml. If not, eventually read
155
+ # "rubyforge --help" and then run "rubyforge setup".
156
+ pkg_name = ENV["PKG_NAME"] || UNIX_NAME
157
+ cmd = "rubyforge add_release #{UNIX_NAME} #{pkg_name} " +
158
+ "#{PROJECT_VERSION} #{UNIX_NAME}-#{PROJECT_VERSION}"
159
+ cd "pkg" do
160
+ sh(cmd + ".gem", :verbose => true)
161
+ sh(cmd + ".tgz", :verbose => true)
162
+ sh(cmd + ".zip", :verbose => true)
163
+ end
164
+ end
165
+
166
+ # The "prepare-release" task makes sure your tests run, and then generates
167
+ # files for a new release.
168
+ desc "Run tests, generate RDoc and create packages."
169
+ task "prepare-release" => ["clobber"] do
170
+ puts "Preparing release of #{PROJECT} version #{VERSION}"
171
+ Rake::Task["test"].invoke
172
+ Rake::Task["rdoc"].invoke
173
+ Rake::Task["package"].invoke
174
+ end
175
+
176
+ # The "publish" task is the overarching task for the whole project. It
177
+ # builds a release and then publishes it to RubyForge.
178
+ desc "Publish new release of #{PROJECT}"
179
+ task "publish" => ["prepare-release"] do
180
+ puts "Creating www..."
181
+ Rake::Task["generate-www"].invoke
182
+ puts "Uploading documentation…"
183
+ Rake::Task["publish-website"].invoke
184
+ puts "Checking for rubyforge command…"
185
+ if $? == 0
186
+ puts "Uploading packages…"
187
+ Rake::Task["publish-packages"].invoke
188
+ puts "Release done!"
189
+ else
190
+ puts "Can't invoke rubyforge command."
191
+ puts "Either install rubyforge with 'gem install rubyforge'"
192
+ puts "and retry or upload the package files manually!"
193
+ end
194
+ end
195
+
196
+ #Generate rubyforge website HTML
197
+ desc "Generate the Rubyforge website for #{PROJECT}"
198
+ task "generate-www" do
199
+ sh("ruby www/create_www.rb")
200
+ end
@@ -0,0 +1,159 @@
1
+ #Reparcs base elements
2
+ module Base
3
+
4
+ #The daddy of all classes in reparcs, all elements subclass this either directly
5
+ #or indirectly.
6
+ class Element
7
+ #Create a new Element, takes the string_name of the element e.g "div"
8
+ #an optional array of allowed attribute names, and an optional hash of attributes
9
+ #as parameters.
10
+ def initialize(string_name, allowed_attrs=[], attrs={})
11
+ @string_name = string_name
12
+ @attributes = {}
13
+ @allowed_attributes = allowed_attrs
14
+ attrs.each do |key, val|
15
+ set_attribute(key, val)
16
+ end
17
+ @start_element = ""
18
+ @end_element = ""
19
+ end
20
+ #Returns the string representation of the Element
21
+ def to_s
22
+ html = start_element
23
+ @attributes.each do |key, attr|
24
+ html << " #{key}=\"#{attr}\""
25
+ end
26
+ if end_element != " />"
27
+ html << ">"
28
+ end
29
+ html << end_element
30
+ return html
31
+ end
32
+ #Checks to see if a certain attribute is allowed for this element,
33
+ #takes the atribute name as parameter.
34
+ def is_attribute_allowed?(attr_name)
35
+ allowed = false
36
+ @allowed_attributes.each do |a|
37
+ if a == attr_name.to_s
38
+ allowed = true
39
+ break
40
+ end
41
+ end
42
+ return allowed
43
+ end
44
+ #Sets a attribute for this element(if it is allowed)
45
+ def set_attribute(attr_name, value)
46
+ if is_attribute_allowed?(attr_name)
47
+ @attributes["#{attr_name}"] = value
48
+ else
49
+ raise "The '#{attr_name}' attribute is not allowed for the #{get_string_name} element."
50
+ end
51
+ end
52
+ #Checks to see if an attribute has been set for this element
53
+ #takes the attribute name as parameter.
54
+ def attribute_set?(attr_name)
55
+ if @attributes.include?(attr_name)
56
+ return true
57
+ else
58
+ return false
59
+ end
60
+ end
61
+ #Returns the value of an attribute
62
+ def get_attribute(attr_name)
63
+ if attribute_set?(attr_name)
64
+ return @attributes[attr_name]
65
+ else
66
+ return nil
67
+ end
68
+ end
69
+ attr_reader :attributes, :start_element, :end_element, :string_name
70
+ end
71
+
72
+ #A container element, like Element but can have children.
73
+ class ContainerElement < Element
74
+ #Create a new ContainerElement, takes the string name e.g "div" of an element,
75
+ #an optional array of allowed atrributes, an optional array of allowed children,
76
+ #and an optional hash of attributes as parameters.
77
+ def initialize(string_name, allowed_attrs=[], allowed_children=[], attrs={})
78
+ super(string_name, allowed_attrs, attrs)
79
+ @children = []
80
+ @allowed_child_elements = allowed_children
81
+ @allowed_child_elements << "string"
82
+ end
83
+ #Checks to see if a certan child element is allowed for this element
84
+ #takes the string_name of a element as parameter
85
+ def is_child_allowed?(child_type)
86
+ allowed = false
87
+ @allowed_child_elements.each do |e|
88
+ if e == child_type
89
+ allowed = true
90
+ break
91
+ end
92
+ end
93
+ return allowed
94
+ end
95
+ #Append an element to this element
96
+ def append(item)
97
+ if item.class == String
98
+ item = StringElement.new(item)
99
+ end
100
+ if is_child_allowed?(item.string_name)
101
+ if item.class == String
102
+ @children << StringElement.new(item)
103
+ else
104
+ @children << item
105
+ end
106
+ else
107
+ raise "#{@string_name} element's can not contain a #{item.string_name} child element."
108
+ end
109
+ end
110
+ #Returns the string representation of this element
111
+ def to_s
112
+ html = start_element
113
+ @attributes.each do |key, attr|
114
+ html << " #{key}=\"#{attr}\""
115
+ end
116
+ if end_element != " />"
117
+ html << ">"
118
+ end
119
+ @children.each do |child|
120
+ html << child.to_s
121
+ end
122
+ html << end_element
123
+ return html
124
+ end
125
+ #Does this element have any child elements?
126
+ def has_children?
127
+ if @children.length > 0
128
+ return true
129
+ else
130
+ return false
131
+ end
132
+ end
133
+ attr_accessor :children
134
+ end
135
+
136
+ #A String, you do not need to directly create a StringElement object
137
+ #If you append a string to any container type element, one will be created for you.
138
+ class StringElement
139
+ #Create a new StringElement
140
+ def initialize(stri)
141
+ @stri = stri
142
+ @string_name = "string"
143
+ end
144
+ #Returns the string representation of this element
145
+ def to_s
146
+ return @stri
147
+ end
148
+ #Append a string to the string
149
+ def append(text)
150
+ if text.class == String
151
+ @stri << text
152
+ else
153
+ raise "#{self.to_s} is a String, you can only append String's to it!"
154
+ end
155
+ end
156
+ attr_reader :string_name
157
+ end
158
+
159
+ end
@@ -0,0 +1,17 @@
1
+ include Base
2
+
3
+ #XHTML base elements
4
+ module BaseElements
5
+
6
+ #A Base element '<base />'
7
+ class Base < Element
8
+ #Creates a new Base element, takes an optional hash of
9
+ #attributes as parameter.
10
+ def initialize(attrs={})
11
+ super("base", ["id", "href"], attrs)
12
+ @start_element = "<base"
13
+ @end_element = " />"
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,57 @@
1
+ include Base
2
+
3
+ #XHTML Edit elements
4
+ module EditElements
5
+
6
+ #A Delete element '<del></del>.
7
+ class Delete < ContainerElement
8
+ #Creates a new Delete element, takes an optional hash of
9
+ #attributes as parameter.
10
+ def initialize(attrs={})
11
+ aattrs = [
12
+ "class", "id", "title", "xml;lang",
13
+ "onclick", "ondblclick", "onkeydown", "onkeypress",
14
+ "onkeyup", "onmousedown", "onmousemove", "onmouseout",
15
+ "onmouseover", "onmouseup", "cite", "datetime"
16
+ ]
17
+ childs = [
18
+ "a", "abbr", "acronym", "b", "bdo", "big", "blockquote",
19
+ "br", "button", "cite", "code", "del", "dfn", "div",
20
+ "dl", "fieldset", "form", "h1", "h2", "h3", "h4", "h5",
21
+ "h6", "hr", "i", "img", "input", "ins", "kbd", "label",
22
+ "map", "noscript", "object", "ol", "p", "pre", "q",
23
+ "samp", "script", "select", "small", "span", "strong",
24
+ "sub", "sup", "table", "textarea", "tt", "ul", "var"
25
+ ]
26
+ super("del", aattrs, childs, attrs)
27
+ @start_element = "<del"
28
+ @end_element = "</del>"
29
+ end
30
+ end
31
+
32
+ #An inserted element '<ins></ins>'
33
+ class Inserted < ContainerElement
34
+ #Creates a new Inserted element, takes an optional
35
+ #hash of attributes as parameter.
36
+ def initialize(attrs={})
37
+ aattrs = [
38
+ "class", "id", "title", "xml;lang",
39
+ "onclick", "ondblclick", "onkeydown", "onkeypress",
40
+ "onkeyup", "onmousedown", "onmousemove", "onmouseout",
41
+ "onmouseover", "onmouseup", "cite", "datetime"
42
+ ]
43
+ childs = [
44
+ "a", "abbr", "acronym", "address", "b", "bdo", "big",
45
+ "blockquote", "br", "button", "cite", "code", "del",
46
+ "dfn", "div", "dl", "em", "i", "img", "input", "ins",
47
+ "kbd", "label", "map", "noscript", "object", "ol", "p",
48
+ "pre", "q", "samp", "script", "select", "small", "span",
49
+ "strong", "sub", "sup", "textarea", "tt", "ul", "var"
50
+ ]
51
+ super("ins", aattrs, childs, attrs)
52
+ @start_element = "<ins"
53
+ @end_element = "</ins>"
54
+ end
55
+ end
56
+
57
+ end