philiprehberger-html_builder 0.3.0 → 0.5.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: a3456c0aafd35720f59ea6e59561f262f43b5365ea60a669064df2b51c6a3e12
4
- data.tar.gz: 9bccfcfc9471afa14a8e5760799ddf9fce538747e7a5adaa0ac971f5a8c6de84
3
+ metadata.gz: c951cd5a9e74ab6642a9181b01235a4a5a63b39015fd05cf5c41ab0157bc6106
4
+ data.tar.gz: ab129d21a86612912a83f460cd49424b4005ac95fe6f7c8c6471f3f5309a6640
5
5
  SHA512:
6
- metadata.gz: eaf644594e2209e715c251d4d6d0c4d6540b620f00ac1adde085f31fdf66658fb52f7e9b819d34c93fad91c76b7765eb58888d3c387752815636e628b3559e35
7
- data.tar.gz: 9998a84aed894d70f92f08752278748df2a54097c4a7b4b866d43f50865ee07f0db48aaa7c6a4cc20a4adb52b67e10ba9862a7b0a258ebe1d93fb620e40276c2
6
+ metadata.gz: c6ceba75c99ca6620ba8434521b02703cb42d0113f662fe0789928dec1b1e38f4e3057c4fd44495eefd30bd53d08127d6b95daa470e4d67bf15ed56f61e085a9
7
+ data.tar.gz: c70fd51be8e402a223a7a1114a0f0950dd66267f2cf20506a1437a32905ba3185634a980fa314fd9785c985600a947890e05768b35dccdaabdbb40a395cb0191
data/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.5.0] - 2026-04-15
11
+
12
+ ### Added
13
+ - `list(items, ordered: false, **attrs, &block)` helper for building `<ul>` or `<ol>` lists from an array of items with optional custom rendering via block
14
+
15
+ ## [0.4.0] - 2026-04-15
16
+
17
+ ### Added
18
+ - `HtmlBuilder.escape(value)` module method for escaping arbitrary strings using the DSL's escaper
19
+
10
20
  ## [0.3.0] - 2026-04-14
11
21
 
12
22
  ### Added
data/README.md CHANGED
@@ -142,6 +142,27 @@ Philiprehberger::HtmlBuilder.build do
142
142
  end
143
143
  ```
144
144
 
145
+ ### Lists
146
+
147
+ Build `<ul>` or `<ol>` lists from an array of items. Items are text-escaped by default. Pass `ordered: true` for an ordered list. Use a block for custom rendering of each item:
148
+
149
+ ```ruby
150
+ Philiprehberger::HtmlBuilder.build do
151
+ list(%w[Apple Banana Cherry])
152
+ end
153
+ # => '<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>'
154
+
155
+ Philiprehberger::HtmlBuilder.build do
156
+ list(%w[First Second], ordered: true, class: 'steps')
157
+ end
158
+ # => '<ol class="steps"><li>First</li><li>Second</li></ol>'
159
+
160
+ Philiprehberger::HtmlBuilder.build do
161
+ list(%w[Alice Bob]) { |name| strong name }
162
+ end
163
+ # => '<ul><li><strong>Alice</strong></li><li><strong>Bob</strong></li></ul>'
164
+ ```
165
+
145
166
  ### CSS Class Helpers
146
167
 
147
168
  Build conditional CSS class strings from mixed arguments. Strings are included as-is, hash keys are included when their value is truthy:
@@ -229,6 +250,15 @@ Philiprehberger::HtmlBuilder.build_pretty(indent_size: 4) do
229
250
  end
230
251
  ```
231
252
 
253
+ ### Escape Helper
254
+
255
+ Escape arbitrary strings outside the DSL using the same entity encoding:
256
+
257
+ ```ruby
258
+ Philiprehberger::HtmlBuilder.escape('<script>alert("xss")</script>')
259
+ # => "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"
260
+ ```
261
+
232
262
  ### Fragment Merging
233
263
 
234
264
  Combine multiple builder outputs into a single HTML string:
@@ -250,6 +280,7 @@ Philiprehberger::HtmlBuilder.merge(header, body, footer)
250
280
  | `HtmlBuilder.build_pretty { ... }` | Build pretty-printed HTML with indentation |
251
281
  | `HtmlBuilder.build_minified { ... }` | Alias for `build`, explicitly produces minified output |
252
282
  | `HtmlBuilder.merge(*fragments)` | Merge multiple HTML fragment strings into one |
283
+ | `HtmlBuilder.escape(value)` | Escape HTML special characters in a string using the DSL's escaper |
253
284
  | `Builder#to_html` | Render builder contents to a minified HTML string |
254
285
  | `Builder#to_pretty_html` | Render builder contents to a pretty-printed HTML string |
255
286
  | `Builder#text(content)` | Add escaped text content to the current element |
@@ -264,6 +295,7 @@ Philiprehberger::HtmlBuilder.merge(header, body, footer)
264
295
  | `Builder#textarea_field(name, content, label_text:, **attrs)` | Build a label + textarea |
265
296
  | `Builder#hidden_field(name, value)` | Generate a hidden input element |
266
297
  | `Builder#submit(text, **attrs)` | Generate a submit button (default text "Submit") |
298
+ | `Builder#list(items, ordered:, **attrs, &block)` | Build a `<ul>` or `<ol>` from an array of items |
267
299
  | `Builder#class_names(*args)` | Build a conditional CSS class string from strings and hashes |
268
300
  | `Builder#cache(key) { ... }` | Cache rendered block output by key; return cached HTML on repeat calls |
269
301
  | `Escape.html(value)` | Escape HTML special characters in a string |
@@ -216,6 +216,26 @@ module Philiprehberger
216
216
  button(text, type: 'submit', **attrs)
217
217
  end
218
218
 
219
+ # Build a list (ul or ol) from an array of items
220
+ #
221
+ # @param items [Array] the list items
222
+ # @param ordered [Boolean] use ol instead of ul (default false)
223
+ # @param attrs [Hash] additional attributes for the list element
224
+ # @yield [item] optional block for custom rendering of each item
225
+ # @return [Node]
226
+ def list(items, ordered: false, **attrs, &block)
227
+ tag_name = ordered ? :ol : :ul
228
+ send(tag_name, **attrs) do
229
+ items.each do |item|
230
+ if block
231
+ li { block.call(item) }
232
+ else
233
+ li item.to_s
234
+ end
235
+ end
236
+ end
237
+ end
238
+
219
239
  # Build a space-joined CSS class string from mixed arguments
220
240
  #
221
241
  # Strings are included as-is. Hash keys are included when their value is truthy.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module HtmlBuilder
5
- VERSION = '0.3.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
@@ -52,5 +52,13 @@ module Philiprehberger
52
52
  def self.merge(*fragments)
53
53
  fragments.join
54
54
  end
55
+
56
+ # Escape HTML special characters in a string using the same escaper as the DSL.
57
+ #
58
+ # @param value [Object] the value to escape (converted to string)
59
+ # @return [String] the escaped string
60
+ def self.escape(value)
61
+ Escape.html(value)
62
+ end
55
63
  end
56
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-html_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-15 00:00:00.000000000 Z
11
+ date: 2026-04-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Build HTML programmatically using a clean tag DSL with nested blocks,
14
14
  automatic content escaping, void element support, and attribute hashes.