Class: CssDryer::Processor::StateMachine
- Defined in:
- lib/css_dryer/processor.rb
Overview
:nodoc:
Instance Method Summary collapse
- #act_on(input) ⇒ Object
-
#initialize(indent = 2) ⇒ StateMachine
constructor
A new instance of StateMachine.
- #result ⇒ Object
Constructor Details
#initialize(indent = 2) ⇒ StateMachine
Returns a new instance of StateMachine.
215 216 217 218 219 220 |
# File 'lib/css_dryer/processor.rb', line 215 def initialize(indent = 2) @state = 'flow' @depth = 0 @output = [] @indent = ' ' * indent end |
Instance Method Details
#act_on(input) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/css_dryer/processor.rb', line 224 def act_on(input) # Implementation notes: # - the correct way to do this would be to use a lexer and parser if @state.eql? 'flow' case input when %r{/[*]} # open comment @state = 'reading_comments' act_on input when /^[^,]*$/ # no commas @output << input when /,.*;\s*$/ # comma separated style values @output << input when /@media/ # @media block @output << input when /,/ # commas @state = 'reading_selectors' @selectors = [] @styles = [] act_on input end return elsif @state.eql? 'reading_comments' # Dodgy hack: remove commas from comments so that the # factor_out_comma_separated_selectors method doesn't # go into an infinite loop. @output << input.gsub(',', ' ') if input =~ %r{[*]/} # close comment @state = 'flow' end return elsif @state.eql? 'reading_selectors' if input !~ /[{]/ @selectors << extract_selectors(input) else @selectors << extract_selectors($`) @state = 'reading_styles' act_on input end return elsif @state.eql? 'reading_styles' case input when /\A[^{}]*\Z/ # no braces @styles << input when /\A[^,]*[{](.*)[}]/ # inline styles (no commas) @styles << (@depth == 0 ? $1 : input) when /[{](.*)[}]/ # inline styles (commas) @styles << $1 when /[{][^}]*\Z/ # open multiline block @styles << input unless @depth == 0 @depth += 1 when /[^{]*[}]/ # close multiline block @depth -= 1 @styles << input unless @depth == 0 end if @depth == 0 && input =~ /[}]/ @selectors.flatten.each { |selector| # Force each style declaration onto a new line. @output << "#{selector} {\n" @styles.each { |style| @output << "#{@indent}#{style.chomp.strip}\n" } @output << "}\n" } @state = 'flow' end return end end |
#result ⇒ Object
221 222 223 |
# File 'lib/css_dryer/processor.rb', line 221 def result @output.join end |