Class: CSSPrimer

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

Defined Under Namespace

Classes: MarkupFileError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CSSPrimer

Returns a new instance of CSSPrimer.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/css_primer.rb', line 27

def initialize(argv)

  @config = OpenStruct.new
  @config.HELP = HELP
  @config.DEFAULT_CSS_FILE_OUT = "primed_styles.css"

  @argv = argv

  @options = OpenStruct.new
  @options.verbose = false
  @options.quiet = false
  @options.sort = false

  @attributes = []

  @markup_file = ""
  @css_file = self.config("DEFAULT_CSS_FILE_OUT")

end

Instance Attribute Details

#css_fileObject

Returns the value of attribute css_file.



25
26
27
# File 'lib/css_primer.rb', line 25

def css_file
  @css_file
end

#markup_fileObject

Returns the value of attribute markup_file.



25
26
27
# File 'lib/css_primer.rb', line 25

def markup_file
  @markup_file
end

Instance Method Details

#prime!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/css_primer.rb', line 47

def prime!

  if self.parsed_options?

    begin

      self.log("Time to pwn...\n")

      # TODO: clean up regex, not tight enough (i.e. will match against inner text and javascript strings if present)
      parser = Proc.new { |full_path|

        self.log("\nOpening up #{full_path}\n") if @options.verbose

        self.read_in_file(full_path) do |line|

          line.scan(/class="((\s|\w|\-)*)"/) do |class_attribute|
            class_attribute[0].split(" ").each do |class_name|
              @attributes << [class_name, "."]
              self.log("Found class => #{class_name}") if @options.verbose
            end
          end

          line.scan(/id="((\w|\-)*)"/) do |id_attribute|
            @attributes << [id_attribute[0], "#"]
            self.log("Found id => #{id_attribute[0]}") if @options.verbose
          end

        end

      }

      if File.directory?(@markup_file)
        self.recurse_directories(@markup_file) do |full_path|
          parser.call(full_path) if full_path =~ /\.(html|xml)$/
        end
      else
        parser.call(@markup_file)
      end

      # TODO: do win or *nix line breaks
      buffer_array = @attributes.uniq
      buffer_array.sort! if @options.sort

      self.save(buffer_array.inject("") { |buffer, item| buffer << "#{item[1]}#{item[0]} {\n\n}\n\n" }, @css_file)

      puts "\nSaved to #{@css_file}\n\nLet's Roll!"

    rescue Exception => e
      self.handle_exception(e)
    end

  end

end