HTML-AutoTag 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8b3e9d049b5f87041ab173a6525b100d9691176
4
+ data.tar.gz: 05ff1a5a8e3e8c7dc66e900810d571164c82f246
5
+ SHA512:
6
+ metadata.gz: ca81f53e28caf46f5860ca0d07dddcb0d4bdada52a41e3e4a11bf759d40a5f55972f9a5bb0e222704d1398c15c99e65c845b79f8ca14f6c93ed1d9344ba41340
7
+ data.tar.gz: 3d4de05a33d46cf030cca71a3a760f966d3b1d3ef7e6951f72644ae626db09ea301d68b0eb101dc32e2bdc4ce6640e0701d82d7b1127d7ebf71ec478311d79ca
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - jruby-18mode
5
+ - jruby-19mode
6
+ - 1.9.3
7
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in HTML-AutoTag.gemspec
4
+ gemspec
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'HTML/AutoTag/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "HTML-AutoTag"
8
+ spec.version = HTML::AutoTag::VERSION
9
+ spec.authors = ["jeffa"]
10
+ spec.email = ["jeffa@cpan.org"]
11
+ spec.description = %q{Just another HTML tag generator for ruby.}
12
+ spec.summary = %q{Generate HTML tags with ease (HTML4, HTML5, XHTML and SVG).}
13
+ spec.homepage = "https://github.com/jeffa/html-autotag-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
data/License.md ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 Jeff Anderson
2
+ ================================
3
+
4
+ MIT License
5
+ -----------
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:test]
4
+
5
+ task :test do
6
+ Dir.glob( './t/*.rb' ).each { |file| require file }
7
+ end
@@ -0,0 +1,115 @@
1
+ module HTML
2
+
3
+ class AutoAttr
4
+
5
+ attr_accessor 'hash', 'sorted'
6
+
7
+ def initialize( hash = {}, sorted = 0 )
8
+ @hash = hash
9
+ @sorted = sorted
10
+ end
11
+
12
+ def to_s
13
+
14
+ keys = @sorted ? @hash.keys.sort : @hash.keys
15
+ str = ''
16
+ seen = {}
17
+
18
+ keys.each do |key|
19
+ unless seen.has_key?( key )
20
+ val = @hash[key]
21
+ val = stringify( val ) if val.kind_of?( Hash )
22
+ val = rotate( val ) if val.kind_of?( Array )
23
+ str += sprintf( ' %s="%s"', key( key ), val( val ) )
24
+ end
25
+ seen[key] = 1
26
+ end
27
+
28
+ return str
29
+ end
30
+
31
+ def key( key )
32
+ key = key.gsub( /\s+/, '' )
33
+ key = key.gsub( /["'>=\/]/, '' )
34
+ return key
35
+ end
36
+
37
+ def val( val )
38
+ return '' if val.match( /^\s+$/ )
39
+ val = val.gsub( /"/, '' )
40
+ return val
41
+ end
42
+
43
+ def rotate( array )
44
+ val = array.shift
45
+ array.push( val )
46
+ return val
47
+ end
48
+
49
+ def stringify( hash )
50
+
51
+ keys = @sorted ? hash.keys.sort : hash.keys
52
+ vals = keys.map{ |key|
53
+ val = ''
54
+ if hash[key].kind_of?( Array )
55
+ val = rotate( hash[key] )
56
+ elsif hash[key].kind_of?( Hash )
57
+ val = @sorted ? hash[key].keys.sort[0] : hash[key].keys[0]
58
+ else
59
+ val = hash[key]
60
+ end
61
+ "#{key}: #{val}"
62
+ }
63
+ return vals.join( '; ' ) + (vals.length ? ';' : '')
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ =begin rdoc
72
+
73
+ = NAME
74
+
75
+ HTML::AutoAttr - Just another HTML attribute generator.
76
+
77
+ == SYNOPSIS
78
+
79
+ <code>
80
+ require 'HTML/AutoAttr'
81
+ attr = HTML::AutoAttr.new( { 'foo' => ['bar','baz','qux'] } )
82
+
83
+ 4.times puts attr.to_s
84
+ </code>
85
+
86
+ == DESCRIPTION
87
+
88
+ This module will translate nested hash keys and values into HTML
89
+ tag attributes that can have ther values automatically rotated.
90
+
91
+ == SEE ALSO
92
+
93
+ * http://www.w3.org/TR/html5/syntax.html#attributes-0
94
+
95
+ * http://www.w3.org/TR/html-markup/syntax.html#syntax-attributes
96
+
97
+ == GITHUB
98
+
99
+ The Github project is https://github.com/jeffa/html-autotag-ruby
100
+
101
+ == SUPPORT
102
+
103
+ You can find documentation for this module with the ri command.
104
+
105
+ ri HTML::AutoAttr
106
+
107
+ == AUTHOR
108
+
109
+ Jeff Anderson, <jeffa at cpan.org>
110
+
111
+ == LICENSE AND COPYRIGHT
112
+
113
+ Copyright 2015 Jeff Anderson. (See License.md shipped with distro)
114
+
115
+ =end
@@ -0,0 +1,5 @@
1
+ module HTML
2
+ class AutoTag
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,114 @@
1
+ require "HTML/AutoTag/version"
2
+ require "HTML/AutoAttr"
3
+
4
+ module HTML
5
+
6
+ class AutoTag
7
+
8
+ attr_accessor 'encodes', 'indent', 'level', 'sorted', 'newline'
9
+
10
+ def initialize( params = {} )
11
+ @encodes = params['encodes'] || ''
12
+ @indent = params['indent'] || ''
13
+ @level = params['level'] || 0
14
+ @sorted = params['sorted'] || 0
15
+ @newline = params['indent'] ? "\n" : ''
16
+
17
+ end
18
+
19
+ def tag( params = {} )
20
+
21
+ attr = HTML::AutoAttr.new( params['attr'] || {}, @sorted )
22
+
23
+ # emtpy tag
24
+ unless params['cdata'] and params['cdata'].to_s.length
25
+ return '<' + params['tag'] + ' />' + @newline
26
+ end
27
+
28
+ cdata = ''
29
+ no_post_indent = 0
30
+ if params['cdata'].kind_of?( Array )
31
+
32
+ if params['cdata'][0].kind_of?( Hash )
33
+
34
+ @level += 1
35
+ cdata = @newline
36
+
37
+ params['cdata'].each do |hash|
38
+ cdata += tag( hash )
39
+ end
40
+ @level -= 1
41
+
42
+ else
43
+ str = ''
44
+ params['data'].each do |scalar|
45
+ str += tag( 'tag' => params['tag'], 'attr' => attr, 'cdata' => scalar )
46
+ end
47
+ return str
48
+ end
49
+
50
+ elsif params['cdata'].kind_of?( Hash )
51
+ @level += 1
52
+ cdata = @newline + tag( params['cdata'] )
53
+ @level -= 1
54
+
55
+ else
56
+ # do encoding here
57
+ cdata = params['cdata']
58
+ no_post_indent = 1
59
+ end
60
+
61
+ return (@indent * @level) \
62
+ + '<' + params['tag'] + attr.to_s + '>' \
63
+ + cdata.to_s + ( no_post_indent ? '' : ( @indent * @level ) ) \
64
+ + '</' + params['tag'] + '>' + @newline
65
+
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ =begin rdoc
73
+
74
+ = NAME
75
+
76
+ HTML::AutoTag - Just another HTML tag generator.
77
+
78
+ == SYNOPSIS
79
+
80
+ <code>
81
+ require 'HTML/AutoTag'
82
+ tag = HTML::AutoTag.new
83
+
84
+ puts auto.tag( 'tag' => 'p', 'cdata' => 'paragraph' )
85
+ </code>
86
+
87
+ == DESCRIPTION
88
+
89
+ Generate nested HTML (HTML4, XHTML and HTML5) tags with custom indentation,
90
+ custom encoding and automatic attribute value rotation.
91
+
92
+ == SEE ALSO
93
+
94
+ * ttp://www.w3.org/TR/html5/syntax.html
95
+
96
+ == GITHUB
97
+
98
+ The Github project is https://github.com/jeffa/html-autotag-ruby
99
+
100
+ == SUPPORT
101
+
102
+ You can find documentation for this module with the ri command.
103
+
104
+ ri HTML::AutoTag
105
+
106
+ == AUTHOR
107
+
108
+ Jeff Anderson, <jeffa at cpan.org>
109
+
110
+ == LICENSE AND COPYRIGHT
111
+
112
+ Copyright 2015 Jeff Anderson. (See License.md shipped with distro)
113
+
114
+ =end
data/readme.md ADDED
@@ -0,0 +1,29 @@
1
+ HTML-AutoTag (ruby)
2
+ ===================
3
+ Just another HTML tag generator for ruby.
4
+
5
+ Description
6
+ -----------
7
+ Generate HTML tags with ease (HTML4, HTML5, XHTML and SVG).
8
+
9
+ Synopsis
10
+ --------
11
+ ```ruby
12
+ require 'HTML/AutoTag'
13
+
14
+ auto = HTML::AutoTag.new
15
+
16
+ puts auto.tag( 'tag' => 'p', 'cdata' => 'a paragraph' )
17
+ ```
18
+
19
+ Installation
20
+ ------------
21
+ We are currently pre-Alpha, so installation is not recommended.
22
+
23
+ * rake test
24
+ * rake build
25
+ * ~~rake install~~ :skull:
26
+
27
+ License and Copyright
28
+ ---------------------
29
+ See [License](License.md).
data/t/01-load.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "test/unit"
2
+
3
+ class TestLoad < Test::Unit::TestCase
4
+
5
+ def test_load
6
+ assert_nothing_raised { require "HTML/AutoTag.rb" }
7
+ assert_nothing_raised { require "HTML/AutoAttr.rb" }
8
+ end
9
+
10
+ end
data/t/02-tag.rb ADDED
@@ -0,0 +1,46 @@
1
+ require "test/unit"
2
+ require "HTML/AutoTag"
3
+
4
+ class TestTag < Test::Unit::TestCase
5
+
6
+ def test_init
7
+ auto = HTML::AutoTag.new
8
+ assert_equal( '', auto.encodes, "no args encodes correct" )
9
+ assert_equal( '', auto.indent, "no args indent correct" )
10
+ assert_equal( 0, auto.level, "no args level correct" )
11
+ assert_equal( 0, auto.sorted, "no args sorted correct" )
12
+ assert_equal( '', auto.newline, "no args newline correct" )
13
+
14
+ auto = HTML::AutoTag.new( 'encodes' => '<=&>', 'indent' => ' ' )
15
+ assert_equal( '<=&>', auto.encodes, "encodes set correct" )
16
+ assert_equal( ' ', auto.indent, "indent set correct" )
17
+ assert_equal( "\n", auto.newline, "newline set correct" )
18
+ end
19
+
20
+ def test_empty
21
+ auto = HTML::AutoTag.new
22
+ assert_equal( '<hr />', auto.tag( 'tag' => 'hr' ), "empty tag correct" )
23
+ end
24
+
25
+ def test_nonempty
26
+ auto = HTML::AutoTag.new
27
+ assert_equal( '<p>0</p>', auto.tag( 'tag' => 'p', 'cdata' => 0 ), "paragraph tag correct" )
28
+ assert_equal( '<ol><li>1</li></ol>', auto.tag( 'tag' => 'ol', 'cdata' => { 'tag' => 'li', 'cdata' => 1 } ), "ol tag correct" )
29
+ assert_equal( '<ol><li>1</li><li>2</li></ol>', auto.tag( 'tag' => 'ol', 'cdata' => [{ 'tag' => 'li', 'cdata' => 1 }, { 'tag' => 'li', 'cdata' => 2 }] ), "ol tag correct" )
30
+ end
31
+
32
+ def test_indent
33
+ auto = HTML::AutoTag.new( 'indent' => ' ' )
34
+ assert_equal( "<p>0</p>\n", auto.tag( 'tag' => 'p', 'cdata' => 0 ), "paragraph tag correct" )
35
+ assert_equal( "<ol>\n <li>1</li>\n</ol>\n", auto.tag( 'tag' => 'ol', 'cdata' => { 'tag' => 'li', 'cdata' => 1 } ), "ol tag correct" )
36
+ assert_equal( "<ol>\n <li>1</li>\n <li>2</li>\n</ol>\n", auto.tag( 'tag' => 'ol', 'cdata' => [{ 'tag' => 'li', 'cdata' => 1 }, { 'tag' => 'li', 'cdata' => 2 }] ), "ol tag correct" )
37
+ end
38
+
39
+ def test_level
40
+ auto = HTML::AutoTag.new( 'indent' => ' ', 'level' => 3 )
41
+ assert_equal( " <p>0</p>\n", auto.tag( 'tag' => 'p', 'cdata' => 0 ), "paragraph tag correct" )
42
+ assert_equal( " <ol>\n <li>1</li>\n</ol>\n", auto.tag( 'tag' => 'ol', 'cdata' => { 'tag' => 'li', 'cdata' => 1 } ), "ol tag correct" )
43
+ assert_equal( " <ol>\n <li>1</li>\n <li>2</li>\n</ol>\n", auto.tag( 'tag' => 'ol', 'cdata' => [{ 'tag' => 'li', 'cdata' => 1 }, { 'tag' => 'li', 'cdata' => 2 }] ), "ol tag correct" )
44
+ end
45
+
46
+ end
data/t/03-attrs.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "test/unit"
2
+ require "HTML/AutoAttr"
3
+
4
+ class TestAttrs < Test::Unit::TestCase
5
+
6
+ def test_keys
7
+
8
+ attr = HTML::AutoAttr.new
9
+ assert_equal( '', attr.key( ' ' ), "only spaces" )
10
+ assert_equal( 'foobar', attr.key( 'foo bar'), "with space" )
11
+ assert_equal( '', attr.key( '">=//=>"' ), "only scrubbed chars" )
12
+ assert_equal( 'foo<bar', attr.key( 'foo<bar' ), "does not scrub < char" )
13
+
14
+ end
15
+
16
+ def test_vals
17
+
18
+ attr = HTML::AutoAttr.new
19
+ assert_equal( '', attr.val( ' ' ), "only spaces" )
20
+ assert_equal( 'foo bar', attr.val( 'foo bar'), "with space" )
21
+ assert_equal( "'foo'", attr.val( "'foo'" ), "single quotes are not scrubbed" )
22
+ assert_equal( 'foo', attr.val( '"foo"' ), "double quotes are scrubbed" )
23
+
24
+ end
25
+
26
+ def test_simple_attrs
27
+ attr = HTML::AutoAttr.new( { 'foo' => 'bar', 'baz' => 'qux' }, 1 )
28
+ assert_equal( ' baz="qux" foo="bar"', attr.to_s, "correct simple attrs" )
29
+ end
30
+
31
+ def test_rotate_attrs
32
+ attr = HTML::AutoAttr.new( { 'foo' => ['bar','baz','qux'], 'baz' => ['foo','qux'] }, 1 )
33
+ assert_equal( ' baz="foo" foo="bar"', attr.to_s, "correct rotate attrs 1" )
34
+ assert_equal( ' baz="qux" foo="baz"', attr.to_s, "correct rotate attrs 2" )
35
+ assert_equal( ' baz="foo" foo="qux"', attr.to_s, "correct rotate attrs 3" )
36
+ assert_equal( ' baz="qux" foo="bar"', attr.to_s, "correct rotate attrs 4" )
37
+ assert_equal( ' baz="foo" foo="baz"', attr.to_s, "correct rotate attrs 5" )
38
+ end
39
+
40
+ def test_nested_attrs
41
+ attr = HTML::AutoAttr.new( { 'foo' => { 'bar' => 'baz', 'qux' => 'foo' } }, 1 )
42
+ assert_equal( ' foo="bar: baz; qux: foo;"', attr.to_s, "correct nested attrs" )
43
+ end
44
+
45
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: HTML-AutoTag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jeffa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Just another HTML tag generator for ruby.
42
+ email:
43
+ - jeffa@cpan.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - HTML-AutoTag.gemspec
52
+ - License.md
53
+ - Rakefile
54
+ - lib/HTML/AutoAttr.rb
55
+ - lib/HTML/AutoTag.rb
56
+ - lib/HTML/AutoTag/version.rb
57
+ - readme.md
58
+ - t/01-load.rb
59
+ - t/02-tag.rb
60
+ - t/03-attrs.rb
61
+ homepage: https://github.com/jeffa/html-autotag-ruby
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Generate HTML tags with ease (HTML4, HTML5, XHTML and SVG).
85
+ test_files: []