# frozen_string_literal: true

require 'safe_yaml/load'

class GitHubBlob
  # From jekyll/jekyll
  # https://github.com/jekyll/jekyll/blob/74373baa550282a8630368e7b609ca9370f6d560/lib/jekyll/document.rb#L13
  YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m

  attr_reader :data, :body

  def initialize(github_repository, sha, **options)
    @blob = github_repository.client.blob(github_repository.full_name, sha, options)
    read_contents
  end

  # Public: Get the utf-8 formated content of the blob
  #
  # Returns a string.
  def utf_content
    decoded_content
  end

  # Public: Get the content of the blob
  #
  # Returns a base64 string.
  def content
    @blob.content
  end

  private

  def decoded_content
    return @blob.content if @blob.content == 'utf-8'
    Base64.decode64(@blob.content)
  end

  # Internal: process yaml front matter
  #
  # Returns nothing.
  def read_contents
    match = YAML_FRONT_MATTER_REGEXP.match(decoded_content)
    return unless match
    @body = match.post_match
    @data = SafeYAML.load(match.to_s)
  end
end

# frozen_string_literal: true

require "safe_yaml/load"

class GitHubBlob
  # From jekyll/jekyll
  # https://github.com/jekyll/jekyll/blob/74373baa550282a8630368e7b609ca9370f6d560/lib/jekyll/document.rb#L13
  YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m

  attr_reader :data, :body

  def initialize(github_repository, sha, **options)
    @blob = github_repository.client.blob(github_repository.full_name, sha, options)
    read_contents
  end

  # Public: Get the utf-8 formated content of the blob
  #
  # Returns a string.
  def utf_content
    decoded_content
  end

  # Public: Get the content of the blob
  #
  # Returns a base64 string.
  def content
    @blob.content
  end

  private

  def decoded_content
    return @blob.content if @blob.content == "utf-8"
    Base64.decode64(@blob.content)
  end

  # Internal: process yaml front matter
  #
  # Returns nothing.
  def read_contents
    match = YAML_FRONT_MATTER_REGEXP.match(decoded_content)
    return unless match
    @body = match.post_match
    @data = SafeYAML.load(match.to_s)
  end
end

Use double quotes instead of single
Use double quotes instead of single
