berns 3.4.0 → 4.1.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
2
  SHA256:
3
- metadata.gz: 844af14032659e36711f21b482d7e07ba1627cdca43045ec1fb30869171ee21a
4
- data.tar.gz: 828da05830d2d5c1a516fe51bcc2a5c5c9c346359f9eedde4b3ee5bd60a6bf25
3
+ metadata.gz: eb2de0ba05a5e234fa5cef7edf3a222411222d0743c0e69492c1e1d5e53541bb
4
+ data.tar.gz: f62f2273f787a46aa18784b668f1110adb80a1bbb8c33226c9c58e585d5acec7
5
5
  SHA512:
6
- metadata.gz: 93e0a7fc35666b0395d338cafc5a7aa9bb8aa3d1467158cdb1fc325640d275b3cfa0033921edfd62f0d6a52cbf8728842b45c31ea4d19552d18ee6440c135b59
7
- data.tar.gz: 6bc2ea5f4749e09d8bbc4dacd2fac2981ad66dfdc649cb14f78a50ed21af5705d057228764edf6bdd32e4eb4dc8e848f6a42ffbab157ec9bf78f05c94899be6c
6
+ metadata.gz: 7d8fb46aeb4dc948729b36bbb6a794625618c34742ee2b18024d1cde99bda330b21bd7fb890bd241010a9b60e956c81fe401c3036464a90d28fb4ebca11ba621
7
+ data.tar.gz: c7c430ae90da5be3b93f15d1ac6ab2bba300b89558cfb131af276b2af9d2e82b118fa0691000402bf7e982e13406defc275c786e2e512304041f660afe0b53cc
data/README.org CHANGED
@@ -1,6 +1,7 @@
1
1
  * Berns
2
2
 
