Sha256: f2d73e02aec68444be7cf4b71d43b664718c66c3f8fa0b263ba3da5870b98a5c

Contents?: true

Size: 1.68 KB

Versions: 8

Compression:

Stored size: 1.68 KB

Contents

module Vedeu

  module Templating

    # preprocess
    #   takes a line at a time
    #   splits line into component parts e.g. text1, ruby, text1
    #     takes text1, adds to JSON stream
    #     processes ruby, adding to a new JSON stream
    #     takes text2, adds to a JSON stream
    #
    # process
    #   converts JSON streams into line/stream objects with correct attributes
    #
    # Pre-processes a template, to convert all lines and lines with directives
    # into Vedeu::Streams.
    #
    # @api private
    class Preprocessor

      # @param lines [Array<String>]
      # @return [Array<Vedeu::Stream>]
      def self.process(lines)
        new(lines).process
      end

      # @param lines [Array<String>]
      # @return [Vedeu::Templating::Preprocessor]
      def initialize(lines)
        @lines = lines
      end

      # @return [Array<Vedeu::Stream>]
      def process
        lines.each_with_object([]) do |line, acc|
          if line =~ markers?
            code = line.gsub(markers, '').chomp

            acc << Directive.process(code)

          else
            acc << Vedeu::Stream.new(value: line.chomp)

          end
          acc
        end
      end

      protected

      # @!attribute [r] lines
      # @return [Array<String>]
      attr_reader :lines

      private

      # Return a pattern to remove directive markers and spaces.
      #
      # @example
      #   line containing {{ or }}
      #
      # @return [Regexp]
      def markers
        /({{\s*|\s*}})/
      end

      # @example
      #   line contains {{ or }}
      #
      # @return [Regexp]
      def markers?
        /^\s*({{)(.*?)(}})$/
      end

    end # Preprocessor

  end # Templating

end # Vedeu

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vedeu-0.4.40 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.39 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.38 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.37 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.36 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.35 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.34 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.33 lib/vedeu/templating/preprocessor.rb