magnars / stasis

Some Clojure functions for creating static websites.
348 stars 27 forks source link

Fix: handle backslashes in file names/paths to make Stasis work on Windows #8

Closed nauhygon closed 10 years ago

nauhygon commented 10 years ago

When I tried to use Stasis on my Windows box, I had the following error.

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
            Pattern.java:1924 java.util.regex.Pattern.error
            Pattern.java:1671 java.util.regex.Pattern.compile
            Pattern.java:1337 java.util.regex.Pattern.<init>
            Pattern.java:1022 java.util.regex.Pattern.compile
                core.clj:4377 clojure.core/re-pattern
                  core.clj:74 stasis.core/just-the-filename
......

After spending some time, I realized it was Windows' backslashes () in the file names or paths that caused it. In stasis.core/just-the-filename, it has (re-pattern (java.io.File/separator)), equivalent to (re-pattern "\\") on Windows, which causes the above Exception. It was also preventing the tests from passing on Windows.

So I made this fix by adding a new local fun normalize-path, which uses java.util.regex.Pattern/quote to safe quote the separator (backslashes on Windows). It normalizes the paths so backslashes (\\) are always converted to forward slashes (/). I also made a small fix in the Exception message in empty-directory! so all tests can pass.

I am pleased this fixed my problem on Windows, but feel free to discard it if you don't think it's the best way to address the issue. Thanks for making Stasis public.

magnars commented 10 years ago

Thanks for finding and fixing this issue. :-)

I notice that (normalize-path (.getPath ...)) is used together quite a lot. Maybe pull it into a local get-path function?

nauhygon commented 10 years ago

Thanks for the suggestion. Refactoring done. Tests passed on my Windows and Linux boxes.

magnars commented 10 years ago

Looking good! Thank you. :-)

magnars commented 10 years ago

1.0.1 released