class Pakyow::Presenter::Templates

Constants

DEFAULT_LAYOUTS_PATH
DEFAULT_PAGES_PATH
DEFAULT_PARTIALS_PATH

Attributes

config[R]
includes[R]
layouts[R]
name[R]
pages[R]
path[R]
processor[R]

Public Class Methods

new(name, path, processor: nil, config: {}) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 18
def initialize(name, path, processor: nil, config: {})
  @name, @path, @processor = name, Pathname(path), processor
  @layouts, @includes, @info = {}, {}, {}
  build_config(config)
  load_templates
end

Public Instance Methods

each() { |info| ... } click to toggle source

Yields each template.

# File lib/pakyow/presenter/templates.rb, line 80
def each
  @info.each do |_path, info|
    yield info[:layout]
    yield info[:page]
    info[:partials].each do |_name, partial|
      yield partial
    end
  end
end
info(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 33
def info(path)
  if view?(path)
    @info[path]
  end
end
layout(name_or_path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 39
def layout(name_or_path)
  if name_or_path.is_a?(Symbol)
    layout_with_name(name_or_path)
  else
    info(name_or_path) & [:layout]
  end
end
layouts_path() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 59
def layouts_path
  path.join(@config[:paths][:layouts])
end
page(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 47
def page(path)
  info(path) & [:page]
end
pages_path() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 67
def pages_path
  path.join(@config[:paths][:pages])
end
partial(path, name) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 55
def partial(path, name)
  partials(path)[name.to_sym]
end
partials(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 51
def partials(path)
  info(path) & [:partials] || {}
end
partials_path() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 63
def partials_path
  path.join(@config[:paths][:partials])
end
paths() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 29
def paths
  @info.keys
end
template?(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 71
def template?(path)
  return false if path.basename.to_s.start_with?(".")
  return false unless path.extname == ".html" || @processor&.process?(path.extname)

  true
end
view?(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 25
def view?(path)
  @info.key?(path)
end

Private Instance Methods

ascend(path) { |path| ... } click to toggle source
# File lib/pakyow/presenter/templates.rb, line 220
def ascend(path)
  return enum_for(:ascend, path) unless block_given?

  path.ascend.each do |path|
    yield path

    if path == @path
      break
    end
  end
end
build_config(config) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 92
def build_config(config)
  @config = {
    prefix: config[:prefix] || "/",
    paths: {
      layouts: config.dig(:paths, :layouts) || DEFAULT_LAYOUTS_PATH,
      pages: config.dig(:paths, :pages) || DEFAULT_PAGES_PATH,
      partials: config.dig(:paths, :partials) || DEFAULT_PARTIALS_PATH,
    }
  }
end
index_page_at_path(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 183
def index_page_at_path(path)
  ascend(path) do |parent_path|
    next unless info = info(normalize_path(parent_path))
    next unless page = info[:page]
    return page
  end
end
layout_with_name(name) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 103
def layout_with_name(name)
  @layouts[name.to_sym]
end
load_layouts() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 113
def load_layouts
  return unless layouts_path.exist?

  layouts_path.children.each_with_object(@layouts) { |file, layouts|
    next unless template?(file)

    if layout = load_view_of_type_at_path(Views::Layout, file)
      layouts[layout.name] = layout
    end
  }
end
load_partials() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 125
def load_partials
  return unless partials_path.exist?

  partials_path.children.each_with_object(@includes) { |file, partials|
    next unless template?(file)

    if partial = load_view_of_type_at_path(Views::Partial, file, normalize_path(file))
      partials[partial.name] = partial
    end
  }
end
load_path_info() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 137
def load_path_info
  pages_path.glob("**/*").select { |path|
    template?(path)
  }.reject { |path|
    path.basename.to_s.start_with?("_")
  }.each do |path|
    if page = page_at_path(path)
      path_to_page = String.normalize_path(
        File.join(
          @config[:prefix], normalize_path(path, pages_path)
        )
      )

      @info[path_to_page] = {
        page: page,

        layout: layout_with_name(
          page.info(:layout)
        ),

        partials: @includes.merge(
          partials_at_path(path)
        )
      }
    end
  rescue FrontMatterParsingError => e
    message = "Could not parse front matter for #{path}:\n\n#{e.context}"

    if e.wrapped_exception
      message << "\n#{e.wrapped_exception.problem} at line #{e.wrapped_exception.line} column #{e.wrapped_exception.column}"
    end

    raise FrontMatterParsingError.new(message)
  end
end
load_templates() click to toggle source
# File lib/pakyow/presenter/templates.rb, line 107
def load_templates
  load_layouts
  load_partials
  load_path_info
end
load_view_of_type_at_path(type, path, logical_path = nil) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 203
def load_view_of_type_at_path(type, path, logical_path = nil)
  extension = File.extname(path)

  if extension.end_with?(".html") || @processor&.process?(extension)
    content = File.read(path)
    info, content = FrontMatterParser.parse_and_scrub(content)

    if @processor
      content = @processor.process(content, extension.delete(".").to_sym)
    end

    type.load(path, info: info, content: content, logical_path: logical_path)
  else
    nil
  end
end
normalize_path(path, relative_from = @path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 232
def normalize_path(path, relative_from = @path)
  # make it relative
  path = path.expand_path.relative_path_from(relative_from.expand_path)

  # we can short-circuit here
  return "/" if path.to_s == "."

  # remove the extension
  path = path.sub_ext("")

  # remove index from the end
  path = path.sub("index", "")

  # actually normalize it
  String.normalize_path(path.to_s)
end
page_at_path(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 173
def page_at_path(path)
  if File.directory?(path)
    if Dir.glob(File.join(path, "index.*")).empty?
      index_page_at_path(path)
    end
  else
    load_view_of_type_at_path(Views::Page, path, normalize_path(path))
  end
end
partials_at_path(path) click to toggle source
# File lib/pakyow/presenter/templates.rb, line 191
def partials_at_path(path)
  ascend(path).select(&:directory?).each_with_object({}) { |parent_path, partials|
    parent_path.children.select { |child|
      child.basename.to_s.start_with?("_")
    }.each_with_object(partials) { |child, child_partials|
      if partial = load_view_of_type_at_path(Views::Partial, child, normalize_path(child))
        child_partials[partial.name] ||= partial
      end
    }
  }
end