Skip to content
This repository
Browse code

- 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
Viktor Trón authored
4  BeautifyRuby.sublime-settings
@@ -3,8 +3,8 @@
3 3
   // The default is two spaces represented by 'space'
4 4
   // anything else will use one tab character
5 5
   "tab_or_space": "space",
6  
-  "ruby": "/usr/bin/env ruby",
7  
-  "file_patterns": [ ".rb", ".rake", "Rakefile", "Gemfile" ],
  6
+  "ruby": "/usr/bin/env ruby"
  7
+  "file_patterns": [ "\\.html\\.erb", "\\.rb", "\\.rake", "Rakefile", "Gemfile" ],
8 8
   "run_on_save": false,
9 9
   "save_on_beautify": true
10 10
 }
28  README.md
Source Rendered
... ...
@@ -1,5 +1,12 @@
  1
+
1 2
 #Update
2 3
 
  4
+  2013-01-11
  5
+  @zelig adds erb template support
  6
+
  7
+  2013-01-09
  8
+  @zelig adds pre-save support
  9
+
3 10
   2012-09-30
4 11
   @danryan adds file_patterns to settings file. Thanks!
5 12
   
@@ -53,6 +60,21 @@ Beautifies Ruby code. This plugin uses the [Ruby Script Beautifier](http://www.a
53 60
 
54 61
 I made very little modification to get it to work with a Sublime Text 2 plugin.
55 62
 
  63
+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. 
  64
+
  65
+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:
  66
+
  67
+    "run_on_save": true,
  68
+
  69
+The sublime command "beautify_ruby" performs a save after formatting. You can disable this default by setting:
  70
+
  71
+    "save_on_beautify": false
  72
+
  73
+You can change the file patterns handled by this plugin in the settings:
  74
+
  75
+    "file_patterns": [ "\\.html\\.erb", "\\.rb", "\\.rake", "Rakefile", "Gemfile" ],   
  76
+
  77
+
56 78
 ### Key Binding
57 79
 
58 80
 ```
@@ -75,6 +97,12 @@ On windows, set Preferences -> Package Settings -> BeautifyRuby -> Settings Defa
75 97
   "ruby": "ruby"
76 98
 ```
77 99
 
  100
+If you use project-specific rubies and gem sets managed with `rvm`, then simply set
  101
+
  102
+      "ruby": "~/.rvm/bin/rvm-auto-ruby",
  103
+
  104
+and then the `htmlbeautify` gem is found even if it is only installed for this project.
  105
+
78 106
 # Installation
79 107
 
80 108
 ### Package Control
27  beautify_ruby.py
@@ -31,10 +31,15 @@ def beautify_file(self):
31 31
     subprocess.Popen(self.cmd(self.filename))
32 32
 
33 33
   def beautify_buffer(self):
34  
-    beautifier = subprocess.Popen(self.cmd(), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  34
+    working_dir = os.path.dirname(self.filename)
35 35
     body = self.active_view.substr(self.buffer_region)
36  
-    out = beautifier.communicate(body)
37  
-    return out[0].decode('utf8')
  36
+    beautifier = subprocess.Popen(self.cmd(), shell=True, cwd=working_dir, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  37
+    out = beautifier.communicate(body)[0].decode('utf8')
  38
+    if (out == "" and body != ""):
  39
+      sublime.error_message("check your ruby interpreter settings")
  40
+      return body
  41
+    else:
  42
+      return out
38 43
 
39 44
   def update_view(self, contents):
40 45
     edit = self.view.begin_edit()
@@ -59,19 +64,29 @@ def save_document_if_dirty(self):
59 64
 
60 65
   def cmd(self, path = "-"):
61 66
     ruby_interpreter = self.settings.get('ruby') or "/usr/bin/env ruby"
62  
-    ruby_script  = os.path.join(sublime.packages_path(), 'BeautifyRuby', 'lib', 'rbeautify.rb')
  67
+    if self.is_erb_file():
  68
+      script_name = 'erbbeautify.rb'
  69
+    else:
  70
+      script_name = 'rbeautify.rb'
  71
+    ruby_script  = os.path.join(sublime.packages_path(), 'BeautifyRuby', 'lib', script_name)
63 72
     args = ["'" + unicode(path) + "'"]
64 73
     if self.settings.get('tab_or_space') != "space":
65 74
       args.insert(0, '-t')
66 75
     command = ruby_interpreter + " '" + ruby_script + "' " + ' '.join(args)
67 76
     return command
68 77
 
  78
+  def is_erb_file(self):
  79
+    if re.search("\.html\.erb", self.fname):
  80
+      return True
  81
+    else:
  82
+      return False
  83
+
69 84
   def is_ruby_file(self):
70 85
     self.filename = self.view.window().active_view().file_name()
71  
-    fname         = os.path.basename(self.filename)
  86
+    self.fname         = os.path.basename(self.filename)
72 87
     file_patterns = self.settings.get('file_patterns') or ['.rb', '.rake']
73 88
     patterns = re.compile(r'\b(?:%s)\b' % '|'.join(file_patterns))
74  
-    if patterns.search(fname):
  89
+    if patterns.search(self.fname):
75 90
       return True
76 91
     else:
77 92
       return False
9  lib/erbbeautify.rb
... ...
@@ -0,0 +1,9 @@
  1
+require 'rubygems'
  2
+require 'htmlbeautifier'
  3
+
  4
+def beautify(input, output)
  5
+  HtmlBeautifier::Beautifier.new(output).scan(input)
  6
+  output << "\n"
  7
+end
  8
+
  9
+beautify $stdin.read, $stdout

0 notes on commit cc174e2

Please sign in to comment.
Something went wrong with that request. Please try again.