Sha256: e867890d56a0a24a39e43ca68c39666cc8dadb51313c7980d4c2c812c7627c3c

Contents?: true

Size: 1.88 KB

Versions: 23

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require_relative "liquid_renderer/file_system"
require_relative "liquid_renderer/file"
require_relative "liquid_renderer/table"

module Bridgetown
  class LiquidRenderer
    extend Forwardable

    private def_delegator :@site, :in_source_dir, :source_dir

    def initialize(site)
      @site = site

      # Set up Liquid file system access to components for the Render tag
      Liquid::Template.file_system = LiquidRenderer::FileSystem.new(
        @site.components_load_paths, "%s.liquid"
      )
      Liquid::Template.file_system.site = site

      Liquid::Template.error_mode = @site.config["liquid"]["error_mode"].to_sym
      reset
    end

    def reset
      @stats = {}
      @cache = {}
      Bridgetown::Converters::LiquidTemplates.cached_partials = {}
    end

    def file(filename)
      filename.match(filename_regex)
      filename = Regexp.last_match(2)
      LiquidRenderer::File.new(self, filename).tap do
        @stats[filename] ||= new_profile_hash
      end
    end

    def increment_bytes(filename, bytes)
      @stats[filename][:bytes] += bytes
    end

    def increment_time(filename, time)
      @stats[filename][:time] += time
    end

    def increment_count(filename)
      @stats[filename][:count] += 1
    end

    def stats_table(num_of_rows = 50)
      LiquidRenderer::Table.new(@stats).to_s(num_of_rows)
    end

    def self.format_error(error, path)
      "#{error.message} in #{path}"
    end

    # A persistent cache to store and retrieve parsed templates based on the filename
    # via `LiquidRenderer::File#parse`
    #
    # It is emptied when `self.reset` is called.
    def cache
      @cache ||= {}
    end

    private

    def filename_regex
      @filename_regex ||= begin
        %r!\A(#{Regexp.escape(source_dir)}/|/*)(.*)!i
      end
    end

    def new_profile_hash
      Hash.new { |hash, key| hash[key] = 0 }
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
bridgetown-core-0.21.5 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-1.0.0.alpha1 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.4 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.3 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.2 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.1 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.0 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.0.beta4 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.0.beta3 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.0.beta2 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.21.0.beta1 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.20.0 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.19.3 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.19.2 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.19.1 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.19.0 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.18.6 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.18.5 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.18.4 lib/bridgetown-core/liquid_renderer.rb
bridgetown-core-0.18.3 lib/bridgetown-core/liquid_renderer.rb