class DssParser

Constants

VERSION

Public Class Methods

new(stylesheet_path) click to toggle source
# File lib/dss_parser.rb, line 8
def initialize(stylesheet_path)
  @stylesheet_path = stylesheet_path
  @parsers = [DssParser::Parser::Name,
              DssParser::Parser::Description,
              DssParser::Parser::States,
              DssParser::Parser::Markup,
              DssParser::Parser::Variables,
              DssParser::Parser::Section]
end

Public Instance Methods

get_dss() click to toggle source
# File lib/dss_parser.rb, line 18
def get_dss
  comments = []

  find_css_files.each do |file_path|
    css_file = IO.read(file_path)
    comments |= parse_for_comments(css_file)
  end

  comments.map! { |c| build_dss(c) }
end
register_parser(parser) click to toggle source
# File lib/dss_parser.rb, line 29
def register_parser(parser)
  @parsers.push parser
end

Private Instance Methods

build_dss(comment) click to toggle source
# File lib/dss_parser.rb, line 44
def build_dss(comment)
  dss = []
  @parsers.each do |parser|
    parsed_content = parser.parse(comment)
    dss.push parsed_content unless parsed_content.nil?
  end
  OpenStruct.new(dss.reduce({}, :merge))
end
css_comment?(line) click to toggle source
# File lib/dss_parser.rb, line 110
def css_comment?(line)
  line.start_with?('/*','*/') && !line.end_with?('*/')
end
find_css_comments(lines) click to toggle source
# File lib/dss_parser.rb, line 82
def find_css_comments(lines)
  in_comment = false
  current_comment = []
  comment_blocks = []

  lines.each do |line_of_css|
    line_of_css.strip!

    if css_comment?(line_of_css) || in_comment == true
      in_comment = true
      current_comment.push line_of_css
      if line_of_css.end_with?('*/')
        in_comment = false
      end
    else
      in_comment = false
      comment_blocks.push current_comment if is_dss_comment?(current_comment)
      current_comment = []
    end
  end

  comment_blocks
end
find_css_files() click to toggle source
# File lib/dss_parser.rb, line 53
def find_css_files
  Dir.glob("#{@stylesheet_path}**/*.css*")
end
find_sass_comments(lines) click to toggle source
# File lib/dss_parser.rb, line 57
def find_sass_comments(lines)
  in_comment = false
  current_comment = []
  comment_blocks = []

  lines.each do |line_of_css|
    line_of_css.strip!

    if scss_comment?(line_of_css) || in_comment == true
      in_comment = true

      current_comment.push line_of_css
      unless line_of_css.start_with?('//')
        in_comment = false
      end
    else
      in_comment = false
      comment_blocks.push current_comment if is_dss_comment?(current_comment)
      current_comment = []
    end
  end

  comment_blocks
end
is_dss_comment?(comment) click to toggle source
# File lib/dss_parser.rb, line 106
def is_dss_comment?(comment)
  comment.any? { |line| line.downcase.include?("@name") || line.downcase.include?("@mixin") }
end
parse_for_comments(css) click to toggle source
# File lib/dss_parser.rb, line 35
def parse_for_comments(css)
  lines = css.split "\n"

  css_comments = find_css_comments(lines)
  sass_comments = find_sass_comments(lines)

  strip_whitespace(css_comments | sass_comments)
end
scss_comment?(line) click to toggle source
# File lib/dss_parser.rb, line 114
def scss_comment?(line)
  line.start_with? '//'
end
strip_whitespace(comment_blocks) click to toggle source
# File lib/dss_parser.rb, line 118
def strip_whitespace(comment_blocks)
  comment_blocks.each do |comment|
    comment.map!{ |c| c.gsub(/^\/\//, '') }
    comment.map!{ |c| c.gsub(/^\/\*/, '') }
    comment.map!{ |c| c.gsub(/^\*\//, '') }
    comment.map!{ |c| c.gsub(/^\*/, '') }
    comment.map!{ |c| c.strip }
    comment.reject!{ |c| c.size < 1 }
  end

  comment_blocks.reject{ |c| c.size < 1 }
end