taggart 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,195 @@
1
+ Taggart allows you to tag Strings with HTML
2
+ ===========================================
3
+ How and what
4
+ ------------
5
+ ```ruby
6
+ "Hello World!".h1
7
+ ```
8
+
9
+ returns
10
+
11
+ ```html
12
+ <h1>Hello World!</h1>
13
+ ```
14
+
15
+ or try (in bad typographical taste)
16
+
17
+ ```ruby
18
+ "Important!".em.strong
19
+ ```
20
+
21
+ to get
22
+
23
+ ```html
24
+ <em><strong>Important!</em></strong>
25
+ ```
26
+
27
+ or do some more nesting
28
+
29
+ ```ruby
30
+ ("Label".td + "Value".td).tr.table
31
+ ```
32
+
33
+ to render a small table
34
+
35
+ ```html
36
+ <table><tr><td>Label</td><td>Value</td></tr></table>
37
+ ```
38
+
39
+ Add HTML attributes as Ruby parameters.
40
+
41
+ ```ruby
42
+ "hello".span(class: 'stuff', id: 'thing')
43
+ ```
44
+
45
+ to get a nice HTML string like this
46
+
47
+ ```html
48
+ <span class="stuff" id="thing">hello</span>
49
+ ```
50
+
51
+ You can also use arrays, how about this table?
52
+
53
+ ```ruby
54
+ (%w(r1c1 r1c2 r1c3).td.tr + %w(r2c1 r2c2 r2c3).td.tr).table
55
+ ```
56
+
57
+ will produce this HTML (author's line breaks and indentation)
58
+
59
+ ```html
60
+ <table>
61
+ <tr>
62
+ <td>r1c1</td>
63
+ <td>r1c2</td>
64
+ <td>r1c3</td>
65
+ </tr>
66
+ <tr>
67
+ <td>r2c1</td>
68
+ <td>r2c2</td>
69
+ <td>r2c3</td>
70
+ </tr>
71
+ </table>
72
+ ```
73
+
74
+ Naturally we can do single tags too.
75
+
76
+ ```ruby
77
+ "Gimme a".br
78
+ ```
79
+
80
+ will return.
81
+
82
+ ```html
83
+ Gimme a<br />
84
+ ```
85
+
86
+ Also try some of the 'cleverness', for example calling `.script` on a `.js` file will yield a different result compared to running it on a Javascript.
87
+ Like this:
88
+
89
+ ```ruby
90
+ "alert('This string will pop up an alert!')".script
91
+ ```
92
+ gives you
93
+ ```html
94
+ <script id="pants_pants" type="text/javascript">alert('This string will pop up an alert!')</script>"
95
+ ```
96
+
97
+ whereas this string
98
+
99
+ ```ruby
100
+ "/script/awesomes_script.js".script
101
+ ```
102
+
103
+ gives you this
104
+
105
+ ```html
106
+ "<script type="text/javascript" src="/script/awesomes_script.js"></script>"
107
+ ```
108
+
109
+ Naturally this also works with attributes.
110
+
111
+ See the `spec/taggart_spec.rb` for more examples (until I have time to do a bigger-better-faster-example file).
112
+
113
+ Background:
114
+ -----------
115
+ I have a lot of Ruby code that marks some String in an HTML tag, usually with a
116
+ specific CSS class. Or perhaps a link or so.
117
+ It's not particularly nice to write `"<strong>#{my_variable}</strong>"`. The more
118
+ complex the Ruby code is, the worse it is to have `"`, `#`, `{` or `}` soiling the code.
119
+ I was looking at the code and I thought, wouldn't it be nice if I could just tell that piece
120
+ of String that it's supposed to be a `<span>` or some other tag. One could simply
121
+ write `my_variable.strong` to get the job done. **Taggart** was born.
122
+
123
+ The Idea
124
+ --------
125
+ The idea was to simplify the code and make it easier and faster for me to add arbitrary HTML into my
126
+ non-HTML code. I wanted to stop breaking my fingers trying to get the _quote-string-hash-curlybrackets-code-curlybrackets-morestrings-quote_
127
+ right on my keyboard.
128
+ It was _never_ intended to be a full-fledged HTML page renderer. If you want to use it for that, I'd love to see the result, though.
129
+
130
+
131
+ Installation:
132
+ -------------
133
+ Install the gem:
134
+
135
+ ```
136
+ gem install taggart
137
+ ```
138
+
139
+ Then load Taggart with:
140
+
141
+ ```
142
+ require 'taggart'
143
+ ```
144
+
145
+ Taggart is now active, which means you can play around.
146
+
147
+
148
+ History
149
+ -------
150
+ - Added <script>-tag. You can now add ``"my-script.js".script`` and ``"alert('Hello World!')".script`.
151
+ - Added several tags (still not a complete list, tweet or send a pull request for more tags.)
152
+ - Removed several 'if DEBUG' lines
153
+ - Removed examples and most comments from taggart.rb file
154
+ - Converted the README to markdown for a bit better formatting fun.
155
+ - Added `.href` and `.img` feature.
156
+ - Created Gem
157
+ - Pushed code to Git.
158
+ - Created test Gem.
159
+ - Added files to create Gem and reorganised the file structure.
160
+ - Made `dual_sub()` pass all the tests, and added the examples from `.tr` (translate) Ruby docs to the test.
161
+ - More work on the "dynamic namespace clash resolver", in other words, `.tr` and sub work in both classic and Taggart way.
162
+ - Initial version of "dynamic namespace clash resolver" to fix issues with `.tr`.
163
+ - Added basic RSpec test.
164
+ - Added namespacing for Strings and Arrays.
165
+ - Implemented arrays; `["Label", "Value"].td.tr.table` and `["First", "Second", "Third"].li.ol`.
166
+ - Tidied up things a bit.
167
+ - Added a version of attributes `"Red".span(class: 'red')`.
168
+ - First version. Basic tags.
169
+
170
+
171
+ Future???
172
+ ---------
173
+ With your blessing. Like Ozzy said; _"The crazier you get, the crazier Ozzy gets!"_, or something.
174
+
175
+ * Potential validations, could check file, size, etc
176
+ * Switch between HTML and XHTML.
177
+ * Full fledged examples.
178
+ * Please send suggestions.
179
+
180
+
181
+ Issues:
182
+ -------
183
+ - `"hello".sub('world', 'world')` returns `<sub world world>hello</sub>`. Not really perfect.
184
+ - Please help me test it out.
185
+
186
+
187
+ Feedback welcome!!
188
+
189
+ Author: Jocke Selin <jocke@selincite.com> @jockeselin
190
+
191
+ Date: 2012-03-09
192
+
193
+ Version: 0.0.5 Build 010
194
+
195
+ Github: <https://github.com/jocke/taggart>
data/lib/taggart.rb CHANGED
@@ -1,74 +1,22 @@
1
- # Taggart is a proof-of-concept to "decorate" strings with HTML tags.
2
- # ===================================================================
3
- # Background:
4
- # It's not particularly nice to write "<strong>#{my_variable}</strong>" when
5
- # One could simply write my_variable.strong. The more complex the Ruby code
6
- # is, the nicer it is to not have ", #, { or } soiling the code.
7
- #
8
- #
9
- # Playtime:
10
- # Install the gem:
11
- # gem install taggart
12
- # Then load Taggart with:
13
- # require 'taggart'
14
- # Taggart is now active, which means you can play around.
15
- # Try:
16
- # "Hello World".h1
17
- # Or (typographically bad taste):
18
- # "Important".em.strong
19
- # Or do some proper nesting:
20
- # ("Label".td + "Value".td)._tr.table
21
- # Add attributes
22
- # "hello".span(class: 'stuff', id: 'thing')
23
- # How about a table using arrays?:
24
- # (%w(r1c1 r1c2 r1c3).td.tr + %w(r2c1 r2c2 r2c3).td.tr).table
25
- # We can also do single tags.
26
- # "Gimme a".br
27
- #
28
- # Issues:
29
- # - "hello".sub('world', 'world') returns
30
- # "<sub world world>hello</sub>"
31
- # Not really perfect
32
- #
33
- # Future??? (with your blessing)
34
- # - Potential validations - could check file, size, etc
35
- #
36
- # History
37
- # - Added href and img feature.
38
- # - Created Gem
39
- # - Pushed code to Git.
40
- # - Created test Gem.
41
- # - Added files to create Gem and reorganised the file structure.
42
- # - Made dual_sub pass all the tests, and added the examples from .tr (translate) Ruby docs to the test.
43
- # - More work on the "dynamic namespace clash resolver", in other words, tr and sub work in both classic and Taggart way.
44
- # - Initial version of "dynamic namespace clash resolver" to fix issues with .tr
45
- # - Added basic RSpec test.
46
- # - Added namespacing for Strings and Arrays
47
- # - Implemented arrays; ["Label", "Value"].td.tr.table and ["First", "Second", "Third"].li.ol
48
- # - Tidied up things a bit.
49
- # - Added a version of attributes ("Red".span(class: 'red'))
50
- # - First version. Basic tags.
51
- #
52
- # Feedback welcome!!
53
- #
1
+ # Taggart allows you to tag Strings with HTML
2
+ # ===========================================
54
3
  # Author: Jocke Selin <jocke@selincite.com>
55
- # Date: 2012-03-05
56
- # Version: 0.0.4 Build 009
4
+ # @jockeselin
5
+ # Date: 2012-03-09
6
+ # Version: 0.0.5 Build 010
57
7
  # Github: https://github.com/jocke/taggart
58
8
 
59
9
  module Taggart
60
10
  module String
61
11
 
62
12
  DEBUG = false
13
+
63
14
 
64
15
  # Redefining <tr> to work with both translate-tr and tag-tr
65
16
  def dual_tr(*args)
66
- puts "Args size: #{args.size}" if DEBUG
67
17
  if ((args.size == 2) && (args[0].is_a? String) && (args[1].is_a? String))
68
- puts "Send '#{args[0]}', '#{args[1]}' to translate-tr" if DEBUG
69
18
  self.translate(args[0], args[1])
70
19
  else
71
- puts "Send to tag-tr" if DEBUG
72
20
  self._tr(args.first)
73
21
  end
74
22
  end
@@ -76,14 +24,11 @@ module Taggart
76
24
 
77
25
  # Redefining <sub> to work with both substitute-sub and tag-sub
78
26
  def dual_sub(*args, &block)
79
- puts "Args size: #{args.size}" if DEBUG
80
27
  if (args[0].is_a? Hash)
81
- puts "Send to tag-sub" if DEBUG
82
28
  self._sub(args.first)
83
29
  elsif args.empty?
84
30
  self._sub
85
31
  else
86
- puts "Send '#{args[0]}', '#{args[1]}' to substitute-sub" if DEBUG
87
32
  if block_given?
88
33
  self.substitute(args[0]) { |*args| block.call(*args) }
89
34
  else
@@ -105,12 +50,23 @@ module Taggart
105
50
  "<img#{option_str} />"
106
51
  end
107
52
 
53
+
54
+ def script(*args)
55
+ if self[-3, 3] == '.js' # We've got a Jabbarscript file (could/should cater for all script types. VBScript, heheeh!)
56
+ option_str = parse_options(args << {type: "text/javascript", src: self})
57
+ "<script#{option_str}></script>"
58
+ else
59
+ option_str = parse_options(args << {type: "text/javascript"})
60
+ "<script#{option_str}>#{self}</script>"
61
+ end
62
+ end
63
+
108
64
 
109
65
  private
66
+
110
67
 
111
68
  # Parses the options for the tags
112
69
  def parse_options(args)
113
- # puts "parse_options: args #{args.inspect})" if DEBUG
114
70
  return "" if args.nil?
115
71
  output = []
116
72
  args.each do |argument|
@@ -122,7 +78,7 @@ module Taggart
122
78
  output << "#{key.to_s}=\"#{value}\""
123
79
  end
124
80
  else
125
- puts "Argument of type #{argument.class.name} is not implemented"
81
+ raise "Argument of type #{argument.class.name} is not implemented"
126
82
  end
127
83
  end
128
84
  return "" if output.empty?
@@ -132,7 +88,6 @@ module Taggart
132
88
 
133
89
  # Defines a single tag, such as <br /> or <hr />
134
90
  def self.single_attribute_tag(tag)
135
- puts "Defining single-tag '#{tag}'" if DEBUG
136
91
  define_method(tag) do |*args|
137
92
  option_str = parse_options(args)
138
93
  "#{self}<#{tag}#{option_str} />"
@@ -151,10 +106,12 @@ module Taggart
151
106
  end
152
107
 
153
108
 
154
- standard_tags = %w{ h1 h2 h3 h4 h5 h6 strong p a em title table thead tbody th td tfoot div span abbr acronym address dd dl dt li ol ul tt pre sup }
155
- special_tags = [['tr', '_tr'], ['sub', '_sub']]
109
+ standard_tags = %w{ h1 h2 h3 h4 h5 h6 a title html head table thead tfoot button fieldset form label
110
+ select legend option textarea body blockquote q tbody th td tfoot style div
111
+ span abbr acronym address dd dl dt li ol caption ul em strong p tt pre sup del
112
+ small cite code } # This is not a complete list - please tweet or pull req.
113
+ special_tags = [['tr', '_tr'], ['sub', '_sub']]
156
114
  tags = standard_tags + special_tags
157
- puts "Tag definitions: #{tags.inspect}" if DEBUG
158
115
  tags.each do |tag|
159
116
  if tag.class.name == 'String'
160
117
  self.attribute_tag(tag)
@@ -163,7 +120,7 @@ module Taggart
163
120
  end
164
121
  end
165
122
 
166
- %w{br hr}.each do |tag|
123
+ %w{br hr input link meta}.each do |tag|
167
124
  self.single_attribute_tag(tag)
168
125
  end
169
126
 
@@ -174,16 +131,15 @@ module Taggart
174
131
 
175
132
  DEBUG = false
176
133
 
134
+
177
135
  # Defines a standard tag that can create attributes
178
136
  def self.array_attribute_tag(tag, tag_name = nil)
179
137
  tag_name ||= tag
180
138
  puts "Defining array tag '#{(tag + "',").ljust(12)} with method name: '#{tag_name}'" if DEBUG
181
139
  define_method(tag_name) do |*args|
182
140
  args = args.first if args # Only using the first of the array
183
- puts "Array:#{tag_name} (with args #{args.inspect})" if DEBUG
184
141
  result = []
185
142
  self.each do |object|
186
- puts "Object: #{object.inspect} (Type: #{object.class.name})" if DEBUG
187
143
  if object.is_a? String
188
144
  result << object.send(tag_name.to_sym, args)
189
145
  elsif object.is_a? Symbol
@@ -196,11 +152,8 @@ module Taggart
196
152
  end
197
153
  end
198
154
 
199
- # Test examples:
200
- # [:aaa, 'hellloooo'].td
201
- # [:aaaa, [:bbb, :ccc, :ddd], :fff, :ggg].td
202
- # ["Joe", "Marcus", "Chris", "Jim", "Aaron"]
203
- # %w{One Two Three Four}.td(class: :numbers)
155
+
156
+ # Defines tags for the Array class.
204
157
  %w{td li}.each do |tag|
205
158
  self.array_attribute_tag(tag)
206
159
  end
@@ -227,4 +180,4 @@ end
227
180
 
228
181
  class Array
229
182
  include Taggart::Array
230
- end
183
+ end
data/spec/taggart_spec.rb CHANGED
@@ -175,6 +175,20 @@ describe Taggart::Array, "#array_attribute_tag" do
175
175
  end
176
176
  end
177
177
 
178
+ context "script tags" do
179
+ it "should render a script tag with src-attribute when the string ends in .js" do
180
+ "/jabbarscript/waste_cpu_cycles.js".script.should == "<script type=\"text/javascript\" src=\"/jabbarscript/waste_cpu_cycles.js\"></script>"
181
+ end
182
+
183
+ it "should render a script tag with src-attribute and an id-attribute when the string ends in .js" do
184
+ "/jabbarscript/fancy_pants.js".script(id: 'fancy_fancy').should == "<script id=\"fancy_fancy\" type=\"text/javascript\" src=\"/jabbarscript/fancy_pants.js\"></script>"
185
+ end
186
+
187
+ it "should render a script tag with when the string does not ends in .js" do
188
+ "document.write(\"Hello World!\");".script(id: 'pants_pants').should == "<script id=\"pants_pants\" type=\"text/javascript\">document.write(\"Hello World!\");</script>"
189
+ end
190
+ end
191
+
178
192
  context "combinations" do
179
193
  it "renders a clickable image" do
180
194
  "/path/to/img.png".img({class: :thumbnail}).a(href: '/hello/world.html').should == "<a href=\"/hello/world.html\"><img class=\"thumbnail\" src=\"/path/to/img.png\" /></a>"
data/taggart.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "taggart"
3
- s.version = "0.0.4"
4
- s.date = "2012-03-05"
5
- s.summary = "Simple HTML tag by String extension."
3
+ s.version = "0.0.5"
4
+ s.date = "2012-03-09"
5
+ s.summary = "Tag Strings with HTML; 'Hello World'.h1"
6
6
  s.description = "Tag your strings in a simple and easy way by running \"Hello World\".h1 for example."
7
7
  s.authors = ["Jocke Selin"]
8
8
  s.email = ["jocke@selincite.com"]
9
9
  s.homepage = "https://github.com/jocke/taggart"
10
10
  s.require_paths = ["lib"]
11
- s.files = ["lib/taggart.rb", "Rakefile", "taggart.gemspec", "README"]
11
+ s.files = ["lib/taggart.rb", "Rakefile", "taggart.gemspec", "README.markdown"]
12
12
  s.test_files = ["spec/taggart_spec.rb"]
13
13
  s.add_development_dependency "rspec"
14
14
  s.post_install_message = "Thanks for showing interest in Taggart! Please provide feedback to jocke@selincite.com!"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taggart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-05 00:00:00.000000000 Z
12
+ date: 2012-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70119724335800 !ruby/object:Gem::Requirement
16
+ requirement: &70190373434460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70119724335800
24
+ version_requirements: *70190373434460
25
25
  description: Tag your strings in a simple and easy way by running "Hello World".h1
26
26
  for example.
27
27
  email:
@@ -33,7 +33,7 @@ files:
33
33
  - lib/taggart.rb
34
34
  - Rakefile
35
35
  - taggart.gemspec
36
- - README
36
+ - README.markdown
37
37
  - spec/taggart_spec.rb
38
38
  homepage: https://github.com/jocke/taggart
39
39
  licenses: []
@@ -59,7 +59,7 @@ rubyforge_project:
59
59
  rubygems_version: 1.8.16
60
60
  signing_key:
61
61
  specification_version: 3
62
- summary: Simple HTML tag by String extension.
62
+ summary: Tag Strings with HTML; 'Hello World'.h1
63
63
  test_files:
64
64
  - spec/taggart_spec.rb
65
65
  has_rdoc:
data/README DELETED
@@ -1,57 +0,0 @@
1
- Taggart is a proof-of-concept to "decorate" strings with HTML tags.
2
- ===================================================================
3
- Background:
4
- It's not particularly nice to write "<strong>#{my_variable}</strong>" when
5
- One could simply write my_variable.strong. The more complex the Ruby code
6
- is, the nicer it is to not have ", #, { or } soiling the code.
7
-
8
-
9
- Playtime:
10
- Install the gem:
11
- gem install taggart
12
- Then load Taggart with:
13
- require 'taggart'
14
- Taggart is now active, which means you can play around.
15
- Try:
16
- "Hello World".h1
17
- Or (typographically bad taste):
18
- "Important".em.strong
19
- Or do some proper nesting:
20
- ("Label".td + "Value".td)._tr.table
21
- Add attributes
22
- "hello".span(class: 'stuff', id: 'thing')
23
- How about a table using arrays?:
24
- (%w(r1c1 r1c2 r1c3).td.tr + %w(r2c1 r2c2 r2c3).td.tr).table
25
- We can also do single tags.
26
- "Gimme a".br
27
-
28
- Issues:
29
- - "hello".sub('world', 'world') returns
30
- "<sub world world>hello</sub>"
31
- Not really perfect
32
-
33
- Future??? (with your blessing)
34
- - Potential validations - could check file, size, etc
35
-
36
- History
37
- - Added href and img feature.
38
- - Created Gem
39
- - Pushed code to Git.
40
- - Created test Gem.
41
- - Added files to create Gem and reorganised the file structure.
42
- - Made dual_sub pass all the tests, and added the examples from .tr (translate) Ruby docs to the test.
43
- - More work on the "dynamic namespace clash resolver", in other words, tr and sub work in both classic and Taggart way.
44
- - Initial version of "dynamic namespace clash resolver" to fix issues with .tr
45
- - Added basic RSpec test.
46
- - Added namespacing for Strings and Arrays
47
- - Implemented arrays; ["Label", "Value"].td.tr.table and ["First", "Second", "Third"].li.ol
48
- - Tidied up things a bit.
49
- - Added a version of attributes ("Red".span(class: 'red'))
50
- - First version. Basic tags.
51
-
52
- Feedback welcome!!
53
-
54
- Author: Jocke Selin <jocke@selincite.com>
55
- Date: 2012-02-16
56
- Version: 0.0.4 Build 009
57
- Github: https://github.com/jocke/taggart