开发者

alternative to file(1) for javascript/css mimetype

On win32 I'm using

开发者_高级运维
$ file -v
file-5.03
magic file from C:\PROGRA~1\gnuwin32/share/misc/magic

but its giving weird results

foo.css; text/x-c; charset=utf-8
json2.js; text/x-c; charset=us-ascii
foo.js; text/x-c++; charset=utf-8
bar.js; text/plain; charset=utf-8

Anyone get similar results? better results? Does anyone know of an alternative?

Thanks


I don't think the file(1) command can do this. The "file" command looks for magic strings in the file - e.g. the string "GIF89a" at the beginning of a GIF file. CSS and JS files aren't self-identifying like this, so you'd have to do something more complicated.

If you have access to the Content-Type header returned by the server, that would be a lot more reliable than trying to guess the type by looking at the file contents.

If you just have the file contents, you could do your own search for strings such as "function" or "var" that aren't likely to be in a CSS file. If you find them, it's probably JavaScript. That's an imperfect heuristic of course - there might be a background image named "function.png".

Going the other way would be more error-prone, since JavaScript can manipulate CSS styles... it wouldn't be unusual to find CSS properties like "text-decoration" in a JS file.

Or you could try running the file through a CSS or JS parser. If you get a ton of errors when parsing as CSS, it's probably not a CSS file :).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