berns 4.0.0 → 4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cfbcf19e2450ffa23b283ad0bb24717cde0bf6191bd8db538bf2cbb3936f205
4
- data.tar.gz: f2639cdade9f1a3ee7b55a66666d23ce9843f5988567d3a630fc7403476825e8
3
+ metadata.gz: eb2de0ba05a5e234fa5cef7edf3a222411222d0743c0e69492c1e1d5e53541bb
4
+ data.tar.gz: f62f2273f787a46aa18784b668f1110adb80a1bbb8c33226c9c58e585d5acec7
5
5
  SHA512:
6
- metadata.gz: f36bec9a6378fa664cc254a24700c63fc4a44720cfd5aad2b7e882dc6aa6ffa523031ec165270cb50d792c85dbcf0e1c0942d3ad22b64b4ec47ee29572170c03
7
- data.tar.gz: a520befecc81e00cb2ae6c4a8b973965ee10cbdca0adb003d49ee6464478302ac97391651412f3945a5e6d92f76eaa2f5b51c5d5ef186a3c463c5b6928de564c
6
+ metadata.gz: 7d8fb46aeb4dc948729b36bbb6a794625618c34742ee2b18024d1cde99bda330b21bd7fb890bd241010a9b60e956c81fe401c3036464a90d28fb4ebca11ba621
7
+ data.tar.gz: c7c430ae90da5be3b93f15d1ac6ab2bba300b89558cfb131af276b2af9d2e82b118fa0691000402bf7e982e13406defc275c786e2e512304041f660afe0b53cc
data/README.org CHANGED
@@ -125,12 +125,30 @@ end
125
125
  Within the block provided to =Berns::Builder.new= every standard element method,
126
126
  void element method, =#element=, and =#void= are available as methods and each
127
127
  time you use one of those methods the result is appended to an internal buffer.
128
- In addition, the =#text= method can be used to append a plain text string to the
129
- buffer and that text will be HTML escaped, so it's good for untrusted content.
128
+ In addition, the =#text= method appends HTML escaped text to the buffer and
129
+ =#raw= appends text to the buffer without modification.
130
130
 
131
- Once initialized, rendering the template to a string can be done with the
132
- =#call= method and any arguments, positional or keyword, will be passed through
133
- as-is to the block provided to =#new=.
131
+ The block provided to =Berns::Builder.new= can take both positional and keyword
132
+ arguments.
133
+
134
+ #+begin_src ruby
135
+ template = Berns::Builder.new do |content, title:|
136
+ h1 { title }
137
+ p(class: 'paragraph') { content }
138
+ end
139
+
140
+ template.call('Some text.', title: 'The title') # =>
141
+ # <h1>
142
+ # The title
143
+ # </h1>
144
+ # <p>
145
+ # Some text.
146
+ # </p>
147
+ #+end_src
148
+
149
+ Once initialized, the =#call= method will render the template to a string. Any
150
+ arguments, positional or keyword, are passed through as-is to the block provided
151
+ to =#new=.
134
152
 
135
153
  #+begin_src ruby
136
154
  string = template.call # =>
@@ -168,23 +186,6 @@ end # =>
168
186
  # </p>
169
187
  #+end_src
170
188
 
171
- Lastly, the block provided to =Berns::Builder.new= can take both positional and
172
- keyword arguments.
173
-
174
- #+begin_src ruby
175
- template = Berns::Builder.new do |content, title:|
176
- h1 { title }
177
- p(class: 'paragraph') { content }
178
- end
179
-
180
- template.call('Some text.', title: 'The title') # =>
181
- # <h1>
182
- # The title
183
- # </h1>
184
- # <p>
185
- # Some text.
186
- # </p>
187
- #+end_src
188
189
 
189
190
  *** Standard and void elements
190
191
 
Binary file
data/lib/berns/builder.rb CHANGED
@@ -5,22 +5,28 @@ module Berns
5
5
  # An HTML builder DSL using Berns' HTML methods.
