Sha256: 4e49ba32bfca6d3ef696137e8562e93ca4dbdc6f5b60bacf676eb8f0dce84093

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 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.
    class Preprocessor

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

      # @param lines [Array<String>]
      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.
      #
      # @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

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.4.29 lib/vedeu/templating/preprocessor.rb
vedeu-0.4.28 lib/vedeu/templating/preprocessor.rb