<commit_before>module Locomotive
  module Mounter
    module Reader
      module FileSystem

        class PagesBuilder < Base

          attr_accessor :pages

          def initialize(runner)
            self.pages = {}
            super
          end

          # Build the tree of pages based on the data specified by the config/site.yml file
          # of the template but also with the filesystem structure
          #
          # @return [ Hash ] The pages organized as a Hash (using the fullpath as the key)
          #
          def build
            self.fetch_from_config

            self.fetch_from_filesystem

            index = self.pages['index']

            index.localize_fullpath(self.locales)

            self.build_relationships(index, self.pages_to_list)

            # Locomotive::Mounter.with_locale(:fr) { self.to_s } # DEBUG
            puts self.to_s

            self.pages
          end

          protected

          # Create a ordered list of pages from the Hash
          #
          # @return [ Array ] An ordered list of pages
          #
          def pages_to_list
            # sort by fullpath first
            list = self.pages.values.sort { |a, b| a.fullpath <=> b.fullpath }
            # sort finally by depth
            list.sort { |a, b| a.depth <=> b.depth }
          end

          def build_relationships(parent, list)
            list.dup.each do |page|
              next unless self.is_subpage_of?(page.fullpath, parent.fullpath)

              # attach the page to the parent (order by position), also set the parent
              parent.add_child(page)

              # localize the fullpath in all the locales
              page.localize_fullpath(self.locales)

              # remove the page from the list
              list.delete(page)

              # go under
              self.build_relationships(page, list)
            end
          end

          # Record pages found in the config file
          def fetch_from_config
            self.pages_from_config.each_with_index do |_page, position|
              fullpath, attributes = _page.keys.first.dasherize, _page.values.first.try(:symbolize_keys) || {}

              self.add(fullpath, attributes.merge(position: position))
            end
          end

          # Record pages found in file system
          def fetch_from_filesystem
            Dir.glob(File.join(self.root_dir, '**/*.{liquid,haml}')).each do |filepath|
              fullpath = self.filepath_to_fullpath(filepath)

              page = self.add(fullpath)

              Locomotive::Mounter.with_locale(self.filepath_locale(filepath)) do
                page.template_filepath = filepath if Locomotive::Mounter.locale
              end
            end<commit_msg>include folder like pages<commit_after>module Locomotive
  module Mounter
    module Reader
      module FileSystem

        class PagesBuilder < Base

          attr_accessor :pages

          def initialize(runner)
            self.pages = {}
            super
          end

          # Build the tree of pages based on the data specified by the config/site.yml file
          # of the template but also with the filesystem structure
          #
          # @return [ Hash ] The pages organized as a Hash (using the fullpath as the key)
          #
          def build
            self.fetch_from_config

            self.fetch_from_filesystem

            index = self.pages['index']

            index.localize_fullpath(self.locales)

            self.build_relationships(index, self.pages_to_list)

            # Locomotive::Mounter.with_locale(:fr) { self.to_s } # DEBUG
            # puts self.to_s

            self.pages
          end

          protected

          # Create a ordered list of pages from the Hash
          #
          # @return [ Array ] An ordered list of pages
          #
          def pages_to_list
            # sort by fullpath first
            list = self.pages.values.sort { |a, b| a.fullpath <=> b.fullpath }
            # sort finally by depth
            list.sort { |a, b| a.depth <=> b.depth }
          end

          def build_relationships(parent, list)
            list.dup.each do |page|
              next unless self.is_subpage_of?(page.fullpath, parent.fullpath)

              # attach the page to the parent (order by position), also set the parent
              parent.add_child(page)

              # localize the fullpath in all the locales
              page.localize_fullpath(self.locales)

              # remove the page from the list
              list.delete(page)

              # go under
              self.build_relationships(page, list)
            end
          end

          # Record pages found in the config file
          def fetch_from_config
            self.pages_from_config.each_with_index do |_page, position|
              fullpath, attributes = _page.keys.first.dasherize, _page.values.first.try(:symbolize_keys) || {}

              self.add(fullpath, attributes.merge(position: position))
            end
          end

          # Record pages found in file system
          def fetch_from_filesystem
            folders = []

            Dir.glob(File.join(self.root_dir, '**/*')).each do |filepath|
              fullpath = self.filepath_to_fullpath(filepath)

              folders.push(fullpath) && next if File.directory?(filepath)

              next unless filepath =~ /\.(haml|liquid)$/

              page = self.add(fullpath)

              Locomotive::Mounter.with_locale(self.filepath_locale(filepath)) do
                page.template_filepath = filepath if Locomotive::Mounter.locale
              end
            end

            folders.each do |fullpath|
              next if self.pages.key?(fullpath)
              self.add(fullpath, title: File.basename(fullpath).humanize)
            end
{'fraction_non_alphanumeric': 0.0728578758337609, 'fraction_numerical': 0.0, 'mean_word_length': 1.6306792622582096, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2493718', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1515, 'n_words': 423}