6
6
  class Builder
7
7
  def initialize(&block)
8
+ raise(ArgumentError, 'Berns::Builder initialized without a block argument', caller) unless block
9
+
8
10
  @block = block
9
- @buffer = +''
10
11
  end
11
12
 
12
13
  # @return [String]
13
- def call(*args, **opts)
14
- instance_exec(*args, **opts, &@block)
15
- to_s
16
- end
14
+ def call(*args, **kwargs)
15
+ @buffer = +''
16
+ content = instance_exec(*args, **kwargs, &@block)
17
17
 
18
- # @return [String]
19
- def to_s
20
- @buffer.freeze
18
+ # This is a special case where the buffer hasn't been appended to but the
19
+ # block returned a string.
20
+ if @buffer.empty? && content.is_a?(String)
21
+ Berns.escape_html(content).freeze
22
+ else
23
+ @buffer.freeze
24
+ end
21
25
  end
26
+ alias to_s call
27
+ alias to_str call
22
28
 
23
- # Append text to the buffer.
29
+ # Append HTML escaped text to the buffer.
24
30
  #
25
31
  # @param string [String]
26
32
  # @return [String]
@@ -28,31 +34,39 @@ module Berns
28
34
  @buffer << Berns.escape_html(string.to_s)
29
35
  end
30
36
 
37
+ # Append raw text to the buffer.
38
+ #
39
+ # @param string [String]
40
+ # @return [String]
41
+ def raw(string)
42
+ @buffer << string.to_s
43
+ end
44
+
31
45
  # Append an arbitrary standard element to the buffer.
32
46
  #
33
47
  # @return [String]
34
- def element(*args, **opts, &block)
35
- content = Builder.new.instance_exec(*args, **opts, &block) if block
36
- @buffer << Berns.element(*args, **opts) { content }
48
+ def element(elm, *args, **kwargs, &block)
49
+ content = Builder.new(&block).call if block
50
+ @buffer << Berns.element(elm, *args, **kwargs) { content }
37
51
  end
38
52
 
39
53
  # Append an arbitrary void element to the buffer.
40
54
  #
41
55
  # @return [String]
42
- def void(*args, **opts)
43
- @buffer << Berns.void(*args, **opts)
56
+ def void(...)
57
+ @buffer << Berns.void(...)
44
58
  end
45
59
 
46
60
  Berns::STANDARD.each do |meth|
47
- define_method(meth) do |*args, **opts, &block|
48
- content = Builder.new.instance_exec(*args, **opts, &block) if block
49
- @buffer << Berns.send(meth, *args, **opts) { content }
61
+ define_method(meth) do |*args, **kwargs, &block|
62
+ content = Builder.new(&block).call if block
63
+ @buffer << Berns.send(meth, *args, **kwargs) { content }
50
64
  end
51
65
  end
52
66
 
53
67
  Berns::VOID.each do |meth|
54
- define_method(meth) do |*args, **opts|
55
- @buffer << Berns.send(meth, *args, **opts)
68
+ define_method(meth) do |*args, **kwargs|
69
+ @buffer << Berns.send(meth, *args, **kwargs)
56
70
  end
57
71
  end
58
72
  end
data/lib/berns/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Berns
3
- VERSION = '4.0.0'
3
+ VERSION = '4.1.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berns
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Beck
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-05-24 00:00:00.000000000 Z
12
+ date: 2022-06-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A utility library for generating HTML strings.
15
15
  email:
@@ -26,9 +26,6 @@ files:
26
26
  - ext/berns/extconf.rb
27
27
  - ext/berns/hescape.c
28
28
  - ext/berns/hescape.h
29
- - ext/berns/sds.c
30
- - ext/berns/sds.h
31
- - ext/berns/sdsalloc.h
32
29
  - lib/berns.rb
33
30
  - lib/berns/berns.bundle
34
31
  - lib/berns/builder.rb