Link to home
Start Free TrialLog in
Avatar of Donelson
Donelson

asked on

Wanted: a Menu command extension to auto-format Javascript in Dreamweaver

Does anyone know of a plug-in or extension (Mac OSX) for Dreamweaver 8 which will auto-format ("prettify", auto-indent, etc) my Javascript code in HTML pages.  I know that you can use:

Commands > Apple Source Formatting

but only for HTML portions of the page.  What I want is something to clean up and format, indent, etc, all the Javascript that I have on that page.

I do not want an External App to clean my Javascript; I want it built-into Dreamweaver as a command via a plug-in or similar.

Thanks.
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Hi Donelson,

It does not exist as a DW extension.  There are tons of third party apps that will do this and there are tons of extensions that will obfuscate js code, but nothing to tidy it.  Sorry.
Avatar of Donelson
Donelson

ASKER

I have seen Javascript formatters, for example:
---------------------------------

// javascript format script
// copyright Stephen Chapman 22 April 2007
var kw = ['abstract', 'as', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'long', 'namespace', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'use', 'var', 'void', 'volatile', 'while', 'with'];
var ob = ['Anchor', 'anchors', 'Applet', 'applets', 'Area', 'Array', 'Body', 'Button', 'Checkbox', 'Date', 'document', 'FileUpload', 'Form', 'forms', 'Frame', 'frames', 'Hidden', 'History', 'history', 'Image', 'images', 'Layer', 'layers', 'Link', 'links', 'location', 'Math', 'MimeType', 'mimeTypes', 'navigator', 'Number', 'Option', 'options', 'Password', 'Plugin', 'plugins', 'Radio', 'RegExp', 'Reset', 'screen', 'Script', 'Select', 'String', 'Style', 'StyleSheet', 'Submit', 'Text', 'Textarea', 'window'];
var sym = '-+*<=>?:&|/!%';
function JSfmt(s) {
   this.i = this.r = this.lvl = this.pr = 0;
   this.row = [''];
   this.lW = this.nC = this.pC = '';
   this.decode = function() {
      s = s.replace(/(\r\n|\r|\n)/g, '\n');
      while (this.i < s.length) {
         var c = s.charAt(this.i);
         if (s.length - 1 == this.i) this.nC = '';
         else this.nC = s.charAt(this.i + 1);
         if (/\w/.test(c)) {if (this.lW) this.lW += c;
         else this.lW = c;
         this.row[this.r] += c;
         }
      else switch (c) {
         case '\n': break;
         case ' ': case '\t': this.hl();
         this.row[this.r] += ' ';
         break;
         case '.': this.hl();
         this.row[this.r] += '.';
         break;
         case '{': this.hl();
         var currentLine = this.row[this.r];
         if (currentLine.length) {
            var lastChar = currentLine[currentLine.length - 1];
            if (lastChar != '&nbsp;' && lastChar != '\t') this.row[this.r] += ' {';
            else this.row[this.r] += '{';
            }
         else this.row[this.r] += '{';
         this.lvl++;
         this.wl();
         break;
         case '}': this.hl();
         this.lvl--;
         this.row[this.r] += '}';
         if (';' != this.nC) this.wl();
         break;
         case ';': this.hl();
         this.row[this.r] += '; ';
         if (this.pr == 0) this.wl();
         break;
         case '(': this.pr++;
         this.hl();
         this.row[this.r] += '(';
         break;
         case ')': this.pr--;
         this.hl();
         this.row[this.r] += ')';
         break;
         case '"': case "'" : this.hl();
         var escaped = false;
         this.row[this.r] += '<span class="literal">' + c;
         while (this.i < s.length - 1) {
            this.i++;
            var ch = s.charAt(this.i);
            if (ch == '\\') escaped = !escaped;
            if (ch == '&') ch = '&amp;';
            if (ch == '<') ch = '&lt;';
            if (ch == '>') ch = '&gt;';
            this.row[this.r] += ch;
            if (c == ch && !escaped) {
               this.row[this.r] += '<\/span>';
               break;
               }
            if (ch != '\\') escaped = false;
            }
         break;
         case '/': if ('/' == this.nC) {
            this.row[this.r] += '<span class="comment">//';
            this.i++;
            while (this.i < s.length - 1) {
               this.i++;
               var c = s.charAt(this.i);
               if (c == '&') c = '&amp;';
               if (c == '<') c = '&lt;';
               if (c == '>') c = '&gt;';
               if (c == '\n') {
                  this.row[this.r] += '<\/span>';
                  this.wl();
                  break;
                  }
               this.row[this.r] += c;
               }
            }
         else if (this.nC == '*') {
            this.row[this.r] += '<span class="comment">/*';
            this.i++;
            var c = '';
            var prevC = '';
            while (this.i < s.length - 1) {
               this.i++;
               prevC = c;
               c = s.charAt(this.i);
               if (c == '&nbsp;' || c == '\t' || c == '\n') {
                  if (c == '&nbsp;') {
                     if (this.row[this.r]) this.row[this.r] += '&nbsp;';
                     }
                  else if (c == '\t') {
                     if (this.row[this.r]) this.row[this.r] += '&nbsp;&nbsp; ';
                     }
                  else if (c == '\n') this.wl();
                  }
               else this.row[this.r] += c;
               if (c == '/' && prevC == '*') {
                  this.row[this.r] += '<\/span>';
                  break;
                  }
               }
            this.wl();
            }
         else if (this.lW) {
            this.hl();
            if (this.nC == '=') this.row[this.r] += ' /';
            else this.row[this.r] += ' / ';
            }
         else if (this.pC == '*') this.row[this.r] += '/ ';
         else if (this.pC == ')') this.row[this.r] += ' / ';
         else {
            if (this.pC == '=') this.row[this.r] += ' /';
            else this.row[this.r] += '/';
            while (this.i < s.length - 1) {
               this.i++;
               var c = s.charAt(this.i);
               if (c == '(') this.pr++;
               if (c == ')') this.pr--;
               if (c == '\\') escaped = !escaped;
               this.row[this.r] += c;
               if (c == ';' && this.pr == 0) {
                  this.wl();
                  break;
                  }
               if (c == '/' && this.pr == 0) if (!escaped) break;
               if (c != '\\') escaped = false;
               }
            }
         break;
         case ',': this.hl();
         this.row[this.r] += ', ';
         break;
         case '-': case '+': case '*': case '%': case '<': case '=': case '>': case '?': case ':': case '&': case '|': case '!': this.hl();
         if (c == '!' && this.nC != '=') {
            this.row[this.r] += c;
            break;
            }
         if (c == ':' && this.pC == "'") {
            this.row[this.r] += c;
            break;
            }
         if (c == '+' || c == '-') if (c == this.nC || c == this.pC) {
            this.row[this.r] += c;
            break;
            }
         if (sym.indexOf(this.pC) != - 1) {
            if (sym.indexOf(this.nC) != - 1) this.row[this.r] += c;
            else this.row[this.r] += c + ' ';
            }
         else {
            if (sym.indexOf(this.nC) != - 1) this.row[this.r] += ' ' + c;
            else this.row[this.r] += ' ' + c + ' ';
            }
         break;
         default : this.hl();
         this.row[this.r] += c;
         break;
         }
      if (!/\w/.test(c)) if (c != ' ' && c != '\t') {this.hl();
      this.lW = '';
      }
   this.pC = c;
   this.i++;
   }
return this.row.join('<br />');
}
this.hl = function() {
if (this.lW && kw.indexOf(this.lW) != - 1) {
   this.row[this.r] = this.row[this.r].substr(0, this.row[this.r].lastIndexOf(this.lW)) + '<span class="keyword">' + this.lW + '<\/span>';
   this.lW = '';
   }
else if (this.lW && ob.indexOf(this.lW) != - 1) {
   this.row[this.r] = this.row[this.r].substr(0, this.row[this.r].lastIndexOf(this.lW)) + '<span class="object">' + this.lW + '<\/span>';
   this.lW = '';
   }
}
this.wl = function() {
this.row.push('');
this.r++;
var i = 0;
while (i < this.lvl) {
   this.row[this.r] += '&nbsp;&nbsp; ';
   i++;
   }
}
}
if (typeof Array.prototype.indexOf == 'undefined') {
Array.prototype.indexOf = function(item) {
for (var i = 0; i < this.length; i++) {
   if ((typeof this[i] == typeof item) && (this[i] == item)) {
      return i;
      }
   }
return - 1;
}
}
function decode() {
var jsformat = new JSfmt(document.getElementById('in').value);
res(jsformat.decode());
}
function res(t) {
var mh = screen.height - 150;
var mw = screen.width - 20;
TheResWin = window.open('', 'format', 'height=' + mh + ',width=' + mw + ',toolbar=no,directories=no,status=no,' + 'menubar=no,scrollbars=yes,resizable=yes');
TheResWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\n<head><title>Results<\/title><style type="text/css">#out {font: normal 10pt Courier,"Courier New",monospace; background: #f5f5f5;}\n.keyword {color:#0000ff;}\n.object {color:#ff00ff;}\n.literal {color:#cc9966;}\n.comment {color:#999999;}\n</style><\/head>\n<body ><p align="right"><a href="#" onclick="self.close();return false;">Close Window<\/a><\/p><div id="out">' + t + '</div><p align="right"><a href="#" onclick="self.close();return false;">Close Window<\/a><\/p><\/body><\/html>');
TheResWin.document.close();
TheResWin.moveTo(0, 0);
TheResWin.focus();
}


