html-tag 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b88800aadc21733d2b0da8e33e274a8b95fd8008a972ebb760864ae29b18fa61
4
+ data.tar.gz: f0108dae498ca7302f6e3de9285676aa0cc01903cd95f58742e5e0ae9d415085
5
+ SHA512:
6
+ metadata.gz: 9fa4dcb68bb10d7b96b63239c44693f7ec92a03fb8ac6de41d5fd344ae5722d79f90f04806a57279ab868666af2a446b8ad4d2e49f951fa27c83517486a44c8e
7
+ data.tar.gz: d36b0ed5b1e20b89513e86626fc17e8db0fb8adf9de711649ee43429ea0ff265e7edb75b57e936190601099c4f6f6d4740336bbc0bc56364bb436a6512f81b94
data/.version ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/html-tag.rb ADDED
@@ -0,0 +1,6 @@
1
+ unless ''.respond_to?(:blank?)
2
+ require 'fast_blank'
3
+ end
4
+
5
+ require_relative './html-tag/base'
6
+ require_relative './html-tag/adapter'
@@ -0,0 +1,33 @@
1
+ module HtmlTagBuilderHelper
2
+ # tag :div, { 'class'=>'iform' } do
3
+ # tag.div(class: :iform) { |n| ... }
4
+ def xtag name=nil, opts={}, data=nil
5
+ return HtmlTagBuilder unless name
6
+ data = yield(opts) if block_given?
7
+ HtmlTagBuilder.tag name, opts, data
8
+ end
9
+ end
10
+
11
+ # Rails
12
+ if defined?(ActionView::Base)
13
+ ActionView::Base.send :include, HtmlTagBuilderHelper
14
+
15
+ # Sinatra
16
+ elsif defined?(Sinatra::Base)
17
+ class Sinatra::Base
18
+ include HtmlTagBuilderHelper
19
+ alias :tag :xtag
20
+ end
21
+
22
+ # Lux and other
23
+ elsif defined?(ApplicationHelper)
24
+ # all other frameworks, including Lux
25
+ module ApplicationHelper
26
+ include HtmlTagBuilderHelper
27
+ alias :tag :xtag
28
+ end
29
+
30
+ end
31
+
32
+
33
+
@@ -0,0 +1,113 @@
1
+ class HtmlTagBuilder
2
+ class << self
3
+ # tag.div -> tag.tag :div
4
+ def method_missing tag_name, *args, &block
5
+ tag tag_name, args[0], args[1], &block
6
+ end
7
+
8
+ # tag :div, { 'class'=>'iform' } do
9
+ def tag name=nil, opts={}, data=nil
10
+ if Array === opts
11
+ # join data given as an array
12
+ data = opts
13
+ opts = {}
14
+ elsif Hash === data
15
+ # tag.button('btn', href: '/') { 'Foo' }
16
+ opts = data.merge class: opts
17
+ data = nil
18
+ end
19
+
20
+ # covert n._row to n(class: 'row')
21
+ name = name.to_s
22
+ if name.to_s[0, 1] == '_'
23
+ opts ||= {}
24
+ opts[:class] = name.to_s.sub('_', '')
25
+ name = :div
26
+ end
27
+
28
+ # covert tag.a '.foo.bar' to class names
29
+ # covert tag.a '#id' to id names
30
+ if (data || block_given?) && opts.is_a?(String)
31
+ given = opts.dup
32
+ opts = {}
33
+
34
+ given.sub(/#([\w\-]+)/) { opts[:id] = $1 }
35
+ klass = given.sub(/^\./, '').gsub('.', ' ')
36
+ opts[:class] = klass unless klass.blank?
37
+ end
38
+
39
+ # fix data and opts unless opts is Hash
40
+ data, opts = opts, {} unless opts.class == Hash
41
+
42
+ if block_given?
43
+ inline = new
44
+ data = yield(inline, opts)
45
+
46
+ # if data is pushed to passed node, use that data
47
+ data = inline.data if inline.data.first
48
+ end
49
+
50
+ data = data.join('') if data.is_a?(Array)
51
+
52
+ build opts, name, data
53
+ end
54
+
55
+ # build html node
56
+ def build attrs, node=nil, text=nil
57
+ opts = []
58
+ attrs.each do |attr_key, attr_value|
59
+ if attr_value.is_a?(Hash)
60
+ for data_key, data_value in attr_value
61
+ __add_opts opts, "#{attr_key}-#{data_key}", data_value
62
+ end
63
+ else
64
+ __add_opts opts, attr_key, attr_value
65
+ end
66
+ end
67
+
68
+ opts = opts.first ? ' '+opts.join(' ') : ''
69
+
70
+ return opts unless node
71
+
72
+ text = yield opts if block_given?
73
+ text ||= '' unless ['input', 'img', 'meta', 'link', 'hr', 'br'].include?(node.to_s)
74
+ out = text ? %{<#{node}#{opts}>#{text}</#{node}>} : %{<#{node}#{opts} />}
75
+ out.respond_to?(:html_safe) ? out.html_safe : out
76
+ end
77
+
78
+ # tag.div(class: 'klasa') do -> tag.('klasa') do
79
+ def call class_name, &block
80
+ tag(:div, class_name, &block)
81
+ end
82
+
83
+ def __add_opts opts, key, value
84
+ unless value.to_s.blank?
85
+ opts.push key.to_s.gsub(/_/,'-')+'="'+value.to_s.gsub(/"/,'&quot;')+'"'
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ ###
92
+
93
+ attr_reader :data
94
+
95
+ def initialize
96
+ @data = []
97
+ end
98
+
99
+ # push data to stack
100
+ def push data
101
+ @data.push data
102
+ end
103
+
104
+ # n.div(class: 'klasa') do -> n.('klasa') do
105
+ def call class_name, &block
106
+ @data.push self.class.tag(:div, { class: class_name }, &block)
107
+ end
108
+
109
+ # forward to class
110
+ def method_missing tag_name, *args, &block
111
+ @data.push self.class.tag(tag_name, args[0], args[1], &block)
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html-tag
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dino Reic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fast_blank
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ description: Fast and powerful tag builder, upgrade to Rails tag helper, framework
28
+ agnostic.
29
+ email: reic.dino@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - "./.version"
35
+ - "./lib/html-tag.rb"
36
+ - "./lib/html-tag/adapter.rb"
37
+ - "./lib/html-tag/base.rb"
38
+ homepage: https://github.com/dux/html-tag
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.0.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: HTML tag builder
61
+ test_files: []