ruby-tags 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 479c8738d0cf378f4c0c76a5b172c882995f9c12d12506db926dc7a9bf4e1c3f
4
+ data.tar.gz: 207a5d848f10ab35ed1eda33b7d7a86670d0aa3eadef7146cf61da884ae79cb5
5
+ SHA512:
6
+ metadata.gz: ff83e2d0e019fed08d8681e4cac4bbba10424bc228c0fc6fc5735b25010e6d4ffd75388a2dbf20258b95b4f549a86ebfc796fc14a76401f55c1cf6cbdf1f5daa
7
+ data.tar.gz: d9b30a0343d4411f732f295aa8eaff5e4942d98ea1fdb5ff5ad1109d5e4c06621782dbb9b3acd6ef1d02e5f188d40d2fde0d2229752fdc90dfd775566bec094e
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/lib/ruby-tags.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'ruby/tags/tags'
2
+ require 'ruby/tags/attribute'
3
+ require 'ruby/tags/html'
4
+
@@ -0,0 +1,69 @@
1
+ module Ruby
2
+ module Tags
3
+ class Attribute
4
+
5
+ def self.blank?(s)
6
+ s.nil? || s.to_s.strip.empty? || s.empty?
7
+ end
8
+
9
+ def initialize(attributes = {})
10
+ @attributes = attributes.to_h { |k,v| [k, v.to_s.split] }
11
+ end
12
+
13
+ def add(attribute)
14
+ if attribute.is_a? Hash
15
+ attribute.each do |k, v|
16
+ key = k&.to_sym
17
+ @attributes[key] = [] unless @attributes[key]
18
+ enum(v).each do |e|
19
+ @attributes[key] << e unless @attributes[key].include? e
20
+ end
21
+ end
22
+ end
23
+ add var(attribute) if attribute.is_a? Attribute
24
+ self
25
+ end
26
+
27
+ def remove(attribute)
28
+ if attribute.is_a? Hash
29
+ attribute.each do |k, v|
30
+ key = k&.to_sym
31
+ enum(v).each { |e| @attributes[key].delete e } if @attributes[key]
32
+ end
33
+ end
34
+ remove var(attribute) if attribute.is_a? Attribute
35
+ @attributes.delete attribute if [String, Symbol].any? { |x| attribute.is_a? x }
36
+ self
37
+ end
38
+
39
+ def render
40
+ @attributes.reject{ |k,v| [k,v].any? { |x| Attribute.blank? x } }
41
+ .reduce(@attributes.empty? && "" || " ") { |m, (k, a)| "#{m}#{k}='#{sanitize(a)}' " }
42
+ .delete_suffix(" ")
43
+ end
44
+
45
+ def ==(other)
46
+ @attributes.keys.sort == var(other).keys.sort &&
47
+ @attributes.values.flatten.sort == var(other).values.flatten.sort
48
+ end
49
+
50
+ private
51
+
52
+ def enum(v)
53
+ if v
54
+ v.is_a?(Enumerable) ? v : v.to_s.split
55
+ else
56
+ []
57
+ end
58
+ end
59
+
60
+ def var(other)
61
+ other.instance_variable_get(:@attributes)
62
+ end
63
+
64
+ def sanitize(s)
65
+ s.join(" ").gsub(/'/,"&#x27;").strip
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,74 @@
1
+ module Ruby
2
+ module Tags
3
+ module Html
4
+
5
+ def html5(*renderable)
6
+ Group.new Text.new("<!DOCTYPE html>"), NonVoid.new("html", *renderable)
7
+ end
8
+
9
+ def attr(attributes = {})
10
+ Attribute.new attributes
11
+ end
12
+
13
+ def text(string)
14
+ Text.new string
15
+ end
16
+
17
+ # Void html elements. W3C [reference](https://www.w3.org/TR/2011/WD-html-markup-20110405/syntax.html#syntax-elements)
18
+ #
19
+ # Example:
20
+ # class A
21
+ # include Ruby::Tags::Html
22
+ # def xxx
23
+ # img(attr(src:"xxx")).render
24
+ # end
25
+ # end
26
+ #
27
+ # >> A.new.xxx
28
+ # => <img src='xxx'/>
29
+ #
30
+ # Arguments:
31
+ # name: (String)
32
+ # attribute: (Attribute), optional
33
+ [
34
+ :area, :base, :br, :col, :command, :embed, :hr, :img, :input,
35
+ :keygen, :link, :meta, :param, :source, :track, :wbr
36
+ ].each do |m|
37
+ define_method(m) do | argument = Attribute.new |
38
+ Void.new m, argument
39
+ end
40
+ end
41
+
42
+ # NonVoid html elements.
43
+ #
44
+ # Example:
45
+ # class A
46
+ # include Ruby::Tags::Html
47
+ # def xxx
48
+ # div(attr(class:"fa fa-up")).render
49
+ # end
50
+ # end
51
+ #
52
+ # >> A.new.xxx
53
+ # => <div class='fa fa-up'></div>
54
+ #
55
+ # Arguments:
56
+ # name: (String)
57
+ # attribute: (Attribute), optional
58
+ [
59
+ :a, :abbr, :address, :article, :aside, :audio, :b, :bdi, :bdo, :blockquote, :body, :button,
60
+ :canvas, :caption, :cite, :code, :colgroup, :data, :datalist, :dd, :del, :dfn, :div, :dl,
61
+ :dt, :em, :fieldset, :figcaption, :figure, :footer, :form, :h1, :h2, :h3, :h4, :h5, :h6,
62
+ :head, :header, :i, :iframe, :ins, :kbd, :label, :legend, :li, :main, :map,
63
+ :mark, :meter, :nav, :noscript, :object, :ol, :optgroup, :option, :output, :p, :pre, :progress,
64
+ :q, :rb, :rp, :rt, :rtc, :ruby, :s, :samp, :script, :section, :select, :small, :span, :strong,
65
+ :style, :sub, :sup, :table, :tbody, :td, :template, :textarea, :tfoot, :th, :thead, :time, :title,
66
+ :tr, :u, :ul, :var, :video
67
+ ].each do |m|
68
+ define_method(m) do | *renderable |
69
+ NonVoid.new m, *renderable
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,81 @@
1
+ module Ruby
2
+ module Tags
3
+
4
+ class Group
5
+
6
+ def initialize(*tags)
7
+ @tags = tags
8
+ end
9
+
10
+ def render
11
+ @tags.map(&:render).join
12
+ end
13
+
14
+ def add(tag)
15
+ @tags << tag
16
+ self
17
+ end
18
+
19
+ def ==(other)
20
+ @tags == other.instance_variable_get(:@tags)
21
+ end
22
+ end
23
+
24
+ class Text
25
+
26
+ def initialize(text)
27
+ @text = text
28
+ end
29
+
30
+ def render
31
+ @text
32
+ end
33
+
34
+ def ==(other)
35
+ @text == other.instance_variable_get(:@text)
36
+ end
37
+ end
38
+
39
+ class NonVoid
40
+
41
+ def initialize(name, *renders)
42
+ @name = name
43
+ @attribute = renders.find { |r| r.is_a? Attribute } || Attribute.new
44
+ @children = renders.reject { |r| r.is_a? Attribute }
45
+ end
46
+
47
+ def render
48
+ @children.reduce("<#{@name}#{@attribute.render}>") { |m, t| m + t.render } + "</#{@name}>"
49
+ end
50
+
51
+ def add(tag)
52
+ @children << tag
53
+ self
54
+ end
55
+
56
+ def ==(other)
57
+ @name == other.instance_variable_get(:@name) &&
58
+ @attribute == other.instance_variable_get(:@attribute) &&
59
+ @children == other.instance_variable_get(:@children)
60
+ end
61
+ end
62
+
63
+ class Void
64
+
65
+ def initialize(name, attribute = Attribute.new)
66
+ @name, @attribute = name, attribute
67
+ end
68
+
69
+ def render
70
+ "<#{@name}#{@attribute.render}/>"
71
+ end
72
+
73
+ def ==(other)
74
+ @name == other.instance_variable_get(:@name) &&
75
+ @attribute == other.instance_variable_get(:@attribute)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-tags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - manlioGit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - manliomodugno@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Rakefile
21
+ - lib/ruby-tags.rb
22
+ - lib/ruby/tags/attribute.rb
23
+ - lib/ruby/tags/html.rb
24
+ - lib/ruby/tags/tags.rb
25
+ homepage: https://github.com/manlioGit/ruby-tags
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.3.0
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.1.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: RubyTags is a small XML/HTML construction templating library for Ruby, inspired
48
+ by JavaTags.
49
+ test_files: []