<commit_before>require 'active_support/inflector'
module Hammer
  class ContentfulPages < Hammer::ContentGenerator
    register_content_source ContentfulPages

    def autobuild_content_types
      return [] unless Settings.contentful['spaces'].is_a?(Hash)
      return @content_types if @content_types
      @content_types = []
      Settings.contentful['spaces'].each do |space_name, space_params|
        next unless space_params['contentTypes'].is_a?(Hash)
        space_params['contentTypes'].each do |content_key, params|
          next unless params.is_a?(Hash) && params['renderOnBuild']
          @content_types << params.merge(
            'space_name' => space_name,
            'content_key' => content_key
          )
        end
      end
      @content_types
    end

    def handle_exeption(e)
      if e.class == Contentful::Unauthorized
        file = File.read(Settings.config_file)
        line = file.lines.find{ |x| x.match('"apiKey"\s*:') }
        raise SmartException.new(
          'Incorrect API key for Contentful',
          { text: 'please review credentials in hammer.json' },
          Settings.config_file,
          file.lines.index(line) + 1,
          @input_directory
        )
      elsif e.class == Contentful::NotFound
        raise SmartException.new(
          "Invalid space id for '#{@params['space_name']}' space. Contentful::NotFound error.",
          { text: 'please review credentials in hammer.json' },
          Settings.config_file,
          nil,
          @input_directory
        )
      else
        raise e
      end
    end
      
    def register_file_paths
      autobuild_content_types.each do |content_params|
        @params = content_params
        ContentProxy.add_paths(get_paths(content_params))
      end
    end

    def generate_pages
      data = []
      autobuild_content_types.each do |content_params|
        @params = content_params
        # example params
        # {
        #  "name"=>"NU Container",
        #  "template"=>"templates/_container.slim",
        #  "urlAliasSource"=>"title",
        #  "renderOnBuild"=>true,
        #  "space_name"=>"default",
        #  "content_key"=>"containers"
        # }
        contents.each do |content|
          ContentProxy.register_variable(
            content_variable_name,
            content
          )
          text = parse_template("#{@input_directory}/#{@params['template']}")
          output_path = write_file(text, content)
          ContentProxy.unregister_variable(content_variable_name)
          data << {
            filename: content_params['template'],
            output_filename: output_path,
            generated: true
          }
        end
      end
      { contentful: data }
    end

    def get_paths(content_params)
      @params = content_params
      contents.map do |content|
        content_path(content)
      end
    end

    def contents
      cached = ContentCache.get('contents', @params)
      return cached if cached
      cf = ContentfulHelper.new(
        Settings.contentful,
        @params['space_name']
      )
      result = cf.send(@params['content_key'].to_sym) || []
      ContentCache.cache('contents', result, @params)
    end

    def content_variable_name
      @params['content_key'].singularize.to_sym
    end

    def parse_template(template_path)
      parsers = Hammer::Parser.for_filename(template_path)
      text = File.read(template_path)
      parsers.each do |parser_class|
        parser = parser_class.new
        parser.directory        = @input_directory
        parser.input_directory  = @input_directory
        parser.output_directory = @output_directory
        parser.path = template_path.sub(@input_directory + '/', '')
        
        text = parser.parse(text, parser.path)
      end
      text
    end

    def write_file(text, content)
      output_path = content_path(content)

      filepath = @output_directory + '/' + output_path<commit_msg>fix assets paths for some cases<commit_after>require 'active_support/inflector'
module Hammer
  class ContentfulPages < Hammer::ContentGenerator
    register_content_source ContentfulPages

    def autobuild_content_types
      return [] unless Settings.contentful['spaces'].is_a?(Hash)
      return @content_types if @content_types
      @content_types = []
      Settings.contentful['spaces'].each do |space_name, space_params|
        next unless space_params['contentTypes'].is_a?(Hash)
        space_params['contentTypes'].each do |content_key, params|
          next unless params.is_a?(Hash) && params['renderOnBuild']
          @content_types << params.merge(
            'space_name' => space_name,
            'content_key' => content_key
          )
        end
      end
      @content_types
    end

    def handle_exeption(e)
      if e.class == Contentful::Unauthorized
        file = File.read(Settings.config_file)
        line = file.lines.find{ |x| x.match('"apiKey"\s*:') }
        raise SmartException.new(
          'Incorrect API key for Contentful',
          { text: 'please review credentials in hammer.json' },
          Settings.config_file,
          file.lines.index(line) + 1,
          @input_directory
        )
      elsif e.class == Contentful::NotFound
        raise SmartException.new(
          "Invalid space id for '#{@params['space_name']}' space. Contentful::NotFound error.",
          { text: 'please review credentials in hammer.json' },
          Settings.config_file,
          nil,
          @input_directory
        )
      else
        raise e
      end
    end
      
    def register_file_paths
      autobuild_content_types.each do |content_params|
        @params = content_params
        ContentProxy.add_paths(get_paths(content_params))
      end
    end

    def generate_pages
      data = []
      autobuild_content_types.each do |content_params|
        @params = content_params
        # example params
        # {
        #  "name"=>"NU Container",
        #  "template"=>"templates/_container.slim",
        #  "urlAliasSource"=>"title",
        #  "renderOnBuild"=>true,
        #  "space_name"=>"default",
        #  "content_key"=>"containers"
        # }
        contents.each do |content|
          ContentProxy.register_variable(
            content_variable_name,
            content
          )
          text = parse_template(
            "#{@input_directory}/#{@params['template']}",
            content_path(content)
          )
          output_path = write_file(text, content)
          ContentProxy.unregister_variable(content_variable_name)
          data << {
            filename: content_params['template'],
            output_filename: output_path,
            generated: true
          }
        end
      end
      { contentful: data }
    end

    def get_paths(content_params)
      @params = content_params
      contents.map do |content|
        content_path(content)
      end
    end

    def contents
      cached = ContentCache.get('contents', @params)
      return cached if cached
      cf = ContentfulHelper.new(
        Settings.contentful,
        @params['space_name']
      )
      result = cf.send(@params['content_key'].to_sym) || []
      ContentCache.cache('contents', result, @params)
    end

    def content_variable_name
      @params['content_key'].singularize.to_sym
    end

    def parse_template(template_path, filename)
      parsers = Hammer::Parser.for_filename(template_path)
      text = File.read(template_path)
      parsers.each do |parser_class|
        parser = parser_class.new
        parser.directory        = @input_directory
        parser.input_directory  = @input_directory
        parser.output_directory = @output_directory
        # parser.path = template_path.sub(@input_directory + '/', '')
        parser.path = Pathname.new(File.join(@input_directory, filename)).relative_path_from(Pathname.new(@input_directory)).to_s
        
        text = parser.parse(text, parser.path)
      end
      text
    end

    def write_file(text, content)
      output_path = content_path(content)

      filepath = @output_directory + '/' + output_path
{'fraction_non_alphanumeric': 0.10055384615384616, 'fraction_numerical': 0.00024615384615384614, 'mean_word_length': 2.3019097927671677, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '5444483', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2257, 'n_words': 442}