File: force_escapable.rb

package info (click to toggle)
ruby-hamlit 2.15.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,996 kB
  • sloc: ruby: 10,548; ansic: 536; sh: 23; makefile: 8
file content (29 lines) | stat: -rw-r--r-- 852 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
require 'hamlit/escapable'

module Hamlit
  # This module allows Temple::Filter to dispatch :fescape on `#compile`.
  module FescapeDispathcer
    def on_fescape(flag, exp)
      [:fescape, flag, compile(exp)]
    end
  end
  ::Temple::Filter.include FescapeDispathcer

  # Unlike Hamlit::Escapable, this escapes value even if it's html_safe.
  class ForceEscapable < Escapable
    def initialize(opts = {})
      super
      @escape_code = options[:escape_code] || "::Hamlit::Utils.escape_html((%s))"
      @escaper = eval("proc {|v| #{@escape_code % 'v'} }")
    end

    alias_method :on_fescape, :on_escape

    # ForceEscapable doesn't touch :escape expression.
    # This method is not used if it's inserted after Hamlit::Escapable.
    def on_escape(flag, exp)
      [:escape, flag, compile(exp)]
    end
  end
end