berns 3.4.0 → 3.5.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: bcded7fea247763fc7b27807c2c945943e0f08895b117ae086e4041bf1b08371
4
+ data.tar.gz: 965518819feb3eef689f19b7cc5470a973198b891e00f9d7f8ac7fd5b4620119
5
5
  SHA512:
6
- metadata.gz: 93e0a7fc35666b0395d338cafc5a7aa9bb8aa3d1467158cdb1fc325640d275b3cfa0033921edfd62f0d6a52cbf8728842b45c31ea4d19552d18ee6440c135b59
7
- data.tar.gz: 6bc2ea5f4749e09d8bbc4dacd2fac2981ad66dfdc649cb14f78a50ed21af5705d057228764edf6bdd32e4eb4dc8e848f6a42ffbab157ec9bf78f05c94899be6c
6
+ metadata.gz: f023bfcde5d5b272477d2dd7e7c2d6de430c4ec544d6a10da75f7ff72b5980349d282710796a2ac5c4bede5e72df5456dd733828c7869f3f1231b26314f450e4
7
+ data.tar.gz: d606bdeed6b199fa467c4f80236d9dab40ef0db13e3105397bbe411caffa71c00274ea5902e1ce3f4764ab915cada0a9574d133905f07e28a348e2e28f9b7cfa
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,11 +122,11 @@ 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 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.
129
130
 
130
131
  Once initialized, rendering the template to a string can be done with the
131
132
  =#call= method and any arguments, positional or keyword, will be passed through
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
@@ -28,6 +28,21 @@ module Berns
28
28
  @buffer << Berns.escape_html(string.to_s)
29
29
  end
30
30
 
31
+ # Append an arbitrary standard element to the buffer.
32
+ #
33
+ # @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 }
37
+ end
38
+
39
+ # Append an arbitrary void element to the buffer.
40
+ #
41
+ # @return [String]
42
+ def void(*args, **opts)
43
+ @buffer << Berns.void(*args, **opts)
44
+ end
45
+
31
46
  Berns::STANDARD.each do |meth|
32
47
  define_method(meth) do |*args, **opts, &block|
33
48
  content = Builder.new.instance_exec(*args, **opts, &block) if block
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 = '3.5.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: 3.5.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-02-14 00:00:00.000000000 Z
12
+ date: 2022-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: benchmark-ips