Skip to content

Adding Additional Formatters

Nick Joodi edited this page Aug 15, 2013 · 10 revisions

Adding additional languages to this command-line formatter is relatively easy. To do this, navigate to bin/conf directory and there will be two files: extension.cfg and hashbang.cfg. The extension.cfg file is used to format files that have a particular extension. The hashbang.cfg file is used to format files that have a particular string in the first line of the source code. To add a formatter that will identify files with a particular file extension, open the extension.cfg file. Inside, there will be comments as well as two lines, one starting with "java" and the other starting with "groovy." These are the two formatters that have already been added to the program. Observe the line:

java = java -jar ${SCRIPT_LOCATION}/formatters/EclipseFormatter.jar -java

This line is telling the program to format all files with the extension, "java." This line is also telling the program to use the following command-line (after the equals sign) to format the file. The bash variable, "SCRIPT_LOCATION," was used to get the path of the "format" bash script file when it was ran. Use this variable if you decide to place the source code of the new formatter in a location that is relative to the "format" bash script. In the example above, the java formatter is located in the formatters folder relative to to the "format" bash script. To add a new formatter to this program, follow this outline. I.e. simply add the extension, an "=", followed by the command.

Now to format files based on the first line of code in the source rather than using the file extension, navigate yourself to the hashBang.cfg file. Inside you will see the same structure; however, instead of a file extension being used to indicate which files to format, enter in the contents that the first line of code the program must have. For instance, there is one formatter located in the file already. this formatter formats all files that have the first line containing "#!/usr/bin/env groovy".

A few requirements of the additional formatters you add:

  • The formatter that you add must take a file to format as the last argument on the command-line.
  • The formatters located in the hashBang.cfg file will only be used on files that have no file extension.
  • When adding to the extension.cfg file, make sure you do not include the "." with the extension.
  • Do not add any comments to the hashBang.cfg file.
  • Make sure the formatting command that you add will replace the input file with the new formatted file. That is, it will format the file that you entered in as an argument.
  • Make sure the command does not create a backup file. The program already has an option to do that.
  • A newline must be the last character of both config files.

Open Source Formatters

The following are command-line formatters that you can install and add to the project. We have not heavily tested them, but we have found them to be effective so far.

JS Beautifier

To install this software on your system:

  • Make sure to have node.js installed as well as the npm package manager. Link
  • Then follow the instructions in this README
  • To add this formatter to the command-line feature, simply uncomment the following line located in the the bin/conf/extension.cfg file:
js = js-beautify -r --replace

HTML Tidy

To install this software on your system:

  • If you are on a Mac OS X, your system most likely already has it installed. Enter the following command in the bash shell to make sure:
tidy -h
  • If you do not have tidy already installed, you can find the binaries [here] (http://tidy.sourceforge.net/#binaries "HTML Tidy")
  • To add this formatter to the command-line feature, simply uncomment the following line located in the the bin/conf/extension.cfg file:
html = tidy -i -m 
  • Uncomment the following line in the bin/conf/extension.cfg file to format XML
xml = tidy -i -m -xml
  • You can also download a java based version. I'd recommend installing this relative to the bin/format bash script by adding this source code to the bin/formatters folder.