Closes #26. Add PHP support.

- Update README for setup and usage.
- Add PHP example code
- Tested on Mac
This commit is contained in:
Glavin Wiechert 2014-06-15 18:37:11 -03:00
parent 479034dae7
commit 1e16af9012
6 changed files with 157 additions and 32 deletions

View File

@ -9,14 +9,24 @@ Atom Package: https://atom.io/packages/atom-beautify
## Language Support
- JavaScript and JSON
- HTML, including
- [x] JavaScript and JSON
- [x] HTML, including
- [Handlebars](http://handlebarsjs.com/)
- XML
- CSS, including
- [x] CSS, including
- [Sass](http://sass-lang.com/)
- [LESS](http://lesscss.org/)
- SQL, special thanks to [pretty-data](https://github.com/vkiryukhin/pretty-data)
- [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 below for setup details with `atom-beautify`
### 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
## Usage
@ -37,6 +47,8 @@ You can also choose to beautify on every file save.
Edit your `.jsbeautifyrc` file in any of the following locations:
- Atom Package Settings
`Atom``Preferences` ➔ Search for `atom-beautify`
- Same directory as current file
- Project root
`atom-beautify` will recursively look up from the current file's directory to find `.jsbeautifyrc`.
@ -102,6 +114,18 @@ See [examples/nested-jsbeautifyrc/.jsbeautifyrc](https://github.com/donaldpipowi
}
```
## Advanced Language Setup
### PHP
To use with PHP we require [PHP_Beautifier](http://pear.php.net/package/PHP_Beautifier)
and that you set the `Php beautifier path` in the package settings.
#### Retrieve the path on Mac & Linux
Run `which php_beautifier` in your Terminal.
## Contributing
[See all contributors on GitHub](https://github.com/donaldpipowitch/atom-beautify/graphs/contributors).

View File

@ -0,0 +1,32 @@
<?php
$hello = "world";
if (!isset($_SESSION)) session_start();
require_once("sql.req.php");
// friend requests
$q = $mysqli->query("");
$num = $q->num_rows;
echo '<a href="notifications.php">';
if ($num>0) { echo '<i class="fa fa-star white"></i>';
}else { echo '<i class="fa fa-star-o"></i>'; }
echo '</a>';
// new messages
$q = $mysqli->query("");
$num = $q->num_rows;
echo '<a href="messages.php">';
if ($num>0) {
echo '<i class="fa fa-envelope white"></i>';
}else {
echo '<i class="fa fa-envelope-o"></i>';
}
echo '</a>';
?>

View File

@ -2,10 +2,7 @@
'use strict';
var beautifyJS = require('js-beautify');
var beautifyHTML = require('js-beautify').html;
var beautifyCSS = require('js-beautify').css;
var beautifySQL = require('./sql-beautify');
// Dependencies
var fs = require('fs');
var path = require('path');
var nopt = require('nopt');
@ -13,6 +10,12 @@ var extend = require('extend');
var _ = require('lodash');
var strip = require('strip-json-comments');
var yaml = require('js-yaml');
// Language Beautifiers
var beautifyJS = require('js-beautify');
var beautifyHTML = require('js-beautify').html;
var beautifyCSS = require('js-beautify').css;
var beautifySQL = require('./langs/sql-beautify');
var beautifyPHP = require('./langs/php-beautify');
// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
@ -155,8 +158,7 @@ function findConfig(config, file) {
// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
var languages = ['js', 'html', 'css', 'sql'];
var languages = ['js', 'html', 'css', 'sql', 'php'];
var defaultLanguageOptions = {
/* jshint ignore: start */
// JavaScript
@ -187,7 +189,9 @@ var defaultLanguageOptions = {
html_wrap_line_length: 250,
// SQL
sql_indent_size: 2,
sql_indent_char: ' '
sql_indent_char: ' ',
// PHP
php_beautifier_path: ''
/* jshint ignore: end */
};
@ -339,33 +343,8 @@ function beautify() {
return options;
}
switch (editor.getGrammar().name) {
case 'JSON':
// Treat JSON as JavaScript, because it will support comments.
// And Glavin001 has tested JSON beauifying with beautifyJS.
case 'JavaScript':
text = beautifyJS(text, getOptions('js', allOptions));
break;
case 'Handlebars':
defaultOptions.indent_handlebars = true; // jshint ignore: line
case 'HTML (Liquid)':
case 'HTML':
case 'XML':
text = beautifyHTML(text, getOptions('html', allOptions));
break;
case 'Sass':
case 'SCSS':
case 'LESS':
case 'CSS':
text = beautifyCSS(text, getOptions('css', allOptions));
break;
case 'SQL (Rails)':
case 'SQL':
text = beautifySQL(text, getOptions('sql', allOptions));
break;
default:
return;
}
// Asynchronously and callback-style
function beautifyCompleted(text) {
if (oldText !== text) {
var posArray = getCursors(editor);
var origScrollTop = editor.getScrollTop();
@ -387,6 +366,42 @@ function beautify() {
}
}
switch (editor.getGrammar().name) {
case 'JSON':
// Treat JSON as JavaScript, because it will support comments.
// And Glavin001 has tested JSON beauifying with beautifyJS.
case 'JavaScript':
text = beautifyJS(text, getOptions('js', allOptions));
beautifyCompleted(text);
break;
case 'Handlebars':
defaultOptions.indent_handlebars = true; // jshint ignore: line
case 'HTML (Liquid)':
case 'HTML':
case 'XML':
text = beautifyHTML(text, getOptions('html', allOptions));
beautifyCompleted(text);
break;
case 'Sass':
case 'SCSS':
case 'LESS':
case 'CSS':
text = beautifyCSS(text, getOptions('css', allOptions));
beautifyCompleted(text);
break;
case 'SQL (Rails)':
case 'SQL':
text = beautifySQL(text, getOptions('sql', allOptions));
beautifyCompleted(text);
break;
case 'PHP':
beautifyPHP(text, getOptions('php', allOptions), beautifyCompleted);
break;
default:
return;
}
}
function handleSaveEvent() {
atom.workspace.eachEditor(function (editor) {
var buffer = editor.getBuffer();

52
lib/langs/php-beautify.js Normal file
View File

@ -0,0 +1,52 @@
/**
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 (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.');
}
}
});
}
});
};

View File

@ -57,7 +57,8 @@
"sass",
"scss",
"less",
"sql"
"sql",
"php"
],
"engines": {
"atom": ">0.50.0"
@ -69,6 +70,7 @@
"nopt": "^3.0.0",
"lodash": "2.4.1",
"strip-json-comments": "^0.1.3",
"js-yaml": "^3.0.2"
"js-yaml": "^3.0.2",
"temp": "^0.8.0"
}
}