Sha256: 93dd55c3645f577378d0c2cb176f3d9fb8bf5368227c12a5c93f4f5c53203fe8

Contents?: true

Size: 1.71 KB

Versions: 10

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "yaml"

module Pakyow
  module Presenter
    module Views
      class Page < View
        DEFAULT_CONTAINER = :main

        attr_reader :name, :path

        class << self
          def load(path, content: nil, **args)
            self.new(File.basename(path, ".*").to_sym, content || File.read(path), **args, path: path)
          end
        end

        def initialize(name, html = "", path: nil, info: {}, **args)
          @containers = {}
          parse_content(html)
          @name, @path = name, path
          info[:layout] ||= :default
          super(html, info: info, **args)
        end

        def initialize_copy(_)
          super

          @containers = Hash[@containers.map { |key, value|
            [key, value.dup]
          }]
        end

        def content(container)
          container = container.to_sym

          if container == DEFAULT_CONTAINER
            @object
          elsif @containers.key?(container)
            @containers[container].object
          else
            nil
          end
        end

        def mixin(partials)
          super

          @containers.values.each do |view|
            view.mixin(partials)
          end
        end

        def container_views
          [View.from_object(@object)].concat(@containers.values)
        end

        private

        WITHIN_REGEX = /<!--\s*@within\s*([a-zA-Z0-9\-_]*)\s*-->(.*?)<!--\s*\/within\s*-->/m

        def parse_content(html)
          html.scan(WITHIN_REGEX) do |match|
            container_name = match[0].to_sym
            @containers[container_name] = View.from_object(StringDoc.new(match[1]))
          end

          html.gsub!(WITHIN_REGEX, "")
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.6 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.5 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.4 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.3 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.2 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.1 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.0 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.0.rc5 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.0.rc4 lib/pakyow/presenter/views/page.rb
pakyow-presenter-1.0.0.rc3 lib/pakyow/presenter/views/page.rb