Module: Hamlit::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/hamlit/helpers.rb

Constant Summary collapse

DEFAULT_PRESERVE_TAGS =
%w[textarea pre code].freeze

Instance Method Summary collapse

Instance Method Details

#capture_haml(*args, &block) ⇒ Object

NOTE: currently Hamlit::Helpers is enabled by default on only Rails environment. Thus you can use capture.



34
35
36
# File 'lib/hamlit/helpers.rb', line 34

def capture_haml(*args, &block)
  capture(*args, &block)
end

#find_and_preserve(input = nil, tags = DEFAULT_PRESERVE_TAGS, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hamlit/helpers.rb', line 9

def find_and_preserve(input = nil, tags = DEFAULT_PRESERVE_TAGS, &block)
  return find_and_preserve(capture_haml(&block), input || tags) if block

  tags = tags.each_with_object('') do |t, s|
    s << '|' unless s.empty?
    s << Regexp.escape(t)
  end

  re = /<(#{tags})([^>]*)>(.*?)(<\/\1>)/im
  input.to_s.gsub(re) do |s|
    s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible
    "<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
  end
end

#preserve(input = nil, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/hamlit/helpers.rb', line 24

def preserve(input = nil, &block)
  return preserve(capture_haml(&block)) if block
  s = input.to_s.chomp("\n")
  s.gsub!(/\n/, '&#x000A;')
  s.delete!("\r")
  s
end