Sha256: 43aa002f408416f2314af2e2a7569613b649bebdbfc2326379ac642a3e936d8b

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

# @Opulent
module Opulent
  # @Compiler
  class Compiler
    # Generate the code for a standard text node
    #
    # @param node [Array] Node code generation data
    # @param indent [Fixnum] Size of the indentation to be added
    #
    def plain(node, indent)
      # Text value
      value = node[@options][:value]

      # Pretty print
      if @settings[:pretty]
        indentation = ' ' * indent

        inline = @sibling_stack[-1][-1] &&
                 @sibling_stack[-1][-1][0] == :node &&
                 Settings::INLINE_NODE.include?(@sibling_stack[-1][-1][1])

        # Add current node to the siblings stack
        @sibling_stack[-1] << [node[@type], node[@value]]

        # If we have a text on multiple lines and the text isn't supposed to be
        # inline, indent all the lines of the text
        if node[@value] == :text 
          if !inline
            value.gsub!(/^(?!$)/, indentation)
          else
            buffer_remove_trailing_newline
            value.strip!
          end
        else
          buffer_freeze indentation
        end
      end

      # Leading whitespace
      buffer_freeze ' ' if node[@options][:leading_whitespace]

      # Evaluate text node if it's marked as such and print nodes in the
      # current context
      if node[@value] == :text
        buffer_split_by_interpolation value, node[@options][:escaped]
      else
        node[@options][:escaped] ? buffer_escape(value) : buffer(value)
      end

      # Trailing whitespace
      buffer_freeze ' ' if node[@options][:trailing_whitespace]

      # Pretty print
      if @settings[:pretty]
        buffer_freeze "\n" if !inline or node[@value] == :print
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opulent-1.8.5 lib/opulent/compiler/text.rb
opulent-1.8.4 lib/opulent/compiler/text.rb
opulent-1.8.3 lib/opulent/compiler/text.rb