|
4 | 4 |
|
5 | 5 | [](https://github.com/aliariff/vscode-erb-beautify/actions/workflows/release.yaml)
|
6 | 6 |
|
7 |
| -Most of solution that exist in the internet is tell you to create a task and call it using ctrl-shift-p menu. |
| 7 | +## Overview |
8 | 8 |
|
9 |
| -This extension basically using [htmlbeautifier](https://github.com/threedaymonk/htmlbeautifier) to format your file using the Formatter API from the vscode, so no need to create a hack using Task, etc. |
| 9 | +The **ERB Formatter/Beautify** extension for Visual Studio Code uses the [htmlbeautifier](https://github.com/threedaymonk/htmlbeautifier) gem to format ERB files, providing a seamless experience with the VS Code Formatter API. Unlike other solutions that require setting up tasks or manual formatting, this extension integrates directly with VS Code to format your ERB files automatically. |
10 | 10 |
|
11 | 11 | ## Features
|
12 | 12 |
|
| 13 | +- Formats ERB files using `htmlbeautifier`. |
| 14 | +- Works with the VS Code Formatter API for a native experience. |
| 15 | +- Supports custom configuration options for indentation, blank lines, and error handling. |
| 16 | +- Ability to ignore specific files or patterns. |
| 17 | + |
13 | 18 | 
|
14 | 19 |
|
15 |
| -## Requirements |
| 20 | +## Installation |
16 | 21 |
|
17 |
| -``` |
| 22 | +### Requirements |
| 23 | + |
| 24 | +To use this extension, you must have `htmlbeautifier` installed on your system. You can install it globally or add it to your project's Gemfile. |
| 25 | + |
| 26 | +#### Install Globally |
| 27 | + |
| 28 | +```bash |
18 | 29 | gem install htmlbeautifier
|
19 | 30 | ```
|
20 | 31 |
|
21 |
| -or |
| 32 | +#### Install with Bundler |
22 | 33 |
|
23 |
| -To use the gem with Bundler, add to your `Gemfile`: |
| 34 | +Add the following to your `Gemfile`: |
24 | 35 |
|
25 | 36 | ```ruby
|
26 | 37 | gem 'htmlbeautifier'
|
27 | 38 | ```
|
28 | 39 |
|
29 |
| -NOTE: For you that have a filename with extension `.html.erb`, your file might be recognized as `html` file, not as `erb` file. In that case, add a setting in your `settings.json` like below: |
| 40 | +Then run: |
| 41 | + |
| 42 | +```bash |
| 43 | +bundle install |
| 44 | +``` |
| 45 | + |
| 46 | +## Configuration |
| 47 | + |
| 48 | +### Resolving File Recognition Issues |
| 49 | + |
| 50 | +If `.html.erb` files are recognized as HTML instead of ERB, add the following to your `settings.json` to associate `.html.erb` files with the ERB language: |
| 51 | + |
| 52 | +```json |
| 53 | +"files.associations": { |
| 54 | + "*.html.erb": "erb" |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Setting Default Formatter and Enabling Format-on-Save |
| 59 | + |
| 60 | +To set the default formatter for ERB files and enable format-on-save, add the following to your `settings.json`: |
30 | 61 |
|
31 | 62 | ```json
|
32 | 63 | "[erb]": {
|
33 | 64 | "editor.defaultFormatter": "aliariff.vscode-erb-beautify",
|
34 | 65 | "editor.formatOnSave": true
|
35 |
| -}, |
36 |
| -"files.associations": { |
37 |
| - "*.html.erb": "erb" |
38 | 66 | }
|
39 | 67 | ```
|
40 | 68 |
|
41 |
| -## Known Issues |
| 69 | +This ensures that the extension formats ERB files automatically whenever they are saved. |
42 | 70 |
|
43 |
| -- `invalid byte sequence in US-ASCII` |
| 71 | +### Disabling Formatting for Specific Files |
44 | 72 |
|
45 |
| - Add below setting. [Reference](https://github.com/aliariff/vscode-erb-beautify/issues/47) |
| 73 | +To disable formatting for specific ERB files, such as email templates, use the `ignoreFormatFilePatterns` setting. Add the following to your `settings.json`: |
46 | 74 |
|
47 |
| - ```json |
48 |
| - "vscode-erb-beautify.customEnvVar": { |
49 |
| - "LC_ALL": "en_US.UTF-8" |
50 |
| - } |
51 |
| - ``` |
| 75 | +```json |
| 76 | +"vscode-erb-beautify.ignoreFormatFilePatterns": ["**/email_templates/**/*.erb"] |
| 77 | +``` |
52 | 78 |
|
53 |
| -## Settings |
| 79 | +This configuration ignores all `.erb` files inside the `email_templates` directory. |
54 | 80 |
|
55 |
| -| Setting | Description | Default | |
56 |
| -| ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------- | -------------- | |
57 |
| -| `vscode-erb-beautify.executePath` | Path to the htmlbeautifier executable, set this to absolute path when you have different htmlbeautifier location | htmlbeautifier | |
58 |
| -| `vscode-erb-beautify.useBundler` | Execute htmlbeautifier using bundler (ie 'bundle exec htmlbeautifier'). If this true, vscode-erb-beautify.executePath is ignored | false | |
59 |
| -| `vscode-erb-beautify.bundlerPath` | Path to the bundler executable, set this to absolute path when you have different bundler location | bundle | |
60 |
| -| `vscode-erb-beautify.tabStops` | Set number of spaces per indent | 2 | |
61 |
| -| `vscode-erb-beautify.tab` | Indent using tabs | false | |
62 |
| -| `vscode-erb-beautify.indentBy` | Indent the output by NUMBER steps | 0 | |
63 |
| -| `vscode-erb-beautify.stopOnErrors` | Stop when invalid nesting is encountered in the input | false | |
64 |
| -| `vscode-erb-beautify.keepBlankLines` | Set number of consecutive blank lines | 0 | |
65 |
| -| `vscode-erb-beautify.customEnvVar` | Custom environment variables to pass to the htmlbeautifier | {} | |
| 81 | +### Fixing Encoding Issues |
66 | 82 |
|
67 |
| -## References |
| 83 | +If you encounter the `Invalid byte sequence in US-ASCII` error, add the following setting to your `settings.json` to set the correct locale: |
68 | 84 |
|
69 |
| -[Issue](https://github.com/threedaymonk/htmlbeautifier/issues/49) |
| 85 | +```json |
| 86 | +"vscode-erb-beautify.customEnvVar": { |
| 87 | + "LC_ALL": "en_US.UTF-8" |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +For more details, see the [related issue](https://github.com/aliariff/vscode-erb-beautify/issues/47). |
70 | 92 |
|
71 |
| -[Issue](https://github.com/rubyide/vscode-ruby/issues/56) |
| 93 | +## Settings |
| 94 | + |
| 95 | +Below is a list of settings you can configure in your `settings.json` file: |
| 96 | + |
| 97 | +| Setting | Description | Default | |
| 98 | +| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------- | |
| 99 | +| `vscode-erb-beautify.executePath` | Path to the `htmlbeautifier` executable. Set this to an absolute path if `htmlbeautifier` is installed in a non-standard location. | `htmlbeautifier` | |
| 100 | +| `vscode-erb-beautify.useBundler` | Execute `htmlbeautifier` using Bundler (e.g., `bundle exec htmlbeautifier`). If true, `vscode-erb-beautify.executePath` is ignored. | `false` | |
| 101 | +| `vscode-erb-beautify.bundlerPath` | Path to the Bundler executable. Set this to an absolute path if Bundler is installed in a non-standard location. | `bundle` | |
| 102 | +| `vscode-erb-beautify.tabStops` | Number of spaces per indent. | `2` | |
| 103 | +| `vscode-erb-beautify.tab` | Indent using tabs instead of spaces. | `false` | |
| 104 | +| `vscode-erb-beautify.indentBy` | Indent the output by a specified number of steps. | `0` | |
| 105 | +| `vscode-erb-beautify.stopOnErrors` | Stop formatting when invalid nesting is encountered in the input. | `false` | |
| 106 | +| `vscode-erb-beautify.keepBlankLines` | Number of consecutive blank lines to keep in the formatted output. | `0` | |
| 107 | +| `vscode-erb-beautify.customEnvVar` | Custom environment variables to pass to `htmlbeautifier`. | `{}` | |
| 108 | +| `vscode-erb-beautify.ignoreFormatFilePatterns` | Glob patterns for files to ignore during formatting. | `[]` | |
| 109 | + |
| 110 | +## References |
72 | 111 |
|
73 |
| -[Ref](https://medium.com/@costa.alexoglou/enable-formatting-with-erb-files-in-vscode-d4b4ff537017) |
| 112 | +- [Issue on `htmlbeautifier`](https://github.com/threedaymonk/htmlbeautifier/issues/49) |
| 113 | +- [VS Code Ruby Extension Issue](https://github.com/rubyide/vscode-ruby/issues/56) |
| 114 | +- [Enable Formatting with ERB Files in VS Code](https://medium.com/@costa.alexoglou/enable-formatting-with-erb-files-in-vscode-d4b4ff537017) |
74 | 115 |
|
75 | 116 | ## Credits
|
76 | 117 |
|
|
0 commit comments