<commit_before># -*- coding: utf-8 -*-

###
### $Release: 0.0.0 $
### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
### $License: MIT License $
###


##
## Yet another eRuby implementation, based on Erubis.
##
## * Small and fast
## * Easy to customize
## * Supports HTML as well as plain text
## * Accepts both template file and template string
##
## Example:
##
##   ## render template string
##   template = BabyErubis::Html.new <<'END', __FILE__, __LINE__+1
##     <h1><%= @title %></h1>
##     <% for item in @items %>
##       <p><%= item %></p>
##     <% end %>
##   END
##   context = {:title=>'Example', :items=>['<AAA>', 'B&B', '"CCC"']}
##   output = template.render(context)
##   print output
##
##   ## render template file
##   templat = BabyErubis::Html.load('example.html.erb', 'utf-8')
##   context = {:title=>'Example', :items=>['<AAA>', 'B&B', '"CCC"']}
##   output = template.render(context)
##   print output
##

module BabyErubis


  class Template

    def self.load(filename, encoding='utf-8')
      input = File.open(filename, "rb:#{encoding}") {|f| f.read() }
      return self.new(input, filename)
    end

    def initialize(input=nil, filename=nil, linenum=1)
      compile(input, filename, linenum) if input
    end

    attr_reader :src

    EMBED_REXP = /(^[ \t]*)?<%(==?|\#)?(.*?)%>([ \t]*\r?\n)?/m

    def compile(input, filename=nil, linenum=1)
      src = convert(input)
      @src = src
      @_proc = eval("proc { #{src} }", TOPLEVEL_BINDING, filename || '(eRuby)', linenum)
      return self
    end

    def convert(input)
      src = "_buf = '';"       # preamble
      pos = 0
      rexp = EMBED_REXP
      input.scan(rexp) do |lspace, ch, code, rspace|
        match = Regexp.last_match
        text  = input[pos...match.begin(0)]
        pos   = match.end(0)
        src << _t(text)
        if ch == '='           # expression (escaping)
          src << _t(lspace) << " _buf << #{escaped_expr(code)};" << _t(rspace)
        elsif ch == '=='       # expression (without escaping)
          src << _t(lspace) << " _buf << (#{code}).to_s;" << _t(rspace)
        elsif ch == '#'        # comment
          src << _t(lspace) << ("\n" * code.count("\n")) << _t(rspace)
        else                   # statement
          if lspace && rspace
            src << "#{lspace}#{code};#{rspace}"
          else
            src << _t(lspace) << code << ';' << _t(rspace)
          end
        end
      end
      rest = $' || input
      src << _t(rest)
      src << "; _buf.to_s\n"   # postamble
      return src
    end

    def render(context={})
      ctxobj = context.is_a?(Hash) ? new_context(context) : context
      return ctxobj.instance_eval(&@_proc)
    end

    protected

    def escaped_expr(code)
      return "(#{code}).to_s"
    end

    def new_context(hash)
      return TemplateContext.new(hash)
    end

    private

    def build_text(text)
      return text && !text.empty? ? " _buf << '#{escape_text(text)}';" : ''
      #return text && !text.empty? ? " _buf << %q`#{escape_text(text)}`;" : ''
    end
    alias _t build_text

    def escape_text(text)
      return text.gsub!(/['\\]/, '\\\\\&') || text
      #return text.gsub!(/[`\\]/, '\\\\\&') || text
    end

  end
  Text = Template              # for shortcut


  class TemplateContext

    def initialize(vars={})
      vars.each do |k, v|<commit_msg>ruby: make 'Template#new_context()' from 'protected' to 'public'<commit_after># -*- coding: utf-8 -*-

###
### $Release: 0.0.0 $
### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
### $License: MIT License $
###


##
## Yet another eRuby implementation, based on Erubis.
##
## * Small and fast
## * Easy to customize
## * Supports HTML as well as plain text
## * Accepts both template file and template string
##
## Example:
##
##   ## render template string
##   template = BabyErubis::Html.new <<'END', __FILE__, __LINE__+1
##     <h1><%= @title %></h1>
##     <% for item in @items %>
##       <p><%= item %></p>
##     <% end %>
##   END
##   context = {:title=>'Example', :items=>['<AAA>', 'B&B', '"CCC"']}
##   output = template.render(context)
##   print output
##
##   ## render template file
##   templat = BabyErubis::Html.load('example.html.erb', 'utf-8')
##   context = {:title=>'Example', :items=>['<AAA>', 'B&B', '"CCC"']}
##   output = template.render(context)
##   print output
##

module BabyErubis


  class Template

    def self.load(filename, encoding='utf-8')
      input = File.open(filename, "rb:#{encoding}") {|f| f.read() }
      return self.new(input, filename)
    end

    def initialize(input=nil, filename=nil, linenum=1)
      compile(input, filename, linenum) if input
    end

    attr_reader :src

    EMBED_REXP = /(^[ \t]*)?<%(==?|\#)?(.*?)%>([ \t]*\r?\n)?/m

    def compile(input, filename=nil, linenum=1)
      src = convert(input)
      @src = src
      @_proc = eval("proc { #{src} }", TOPLEVEL_BINDING, filename || '(eRuby)', linenum)
      return self
    end

    def convert(input)
      src = "_buf = '';"       # preamble
      pos = 0
      rexp = EMBED_REXP
      input.scan(rexp) do |lspace, ch, code, rspace|
        match = Regexp.last_match
        text  = input[pos...match.begin(0)]
        pos   = match.end(0)
        src << _t(text)
        if ch == '='           # expression (escaping)
          src << _t(lspace) << " _buf << #{escaped_expr(code)};" << _t(rspace)
        elsif ch == '=='       # expression (without escaping)
          src << _t(lspace) << " _buf << (#{code}).to_s;" << _t(rspace)
        elsif ch == '#'        # comment
          src << _t(lspace) << ("\n" * code.count("\n")) << _t(rspace)
        else                   # statement
          if lspace && rspace
            src << "#{lspace}#{code};#{rspace}"
          else
            src << _t(lspace) << code << ';' << _t(rspace)
          end
        end
      end
      rest = $' || input
      src << _t(rest)
      src << "; _buf.to_s\n"   # postamble
      return src
    end

    def render(context={})
      ctxobj = context.is_a?(Hash) ? new_context(context) : context
      return ctxobj.instance_eval(&@_proc)
    end

    def new_context(hash)
      return TemplateContext.new(hash)
    end

    protected

    def escaped_expr(code)
      return "(#{code}).to_s"
    end

    private

    def build_text(text)
      return text && !text.empty? ? " _buf << '#{escape_text(text)}';" : ''
      #return text && !text.empty? ? " _buf << %q`#{escape_text(text)}`;" : ''
    end
    alias _t build_text

    def escape_text(text)
      return text.gsub!(/['\\]/, '\\\\\&') || text
      #return text.gsub!(/[`\\]/, '\\\\\&') || text
    end

  end
  Text = Template              # for shortcut


  class TemplateContext

    def initialize(vars={})
      vars.each do |k, v|
{'fraction_non_alphanumeric': 0.19135531135531136, 'fraction_numerical': 0.005274725274725275, 'mean_word_length': 2.3642188270083784, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '187758', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2331, 'n_words': 545}