Skip to content

Commit

Permalink
Closes #21. Parsing .jsbeautifyrc falls back to YAML, when JSON fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Jun 14, 2014
1 parent 66933d9 commit 78e0382
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 61 deletions.
63 changes: 32 additions & 31 deletions examples/nested-jsbeautifyrc/.jsbeautifyrc
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"html": {
"brace_style": "collapse",
"indent_char": " ",
"indent_scripts": "normal",
"indent_size": 6,
"max_preserve_newlines": 1,
"preserve_newlines": true,
"unformatted": ["a", "sub", "sup", "b", "i", "u"],
"wrap_line_length": 0
},
"css": {
"indent_char": " ",
"indent_size": 4
},
"js": {
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true
},
"sql": {
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false
}
}
---
html:
brace_style: "collapse"
indent_char: " "
indent_scripts: "normal"
indent_size: 6
max_preserve_newlines: 1
preserve_newlines: true
unformatted:
- "a"
- "sub"
- "sup"
- "b"
- "i"
- "u"
wrap_line_length: 0
css:
indent_char: " "
indent_size: 4
js:
indent_size: 2
indent_char: " "
indent_level: 0
indent_with_tabs: false
preserve_newlines: true
max_preserve_newlines: 2
jslint_happy: true
sql:
indent_size: 4
indent_char: " "
indent_level: 0
indent_with_tabs: false
31 changes: 31 additions & 0 deletions examples/nested-jsbeautifyrc/.jsbeautifyrc_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"html": {
"brace_style": "collapse",
"indent_char": " ",
"indent_scripts": "normal",
"indent_size": 6,
"max_preserve_newlines": 1,
"preserve_newlines": true,
"unformatted": ["a", "sub", "sup", "b", "i", "u"],
"wrap_line_length": 0
},
"css": {
"indent_char": " ",
"indent_size": 4
},
"js": {
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true
},
"sql": {
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false
}
}
10 changes: 5 additions & 5 deletions examples/nested-jsbeautifyrc/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<html>

<head>
<title>Test Page</title>
<title>Test Page</title>
</head>

<body>
<h1>Hello</h1>
<p>
World!
</p>
<h1>Hello</h1>
<p>
World!
</p>
</body>

</html>
28 changes: 21 additions & 7 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var path = require('path');
var nopt = require('nopt');
var extend = require('extend');
var _ = require('lodash');
var strip = require('strip-json-comments');
var yaml = require('js-yaml');

// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
Expand Down Expand Up @@ -180,14 +182,26 @@ function beautify() {

var externalOptions;
if (configPath) {
var strip = require('strip-json-comments');
try {
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
encoding: 'utf8'
})));
} catch (e) {
console.log('Failed parsing config JSON at ' + configPath);
var contents = fs.readFileSync(configPath, {
encoding: 'utf8'
});
if (!contents) {
externalOptions = {};
} else {
try {
externalOptions = JSON.parse(strip(contents));
} catch (e) {
console.log('Failed parsing config as JSON: ' + configPath);

// Attempt as YAML
try {
externalOptions = yaml.safeLoad(contents);
} catch (e) {
console.log('Failed parsing config as YAML: ' + configPath);
externalOptions = {};
}

}
}
} else {
externalOptions = {};
Expand Down
43 changes: 25 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,29 @@
"email": "[email protected]",
"url": "https://github.com/donaldpipowitch"
},
"contributors": [{
"name": "László Károlyi",
"url": "https://github.com/karolyi"
}, {
"name": "Glavin Wiechert",
"email": "[email protected]",
"url": "https://github.com/Glavin001"
}, {
"name": "Marco Tanzi",
"url": "https://github.com/mtanzi"
}, {
"name": "gvn lazar suntop",
"url": "https://github.com/gvn"
}, {
"name": "Vadim K.",
"url": "https://github.com/vadirn"
}],
"contributors": [
{
"name": "László Károlyi",
"url": "https://github.com/karolyi"
},
{
"name": "Glavin Wiechert",
"email": "[email protected]",
"url": "https://github.com/Glavin001"
},
{
"name": "Marco Tanzi",
"url": "https://github.com/mtanzi"
},
{
"name": "gvn lazar suntop",
"url": "https://github.com/gvn"
},
{
"name": "Vadim K.",
"url": "https://github.com/vadirn"
}
],
"keywords": [
"atom",
"beautify",
Expand Down Expand Up @@ -62,6 +68,7 @@
"js-beautify": "~1.5.1",
"nopt": "^3.0.0",
"lodash": "2.4.1",
"strip-json-comments": "^0.1.3"
"strip-json-comments": "^0.1.3",
"js-yaml": "^3.0.2"
}
}

0 comments on commit 78e0382

Please sign in to comment.