<commit_before>require 'erb'

class Doccex::Template < Doccex::Base
  def initialize(template, view_assigns)
    view_assigns.keys.each do |key|
      instance_variable_set("@"+key, view_assigns[key])
    end
    create_template(template)
  end

  def create_template(template)
    FileUtils.cp_r(Rails.application.root.join("app", template), Rails.application.root.join('tmp'))
    temp = template.split("/")[1]
    FileUtils.mv("tmp/#{temp}","tmp/docx", :force => true)
  end

  def render_to_string
    source = Rails.application.root.join('tmp/docx')
    interpolate_variables
    zip_package(source)
    read_zipfile
  end

  def interpolate_variables<commit_msg>now accepts either relative or absolute path for template, so that template can reside in an engine or the main app<commit_after>require 'erb'

class Doccex::Template < Doccex::Base
  def initialize(template, view_assigns)
    view_assigns.keys.each do |key|
      instance_variable_set("@"+key, view_assigns[key])
    end
    create_template(template)
  end

  def create_template(template)
    src = Pathname(template).absolute? ? template : Rails.application.root.join("app", template)
    FileUtils.cp_r(src , Rails.application.root.join('tmp'))
    temp = template.split("/")[-1]
    FileUtils.mv("tmp/#{temp}","tmp/docx", :force => true)
  end

  def render_to_string
    source = Rails.application.root.join('tmp/docx')
    interpolate_variables
    zip_package(source)
    read_zipfile
  end

  def interpolate_variables
{'fraction_non_alphanumeric': 0.12098930481283422, 'fraction_numerical': 0.001336898395721925, 'mean_word_length': 4.365591397849462, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2449048', 'n_tokens_mistral': 493, 'n_tokens_neox': 474, 'n_words': 97}