Sha256: 008aafcff0e9412a7bfdb5a0ba915c439aaa2b1dea8ab167a6696d0598e90729

Contents?: true

Size: 1.44 KB

Versions: 16

Compression:

Stored size: 1.44 KB

Contents

require 'ripper'

module ExpressTemplates
  module Template
    class Handler
      def self.call(template)
        new.call(template)
      end

      def call(template)
        # call ripper stuff method

        # returns a string to be eval'd
        source = "(#{ExpressTemplates.compile(template)}).html_safe"
        warn_contains_logic(source) if ENV['NO_TEMPLATE_WARN'].nil? # pass the source code
        source
      end

      def warn_contains_logic(compiled_template)
        keywords = %w(if until unless case for do loop while)                                                                 # array of conditional keywords
        tokens = []
        if Ripper.lex(compiled_template).select do |element|                                                                  # since it outputs an array [[line, col], type, token]
          element[1]==:on_kw                                                                                                  # type must match ':on_kw' type (type is keyword)
        end.each { |match| tokens.push(match) if keywords.include? match[2] }                                                 # check if token is in given /keyword/ array, then push to new array match
          tokens.each do |first|
            warn "PAGE TEMPLATE INCLUDES #{first[2]} STATEMENT AT LINE #{first[0][0]}: #{first}\n#{compiled_template}"        # warn on first occurence of conditional logic
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
express_templates-0.11.10 lib/express_templates/template/handler.rb
express_templates-0.11.9 lib/express_templates/template/handler.rb
express_templates-0.11.8 lib/express_templates/template/handler.rb
express_templates-0.11.7 lib/express_templates/template/handler.rb
express_templates-0.11.6 lib/express_templates/template/handler.rb
express_templates-0.11.5 lib/express_templates/template/handler.rb
express_templates-0.11.4 lib/express_templates/template/handler.rb
express_admin-1.7.5 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.7.4 vendor/gems/express_templates/express_templates/lib/express_templates/template/handler.rb
express_admin-1.7.3 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.7.2 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.7.1 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.6.13 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.6.12 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.6.11 vendor/gems/express_templates/lib/express_templates/template/handler.rb
express_admin-1.6.10 vendor/gems/express_templates/lib/express_templates/template/handler.rb