Sha256: 3a5fe1e1e91427da2fcfdacb9f8b6b61ef62ea4815a36fdb9b43beaad961c137

Contents?: true

Size: 1.43 KB

Versions: 23

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

class Theme
  attr_accessor :name, :path, :description_html

  def initialize(name, path)
    @name = name
    @path = path
  end

  def layout(action = :default)
    if action.to_s == "view_page"
      return "layouts/pages" if File.exist? "#{view_path}/layouts/pages.html.erb"
    end
    "layouts/default"
  end

  def description
    about_file = "#{path}/about.markdown"
    if File.exist? about_file
      File.read about_file
    else
      "### #{name}"
    end
  end

  def view_path
    "#{path}/views"
  end

  # Find a theme, given the theme name
  def self.find(name)
    registered_themes[name]
  end

  # List all themes
  def self.find_all
    registered_themes.values
  end

  def self.register_theme(path)
    theme = theme_from_path(path)
    registered_themes[theme.name] = theme
  end

  def self.register_themes(themes_root)
    search_theme_directory(themes_root).each do |path|
      register_theme path
    end
  end

  # Private

  def self.registered_themes
    @registered_themes ||= {}
  end

  def self.theme_from_path(path)
    name = path.scan(/[-\w]+$/i).flatten.first
    new(name, path)
  end

  def self.search_theme_directory(themes_root)
    glob = "#{themes_root}/[a-zA-Z0-9]*"
    Dir.glob(glob).select do |file|
      File.readable?("#{file}/about.markdown")
    end.compact
  end

  private_class_method :search_theme_directory,
                       :theme_from_path, :registered_themes
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
HornsAndHooves-publify_core-10.5.0 lib/theme.rb
HornsAndHooves-publify_core-10.4.0 lib/theme.rb
HornsAndHooves-publify_core-10.3.0 lib/theme.rb
HornsAndHooves-publify_core-10.2.0 lib/theme.rb
publify_core-10.0.1 lib/theme.rb
publify_core-10.0.0 lib/theme.rb
publify_core-9.2.10 lib/theme.rb
HornsAndHooves-publify_core-10.1.1 lib/theme.rb
HornsAndHooves-publify_core-10.1.0 lib/theme.rb
HornsAndHooves-publify_core-10.0.3 lib/theme.rb
HornsAndHooves-publify_core-10.0.2 lib/theme.rb
HornsAndHooves-publify_core-10.0.1 lib/theme.rb
HornsAndHooves-publify_core-10.0.0 lib/theme.rb
publify_core-9.2.9 lib/theme.rb
publify_core-9.2.8 lib/theme.rb
publify_core-9.2.7 lib/theme.rb
publify_core-9.2.6 lib/theme.rb
publify_core-9.2.5 lib/theme.rb
publify_core-9.2.4 lib/theme.rb
publify_core-9.2.3 lib/theme.rb