Last active
January 12, 2017 09:28
-
-
Save xream/519c29eff6e0760a51e673312a0f4c06 to your computer and use it in GitHub Desktop.
curl https://gist.githubusercontent.com/xream/519c29eff6e0760a51e673312a0f4c06/raw/a538f6904276fe3f4dcf7489195cc4875e043586/vue-beautifier.coffee -o ~/.atom/packages/atom-beautify/src/beautifiers/vue-beautifier.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
Beautifier = require('./beautifier') | |
prettydiff = require("prettydiff") | |
_ = require('lodash') | |
module.exports = class VueBeautifier extends Beautifier | |
name: "Vue Beautifier" | |
options: | |
Vue: true | |
beautify: (text, language, options) -> | |
opt = | |
brace_style: "collapse-preserve-inline" | |
end_with_comma: true | |
# end_with_newline: true | |
indent_size: 2 | |
max_preserve_newlines: 2 | |
# preserve_newlines: false | |
# insert_final_newline: false | |
_.assign options, opt | |
return new @Promise((resolve, reject) -> | |
regexp = /(<(template|script|style)[^>]*>)((\s|\S)*?)<\/\2>/gi | |
resolve(text.replace(regexp, (match, begin, type, text) -> | |
lang = /lang\s*=\s*['"](\w+)["']/.exec(begin)?[1] | |
switch type | |
when "template" | |
return match | |
switch lang | |
when "pug", "jade" | |
match.replace(text, "\n" + require("pug-beautify")(text, options) + "\n") | |
when undefined | |
match.replace(text, "\n" + require("js-beautify").html(text, options) + "\n") | |
else | |
match | |
when "script" | |
match.replace(text, "\n" + require("js-beautify")(text, options) + "\n") | |
when "style" | |
return match | |
switch lang | |
when "sass", "scss" | |
options = _.merge options, | |
source: text | |
lang: "scss" | |
mode: "beautify" | |
match.replace(text, prettydiff.api(options)[0]) | |
when "less" | |
options = _.merge options, | |
source: text | |
lang: "less" | |
mode: "beautify" | |
match.replace(text, prettydiff.api(options)[0]) | |
when undefined | |
match.replace(text, "\n" + require("js-beautify").css(text, options) + "\n") | |
else | |
match | |
)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment