<commit_before>module Pakyow
  module Presenter
    class Page
      MATTER_MATCHER = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m

      class << self
        def load(path)
          format   = StringUtils.split_at_last_dot(path)[-1]
          name     = File.basename(path, '.*').to_sym
          contents = FileTest.file?(path) ? File.read(path) : nil

          return Page.new(name, contents, path, format)
        end
      end

      attr_reader :contents, :path

      def initialize(name, contents, path, format = :html)
        @name, @contents, @path, @format = name, contents, path, format

        @info    = {}
        @content = {}

        unless @contents.nil?
          parse_info
          parse_content
        end

        partials
      end

      def initialize_copy(original_page)
        super

        # copy content
        @content = {}
        original_page.content.each_pair do |k, v|
          @content[k] = v.dup
        end
      end

      def build(partial_map)
        partials.each do |container, partial_list|
          partial_list.each do |partial_name|
            regex = /<!--\s*@include\s*#{partial_name}\s*-->/
            @content[container].gsub!(regex, partial_map[partial_name].to_s)

            partial_map.delete(partial_name)
          end
        end

        # we have more partials
        if partial_map.count > 0
          # initiate another build if content contains partials
          build(partial_map) if partials(true).count > 0
        end

        return self
      end

      def content(container = nil)
        return container.nil? ? @content : @content[container.to_sym]
      end

      def template
        @info[:template] || :pakyow
      end

      def ==(page)
        @contents == page.contents
      end

      def partials(refind = false)
        @partials = (!@partials || refind) ? find_partials : @partials
      end

      private

      def parse_info
        info = parse_front_matter(@contents)
        info = {} if !info || !info.is_a?(Hash)

        @info = HashUtils.symbolize(info)
      end

      def parse_content
        # remove yaml front matter
        @contents.gsub!(/---(.|\n)*---/, '')

        # process contents
        @contents = Presenter.process(@contents, @format)

        # find content in named containers
        within_regex = /<!--\s*@within\s*([a-zA-Z0-9\-_]*)\s*-->(.*?)<!--\s*\/within\s*-->/m

        @contents.scan(within_regex) do |m|
          container = m[0].to_sym
          @content[container] = m[1]
        end<commit_msg>Method for converting Page content to View objects<commit_after>module Pakyow
  module Presenter
    class Page
      MATTER_MATCHER = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m

      class << self
        def load(path)
          format   = StringUtils.split_at_last_dot(path)[-1]
          name     = File.basename(path, '.*').to_sym
          contents = FileTest.file?(path) ? File.read(path) : nil

          return Page.new(name, contents, path, format)
        end
      end

      attr_reader :contents, :path

      def initialize(name, contents, path, format = :html)
        @name, @contents, @path, @format = name, contents, path, format

        @info    = {}
        @content = {}

        unless @contents.nil?
          parse_info
          parse_content
        end

        partials
      end

      def initialize_copy(original_page)
        super

        # copy content
        @content = {}
        original_page.content.each_pair do |k, v|
          @content[k] = v.dup
        end
      end

      def build(partial_map)
        partials.each do |container, partial_list|
          partial_list.each do |partial_name|
            regex = /<!--\s*@include\s*#{partial_name}\s*-->/
            @content[container].gsub!(regex, partial_map[partial_name].to_s)

            partial_map.delete(partial_name)
          end
        end

        # we have more partials
        if partial_map.count > 0
          # initiate another build if content contains partials
          build(partial_map) if partials(true).count > 0
        end

        return self
      end

      def content(container = nil)
        return container.nil? ? @content : @content[container.to_sym]
      end

      def template
        @info[:template] || :pakyow
      end

      def ==(page)
        @contents == page.contents
      end

      def partials(refind = false)
        @partials = (!@partials || refind) ? find_partials : @partials
      end

      def to_view(container = :default)
        View.new(@content[container])
      end

      private

      def parse_info
        info = parse_front_matter(@contents)
        info = {} if !info || !info.is_a?(Hash)

        @info = HashUtils.symbolize(info)
      end

      def parse_content
        # remove yaml front matter
        @contents.gsub!(/---(.|\n)*---/, '')

        # process contents
        @contents = Presenter.process(@contents, @format)

        # find content in named containers
        within_regex = /<!--\s*@within\s*([a-zA-Z0-9\-_]*)\s*-->(.*?)<!--\s*\/within\s*-->/m

        @contents.scan(within_regex) do |m|
          container = m[0].to_sym
          @content[container] = m[1]
        end
{'fraction_non_alphanumeric': 0.12682551883166795, 'fraction_numerical': 0.002690238278247502, 'mean_word_length': 1.807443365695793, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4425672', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1548, 'n_words': 355}