JavaScript reformatter for Linux?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Kamens

    JavaScript reformatter for Linux?

    OK, so I've searched high and low for the answer to this
    question. Perhaps there is an answer that I just couldn't
    find because it was too hard to filter out the signal from
    the noise in the search results.

    I want a tool I can install on Linux that will reformat
    JavaScript. I've found the javascript-mode that comes with
    XEmacs and installed it with no trouble into my GNU Emacs
    installation, and I can use that to reindent, but I can't use
    that to format the code nicely if, e.g., all the extra
    newlines and whitespace were removed to reduce download time
    and obfuscate the code. I've also found that GNU indent can
    "mostly" indent JavaScript, but it has problems with missing
    semicolons, JavaScript string constants bounded by single
    quotes instead of double quotes, and JavaScript regular
    expressions operators.

    So, is there something for Linux specifically designed to
    take ugly JavaScript and make it pretty?

    Thanks in advance for any help you can provide. And I
    apologize if this question has been asked and answered; I
    simply can't find the answer, despite looking pretty hard for
    it.
  • Lasse Reichstein Nielsen

    #2
    Re: JavaScript reformatter for Linux?

    jik@kamens.broo kline.ma.us (Jonathan Kamens) writes:
    [color=blue]
    > I want a tool I can install on Linux that will reformat
    > JavaScript.[/color]

    Try Firefox :)
    E.g., put this in a page:
    ---
    <script type="text/javascript">
    function prettyScript(co de) {
    try {
    var fcode = (new Function(code)) .toString();
    fcode = fcode.replace(/^\s*function\s+ (?:\w+\(\)\s*)? \s*\{\s*\n/, "");
    fcode = fcode.replace(/\s*}\s*$/, "");
    return fcode;
    } catch (e) {
    return "SYNTAX ERROR: " + e.message;
    }
    }
    </script>
    <form action=""
    onsubmit="this. elements.output .value =
    prettyScript(th is.elements.inp ut.value);
    return false;">
    <textarea id="input" cols="80" rows="5">"code to format here"</textarea><br>
    <input type="submit" value="format"> <br>
    <textarea name="output" cols="80" rows="10" readonly="reado nly">Output here</textarea><br>
    </form>
    ---

    Nothing beats a Javascript parser for understanding the syntax of
    Javascript :)

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Jonathan Kamens

      #3
      Re: JavaScript reformatter for Linux?

      Thanks, that's cool, and I suppose I can use it if there are
      no other options, but I'm sort of hoping for a CLI-based tool,
      not something I have to use through a Web browser.

      Comment

      • Jim Ley

        #4
        Re: JavaScript reformatter for Linux?

        On Sun, 12 Jun 2005 20:27:55 +0000 (UTC), jik@kamens.broo kline.ma.us
        (Jonathan Kamens) wrote:
        [color=blue]
        >Thanks, that's cool, and I suppose I can use it if there are
        >no other options, but I'm sort of hoping for a CLI-based tool,
        >not something I have to use through a Web browser.[/color]

        The same code will run in any javascript command line interpreter too

        Jim.

        Comment

        • Danny

          #5
          Re: JavaScript reformatter for Linux?


          Define 'reformat', if you mean a code coloring tool like a syntax
          hightlight and such, I use Bluefish and my own regexp custom set for it,
          works nice, covers well most objects and many languages not just JS,
          though I name my set javascript_1. I'd send you my highlight file for
          bluefish with shortcuts and regexps, if that's what you mean. Bluefish
          does come with its own set but it isn't so great, fares well though.


          Danny


          On Sun, 12 Jun 2005 11:30:19 -0700, Jonathan Kamens
          <jik@kamens.bro okline.ma.us> wrote:
          [color=blue]
          > OK, so I've searched high and low for the answer to this
          > question. Perhaps there is an answer that I just couldn't
          > find because it was too hard to filter out the signal from
          > the noise in the search results.
          >
          > I want a tool I can install on Linux that will reformat
          > JavaScript. I've found the javascript-mode that comes with
          > XEmacs and installed it with no trouble into my GNU Emacs
          > installation, and I can use that to reindent, but I can't use
          > that to format the code nicely if, e.g., all the extra
          > newlines and whitespace were removed to reduce download time
          > and obfuscate the code. I've also found that GNU indent can
          > "mostly" indent JavaScript, but it has problems with missing
          > semicolons, JavaScript string constants bounded by single
          > quotes instead of double quotes, and JavaScript regular
          > expressions operators.
          >
          > So, is there something for Linux specifically designed to
          > take ugly JavaScript and make it pretty?
          >
          > Thanks in advance for any help you can provide. And I
          > apologize if this question has been asked and answered; I
          > simply can't find the answer, despite looking pretty hard for
          > it.[/color]



          --
          Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

          Comment

          • Jonathan Kamens

            #6
            Re: JavaScript reformatter for Linux?

            Danny <dann90038@blue bottle.com> writes:[color=blue]
            > Define 'reformat', if you mean a code coloring tool like a syntax
            >hightlight and such, I use Bluefish[/color]

            I'm not familiar with bluefish. What is it?

            By "reformat", I mean not just syntax highlighting, but rather
            inserting and removing line breaks, whitespace, indentation, etc. in
            the code to make it more readable.

            Comment

            • Jonathan Kamens

              #7
              Re: JavaScript reformatter for Linux?

              jim@jibbering.c om (Jim Ley) writes:[color=blue]
              >The same code will run in any javascript command line interpreter too[/color]

              It also discards comments. For my needs, that's a fatal flaw in this
              solution.

              Comment

              Working...