3
3
  [[https://badge.fury.io/rb/berns][https://badge.fury.io/rb/berns.svg]]
4
+ [[https://github.com/evanleck/berns/actions/workflows/main.yml][https://github.com/evanleck/berns/actions/workflows/main.yml/badge.svg]]
4
5
 
5
6
  A utility library for generating HTML strings.
6
7
 
@@ -121,15 +122,33 @@ template = Berns::Builder.new do
121
122
  end
122
123
  #+end_src
123
124
 
124
- Within the block provided to =Berns::Builder.new= every standard and void
125
- element is available as a method and each time you use one of those methods the
126
- result is appended to an internal buffer. In addition, the =#text= method can be
127
- used to append a plain text string to the buffer and that text will be HTML
128
- escaped, so it's good for untrusted content.
125
+ Within the block provided to =Berns::Builder.new= every standard element method,
126
+ void element method, =#element=, and =#void= are available as methods and each
127
+ time you use one of those methods the result is appended to an internal buffer.
128
+ In addition, the =#text= method appends HTML escaped text to the buffer and
129
+ =#raw= appends text to the buffer without modification.
129
130
 
130
- Once initialized, rendering the template to a string can be done with the
131
- =#call= method and any arguments, positional or keyword, will be passed through
132
- 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=.
133
152
 
134
153
  #+begin_src ruby
135
154
  string = template.call # =>
@@ -167,6 +186,7 @@ end # =>
167
186
  # </p>
168
187
  #+end_src
169
188
 
189
+
170
190
  *** Standard and void elements
171
191
 
172
192
  All standard and void HTML elements are defined as methods on Berns, so you can
data/ext/berns/berns.c CHANGED
@@ -228,7 +228,7 @@ static char * hash_value_to_attribute(const char *attr, const size_t attrlen, VA
228
228
 
229
229
  Check_Type(value, T_HASH);
230
230
 
231
- if (rb_hash_size(value) == 1) {
231
+ if (RHASH_SIZE(value) == 0) {
232
232
  return strdup("");
233
233
  }
234
234
 
@@ -439,7 +439,7 @@ static VALUE external_to_attribute(RB_UNUSED_VAR(VALUE self), VALUE attr, VALUE
439
439
  static VALUE external_to_attributes(RB_UNUSED_VAR(VALUE self), VALUE attributes) {
440
440
  Check_Type(attributes, T_HASH);
441
441
 
442
- if (rb_hash_size(attributes) == 1) {
442
+ if (RHASH_SIZE(attributes) == 0) {
443
443
  return rb_utf8_str_new_cstr("");
444
444
  }
445
445
 
@@ -453,37 +453,32 @@ static VALUE external_to_attributes(RB_UNUSED_VAR(VALUE self), VALUE attributes)
453
453
  }
454
454
 
455
455
  static char * void_element(const char *tag, size_t tlen, VALUE attributes) {
456
- /* T_IMEMO is what we get if an optional argument was not passed. */
457
- if (TYPE(attributes) == T_IMEMO) {
458
- size_t total = tag_olen + tlen + tag_clen + 1;
459
- char *string = malloc(total);
460
- char *ptr;
461
- char *end = string + total;
456
+ const char *empty = "";
457
+ char *attrs = hash_value_to_attribute(empty, 0, attributes);
458
+ size_t alen = strlen(attrs);
462
459
 
463
- ptr = stecpy(string, tag_open, end);
464
- ptr = stecpy(ptr, tag, end);
465
- ptr = stecpy(ptr, tag_close, end);
460
+ size_t total = tag_olen + tlen + tag_clen + 1;
466
461
 
467
- return string;
468
- } else {
469
- const char *empty = "";
470
- char *attrs = hash_value_to_attribute(empty, 0, attributes);
462
+ /* If we have some attributes, add a space and the attributes' length. */
463
+ if (alen > 0) {
464
+ total += splen + alen;
465
+ }
471
466
 
472
- size_t total = tag_olen + tlen + splen + strlen(attrs) + tag_clen + 1;
473
- char *string = malloc(total);
474
- char *ptr;
475
- char *end = string + total;
467
+ char *dest = malloc(total);
468
+ char *ptr = NULL;
469
+ char *end = dest + total;
476
470
 
477
- ptr = stecpy(string, tag_open, end);
478
- ptr = stecpy(ptr, tag, end);
471
+ ptr = stecpy(dest, tag_open, end);
472
+ ptr = stecpy(ptr, tag, end);
473
+
474
+ if (alen > 0) {
479
475
  ptr = stecpy(ptr, space, end);
480
476
  ptr = stecpy(ptr, attrs, end);
481
- ptr = stecpy(ptr, tag_close, end);
477
+ }
482
478
 
483
- free(attrs);
479
+ ptr = stecpy(ptr, tag_close, end);
484
480
 
485
- return string;
486
- }
481
+ return dest;
487
482
  }
488
483
 
489
484
  /*
data/ext/berns/extconf.rb CHANGED
@@ -9,5 +9,6 @@ append_cflags '-Wstrict-overflow'
9
9
  append_cflags '-flto'
10
10
  append_cflags '-fno-strict-aliasing'
11
11
  append_cflags '-msse4'
12
+ append_cflags '-std=c99'
12
13
 
13
14
  create_makefile 'berns/berns'
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,16 +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
+
45
+ # Append an arbitrary standard element to the buffer.
46
+ #
47
+ # @return [String]
48
+ def element(elm, *args, **kwargs, &block)
49
+ content = Builder.new(&block).call if block
50
+ @buffer << Berns.element(elm, *args, **kwargs) { content }
51
+ end
52
+
53
+ # Append an arbitrary void element to the buffer.
54
+ #
55
+ # @return [String]
56
+ def void(...)
57
+ @buffer << Berns.void(...)
58
+ end
59
+
31
60
  Berns::STANDARD.each do |meth|
32
- define_method(meth) do |*args, **opts, &block|
33
- content = Builder.new.instance_exec(*args, **opts, &block) if block
34
- @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 }
35
64
  end
36
65
  end
37
66
 
38
67
  Berns::VOID.each do |meth|
39
- define_method(meth) do |*args, **opts|
40
- @buffer << Berns.send(meth, *args, **opts)
68
+ define_method(meth) do |*args, **kwargs|
69
+ @buffer << Berns.send(meth, *args, **kwargs)
41
70
  end
42
71
  end
43
72
  end
data/lib/berns/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Berns
3
- VERSION = '3.4.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: 3.4.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Beck
@@ -9,148 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-02-14 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: benchmark-ips
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ">="
19
- - !ruby/object:Gem::Version
20
- version: '0'
21
- type: :development
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- version: '0'
28
- - !ruby/object:Gem::Dependency
29
- name: bundler
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: minitest
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
- - !ruby/object:Gem::Dependency
57
- name: rake
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: '0'
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- - !ruby/object:Gem::Dependency
71
- name: rake-compiler
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: rubocop
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
- - !ruby/object:Gem::Dependency
99
- name: rubocop-minitest
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '0'
105
- type: :development
106
- prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- version: '0'
112
- - !ruby/object:Gem::Dependency
113
- name: rubocop-packaging
114
- requirement: !ruby/object:Gem::Requirement
115
- requirements:
116
- - - ">="
117
- - !ruby/object:Gem::Version
118
- version: '0'
119
- type: :development
120
- prerelease: false
121
- version_requirements: !ruby/object:Gem::Requirement
122
- requirements:
123
- - - ">="
124
- - !ruby/object:Gem::Version
125
- version: '0'
126
- - !ruby/object:Gem::Dependency
127
- name: rubocop-performance
128
- requirement: !ruby/object:Gem::Requirement
129
- requirements:
130
- - - ">="
131
- - !ruby/object:Gem::Version
132
- version: '0'
133
- type: :development
134
- prerelease: false
135
- version_requirements: !ruby/object:Gem::Requirement
136
- requirements:
137
- - - ">="
138
- - !ruby/object:Gem::Version
139
- version: '0'
140
- - !ruby/object:Gem::Dependency
141
- name: rubocop-rake
142
- requirement: !ruby/object:Gem::Requirement
143
- requirements:
144
- - - ">="
145
- - !ruby/object:Gem::Version
146
- version: '0'
147
- type: :development
148
- prerelease: false
149
- version_requirements: !ruby/object:Gem::Requirement
150
- requirements:
151
- - - ">="
152
- - !ruby/object:Gem::Version
153
- version: '0'
12
+ date: 2022-06-14 00:00:00.000000000 Z
13
+ dependencies: []
154
14
  description: A utility library for generating HTML strings.
155
15
  email:
156
16
  - beck.taylorg@gmail.com
@@ -175,6 +35,8 @@ licenses:
175
35
  - MIT
176
36
  metadata:
177
37
  bug_tracker_uri: https://github.com/evanleck/berns/issues
38
+ changelog_uri: https://github.com/evanleck/berns/blob/main/CHANGELOG.org
39
+ homepage_uri: https://github.com/evanleck/berns
178
40
  rubygems_mfa_required: 'true'
179
41
  source_code_uri: https://github.com/evanleck/berns
180
42
  post_install_message:
@@ -185,14 +47,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
185
47
  requirements:
186
48
  - - ">="
187
49
  - !ruby/object:Gem::Version
188
- version: 2.5.0
50
+ version: 2.7.0
189
51
  required_rubygems_version: !ruby/object:Gem::Requirement
190
52
  requirements:
191
53
  - - ">="
192
54
  - !ruby/object:Gem::Version
193
55
  version: '2.0'
194
56
  requirements: []
195
- rubygems_version: 3.3.3
57
+ rubygems_version: 3.3.7
196
58
  signing_key:
197
59
  specification_version: 4
198
60
  summary: A utility library for generating HTML strings.