Sha256: 0cf0c7f1b0a076dedf935f85027a9656ec7d4b84d9c6f5ebdaac0da3c4a05d4d

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

module WLang
  class Source
    class FrontMatter < Source

      def initialize(*args, &block)
        super
        compile
      end

      def locals
        @locals
      end

      def template_content
        @template_content
      end

      private

        def compiler
          template.compiler
        end

        def compile
          raw = raw_content
          if raw =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
            @locals, @template_content = {}, $'

            require 'yaml'
            yaml = YAML::load($1) || {}

            # append explicit locals
            @locals.merge!(yaml.delete("locals") || {})

            # compile explicit partials
            partials = yaml.delete("partials") || {}
            partials.each_pair do |k,tpl|
              @locals[k] = compiler.to_ruby_proc(tpl)
            end

            # append remaining data
            @locals.merge!(yaml) unless yaml.empty?
          else
            @locals = {}
            @template_content = raw
          end
        end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wlang-3.0.1 lib/wlang/source/front_matter.rb
wlang-3.0.0 lib/wlang/source/front_matter.rb
wlang-2.3.1 lib/wlang/source/front_matter.rb
wlang-2.3.0 lib/wlang/source/front_matter.rb
wlang-2.2.4 lib/wlang/source/front_matter.rb
wlang-2.2.3 lib/wlang/source/front_matter.rb
wlang-2.2.2 lib/wlang/source/front_matter.rb
wlang-2.2.1 lib/wlang/source/front_matter.rb