Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- html/erb template support using htmlbeautify gem (assumed to be ins…

…talled)

- corrected file patterns in settings (. -> \\.)
- do not apply empty output and complain
- updated docs
  • Loading branch information...
commit cc174e224c4907215ba872224d7506535c52edb5 1 parent 2287a70
@zelig zelig authored
View
4 BeautifyRuby.sublime-settings
@@ -3,8 +3,8 @@
// The default is two spaces represented by 'space'
// anything else will use one tab character
"tab_or_space": "space",
- "ruby": "/usr/bin/env ruby",
- "file_patterns": [ ".rb", ".rake", "Rakefile", "Gemfile" ],
+ "ruby": "/usr/bin/env ruby"
+ "file_patterns": [ "\\.html\\.erb", "\\.rb", "\\.rake", "Rakefile", "Gemfile" ],
"run_on_save": false,
"save_on_beautify": true
}
View
28 README.md
@@ -1,5 +1,12 @@
+
#Update
+ 2013-01-11
+ @zelig adds erb template support
+
+ 2013-01-09
+ @zelig adds pre-save support
+
2012-09-30
@danryan adds file_patterns to settings file. Thanks!
@@ -53,6 +60,21 @@ Beautifies Ruby code. This plugin uses the [Ruby Script Beautifier](http://www.a
I made very little modification to get it to work with a Sublime Text 2 plugin.
+Erb html templates uses [Paul Beatley's htmlbeautify gem](https://github.com/threedaymonk/htmlbeautifier). This (as well as rubygems) is assumed to be installed as seen by the ruby interpreter.
+
+This package offers a pre-save hook, i.e., your ruby and erb files will be reformatted automatically before saving. To activate this feature, set:
+
+ "run_on_save": true,
+
+The sublime command "beautify_ruby" performs a save after formatting. You can disable this default by setting:
+
+ "save_on_beautify": false
+
+You can change the file patterns handled by this plugin in the settings:
+
+ "file_patterns": [ "\\.html\\.erb", "\\.rb", "\\.rake", "Rakefile", "Gemfile" ],
+
+
### Key Binding
```
@@ -75,6 +97,12 @@ On windows, set Preferences -> Package Settings -> BeautifyRuby -> Settings Defa
"ruby": "ruby"
```
+If you use project-specific rubies and gem sets managed with `rvm`, then simply set
+
+ "ruby": "~/.rvm/bin/rvm-auto-ruby",
+
+and then the `htmlbeautify` gem is found even if it is only installed for this project.
+
# Installation
### Package Control
View
27 beautify_ruby.py
@@ -31,10 +31,15 @@ def beautify_file(self):
subprocess.Popen(self.cmd(self.filename))
def beautify_buffer(self):
- beautifier = subprocess.Popen(self.cmd(), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+ working_dir = os.path.dirname(self.filename)
body = self.active_view.substr(self.buffer_region)
- out = beautifier.communicate(body)
- return out[0].decode('utf8')
+ beautifier = subprocess.Popen(self.cmd(), shell=True, cwd=working_dir, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+ out = beautifier.communicate(body)[0].decode('utf8')
+ if (out == "" and body != ""):
+ sublime.error_message("check your ruby interpreter settings")
+ return body
+ else:
+ return out
def update_view(self, contents):
edit = self.view.begin_edit()
@@ -59,19 +64,29 @@ def save_document_if_dirty(self):
def cmd(self, path = "-"):
ruby_interpreter = self.settings.get('ruby') or "/usr/bin/env ruby"
- ruby_script = os.path.join(sublime.packages_path(), 'BeautifyRuby', 'lib', 'rbeautify.rb')
+ if self.is_erb_file():
+ script_name = 'erbbeautify.rb'
+ else:
+ script_name = 'rbeautify.rb'
+ ruby_script = os.path.join(sublime.packages_path(), 'BeautifyRuby', 'lib', script_name)
args = ["'" + unicode(path) + "'"]
if self.settings.get('tab_or_space') != "space":
args.insert(0, '-t')
command = ruby_interpreter + " '" + ruby_script + "' " + ' '.join(args)
return command
+ def is_erb_file(self):
+ if re.search("\.html\.erb", self.fname):
+ return True
+ else:
+ return False
+
def is_ruby_file(self):
self.filename = self.view.window().active_view().file_name()
- fname = os.path.basename(self.filename)
+ self.fname = os.path.basename(self.filename)
file_patterns = self.settings.get('file_patterns') or ['.rb', '.rake']
patterns = re.compile(r'\b(?:%s)\b' % '|'.join(file_patterns))
- if patterns.search(fname):
+ if patterns.search(self.fname):
return True
else:
return False
View
9 lib/erbbeautify.rb
@@ -0,0 +1,9 @@
+require 'rubygems'
+require 'htmlbeautifier'
+
+def beautify(input, output)
+ HtmlBeautifier::Beautifier.new(output).scan(input)
+ output << "\n"
+end
+
+beautify $stdin.read, $stdout
Please sign in to comment.
Something went wrong with that request. Please try again.