Class: Kss::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/kss/parser.rb

Overview

Public: The main KSS parser. Takes a directory full of SASS / SCSS / CSS files and parses the KSS within them.

Constant Summary collapse

STYLEGUIDE_PATTERN =
(/(?<!No )Styleguide [[:alnum:]]/i).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths_or_strings) ⇒ Parser

Public: Initializes a new parser based on a directory of files or kss strings. Scans within the directory recursively or the strings for any comment blocks that look like KSS.

paths_or_strings - Each path String where style files are located, or each String containing KSS.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kss/parser.rb', line 15

def initialize(*paths_or_strings)
  @sections = {}

  paths_or_strings.each do |path_or_string|
    if Dir.exists?(path_or_string)
      # argument is a path
      path = path_or_string
      Dir["#{path}/**/*.{css,less,sass,scss}"].each do |filename|
        parser = CommentParser.new(filename)
        parser.blocks.each do |comment_block|
          add_section comment_block, filename if self.class.kss_block?(comment_block)
        end
      end
    else
      # argument is a KSS string
      kss_string = path_or_string
      parser = CommentParser.new(kss_string)
      parser.blocks.each do |comment_block|
        add_section comment_block if self.class.kss_block?(comment_block)
      end
    end
  end
end

Instance Attribute Details

#sectionsObject

Public: Returns a hash of Sections.



8
9
10
# File 'lib/kss/parser.rb', line 8

def sections
  @sections
end

Class Method Details

.kss_block?(cleaned_comment) ⇒ Boolean

Public: Takes a cleaned (no comment syntax like // or /* */) comment block and determines whether it is a KSS documentation block.

Returns a boolean indicating whether the block conforms to KSS.

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/kss/parser.rb', line 49

def self.kss_block?(cleaned_comment)
  return false unless cleaned_comment.is_a? String

  possible_reference = cleaned_comment.split("\n\n").last
  possible_reference =~ STYLEGUIDE_PATTERN
end

Instance Method Details

#add_section(comment_text, filename = '') ⇒ Object



39
40
41
42
43
# File 'lib/kss/parser.rb', line 39

def add_section comment_text, filename = ''
  base_name = File.basename(filename)
  section = Section.new(comment_text, base_name)
  @sections[section.section] = section
end

#section(reference) ⇒ Object

Public: Finds the Section for a given styleguide reference.

Returns a Section for a reference, or a blank Section if none found.



59
60
61
# File 'lib/kss/parser.rb', line 59

def section(reference)
  @sections[reference] || Section.new
end