在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:beautify-web/js-beautify开源软件地址:https://github.com/beautify-web/js-beautify开源编程语言:JavaScript 41.8%开源软件介绍:This little beautifier will reformat and re-indent bookmarklets, ugly JavaScript, unpack scripts packed by Dean Edward’s popular packer, as well as partly deobfuscate scripts processed by the npm package javascript-obfuscator. Open beautifier.io to try it out. Options are available via the UI. Contributors NeededI'm putting this front and center above because existing owners have very limited time to work on this project currently. This is a popular project and widely used but it desperately needs contributors who have time to commit to fixing both customer facing bugs and underlying problems with the internal design and implementation. If you are interested, please take a look at the CONTRIBUTING.md then fix an issue marked with the "Good first issue" label and submit a PR. Repeat as often as possible. Thanks! InstallationYou can install the beautifier for node.js or python. Node.js JavaScriptYou may install the NPM package $ npm -g install js-beautify
$ js-beautify foo.js You can also use $ npm install js-beautify Node.js JavaScript (vNext)The above install the latest stable release. To install beta or RC versions: $ npm install js-beautify@next Web LibraryThe beautifier can be added on your page as web library. JS Beautifier is hosted on two CDN services: cdnjs and rawgit. To pull the latest version from one of these services include one set of the script tags below in your document: <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.4/beautify-html.min.js"></script> Older versions are available by changing the version number. Disclaimer: These are free services, so there are no uptime or support guarantees. PythonTo install the Python version of the beautifier: $ pip install jsbeautifier Unlike the JavaScript version, the Python version can only reformat JavaScript. It does not work against HTML or CSS files, but you can install css-beautify for CSS: $ pip install cssbeautifier UsageYou can beautify javascript using JS Beautifier in your web browser, or on the command-line using node.js or python. Web BrowserOpen beautifier.io. Options are available via the UI. Web LibraryThe script tags above expose three functions: Node.js JavaScriptWhen installed globally, the beautifier provides an executable $ js-beautify foo.js To use The configuration option names are the same as the CLI names but with underscores instead of dashes. For example, var beautify = require('js-beautify').js,
fs = require('fs');
fs.readFile('foo.js', 'utf8', function (err, data) {
if (err) {
throw err;
}
console.log(beautify(data, { indent_size: 2, space_in_empty_paren: true }));
}); PythonAfter installing, to beautify using Python: $ js-beautify file.js Beautified output goes to To use import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js') ...or, to specify some options: opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.space_in_empty_paren = True
res = jsbeautifier.beautify('some javascript', opts) The configuration option names are the same as the CLI names but with underscores instead of dashes. The example above would be set on the command-line as OptionsThese are the command-line flags for both Python and JS scripts:
Which correspond to the underscored option keys for both library interfaces defaults per CLI options {
"indent_size": 4,
"indent_char": " ",
"indent_with_tabs": false,
"editorconfig": false,
"eol": "\n",
"end_with_newline": false,
"indent_level": 0,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"space_in_paren": false,
"space_in_empty_paren": false,
"jslint_happy": false,
"space_after_anon_function": false,
"space_after_named_function": false,
"brace_style": "collapse",
"unindent_chained_methods": false,
"break_chained_methods": false,
"keep_array_indentation": false,
"unescape_strings": false,
"wrap_line_length": 0,
"e4x": false,
"comma_first": false,
"operator_position": "before-newline",
"indent_empty_lines": false,
"templating": ["auto"]
} defaults not exposed in the cli {
"eval_code": false,
"space_before_conditional": true
} Notice not all defaults are exposed via the CLI. Historically, the Python and JS APIs have not been 100% identical. There are still a few other additional cases keeping us from 100% API-compatibility. Loading settings from environment or .jsbeautifyrc (JavaScript-Only)In addition to CLI arguments, you may pass config to the JS executable via:
Configuration sources provided earlier in this stack will override later ones. Setting inheritance and Language-specific overridesThe settings are a shallow tree whose values are inherited for all languages, but can be overridden. This works for settings passed directly to the API in either implementation. In the Javascript implementation, settings loaded from a config file, such as .jsbeautifyrc, can also use inheritance/overriding. Below is an example configuration tree showing all the supported locations
for language override nodes. We'll use {
"indent_size": 4,
"html": {
"end_with_newline": true,
"js": {
"indent_size": 2
},
"css": {
"indent_size": 2
}
},
"css": {
"indent_size": 1
},
"js": {
"preserve-newlines": true
}
} Using the above example would have the following result:
CSS & HTMLIn addition to the // Programmatic access
var beautify_js = require('js-beautify'); // also available under "js" export
var beautify_css = require('js-beautify').css;
var beautify_html = require('js-beautify').html;
// All methods accept two arguments, the string to be beautified, and an options object. The CSS & HTML beautifiers are much simpler in scope, and possess far fewer options.
DirectivesDirectives let you control the behavior of the Beautifier from within your source files. Directives are placed in comments inside the file. Directives are in the format Ignore directiveThe // Use ignore when the content is not parsable in the current language, JavaScript in this case.
var a = 1;
/* beautify ignore:start */
{This is some strange{template language{using open-braces?
/* beautify ignore:end */ Preserve directiveNOTE: this directive only works in HTML and JavaScript, not CSS. The The input below will remain unchanged after beautification: // Use preserve when the content is valid syntax in the current language, JavaScript in this case.
// This will parse the code and preserve the existing formatting.
/* beautify preserve:start */
{
browserName: 'internet explorer',
platform: 'Windows 7',
version: '8'
}
/* beautify preserve:end */ LicenseYou are free to use this in any way you want, in case you find this useful or working for you but you must keep the copyright notice and license. (MIT) Credits
Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull, Mathias Bynens, Vittorio Gambaletta and others. (README.md: [email protected]) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论