hiccdown 1.4.1 → 1.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hiccdown.rb +32 -14
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d04561bd25ef616c71cafb7500943039904cc59269de01b7536b3fefdf68a787
4
- data.tar.gz: 51088222ce3a32d48df3fe339a96a0ba98f9051745823ef9ca915a413867d141
3
+ metadata.gz: 36a1c3617a8af680f1b88452903b7a0c08ccdb333ec08606c5ed2bab3516aa49
4
+ data.tar.gz: 3274a2355fc775d86fd63c31f8ddf8f8becbc3ffc131886da2037961f991b7c7
5
5
  SHA512:
6
- metadata.gz: fe92789f9e885d48745adfeea619c85e9357c640af4c3675d8869c979a9bcdc813da0c1b6cb1c509f6dc69e8946fc987e2e4b0e75d3989b7b12fcda7fd2b1a2f
7
- data.tar.gz: 9d24cb9dcbf33caf43b204ccc7b616070918263d0f3c70078d953ef0cabd4e6b10506aa57ed3ed3864517c49e8bf037b8a0e115c5462542b97d7c096e2ed4377
6
+ metadata.gz: b42be7e28718f8f6874ad99655720d11965ae4c3d8ae416c5dd72bbfdf956ffa59206d3cb5fa0d36a43155c0a9725b3cda1d135d79df00a2ee72a6353c4abf9e
7
+ data.tar.gz: fa1a2114d426eab57a2b463e700a59fa681de9b677310918e7db1f23be4aa767d11c1b46097a012a76fb6fcfb13d0c1886e297bf662f2add185ca11b696a0f19
data/lib/hiccdown.rb CHANGED
@@ -5,14 +5,15 @@ require 'action_view'
5
5
  module Hiccdown
6
6
  module ViewHelpers
7
7
  def self.included(base)
8
- base.prepend(MethodOverrides)
8
+ base.prepend(ViewHelperOverrides)
9
+ ActionView::Helpers::RenderingHelper.prepend(RenderingHelperOverrides)
9
10
  end
10
11
 
11
12
  def scope *args, &block
12
13
  Hiccdown::scope(*args, &block)
13
14
  end
14
15
 
15
- module MethodOverrides
16
+ module ViewHelperOverrides
16
17
  def self.prepended(base)
17
18
  # `capture` is at the root of seemingly all Rails methods tasked with
18
19
  # rendering content, including `content_tag` and `tag`, which in turn
@@ -29,6 +30,23 @@ module Hiccdown
29
30
  end
30
31
  end
31
32
  end
33
+
34
+ module RenderingHelperOverrides
35
+ def self.prepended(base)
36
+ # This teaches the view renderer (which is different from the controller renderer,
37
+ # h/t https://stackoverflow.com/questions/78801079/rails-custom-renderer-for-turbo-stream?noredirect=1#comment138933498_78801079)
38
+ # to accept a `:hiccdown` option, thus enabling the rendering of hiccdown in turbo-stream
39
+ # actions as well:
40
+ # `turbo_stream.update(..., hiccdown: [:h1, 'hello world'])
41
+ define_method(:render) do |options = {}, locals = {}, &block|
42
+ if options.is_a?(Hash) && options.key?(:hiccdown)
43
+ Hiccdown::to_html(options[:hiccdown]).html_safe
44
+ else
45
+ super(options, locals, &block)
46
+ end
47
+ end
48
+ end
49
+ end
32
50
  end
33
51
 
34
52
  def self.scope *args, &block
@@ -39,35 +57,35 @@ module Hiccdown
39
57
  Set.new([:area, :base, :br, :col, :command, :embed, :hr, :img, :input, :keygen, :link, :menuitem, :meta, :param, :source, :track, :wbr])
40
58
  end
41
59
 
42
- def self.process_hash(hash, prefix = nil, escape)
43
- hash.flat_map do |key, value|
60
+ def self.process_attributes(hash, prefix = nil, escape)
61
+ hash.map do |key, value|
44
62
  attribute_key = prefix ? "#{prefix}-#{key}" : key.to_s
45
63
 
46
64
  if value.is_a?(Hash)
47
- process_hash(value, attribute_key, escape)
65
+ process_attributes(value, attribute_key, escape)
48
66
  elsif value.is_a?(Array)
49
- value_str = value.map { |v| maybe_escape(v.to_s, escape) }.join(' ')
50
- ["#{attribute_key}=\"#{value_str}\""]
67
+ value_str = value
68
+ .reject { |v| v.to_s == '' }
69
+ .map { |v| maybe_escape(v.to_s, escape) }
70
+ .join(' ')
71
+
72
+ %{#{attribute_key}="#{value_str}"}
51
73
  else
52
74
  value_str = maybe_escape(value.to_s, escape)
53
- ["#{attribute_key}=\"#{value_str}\""]
75
+ %{#{attribute_key}="#{value_str}"}
54
76
  end
55
77
  end
56
78
  end
57
79
 
58
- def self.hash_to_html_attributes(hash, escape)
59
- process_hash(hash, nil, escape).join(' ')
60
- end
61
-
62
80
  def self.to_html structure, escape = true
63
81
  if structure.is_a? Hash
64
- self.hash_to_html_attributes(structure, escape)
82
+ self.process_attributes(structure, nil, escape).join(' ')
65
83
  elsif structure.is_a? Array
66
84
  if structure.empty?
67
85
  return nil
68
86
  end
69
87
 
70
- if structure.first.is_a?(Array)
88
+ if structure.first.is_a?(Array) || structure.first.is_a?(String)
71
89
  return structure.map { |s| to_html(s, escape) }.join
72
90
  end
73
91
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiccdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Hackethal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-26 00:00:00.000000000 Z
11
+ date: 2024-07-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Generates an HTML string from a Hiccup structure and improves Rails views.
14
14
  email: engineering@dennishackethal.com