Closes #24. Add Python (PEP 8) support.
- Add reusable cli-beautify for external, non-Node beautifiers.
This commit is contained in:
parent
5796618ca3
commit
612c7dc17c
14
README.md
14
README.md
|
@ -19,11 +19,12 @@ Atom Package: https://atom.io/packages/atom-beautify
|
|||
- [x] SQL, special thanks to [pretty-data](https://github.com/vkiryukhin/pretty-data)
|
||||
- [x] [PHP](https://github.com/donaldpipowitch/atom-beautify/issues/26)
|
||||
- Requires [PHP_Beautifier](http://pear.php.net/package/PHP_Beautifier) to be already installed.
|
||||
- [See `PHP` under `Advanced Language Setup` for setup details](https://github.com/donaldpipowitch/atom-beautify#php)
|
||||
- [x] [Python](https://github.com/donaldpipowitch/atom-beautify/issues/24)
|
||||
- Requires [autopep8](https://github.com/hhatto/autopep8) to be already installed.
|
||||
- Beautifies to [PEP 8](http://legacy.python.org/dev/peps/pep-0008/).
|
||||
|
||||
### Coming Soon
|
||||
|
||||
- [ ] Python, see https://github.com/donaldpipowitch/atom-beautify/issues/24
|
||||
- [ ] Ruby, see https://github.com/donaldpipowitch/atom-beautify/issues/25
|
||||
- [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31
|
||||
|
||||
|
@ -125,6 +126,15 @@ and that you set the `Php beautifier path` in the package settings.
|
|||
|
||||
Run `which php_beautifier` in your Terminal.
|
||||
|
||||
### Python
|
||||
|
||||
To use with Python we require [autopep8](https://github.com/hhatto/autopep8)
|
||||
and that you set the `Python autopep8 path` in the package settings.
|
||||
|
||||
#### Retrieve the path on Mac & Linux
|
||||
|
||||
Run `which autopep8` in your Terminal.
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import math, sys;
|
||||
|
||||
def example1():
|
||||
####This is a long comment. This should be wrapped to fit within 72 characters.
|
||||
some_tuple=( 1,2, 3,'a' );
|
||||
some_variable={'long':'Long code lines should be wrapped within 79 characters.',
|
||||
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
|
||||
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
|
||||
20,300,40000,500000000,60000000000000000]}}
|
||||
return (some_tuple, some_variable)
|
||||
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
|
||||
class Example3( object ):
|
||||
def __init__ ( self, bar ):
|
||||
#Comments should have a space after the hash.
|
||||
if bar : bar+=1; bar=bar* bar ; return bar
|
||||
else:
|
||||
some_string = """
|
||||
Indentation in multiline strings should not be touched.
|
||||
Only actual code should be reindented.
|
||||
"""
|
||||
return (sys.path, some_string)
|
|
@ -16,6 +16,7 @@ var beautifyHTML = require('js-beautify').html;
|
|||
var beautifyCSS = require('js-beautify').css;
|
||||
var beautifySQL = require('./langs/sql-beautify');
|
||||
var beautifyPHP = require('./langs/php-beautify');
|
||||
var beautifyPython = require('./langs/python-beautify');
|
||||
|
||||
// TODO: Copied from jsbeautify, please update it from time to time
|
||||
var knownOpts = {
|
||||
|
@ -158,7 +159,7 @@ function findConfig(config, file) {
|
|||
|
||||
// Supported unique configuration keys
|
||||
// Used for detecting nested configurations in .jsbeautifyrc
|
||||
var languages = ['js', 'html', 'css', 'sql', 'php'];
|
||||
var languages = ['js', 'html', 'css', 'sql', 'php', 'python'];
|
||||
var defaultLanguageOptions = {
|
||||
/* jshint ignore: start */
|
||||
// JavaScript
|
||||
|
@ -191,7 +192,9 @@ var defaultLanguageOptions = {
|
|||
sql_indent_size: 2,
|
||||
sql_indent_char: ' ',
|
||||
// PHP
|
||||
php_beautifier_path: ''
|
||||
php_beautifier_path: '',
|
||||
// Python
|
||||
python_autopep8_path: ''
|
||||
/* jshint ignore: end */
|
||||
};
|
||||
|
||||
|
@ -397,6 +400,9 @@ function beautify() {
|
|||
case 'PHP':
|
||||
beautifyPHP(text, getOptions('php', allOptions), beautifyCompleted);
|
||||
break;
|
||||
case 'Python':
|
||||
beautifyPython(text, getOptions('python', allOptions), beautifyCompleted);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
Requires http://pear.php.net/package/PHP_Beautifier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var fs = require('fs');
|
||||
var temp = require('temp').track();
|
||||
var exec = require('child_process').exec;
|
||||
|
||||
module.exports = function (getCmd, isStdout) {
|
||||
|
||||
return function (text, options, callback) {
|
||||
// Create temp input file
|
||||
temp.open('input', function (err, info) {
|
||||
if (!err) {
|
||||
// Save current text to input file
|
||||
fs.write(info.fd, text || '');
|
||||
fs.close(info.fd, function (err) {
|
||||
if (!err) {
|
||||
// Create temp output file
|
||||
var outputPath = temp.path();
|
||||
var deleteOutputFile = function () {
|
||||
// Delete the output path
|
||||
fs.unlink(outputPath, function () {});
|
||||
};
|
||||
// Get the command
|
||||
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line
|
||||
if (cmd) {
|
||||
|
||||
var config = {
|
||||
env: process.env
|
||||
};
|
||||
|
||||
var isWin = /^win/.test(process.platform);
|
||||
if (!isWin) {
|
||||
// We need the $PATH to be correct when executing the command.
|
||||
// This should normalize the $PATH
|
||||
// by calling the external files that would usually
|
||||
// change the $PATH variable on user login.
|
||||
var $path = '[ -f ~/.bash_profile ] && source ~/.bash_profile;';
|
||||
$path += '[ -f ~/.bash_rc ] && source ~/.bash_rc;';
|
||||
// See http://stackoverflow.com/a/638980/2578205
|
||||
// for checking if file exists in Bash
|
||||
cmd = $path + cmd;
|
||||
}
|
||||
|
||||
// Execute and beautify!
|
||||
exec(cmd, config, function (err, stdout) {
|
||||
if (!err) {
|
||||
// Beautification has completed
|
||||
if (isStdout) {
|
||||
// Execute callback with resulting output text
|
||||
callback(stdout);
|
||||
deleteOutputFile();
|
||||
} else {
|
||||
// Read contents of output file
|
||||
fs.readFile(outputPath, 'utf8', function (err, newText) {
|
||||
// Execute callback with resulting output text
|
||||
callback(newText);
|
||||
deleteOutputFile();
|
||||
});
|
||||
}
|
||||
} else {
|
||||
deleteOutputFile();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('Beautify CLI command not valid.');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
};
|
|
@ -4,49 +4,16 @@ Requires http://pear.php.net/package/PHP_Beautifier
|
|||
|
||||
'use strict';
|
||||
|
||||
var fs = require('fs');
|
||||
var temp = require('temp').track();
|
||||
var exec = require('child_process').exec;
|
||||
var cliBeautify = require('./cli-beautify');
|
||||
|
||||
module.exports = function (text, options, callback) {
|
||||
// Create temp input file
|
||||
temp.open('input.php', function (err, info) {
|
||||
if (!err) {
|
||||
// Save current text to input file
|
||||
fs.write(info.fd, text);
|
||||
fs.close(info.fd, function (err) {
|
||||
if (!err) {
|
||||
// Create temp output file
|
||||
var outputPath = temp.path();
|
||||
var deleteOutputFile = function () {
|
||||
// Delete the output path
|
||||
fs.unlink(outputPath, function () {});
|
||||
};
|
||||
|
||||
var phpBeautifierPath = options.beautifier_path; // jshint ignore: line
|
||||
if (phpBeautifierPath) {
|
||||
// Beautify
|
||||
var config = {
|
||||
env: process.env
|
||||
};
|
||||
exec('php "' + phpBeautifierPath + '" "' + info.path + '" "' + outputPath + '"', config, function (err) {
|
||||
if (!err) {
|
||||
// Beautification has completed
|
||||
// Read contents of output file
|
||||
fs.readFile(outputPath, 'utf8', function (err, newText) {
|
||||
// Execute callback with resulting output text
|
||||
callback(newText);
|
||||
deleteOutputFile();
|
||||
});
|
||||
} else {
|
||||
deleteOutputFile();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('PHP Beautifier Path not set in Package settings.');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
function getCmd(inputPath, outputPath, options) {
|
||||
var phpBeautifierPath = options.beautifier_path; // jshint ignore: line
|
||||
if (phpBeautifierPath) {
|
||||
// Use absolute path
|
||||
return 'php "' + phpBeautifierPath + '" "' + inputPath + '" "' + outputPath + '"';
|
||||
} else {
|
||||
// Use command available in $PATH
|
||||
return 'php_beautifier "' + inputPath + '" "' + outputPath + '"';
|
||||
}
|
||||
}
|
||||
module.exports = cliBeautify(getCmd);
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
Requires https://github.com/hhatto/autopep8
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var cliBeautify = require('./cli-beautify');
|
||||
|
||||
function getCmd(inputPath, outputPath, options) {
|
||||
var path = options.autopep8_path; // jshint ignore: line
|
||||
if (path) {
|
||||
// Use absolute path
|
||||
return 'python "' + path + '" "' + inputPath + '"';
|
||||
} else {
|
||||
// Use command available in $PATH
|
||||
return 'autopep8 "' + inputPath + '"';
|
||||
}
|
||||
}
|
||||
var isStdin = true;
|
||||
module.exports = cliBeautify(getCmd, isStdin);
|
|
@ -58,7 +58,8 @@
|
|||
"scss",
|
||||
"less",
|
||||
"sql",
|
||||
"php"
|
||||
"php",
|
||||
"python"
|
||||
],
|
||||
"engines": {
|
||||
"atom": ">0.50.0"
|
||||
|
|
Loading…
Reference in New Issue