html_rb 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 907ff51c68c17cdcf035eacaa2ef82b3a2a3050b
4
- data.tar.gz: d70f50da58bc048b61ebf02873028dc3794f0a64
2
+ SHA256:
3
+ metadata.gz: ae6b4e2173e7362e79f1bf874e6a7090b70e015a4e2fd26c5fe423004b578c7c
4
+ data.tar.gz: 802669903c2e3b34fd87f7f32dc64b7dd66b4f3ced64f975afaad3791bdfd073
5
5
  SHA512:
6
- metadata.gz: 22c13f0bbdc20394782c704e59a8d92dde4d0a7361f4d8eaa83109feb21d2ce905578b2ac7c9dd0308378881f512a6912f609c24f761fc756f6d691bb116a6bc
7
- data.tar.gz: a3098a8f2623954a6e81a1aece656ee22dd598afa76e31c6b404ce4028e958a2fa82148bf042525b1d7ec2867b8799d3ea0d2ea45186e5836e6d1d22b654b3a6
6
+ metadata.gz: c4e2292acfa4c0a264e2835bc886a7cd03201ddfe33ed3d6b2cd3a65981af6d470ebd459aaba90390d39815de807ed92640d9f0d5d8b8ca382c45897168f8fca
7
+ data.tar.gz: df9a847a624f75eed9f7efa4d61a24b750b141e59f804552d9c50264d617e2d40895fd913199886751d474267ef59c497a2202747f942ef7b844c4eb4a5c56cf
data/lib/html_rb.rb CHANGED
@@ -1,83 +1,17 @@
1
- require 'html_rb/constants'
2
-
3
1
  module HtmlRB
4
- VERSION = "1.0.0".freeze
5
-
2
+ VERSION = "2.0.0".freeze
6
3
  Error = Class.new(StandardError)
4
+ end
7
5
 
8
- class Tag
9
- def initialize(name=nil,content=nil,**attrs,&block)
10
- @strings = []
11
- build name, content, **attrs, &block
12
- end
13
-
14
- def to_s
15
- @strings.join
16
- end
17
-
18
- private
19
- def build(name=nil,content=nil,**attrs,&block)
20
- void = HtmlRB::VOID_ELEMENTS.include?(name)
21
- raise HtmlRB::Error, "May not provide both content and block" if content && block_given?
22
- raise HtmlRB::Error, "Void elements cannot enclose content" if void && (content || block_given?)
23
-
24
- @strings << "<#{[name,attribute_string(attrs)].join(" ").strip}>" if name
25
- unless void
26
- @strings << content if content
27
- instance_eval(&block) if block_given?
28
- @strings << "</#{name}>" if name
29
- end
30
- end
31
-
32
- # Define all the tag methods
33
- (HtmlRB::STD_ELEMENTS + HtmlRB::VOID_ELEMENTS).each do |el|
34
- define_method(el) do |content=nil,**attrs,&block|
35
- build el, content, **attrs, &block
36
- end
37
- end
38
-
39
- # Used for Text Nodes
40
- def text(content)
41
- build nil, content
42
- end
43
-
44
- # ATTRIBUTE HANDLING
45
- def attribute_string(hash={})
46
- hash.delete_if{|k,v| v.nil? || v == "" }
47
- .map{|k,v| attribute_pair(k,v) }
48
- .compact
49
- .join(" ")
50
- end
51
-
52
- def attribute_pair(k,v)
53
- if HtmlRB::BOOL_ATTRS.include?(k)
54
- attribute_key(k) if v
55
- else
56
- %Q|#{attribute_key(k)}="#{v}"|
57
- end
58
- end
59
-
60
- def attribute_key(k)
61
- str = k.to_s
62
- if str.start_with? "!"
63
- str.slice!(0)
64
- else
65
- str.gsub!("_","-")
66
- end
67
- str
68
- end
69
- end
6
+ require 'html_rb/constants'
7
+ require 'html_rb/tag'
70
8
 
71
- def html(content=nil,document:false,doctype:"html",**attrs,&block)
72
- name = nil
73
- dt = ""
9
+ module HtmlRB
10
+ module_function def html(content=nil, document: false, **attrs, &block)
74
11
  if document
75
- name = :html
76
- dt = "<!DOCTYPE #{doctype}>" if doctype
12
+ "<!DOCTYPE html>" + Tag.new('html', content, **attrs, &block).to_s
13
+ else
14
+ Tag.new(nil, content, **attrs, &block).to_s
77
15
  end
78
- dt + Tag.new(name, content, **attrs, &block).to_s
79
16
  end
