corrade-nucleus-nucleons – Blame information for rev 20
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 1 | # JS Beautifier |
2 | [![Build Status](https://img.shields.io/travis/beautify-web/js-beautify/master.svg)](http://travis-ci.org/beautify-web/js-beautify) |
||
3 | [![Build status](https://ci.appveyor.com/api/projects/status/5bxmpvew5n3e58te/branch/master?svg=true)](https://ci.appveyor.com/project/beautify-web/js-beautify/branch/master) |
||
4 | [![CDNJS version](https://img.shields.io/cdnjs/v/js-beautify.svg)](https://cdnjs.com/libraries/js-beautify) |
||
5 | [![NPM version](https://img.shields.io/npm/v/js-beautify.svg)](https://www.npmjs.com/package/js-beautify) |
||
6 | [![Download stats](https://img.shields.io/npm/dm/js-beautify.svg)](https://www.npmjs.com/package/js-beautify) |
||
7 | [![Join the chat at https://gitter.im/beautify-web/js-beautify](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/beautify-web/js-beautify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
||
8 | |||
9 | [![NPM stats](https://nodei.co/npm/js-beautify.svg?downloadRank=true&downloads=true)](https://www.npmjs.org/package/js-beautify) |
||
10 | |||
11 | |||
12 | This little beautifier will reformat and reindent bookmarklets, ugly |
||
13 | JavaScript, unpack scripts packed by Dean Edward’s popular packer, |
||
14 | as well as deobfuscate scripts processed by |
||
15 | [javascriptobfuscator.com](http://javascriptobfuscator.com/). |
||
16 | |||
17 | # Usage |
||
18 | You can beautify javascript using JS Beautifier in your web browser, or on the command-line using node.js or python. |
||
19 | |||
20 | JS Beautifier is hosted on two CDN services: [cdnjs](https://cdnjs.com/libraries/js-beautify) and rawgit. |
||
21 | |||
22 | To pull from one of these services include one set of the script tags below in your document: |
||
23 | ```html |
||
24 | <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.14/beautify.js"></script> |
||
25 | <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.14/beautify-css.js"></script> |
||
26 | <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.14/beautify-html.js"></script> |
||
27 | |||
28 | <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.14/beautify.min.js"></script> |
||
29 | <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.14/beautify-css.min.js"></script> |
||
30 | <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.14/beautify-html.min.js"></script> |
||
31 | |||
32 | <script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.6.14/js/lib/beautify.js"></script> |
||
33 | <script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.6.14/js/lib/beautify-css.js"></script> |
||
34 | <script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.6.14/js/lib/beautify-html.js"></script> |
||
35 | ``` |
||
36 | Disclaimer: These are free services, so there are [no uptime or support guarantees](https://github.com/rgrove/rawgit/wiki/Frequently-Asked-Questions#i-need-guaranteed-100-uptime-should-i-use-cdnrawgitcom). |
||
37 | |||
38 | ## Web Browser |
||
39 | Open [jsbeautifier.org](http://jsbeautifier.org/). Options are available via the UI. |
||
40 | |||
41 | ## Python |
||
42 | To beautify using python: |
||
43 | |||
44 | ```bash |
||
45 | $ pip install jsbeautifier |
||
46 | $ js-beautify file.js |
||
47 | ``` |
||
48 | |||
49 | Beautified output goes to `stdout`. |
||
50 | |||
51 | To use `jsbeautifier` as a library is simple: |
||
52 | |||
53 | ``` python |
||
54 | import jsbeautifier |
||
55 | res = jsbeautifier.beautify('your javascript string') |
||
56 | res = jsbeautifier.beautify_file('some_file.js') |
||
57 | ``` |
||
58 | |||
59 | ...or, to specify some options: |
||
60 | |||
61 | ``` python |
||
62 | opts = jsbeautifier.default_options() |
||
63 | opts.indent_size = 2 |
||
64 | res = jsbeautifier.beautify('some javascript', opts) |
||
65 | ``` |
||
66 | |||
67 | ## JavaScript |
||
68 | |||
69 | As an alternative to the Python script, you may install the NPM package `js-beautify`. When installed globally, it provides an executable `js-beautify` script. As with the Python script, the beautified result is sent to `stdout` unless otherwise configured. |
||
70 | |||
71 | ```bash |
||
72 | $ npm -g install js-beautify |
||
73 | $ js-beautify foo.js |
||
74 | ``` |
||
75 | |||
76 | You can also use `js-beautify` as a `node` library (install locally, the `npm` default): |
||
77 | |||
78 | ```bash |
||
79 | $ npm install js-beautify |
||
80 | ``` |
||
81 | |||
82 | ```js |
||
83 | var beautify = require('js-beautify').js_beautify, |
||
84 | fs = require('fs'); |
||
85 | |||
86 | fs.readFile('foo.js', 'utf8', function (err, data) { |
||
87 | if (err) { |
||
88 | throw err; |
||
89 | } |
||
90 | console.log(beautify(data, { indent_size: 2 })); |
||
91 | }); |
||
92 | ``` |
||
93 | |||
94 | ## Options |
||
95 | |||
96 | These are the command-line flags for both Python and JS scripts: |
||
97 | |||
98 | ```text |
||
99 | CLI Options: |
||
100 | -f, --file Input file(s) (Pass '-' for stdin) |
||
101 | -r, --replace Write output in-place, replacing input |
||
102 | -o, --outfile Write output to file (default stdout) |
||
103 | --config Path to config file |
||
104 | --type [js|css|html] ["js"] |
||
105 | -q, --quiet Suppress logging to stdout |
||
106 | -h, --help Show this help |
||
107 | -v, --version Show the version |
||
108 | |||
109 | Beautifier Options: |
||
110 | -s, --indent-size Indentation size [4] |
||
111 | -c, --indent-char Indentation character [" "] |
||
112 | -t, --indent-with-tabs Indent with tabs, overrides -s and -c |
||
113 | -e, --eol Character(s) to use as line terminators. |
||
114 | [first newline in file, otherwise "\n] |
||
115 | -n, --end-with-newline End output with newline |
||
116 | --editorconfig Use EditorConfig to set up the options |
||
117 | -l, --indent-level Initial indentation level [0] |
||
118 | -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables) |
||
119 | -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10] |
||
120 | -P, --space-in-paren Add padding spaces within paren, ie. f( a, b ) |
||
121 | -E, --space-in-empty-paren Add a single space inside empty paren, ie. f( ) |
||
122 | -j, --jslint-happy Enable jslint-stricter mode |
||
123 | -a, --space-after-anon-function Add a space before an anonymous function's parens, ie. function () |
||
124 | -b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline] |
||
125 | -B, --break-chained-methods Break chained method calls across subsequent lines |
||
126 | -k, --keep-array-indentation Preserve array indentation |
||
127 | -x, --unescape-strings Decode printable characters encoded in xNN notation |
||
128 | -w, --wrap-line-length Wrap lines at next opportunity after N characters [0] |
||
129 | -X, --e4x Pass E4X xml literals through untouched |
||
130 | --good-stuff Warm the cockles of Crockford's heart |
||
131 | -C, --comma-first Put commas at the beginning of new line instead of end |
||
132 | -O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline] |
||
133 | ``` |
||
134 | |||
135 | Which correspond to the underscored option keys for both library interfaces |
||
136 | |||
137 | **defaults per CLI options** |
||
138 | ```json |
||
139 | { |
||
140 | "indent_size": 4, |
||
141 | "indent_char": " ", |
||
142 | "indent_with_tabs": false, |
||
143 | "eol": "\n", |
||
144 | "end_with_newline": false, |
||
145 | "indent_level": 0, |
||
146 | "preserve_newlines": true, |
||
147 | "max_preserve_newlines": 10, |
||
148 | "space_in_paren": false, |
||
149 | "space_in_empty_paren": false, |
||
150 | "jslint_happy": false, |
||
151 | "space_after_anon_function": false, |
||
152 | "brace_style": "collapse", |
||
153 | "break_chained_methods": false, |
||
154 | "keep_array_indentation": false, |
||
155 | "unescape_strings": false, |
||
156 | "wrap_line_length": 0, |
||
157 | "e4x": false, |
||
158 | "comma_first": false, |
||
159 | "operator_position": "before-newline" |
||
160 | } |
||
161 | ``` |
||
162 | |||
163 | **defaults not exposed in the cli** |
||
164 | ```json |
||
165 | { |
||
166 | "eval_code": false, |
||
167 | "space_before_conditional": true |
||
168 | } |
||
169 | ``` |
||
170 | |||
171 | Notice not all defaults are exposed via the CLI. Historically, the Python and |
||
172 | JS APIs have not been 100% identical. For example, `space_before_conditional` is |
||
173 | currently JS-only, and not addressable from the CLI script. There are still a |
||
174 | few other additional cases keeping us from 100% API-compatibility. |
||
175 | |||
176 | |||
177 | ### Loading settings from environment or .jsbeautifyrc (JavaScript-Only) |
||
178 | |||
179 | In addition to CLI arguments, you may pass config to the JS executable via: |
||
180 | |||
181 | * any `jsbeautify_`-prefixed environment variables |
||
182 | * a `JSON`-formatted file indicated by the `--config` parameter |
||
183 | * a `.jsbeautifyrc` file containing `JSON` data at any level of the filesystem above `$PWD` |
||
184 | |||
185 | Configuration sources provided earlier in this stack will override later ones. |
||
186 | |||
187 | ### Setting inheritance and Language-specific overrides |
||
188 | |||
189 | The settings are a shallow tree whose values are inherited for all languages, but |
||
190 | can be overridden. This works for settings passed directly to the API in either implementation. |
||
191 | In the Javascript implementation, settings loaded from a config file, such as .jsbeautifyrc, |
||
192 | can also use inheritance/overriding. |
||
193 | |||
194 | Below is an example configuration tree showing all the supported locations |
||
195 | for language override nodes. We'll use `indent_size` to discuss how this configuration |
||
196 | would behave, but any number of settings can be inherited or overridden: |
||
197 | |||
198 | ```json |
||
199 | { |
||
200 | "indent_size": 4, |
||
201 | "html": { |
||
202 | "end_with_newline": true, |
||
203 | "js": { |
||
204 | "indent_size": 2 |
||
205 | }, |
||
206 | "css": { |
||
207 | "indent_size": 2 |
||
208 | } |
||
209 | }, |
||
210 | "css": { |
||
211 | "indent_size": 1 |
||
212 | }, |
||
213 | "js": { |
||
214 | "preserve-newlines": true |
||
215 | } |
||
216 | } |
||
217 | ``` |
||
218 | |||
219 | Using the above example would have the following result: |
||
220 | |||
221 | * HTML files |
||
222 | * Inherit `indent_size` of 4 spaces from the top-level setting. |
||
223 | * The files would also end with a newline. |
||
224 | * JavaScript and CSS inside HTML |
||
225 | * Inherit the HTML `end_with_newline` setting |
||
226 | * Override their indentation to 2 spaces |
||
227 | * CSS files |
||
228 | * Override the top-level setting to an `indent_size` of 1 space. |
||
229 | * JavaScript files |
||
230 | * Inherit `indent_size` of 4 spaces from the top-level setting |
||
231 | * Set `preserve-newlines` to `true` |
||
232 | |||
233 | ### CSS & HTML |
||
234 | |||
235 | In addition to the `js-beautify` executable, `css-beautify` and `html-beautify` |
||
236 | are also provided as an easy interface into those scripts. Alternatively, |
||
237 | `js-beautify --css` or `js-beautify --html` will accomplish the same thing, respectively. |
||
238 | |||
239 | ```js |
||
240 | // Programmatic access |
||
241 | var beautify_js = require('js-beautify'); // also available under "js" export |
||
242 | var beautify_css = require('js-beautify').css; |
||
243 | var beautify_html = require('js-beautify').html; |
||
244 | |||
245 | // All methods accept two arguments, the string to be beautified, and an options object. |
||
246 | ``` |
||
247 | |||
248 | The CSS & HTML beautifiers are much simpler in scope, and possess far fewer options. |
||
249 | |||
250 | ```text |
||
251 | CSS Beautifier Options: |
||
252 | -s, --indent-size Indentation size [4] |
||
253 | -c, --indent-char Indentation character [" "] |
||
254 | -t, --indent-with-tabs Indent with tabs, overrides -s and -c |
||
255 | -e, --eol Character(s) to use as line terminators. (default newline - "\\n") |
||
256 | -n, --end-with-newline End output with newline |
||
257 | -L, --selector-separator-newline Add a newline between multiple selectors |
||
258 | -N, --newline-between-rules Add a newline between CSS rules |
||
259 | |||
260 | HTML Beautifier Options: |
||
261 | -s, --indent-size Indentation size [4] |
||
262 | -c, --indent-char Indentation character [" "] |
||
263 | -t, --indent-with-tabs Indent with tabs, overrides -s and -c |
||
264 | -e, --eol Character(s) to use as line terminators. (default newline - "\\n") |
||
265 | -n, --end-with-newline End output with newline |
||
266 | -p, --preserve-newlines Preserve existing line-breaks (--no-preserve-newlines disables) |
||
267 | -m, --max-preserve-newlines Maximum number of line-breaks to be preserved in one chunk [10] |
||
268 | -I, --indent-inner-html Indent <head> and <body> sections. Default is false. |
||
269 | -b, --brace-style [collapse-preserve-inline|collapse|expand|end-expand|none] ["collapse"] |
||
270 | -S, --indent-scripts [keep|separate|normal] ["normal"] |
||
271 | -w, --wrap-line-length Maximum characters per line (0 disables) [250] |
||
272 | -A, --wrap-attributes Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline] ["auto"] |
||
273 | -i, --wrap-attributes-indent-size Indent wrapped attributes to after N characters [indent-size] (ignored if wrap-attributes is "force-aligned") |
||
274 | -U, --unformatted List of tags (defaults to inline) that should not be reformatted |
||
275 | -T, --content_unformatted List of tags (defaults to pre) that its content should not be reformatted |
||
276 | -E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline before them. |
||
277 | --editorconfig Use EditorConfig to set up the options |
||
278 | ``` |
||
279 | |||
280 | ## Directives to Ignore or Preserve sections (Javascript only) |
||
281 | |||
282 | Beautifier for supports directives in comments inside the file. |
||
283 | This allows you to tell the beautifier to preserve the formatting of or completely ignore part of a file. |
||
284 | The example input below will remain changed after beautification |
||
285 | |||
286 | ```js |
||
287 | // Use preserve when the content is not javascript, but you don't want it reformatted. |
||
288 | /* beautify preserve:start */ |
||
289 | { |
||
290 | browserName: 'internet explorer', |
||
291 | platform: 'Windows 7', |
||
292 | version: '8' |
||
293 | } |
||
294 | /* beautify preserve:end */ |
||
295 | |||
296 | // Use ignore when the content is not parsable as javascript. |
||
297 | var a = 1; |
||
298 | /* beautify ignore:start */ |
||
299 | {This is some strange{template language{using open-braces? |
||
300 | /* beautify ignore:end */ |
||
301 | ``` |
||
302 | |||
303 | # License |
||
304 | |||
305 | You are free to use this in any way you want, in case you find this |
||
306 | useful or working for you but you must keep the copyright notice and license. (MIT) |
||
307 | |||
308 | # Credits |
||
309 | |||
310 | * Created by Einar Lielmanis, <einar@jsbeautifier.org> |
||
311 | * Python version flourished by Stefano Sanfilippo <a.little.coder@gmail.com> |
||
312 | * Command-line for node.js by Daniel Stockman <daniel.stockman@gmail.com> |
||
313 | * Maintained and expanded by Liam Newman <bitwiseman@gmail.com> |
||
314 | |||
315 | Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave |
||
316 | Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull, |
||
317 | Mathias Bynens, Vittorio Gambaletta and others. |
||
318 | |||
319 | (README.md: js-beautify@1.6.14) |