Sha256: 075bcb1e7dd48aa6f388bd1e3749ad02c7f05dd3a27134c072013ec3d68b5bfc

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

module SlimLint
  # Represents a parsed Slim document and its associated metadata.
  class Document
    attr_reader :config, :file, :sexp, :source, :source_lines

    # Parses the specified Slim code into a {Document}.
    #
    # @param source [String] Slim code to parse
    # @param options [Hash]
    # @option file [String] file name of document that was parsed
    # @raise [Slim::Parser::Error] if there was a problem parsing the document
    def initialize(source, options)
      @config = options[:config]
      @file = options.fetch(:file, '(string)')

      process_source(source)
    end

    private

    # @param source [String] Slim code to parse
    # @raise [Slim::Parser::Error] if there was a problem parsing the document
    def process_source(source)
      @source = strip_frontmatter(source)
      @source_lines = @source.split("\n")

      @engine = SlimLint::Engine.new(file: @file)
      @sexp = @engine.call(source)
    end

    # Removes YAML frontmatter
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slim_lint-0.2.0 lib/slim_lint/document.rb
slim_lint-0.1.0 lib/slim_lint/document.rb