80
-
81
- alias_method :markup, :html
82
- module_function :markup
83
17
  end
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  module HtmlRB
2
4
  STD_ELEMENTS=[
3
5
  :a,
@@ -107,7 +109,7 @@ module HtmlRB
107
109
  :source,
108
110
  :track,
109
111
  :wbr,
110
- ].to_set.freeze
112
+ ].to_set.freeze
111
113
 
112
114
  BOOL_ATTRS=[
113
115
  :allowfullscreen,
@@ -0,0 +1,76 @@
1
+ module HtmlRB
2
+ class Tag
3
+ def initialize(name=nil,content=nil,**attrs,&block)
4
+ @strings = []
5
+ tag name, content, **attrs, &block
6
+ end
7
+
8
+ def to_s
9
+ @strings.join
10
+ end
11
+
12
+ def self.register(tag_name, void: false)
13
+ method_name = tag_name.to_s.downcase.gsub('-','_') # Rubify the tag name.
14
+ define_method(method_name) do |content=nil,**attrs,&block|
15
+ tag tag_name, content, _void: void, **attrs, &block
16
+ end
17
+ end
18
+
19
+ def self.unregister(tag_name)
20
+ method_name = tag_name.to_s.downcase.gsub('-','_') # Rubify the tag name.
21
+ remove_method method_name
22
+ end
23
+
24
+ # Define all the tag methods
25
+ HtmlRB::STD_ELEMENTS.each {|tag_name| register tag_name }
26
+ HtmlRB::VOID_ELEMENTS.each {|tag_name| register tag_name, void: true }
27
+
28
+ private
29
+
30
+ def tag(name=nil, content=nil, _void: false, **attrs, &block)
31
+ void = _void
32
+ raise HtmlRB::Error, "May not provide both content and block" if content && block_given?
33
+ raise HtmlRB::Error, "Void elements cannot enclose content" if void && (content || block_given?)
34
+
35
+ @strings << "<#{[name,attribute_string(attrs)].join(" ").strip}>" if name
36
+
37
+ return if void
38
+
39
+ @strings << content if content
40
+ instance_eval(&block) if block_given?
41
+ @strings << "</#{name}>" if name
42
+ end
43
+
44
+ # Used for Text Nodes
45
+ def text(content)
46
+ tag nil, content
47
+ end
48
+
49
+ # Really, text is just appending a string, so we can append anything.
50
+ # But saying `text` before some block of HTML stored in a variable feels
51
+ # wrong, so we'll add an alias `render` which feels less strange.
52
+ alias render text
53
+
54
+ # ATTRIBUTE HANDLING
55
+ def attribute_string(hash={})
56
+ hash.delete_if{|k,v| v.nil? || v == "" }
57
+ .map{|k,v| attribute_pair(k,v) }
58
+ .compact
59
+ .join(" ")
60
+ end
61
+
62
+ def attribute_pair(k,v)
63
+ if HtmlRB::BOOL_ATTRS.include?(k)
64
+ attribute_key(k) if v
65
+ else
66
+ %Q|#{attribute_key(k)}="#{v}"|
67
+ end
68
+ end
69
+
70
+ def attribute_key(k)
71
+ return k if k.is_a?(String)
72
+
73
+ k.to_s.gsub("_","-")
74
+ end
75
+ end
76
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Burleson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-14 00:00:00.000000000 Z
11
+ date: 2021-08-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A beautifully simple DSL for writing HTML in ruby. Check out the readme
14
14
  at github.com/burlesona/html_rb for detailed examples, or see the tests.
@@ -19,11 +19,12 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/html_rb.rb
21
21
  - lib/html_rb/constants.rb
22
+ - lib/html_rb/tag.rb
22
23
  homepage: http://github.com/burlesona/html
23
24
  licenses:
24
25
  - MIT
25
26
  metadata: {}
26
- post_install_message:
27
+ post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths:
29
30
  - lib
@@ -31,17 +32,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
32
  requirements:
32
33
  - - ">="
33
34
  - !ruby/object:Gem::Version
34
- version: '2.0'
35
+ version: '2.7'
35
36
  required_rubygems_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
38
  - - ">="
38
39
  - !ruby/object:Gem::Version
39
40
  version: '0'
40
41
  requirements: []
41
- rubyforge_project:
42
- rubygems_version: 2.5.1
43
- signing_key:
42
+ rubygems_version: 3.2.15
43
+ signing_key:
44
44
  specification_version: 4
45
45
  summary: A beautifully simple DSL for writing HTML in ruby
46
46
  test_files: []
47
- has_rdoc: