rubified 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ext/object.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def to_html
3
+ to_s
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ module Rubified
2
+ # Used to shorten the creation of tags; just include or extend this module to use it.
3
+ # A small HTML page without Canvas:
4
+ # Rubified::Tag::Html.new.to_html {
5
+ # Rubified::Tag::Head.new.to_html {
6
+ # Rubified::Tag::Title.new.to_html {"Generated with Rubified"}
7
+ # }
8
+ # Rubified::Tag::Body.new.to_html {
9
+ # Rubified::Tag::Div.new({:id=>"foo", :class=>"bar"}) {"Hello world!"}
10
+ # }
11
+ # }
12
+ #
13
+ # Instead you can use:
14
+ # include Rubifed::Canvas
15
+ # html {
16
+ # head {
17
+ # title {"Generated with Rubified"}
18
+ # }
19
+ # body {
20
+ # div({id=>"foo", :class=>"bar"})
21
+ # }
22
+ # }
23
+ # True, one more line (the include), but a LOT less repetitious and more readable.
24
+ # Both of these will produce:
25
+ # <html>
26
+ # <head>
27
+ # <title>Generated with Rubified</title>
28
+ # </head>
29
+ # <body>
30
+ # <div id=>"foo" class=>"bar">Hello world!</div>
31
+ # </body>
32
+ # </html
33
+ module Canvas
34
+ def self.add_tag_method(tclass)
35
+ define_method(tclass.to_s.split("::").last.downcase) do |params={}, &block|
36
+ # Figure out what class to use by the method name.
37
+ tagclass = Rubified::Tag.const_get((__method__).capitalize)
38
+ # Create a new tag then run it
39
+ tagclass.new(params).to_html(&block)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ class Rubified::Tag
2
+ # Create and returns a new tag class with the name +name+. To create a tag that
3
+ # does NOT use a matched pair, set paired to false. Tags like <img> need this.
4
+ def self.new_tag(name, paired=true)
5
+ # Store the new tag's name in a constant; the block to create the new class
6
+ # won't keep local variables. A bit of a cheat
7
+ const_set(:Name, name)
8
+ newbie = Class.new(self)
9
+ remove_const(:Name)
10
+ const_set(name.capitalize, newbie)
11
+ newbie.const_set(:Paired, paired)
12
+ newbie
13
+ end
14
+
15
+ # The arguments to this method is a hash key of the parameters to the HTML tag.
16
+ def initialize(params={})
17
+ # The name of the tag, e.g. font
18
+ @tname = self.class.to_s.split("::").last.downcase
19
+ # Any parameters of this tag, e.g. if you have the HTML tag:
20
+ # <div id="foo" class="bar">
21
+ # then the parameters would be +id+ and +class+.
22
+ @params = params
23
+ end
24
+
25
+ # Dumps this tag to an HTML string.
26
+ def to_html
27
+ pstring = ""
28
+ @params.each {|key, val| pstring << " #{key}=\"#{val}\""}
29
+ result = "<#{@tname}#{pstring}>"
30
+ if block_given?
31
+ result << yield.to_html
32
+ end
33
+ if self.class::Paired
34
+ result << "</#{@tname}>"
35
+ end
36
+ result
37
+ end
38
+ end
@@ -0,0 +1,19 @@
1
+ # Pre-load a small list of HTML tags.
2
+ tags = {
3
+ :html=>true,
4
+ :head=>true,
5
+ :body=>true,
6
+ :p=>true,
7
+ :div=>true,
8
+ :span=>true,
9
+ :font=>true,
10
+ :b=>true,
11
+ :i=>true,
12
+ :tt=>true,
13
+
14
+ :img=>false
15
+ }
16
+
17
+ tags.each {|tag, paired|
18
+ Rubified::Tag.new_tag(tag, paired)
19
+ }
data/lib/rubified.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Rubified
2
+ require 'ext/object'
3
+ require 'rubified/tag'
4
+ require 'rubified/tags'
5
+ require 'rubified/canvas'
6
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubified
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - J. Wostenberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-02-09 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Generate XML or HTML documents using only Ruby without having to worry about syntax.
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/rubified/tag.rb
27
+ - lib/rubified/tags.rb
28
+ - lib/rubified/canvas.rb
29
+ - lib/rubified.rb
30
+ - lib/ext/object.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/awostenberg/Rubified
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.6.2
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Create XML or HTML pages using only Ruby.
60
+ test_files: []
61
+