class SlimLint::Document

Represents a parsed Slim document and its associated metadata.

Attributes

config[R]

@return [SlimLint::Configuration] Configuration used to parse template

file[R]

@return [String] Slim template file path

sexp[R]

@return [SlimLint::Sexp] Sexpression representing the parsed document

source[R]

@return [String] original source code

source_lines[R]

@return [Array<String>] original source code as an array of lines

Public Class Methods

new(source, options) click to toggle source

Parses the specified Slim code into a {Document}.

@param source [String] Slim code to parse @param options [Hash] @option options :file [String] file name of document that was parsed @raise [Slim::Parser::Error] if there was a problem parsing the document

# File lib/slim_lint/document.rb, line 27
def initialize(source, options)
  @config = options[:config]
  @file = options.fetch(:file, nil)

  process_source(source)
end

Private Instance Methods

process_encoding(source) click to toggle source

Ensure the string's encoding is valid.

@param source [String] @return [String] source encoded in a valid encoding

# File lib/slim_lint/document.rb, line 51
def process_encoding(source)
  ::Temple::Filters::Encoding.new.call(source)
end
process_source(source) click to toggle source

@param source [String] Slim code to parse @raise [SlimLint::Exceptions::ParseError] if there was a problem parsing the document

# File lib/slim_lint/document.rb, line 38
def process_source(source)
  @source = process_encoding(source)
  @source = strip_frontmatter(source)
  @source_lines = @source.split("\n")

  engine = SlimLint::Engine.new(file: @file)
  @sexp = engine.parse(@source)
end
strip_frontmatter(source) click to toggle source

Removes YAML frontmatter

# File lib/slim_lint/document.rb, line 56
def strip_frontmatter(source)
  if config['skip_frontmatter'] &&
    source =~ /
      # From the start of the string
      \A
      # First-capture match --- followed by optional whitespace up
      # to a newline then 0 or more chars followed by an optional newline.
      # This matches the --- and the contents of the frontmatter
      (---\s*\n.*?\n?)
      # From the start of the line
      ^
      # Second capture match --- or ... followed by optional whitespace
      # and newline. This matches the closing --- for the frontmatter.
      (---|\.\.\.)\s*$\n?/mx
    source = $POSTMATCH
  end

  source
end