Muffinresearch Labs by Stuart Colville

Beautify JavaScript from TextMate or CLI | Comments (5)

Posted in Code, Software on 14th January 2008, 10:24 am by Stuart

As Einars has pointed out this functionality is part of the built-in JavaScript Bundle under “Reformat Document”. I’ll leave the rest of this post here as it does provide an example of how to set-up a command with an existing PHP script and if you’re not a TextMate user then this will work as a CLI script too and you can find more info on using PHP for CLI in TextMate here

I’ve just been reviewing some code that’s minned and I remembered looking at beautifier for JavaScript recently that was designed to convert minned code back to something more readable. The script I remembered was this one: http://elfz.laacz.lv/beautify/. The beautifier makes it really easy to dive into source to view code without having to resort to getting hold of the un-minned source. This is also especially useful when you don’t have access to the un-minned source.

By taking the PHP source and converting it to CLI, I was able to add a beautify JS command to TextMate.

Thanks very much to Einars "elfz" Lielmanis for making this script freely available it made it really easy to add this functionality to TextMate. My additions are highlighted.

#!/usr/bin/env php
<?php 

…See the main Beautify.js source here…

$fstat = fstat(STDIN);
$stdin = fread(STDIN,$fstat['size']);
echo js_beautify($stdin);

?>

The settings for the command are as follows:

Save:
Nothing
Input:
Selected Text or Document
Output:
Create New Document

With the above you can either build your own command or download this one pre-made: BeautifyJS.zip

Post Tools

Comments: Add yours

1. On January 14th, 2008 at 12:00 pm Robert Nyman said:

Nice Also, that must have been the longest block of code I’ve seen published for quite some time. :-)

2. On January 14th, 2008 at 12:41 pm Stuart Colville said:

@Robert: Hehe - probably this is a good example of a need for me to implement a show/hide link on the code example! For now you can view it as text. At least that makes it a tad more readable.

3. On January 14th, 2008 at 6:10 pm Einars said:

Glad you found it useful =)
Michael Sheets of Textmate contacted me last november about inclusion of the beautifier to the standart javascript bundle; don’t know how often do they release Textmate, but it’s possible it’s in there already (can’t check it, I’m not a macperson (yet), but at least i see it in the svn, http://macromates.com/svn/Bundles/trunk/Bundles/JavaScript.tmbundle/Support/lib/).

4. On January 15th, 2008 at 8:56 am Stuart Colville said:

@Einars: OK so I’ve just realised it’s in the JS menu under “reformat document.”

I’ll update the post accordingly!

5. On January 17th, 2008 at 9:23 am Javascript said:

[...] Beautify JavaScript from TextMate [...]







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Ubuntu: Turn off changing workspace with mouse wheel|(0)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Ubuntu: Mounting a TrueCrypt volume at startup or from the CLI|(0)

All you need is a mount point e.g: mkdir /media/ and then use the following: truecrypt /path/to/truecrypt/diskimage /media/. This is handy when you want to mount a truecrypt image at start-up or just do it quickly from a shell. It’s far more direct than using the GUI.

Photos on Flickr

© Copyright 2004-09 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.832s.