Sha256: d17e6dc253e2cf5dbc2eba1ebabee8f3510c7790076cbb2c68e7c63f086aa3e1

Contents?: true

Size: 1.8 KB

Versions: 16

Compression:

Stored size: 1.8 KB

Contents

module Lookbook
  module Utils
    POSITION_PREFIX_REGEX = /^(\d+?)[-_]/
    FRONTMATTER_REGEX = /\A---(.|\n)*?---/

    protected

    def generate_id(*args)
      parts = args.map { |arg| arg.to_s.parameterize.underscore }
      parts.join("-").tr("/", "-").tr("_", "-").delete_prefix("-").delete_suffix("-").gsub("--", "-")
    end

    def preview_class_basename(klass)
      class_name(klass).to_s.chomp("ComponentPreview").chomp("Component::Preview").chomp("::Preview").chomp("Component").chomp("::")
    end

    def preview_class_name(klass)
      preview_class_basename(klass).underscore
    end

    def class_name(klass)
      klass.is_a?(Class) ? klass.name : klass
    end

    def normalize_matchers(*matchers)
      matchers.flatten.map { |m| m.gsub(/\s/, "").downcase }
    end

    def get_position_prefix(str)
      parse_position_prefix(str).first
    end

    def remove_position_prefix(str)
      parse_position_prefix(str).last
    end

    def get_frontmatter(str)
      parse_frontmatter(str).first
    end

    def strip_frontmatter(str)
      parse_frontmatter(str).last
    end

    def to_lookup_path(path)
      path.split("/").map { |segment| remove_position_prefix(segment) }.join("/")
    end

    def to_preview_path(*args)
      args.flatten.map { |arg| preview_class_name(arg) }.join("/")
    end

    private

    def parse_frontmatter(content)
      frontmatter = content.match(FRONTMATTER_REGEX)
      if frontmatter.nil?
        [{}, content]
      else
        [YAML.safe_load(frontmatter[0]), content.gsub(FRONTMATTER_REGEX, "")]
      end
    end

    def parse_position_prefix(str)
      pos = str.match(POSITION_PREFIX_REGEX)
      if pos.nil?
        [10000, str]
      else
        cleaned_str = str.gsub(POSITION_PREFIX_REGEX, "")
        [pos[1].to_i, cleaned_str]
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
lookbook-0.8.3 lib/lookbook/utils.rb
lookbook-0.8.2 lib/lookbook/utils.rb
lookbook-0.8.1 lib/lookbook/utils.rb
lookbook-0.8.0 lib/lookbook/utils.rb
lookbook-0.8.0.beta.0 lib/lookbook/utils.rb
lookbook-0.7.4 lib/lookbook/utils.rb
lookbook-0.7.3 lib/lookbook/utils.rb
lookbook-0.7.2 lib/lookbook/utils.rb
lookbook-0.7.2.beta.5 lib/lookbook/utils.rb
lookbook-0.7.2.beta.4 lib/lookbook/utils.rb
lookbook-0.7.2.beta.3 lib/lookbook/utils.rb
lookbook-0.7.2.beta.2 lib/lookbook/utils.rb
lookbook-0.7.2.beta.1 lib/lookbook/utils.rb
lookbook-0.7.2.beta.0 lib/lookbook/utils.rb
lookbook-0.7.1 lib/lookbook/utils.rb
lookbook-0.7.0 lib/lookbook/utils.rb