2015-10-06 08:21:20 -04:00
# Hamlit
2015-11-30 07:03:00 -05:00
[![Gem Version ](https://badge.fury.io/rb/hamlit.svg )](http://badge.fury.io/rb/hamlit)
[![Build Status ](https://travis-ci.org/k0kubun/hamlit.svg?branch=master )](https://travis-ci.org/k0kubun/hamlit)
2015-11-21 13:21:08 -05:00
Hamlit is a high performance [Haml ](https://github.com/haml/haml ) implementation.
## Introduction
### What is Hamlit?
2015-11-27 15:56:47 -05:00
Hamlit is another implementation of [Haml ](https://github.com/haml/haml ).
With some [limitations ](REFERENCE.md#limitations ) by design for performance,
2017-05-26 20:29:56 -04:00
Hamlit is **2.39x times faster** than original haml gem in [this benchmark ](benchmark/slim/run-benchmarks.rb ),
which is an HTML-escaped version of [slim-template/slim's one ](https://github.com/slim-template/slim/blob/v3.0.8/benchmarks/run-benchmarks.rb ) for fairness. ([Result on Travis](https://travis-ci.org/k0kubun/hamlit/jobs/236567391))
2015-11-21 13:21:08 -05:00
2017-05-26 20:34:44 -04:00
< img src = "https://i.gyazo.com/0f0c0362b6bd69f82715bec1d8caa191.png" width = "600px" alt = "Hamlit Benchmark" / >
2015-11-21 13:21:08 -05:00
2015-11-22 15:09:48 -05:00
```
2017-05-26 20:29:56 -04:00
hamlit v2.8.1: 131048.9 i/s
erubi v1.6.0: 125445.4 i/s - 1.04x slower
slim v3.0.8: 121390.4 i/s - 1.08x slower
faml v0.8.1: 100750.5 i/s - 1.30x slower
haml v5.0.1: 54882.6 i/s - 2.39x slower
2015-11-22 15:09:48 -05:00
```
2015-11-21 13:21:08 -05:00
### Why is Hamlit faster?
#### Less string concatenation by design
As written in [limitations ](REFERENCE.md#limitations ), Hamlit drops some not-so-important features which require
works on runtime. With the optimized language design, we can reduce the string concatenation
to build attributes.
#### Static analyzer
Hamlit analyzes Ruby expressions with Ripper and render it on compilation if the expression
2015-11-23 12:13:36 -05:00
is static. And Hamlit can also compile string literal with string interpolation to reduce
string allocation and concatenation on runtime.
2015-11-21 13:21:08 -05:00
#### C extension to build attributes
While Hamlit has static analyzer and static attributes are rendered on compilation,
2015-11-23 12:13:36 -05:00
dynamic attributes must be rendered on runtime. So Hamlit optimizes rendering on runtime
2015-11-21 13:21:08 -05:00
with C extension.
## Usage
2016-05-12 12:01:24 -04:00
Hamlit currently supports Ruby 2.1 and higher. See [REFERENCE.md ](REFERENCE.md ) for detail features of Hamlit.
2015-11-21 13:21:08 -05:00
### Rails
Add this line to your application's Gemfile or just replace `gem "haml"` with `gem "hamlit"` .
It enables rendering by Hamlit for \*.haml automatically.
```rb
gem 'hamlit'
```
If you want to use view generator, consider using [hamlit-rails ](https://github.com/mfung/hamlit-rails ).
### Sinatra
Replace `gem "haml"` with `gem "hamlit"` in Gemfile, and require "hamlit".
While Haml disables `escape_html` option by default, Hamlit enables it for security.
If you want to disable it, please write:
```rb
set :haml, { escape_html: false }
```
## Command line interface
2015-11-28 04:22:29 -05:00
You can see compiled code or rendering result with "hamlit" command.
2015-11-21 13:21:08 -05:00
```bash
2015-11-28 04:22:29 -05:00
$ gem install hamlit
2015-11-21 13:21:08 -05:00
$ hamlit --help
Commands:
hamlit compile HAML # Show compile result
hamlit help [COMMAND] # Describe available commands or one specific command
hamlit parse HAML # Show parse result
hamlit render HAML # Render haml template
2015-11-22 09:29:28 -05:00
hamlit temple HAML # Show temple intermediate expression
2015-11-21 13:21:08 -05:00
$ cat in.haml
2015-11-28 04:22:29 -05:00
- user_id = 123
%a{ href: "/users/#{user_id}" }
2015-11-21 13:21:08 -05:00
# Show compiled code
$ hamlit compile in.haml
2015-11-28 04:22:29 -05:00
_buf = []; user_id = 123;
; _buf << ("< a href = '/users/".freeze); _buf << (::Hamlit::Utils.escape_html((user_id))); _buf << ("' ></ a > \n".freeze); _buf = _buf.join
2015-11-21 13:21:08 -05:00
# Render html
$ hamlit render in.haml
2015-11-28 04:22:29 -05:00
< a href = '/users/123' > < / a >
2015-11-21 13:21:08 -05:00
```
## Contributing
2016-07-03 00:32:46 -04:00
### Test latest version
```rb
# Gemfile
gem 'hamlit', github: 'k0kubun/hamlit', submodules: true
```
2015-11-21 13:21:08 -05:00
### Development
2015-11-22 09:29:28 -05:00
Contributions are welcomed. It'd be good to see
[Temple's EXPRESSIONS.md ](https://github.com/judofyr/temple/blob/v0.7.6/EXPRESSIONS.md )
to learn Temple which is a template engine framework used in Hamlit.
2015-11-21 13:21:08 -05:00
```bash
2016-02-22 06:34:30 -05:00
$ git clone --recursive https://github.com/k0kubun/hamlit
2015-11-21 13:21:08 -05:00
$ cd hamlit
$ bundle install
# Run all tests
$ bundle exec rake test
# Run one test
$ bundle exec ruby -Ilib:test -rtest_helper test/hamlit/line_number_test.rb -l 12
# Show compiling/rendering result of some template
$ bundle exec exe/hamlit compile in.haml
$ bundle exec exe/hamlit render in.haml
# Use rails app to debug Hamlit
$ cd sample/rails
$ bundle install
$ bundle exec rails s
```
### Reporting an issue
Please report an issue with following information:
- Full error backtrace
- Haml template
- Ruby version
- Hamlit version
- Rails/Sinatra version
2015-10-06 08:21:20 -04:00
2019-01-23 05:09:08 -05:00
### Coding styles
Please follow the existing coding styles and do not send patches including cosmetic changes.
2015-10-06 08:21:20 -04:00
## License
2015-11-21 13:21:08 -05:00
Copyright (c) 2015 Takashi Kokubun