diff --git a/README.md b/README.md index f75b919..81052ee 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ Or Settings/Preferences ➔ Packages ➔ Search for `atom-beautify` - [x] XML - [x] SVG - [x] [Marko](https://github.com/marko-js/marko) - - Requires [language-marko](https://github.com/marko-js/atom-language-marko) - [x] CSS, including - [Sass](http://sass-lang.com/) - [Less](http://lesscss.org/) diff --git a/docs/add-languages-and-beautifiers.md b/docs/add-languages-and-beautifiers.md index 63de982..e10a062 100644 --- a/docs/add-languages-and-beautifiers.md +++ b/docs/add-languages-and-beautifiers.md @@ -26,7 +26,12 @@ Now your Language is available and can be detected and beautifiers can support i - Prettydiff is a good example of complex options: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/prettydiff.coffee - PHP-CS-Fixer is a good example of a CLI beautifier with arguments: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L15-L39 - `options` - the key represents the Language's name. The value could be `true` (supports all options), `false` (supports language, with no options), or an `object` whose keys are option keys and values are complex mappings. If you need to use these, let me know. `true` is probably what you want. - - The `beautify` function should return a `Promise` (use `@Promise` as shown). The arguments passed are: `text`, the source code from Atom's Text Editor, `language` is a string of the language's name (`JavaScript`), and `options` is an object of all of the options in their form as described by your Language definition (see `Configure the new language` above). + - The `beautify` function should return a `Promise` (use `@Promise` as shown). The arguments passed are: + - __`text`__ - the source code from Atom's Text Editor + - __`language`__ - the language's name (`JavaScript`) + - __`options`__ - an object of all of the options in their form as described by your Language definition (see `Configure the new language` above). + - __`context`__ - an object with extra information: + - __`filePath`__ - The file path associated with the text being beautified (may be null) 3. Add beautifier to list of `beautifierNames`: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/index.coffee#L34 4. Add test example files in https://github.com/Glavin001/atom-beautify/tree/master/examples - You can put your test in `nested-jsbeautifyrc` directory, https://github.com/Glavin001/atom-beautify/tree/master/examples/nested-jsbeautifyrc diff --git a/examples/nested-jsbeautifyrc/marko/expected/test.marko b/examples/nested-jsbeautifyrc/marko/expected/test.marko index 27e2187..89301a8 100644 --- a/examples/nested-jsbeautifyrc/marko/expected/test.marko +++ b/examples/nested-jsbeautifyrc/marko/expected/test.marko @@ -1,8 +1,6 @@ - - + +
${item.name}
- - content - \ No newline at end of file +content diff --git a/examples/nested-jsbeautifyrc/marko/original/test.marko b/examples/nested-jsbeautifyrc/marko/original/test.marko index 8bf706a..d9154ae 100644 --- a/examples/nested-jsbeautifyrc/marko/original/test.marko +++ b/examples/nested-jsbeautifyrc/marko/original/test.marko @@ -1,8 +1,6 @@ - - -
${item.name}
+ + +
${item.name}
- - content - +content \ No newline at end of file diff --git a/examples/simple-jsbeautifyrc/marko/expected/test.marko b/examples/simple-jsbeautifyrc/marko/expected/test.marko index 20e1097..9a6db41 100644 --- a/examples/simple-jsbeautifyrc/marko/expected/test.marko +++ b/examples/simple-jsbeautifyrc/marko/expected/test.marko @@ -1,6 +1,2 @@ - - content - - - content - \ No newline at end of file +content +content diff --git a/examples/simple-jsbeautifyrc/marko/original/test.marko b/examples/simple-jsbeautifyrc/marko/original/test.marko index 43459e8..528cbe3 100644 --- a/examples/simple-jsbeautifyrc/marko/original/test.marko +++ b/examples/simple-jsbeautifyrc/marko/original/test.marko @@ -1,6 +1,4 @@ - -content - - -content - +if(data.items) + - content +else + - content \ No newline at end of file diff --git a/package.json b/package.json index 96b5fc6..65574b9 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,10 @@ { "name": "Frederic Delbos", "url": "https://github.com/fdelbos" + }, + { + "name": "Patrick Steele-Idem", + "url": "https://github.com/psteeleidem" } ], "engines": { @@ -108,6 +112,7 @@ "jscs": "^3.0.3", "lodash": "^4.8.2", "loophole": "^1.0.0", + "marko-prettyprint": "^1.1.0", "node-dir": "^0.1.8", "node-uuid": "^1.4.3", "prettydiff": "^1.16.27", diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 3c15caf..5c97d32 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -66,6 +66,7 @@ module.exports = class Beautifiers extends EventEmitter 'typescript-formatter' 'yapf' 'erl_tidy' + 'marko-beautifier' ] ### @@ -276,7 +277,11 @@ module.exports = class Beautifiers extends EventEmitter # Beautify text with language options @emit "beautify::start" - beautifier.beautify(text, language.name, options) + + context = + filePath: filePath + + beautifier.beautify(text, language.name, options, context) .then(resolve) .catch(reject) .finally(=> diff --git a/src/beautifiers/js-beautify.coffee b/src/beautifiers/js-beautify.coffee index efc1e3b..17e323c 100644 --- a/src/beautifiers/js-beautify.coffee +++ b/src/beautifiers/js-beautify.coffee @@ -9,7 +9,6 @@ module.exports = class JSBeautify extends Beautifier XML: true Handlebars: true Mustache: true - Marko: true JavaScript: true JSON: true CSS: @@ -41,7 +40,7 @@ module.exports = class JSBeautify extends Beautifier beautifyHTML = require("js-beautify").html text = beautifyHTML(text, options) resolve text - when "HTML (Liquid)", "HTML", "XML", "Marko", "Web Form/Control (C#)", "Web Handler (C#)" + when "HTML (Liquid)", "HTML", "XML", "Web Form/Control (C#)", "Web Handler (C#)" beautifyHTML = require("js-beautify").html text = beautifyHTML(text, options) @debug("Beautified HTML: #{text}") diff --git a/src/beautifiers/marko-beautifier.coffee b/src/beautifiers/marko-beautifier.coffee new file mode 100644 index 0000000..6c29ee6 --- /dev/null +++ b/src/beautifiers/marko-beautifier.coffee @@ -0,0 +1,33 @@ +"use strict" +Beautifier = require('./beautifier') + +module.exports = class MarkoBeautifier extends Beautifier + name: 'Marko Beautifier' + + options: + Marko: true + + beautify: (text, language, options, context) -> + + return new @Promise((resolve, reject) -> + markoPrettyprint = require('marko-prettyprint') + + indent_char = options.indent_char || ' ' + indent_size = options.indent_size || 4 + + indent = '' + + for i in [0..indent_size - 1] + indent += indent_char + + prettyprintOptions = + syntax : options.syntax + filename: if context.filePath then context.filePath else require.resolve('marko-prettyprint') + indent: indent + + try + resolve(markoPrettyprint(text, prettyprintOptions)) + catch error + # Error occurred + reject(error) + ) diff --git a/src/languages/marko.coffee b/src/languages/marko.coffee index f8df162..e1db019 100644 --- a/src/languages/marko.coffee +++ b/src/languages/marko.coffee @@ -1,3 +1,11 @@ +# Get Atom defaults +scope = ['text.marko'] +tabLength = atom?.config.get('editor.tabLength', scope: scope) ? 4 +softTabs = atom?.config.get('editor.softTabs', scope: scope) ? true +defaultIndentSize = (if softTabs then tabLength else 4) +defaultIndentChar = (if softTabs then " " else "\t") +defaultIndentWithTabs = not softTabs + module.exports = { name: "Marko" @@ -18,6 +26,22 @@ module.exports = { "marko" ] - options: [] + options: + indent_size: + type: 'integer' + default: defaultIndentSize + minimum: 0 + description: "Indentation size/length" + indent_char: + type: 'string' + default: defaultIndentChar + description: "Indentation character" + syntax: + type: 'string' + default: "html" + enum: ["html", "concise"] + description: "[html|concise]" + + defaultBeautifier: "Marko Beautifier" }