How hard is it to write plug-in Commands for DW?  Can they be Javascript?


I note the "Get More Commands..." menu item in DW8...

ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How hard would it be?

1,000 points?  2,000 points? Cash ?  Would it have value when done?
>> How hard would it be?

I'm not sure.  I've never felt the need to write my own js extensions, especially for something that exists in so many other forms so I have no idea how much work would go into it.  I have written some menu extensions to get a command I use frequently to be more accessible and that was pretty simple...

>> 1,000 points?  2,000 points? Cash ?  Would it have value when done?

I doubt you find any takers willing to work for EE points.  Considering that it does not exist as an extension currently, that either means it would have no value (no demand) or potentially a lot of value as the only extension that does what it does on the market.  Would you be able to sell it?  Probably.  Would I buy it?  Nope.
Hmmm...

As shareware, I'd pay $10 for it, for example.

Whether others would or not: Probably some would, IF they knew about it....

Seems the easiest way would be to work from the Hilited selection on a page, or from the clipboard, then paste the results back into the page.

Shouldn't be too hard for someone who has written Commands for DW already...

Anyone?

Donelson,

No one here writes extensions for DW.  You are on your own.
Okay, thanks.

Let's leave this here in case someone comes along and can do it in future...

William

That's not really the way this site works.  You should close out now since your question has been answered.

EE is not Rent-A-Coder and you appear to be in the market for a coder for hire at this point.
So who gets the points?
There's only one person here who answered you...