1

I have a CSS framework submodule in my Git repo that includes a bunch of README, component.json and other files. I don’t want to modify or delete the files because I’d imagine it’d cause problems when updates are pushed to the submodule. Yet Middleman wants to process them.

I currently have this in my config.rb file:

# Ignore everything that's not a CSS file inside inuit.css
ignore 'css/inuit.css/*.html'
ignore 'css/inuit.css/*.json'
ignore 'css/inuit.css/LICENSE'

How could I express this with a file pattern or a regex?

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
reneruiz
  • 2,436
  • 2
  • 20
  • 18
  • 1
    It would help if you didn't have folders with `.css` in their names. `_css` would be better. Then it's a matter of using a regex that matches `.css` at the end of the file name. Otherwise you need to check whether things like `inuit.css` are folders or files. – Shaz Aug 22 '13 at 20:44
  • 4
    Those aren't regular expressions. Those are file matching patterns, or fileglobs. – Andy Lester Aug 22 '13 at 20:46
  • @AndyLester According to [Middleman’s documentation](http://middlemanapp.com/dynamic-pages/index.html), “you can give `ignore` exact source paths, filename globs, or regexes.” – Rory O'Kane Aug 22 '13 at 20:49
  • It may allow you to ignore using regexes, but `css/inuit.css/*.html` is a filename glob, not a regex. – Andy Lester Aug 22 '13 at 20:51
  • @AndyLester Oh, I know that. I was just indicating that regexes *could* be used in this question. And the answer probably will use regexes, since they are more powerful than file globs. – Rory O'Kane Aug 22 '13 at 20:54
  • Here’s [a regex starting point with example file paths to test on](http://rubular.com/r/hFHTfSW8Er). That regex in that link matches *only* CSS files, the opposite of what we want. – Rory O'Kane Aug 22 '13 at 20:56

2 Answers2

1

Since ignore can take a regex, pass ignore a Ruby regex // instead of a string "" with a filename glob. In the regex, use negative lookahead (?!) and the end-of-string anchor $ to check that the filename doesn’t end in “.css”.

ignore /^ css\/inuit\.css\/ (?:  [^.]+  |  .+ \. (?!css) \w+   ) $/ix

This regex correctly handles all of these test cases:

  • Should match:
    • css/inuit.css/abc.html
    • css/inuit.css/thecssthing.json
    • css/inuit.css/sub/in_a_folder.html
    • css/inuit.css/sub/crazily.named.css.json
    • css/inuit.css/sub/crazily.css.named.json
    • css/inuit.css/LICENSE
  • Shouldn’t match:
    • css/inuit.css/realcss.css
    • css/inuit.css/main.css
    • css/inuit.css/sub/in_a_folder.css
    • css/inuit.css/sub/crazily.css.named.css
    • css/inuit.css/sub/crazily.named.css.css

The first alternation of the (?:) non-capturing group handles the case of files with no extension (no “.”). Otherwise, the second case checks that the last “.” in the path is not followed by “css”, which would indicate a “.css” extension.

I use the x flag to ignore whitespace in the regex, so that I can add spaces in the regex to make it clearer.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
1

I’m not familiar with Middleman, but doesn’t this work?

ignore /^css\/inuit\.css\/.*(?<![.]css)$/
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
jaeheung
  • 1,208
  • 6
  • 7
  • That also needs an `i` flag to that so it can handle “.CSS” files, but if you add `i` then yes, that should work – it [passes my test cases](http://rubular.com/r/93F65OcOt3). Good use of [negative lookbehind](http://www.regular-expressions.info/lookaround.html#lookbehind) `(?<!)`. – Rory O'Kane Aug 23 '13 at 14:17