Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,44 @@ Metrics/CyclomaticComplexity:
Exclude:
- lib/herb/project.rb
- lib/herb/ast/nodes.rb
- lib/herb/range.rb
- lib/herb/parse_result.rb
- lib/herb/errors.rb
- lib/herb/cli.rb

Metrics/MethodLength:
Max: 20
Exclude:
- lib/herb/ast/nodes.rb
- lib/herb/cli.rb
- lib/herb/project.rb
- lib/herb/errors.rb
- lib/herb/range.rb
- templates/template.rb
- test/fork_helper.rb
- test/snapshot_utils.rb
- test/**

Metrics/AbcSize:
Exclude:
- lib/herb/ast/node.rb
- lib/herb/ast/nodes.rb
- lib/herb/lex_result.rb
- lib/herb/cli.rb
- lib/herb/errors.rb
- lib/herb/project.rb
- lib/herb/parse_result.rb
- lib/herb/range.rb
- lib/herb/token.rb
- lib/herb/lint_result.rb
- templates/template.rb
- test/fork_helper.rb
- test/snapshot_utils.rb
- test/**

Metrics/ClassLength:
Exclude:
- lib/herb/cli.rb
- lib/herb/project.rb
- lib/herb/visitor.rb
- lib/herb/backend.rb
- lib/herb/ast/nodes.rb
- test/**/*_test.rb

Metrics/BlockLength:
Expand All @@ -83,19 +94,25 @@ Metrics/BlockLength:
- Rakefile
- "*.gemspec"
- "**/*.rake"
- lib/herb/cli.rb
- lib/herb/project.rb
- test/**/*_test.rb

Metrics/ParameterLists:
Exclude:
- lib/herb/ast/nodes.rb
- lib/herb/errors.rb
- lib/herb/lint_offense.rb

Metrics/PerceivedComplexity:
Max: 15
Exclude:
- lib/herb/ast/nodes.rb
- lib/herb/cli.rb
- lib/herb/project.rb
- lib/herb/range.rb
# - lib/herb/errors.rb
# - lib/herb/lex_result.rb
- test/snapshot_utils.rb

Layout/LineLength:
Expand All @@ -104,6 +121,10 @@ Layout/LineLength:
- test/**/*_test.rb
- lib/herb/token.rb
- lib/herb/ast/nodes.rb
- lib/herb/errors.rb
- lib/herb/backend.rb
- lib/herb/backends/native_backend.rb
- lib/herb/backends/node_backend.rb

Layout/EmptyLines:
Exclude:
Expand All @@ -125,6 +146,18 @@ Layout/FirstHashElementIndentation:
Layout/LeadingCommentSpace:
Enabled: false

Layout/IndentationWidth:
Exclude:
- lib/herb/ast/nodes.rb

Layout/ElseAlignment:
Exclude:
- lib/herb/ast/nodes.rb

Layout/EndAlignment:
Exclude:
- lib/herb/ast/nodes.rb

Security/Eval:
Exclude:
- Rakefile
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem "prism", github: "ruby/prism", tag: "v1.4.0"
gem "lz_string"
gem "maxitest"
gem "minitest-difftastic", "~> 0.2"
gem "nodo", "~> 1.8"
gem "rake", "~> 13.2"
gem "rake-compiler", "~> 1.2"
gem "rake-compiler-dock", "~> 1.9"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ GEM
minitest-difftastic (0.2.1)
difftastic (~> 0.6)
mutex_m (0.3.0)
nodo (1.8.1)
logger
parallel (1.27.0)
parser (3.3.8.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -159,6 +161,7 @@ DEPENDENCIES
lz_string
maxitest
minitest-difftastic (~> 0.2)
nodo (~> 1.8)
prism!
rake (~> 13.2)
rake-compiler (~> 1.2)
Expand Down
1 change: 1 addition & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ target :lib do
ignore "lib/herb/libherb.rb"
ignore "lib/herb/libherb"
ignore "lib/herb/project.rb"
ignore "lib/herb/backends/node_backend.rb"
end
70 changes: 69 additions & 1 deletion docs/docs/bindings/ruby/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,72 @@ require "herb"
```
:::

You are now ready to parse HTML+ERB in Ruby.
You are now ready to parse, format, lint, and print HTML+ERB in Ruby. See the [Quick Start](#quick-start) section below for basic usage.

## Multi-Backend Architecture

Herb for Ruby features a sophisticated multi-backend architecture that allows you to choose the best backend for your specific use case:

- **Native Backend** (default) - Ruby C extension for parsing and lexing
- **Node Backend** - Uses Node.js packages for advanced features like formatting and linting
- **FFI Backend** - Foreign Function Interface backend (planned for future release)
- **WASM Backend** - WebAssembly backend (planned for future release)

### Backend Selection

Herb automatically selects the best available backend, but you can choose manually:

```ruby
Herb.switch_backend(:native) # Native Ruby C-Extension Backend
Herb.switch_backend(:node) # Full feature set

Herb.current_backend
# => "native"

Herb.available_backends
# => [:native, :node]
```

### Cross-Backend Operations

You can use different backends for different operations without switching:


```ruby
# Parse with native backend
result = Herb.parse(source)

# Parse with Node backend
formatted = Herb.parse(source, backend: :node)

# Switch to Node backend
Herb.switch_backend(:node)

# Now uses Node backend for parsing
result = Herb.parse(source)
```

## Quick Start

Once installed, you can start parsing, formatting, and linting HTML+ERB templates:

:::code-group
```ruby
require "herb"

# Parse HTML+ERB templates
result = Herb.parse('<div>Hello <%= user.name %>!</div>')

# Format templates
formatted = Herb.format('<div><span>Messy</span></div>')

# Lint templates for issues
lint_result = Herb.lint('<img src="photo.jpg">')

# Convert AST back to HTML
node = result.value
html_output = node.to_source
```
:::

For detailed examples and API reference, see the [Ruby Reference](/bindings/ruby/reference) documentation.
Loading
Loading