class Bridgetown::Model::RepoOrigin

Constants

RUBY_FRONT_MATTER_HEADER
RUBY_FRONT_MATTER_REGEXP
YAML_FRONT_MATTER_REGEXP

Attributes

content[RW]

@return [String]

front_matter_line_count[RW]

@return [Integer]

Public Class Methods

data_file_extensions() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 25
def data_file_extensions
  %w(.yaml .yml .json .csv .tsv .rb).freeze
end
handle_scheme?(scheme) click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 21
def handle_scheme?(scheme)
  scheme == "repo"
end

Public Instance Methods

collection() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 59
def collection
  return @collection if @collection

  collection_name = if url.host.ends_with?(".collection")
                      url.host.chomp(".collection")
                    else
                      "pages"
                    end
  @collection = Bridgetown::Current.site.collections[collection_name]
end
exists?() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 74
def exists?
  File.exist?(original_path)
end
original_path() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 70
def original_path
  @original_path ||= relative_path.expand_path(Bridgetown::Current.site.source)
end
read() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 30
def read
  begin
    @data = (in_data_collection? ? read_file_data : read_front_matter(original_path)) || {}
  rescue SyntaxError => e
    Bridgetown.logger.error "Error:",
                            "Ruby Exception in #{e.message}"
  rescue StandardError => e
    handle_read_error(e)
  end

  @data ||= {}
  @data[:_id_] = id
  @data[:_origin_] = self
  @data[:_collection_] = collection
  @data[:_content_] = content if content

  @data
end
relative_path() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 53
def relative_path
  @relative_path ||= Pathname.new(
    Addressable::URI.unescape(url.path.delete_prefix("/"))
  )
end
url() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 49
def url
  @url = URI.parse(id)
end

Private Instance Methods

handle_read_error(error) click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 110
def handle_read_error(error)
  if error.is_a? Psych::SyntaxError
    Bridgetown.logger.error "Error:",
                            "YAML Exception reading #{original_path}: #{error.message}"
  else
    Bridgetown.logger.error "Error:",
                            "could not read file #{original_path}: #{error.message}"
  end

  if Bridgetown::Current.site.config["strict_front_matter"] ||
      error.is_a?(Bridgetown::Errors::FatalException)
    raise error
  end
end
in_data_collection?() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 80
def in_data_collection?
  original_path.extname.downcase.in?(self.class.data_file_extensions) &&
    collection.data?
end
read_file_data() click to toggle source
# File lib/bridgetown-core/model/repo_origin.rb, line 85
def read_file_data # rubocop:todo Metrics/MethodLength
  case original_path.extname.downcase
  when ".csv"
    {
      rows:
        CSV.read(original_path,
                 headers: true,
                 encoding: Bridgetown::Current.site.config["encoding"]).map(&:to_hash),
    }
  when ".tsv"
    {
      rows:
        CSV.read(original_path,
                 col_sep: "\t",
                 headers: true,
                 encoding: Bridgetown::Current.site.config["encoding"]).map(&:to_hash),
    }
  when ".rb"
    process_ruby_data(File.read(original_path), original_path, 1)
  else
    yaml_data = YAMLParser.load_file(original_path)
    yaml_data.is_a?(Array) ? { rows: yaml_data } : yaml_data
  end
end