1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
HTML Abstraction Markup Language - A Markup Haiku
Find a file
2019-09-12 22:59:04 +09:00
benchmark Upgrade Haml to 5 2017-05-27 09:08:39 +09:00
bin Drop bin/lineprof 2016-12-31 14:43:21 +09:00
exe fix exe load path 2016-03-10 14:47:47 +01:00
ext/hamlit Attempt to support truffleruby 2019-09-08 16:14:31 +09:00
lib Use safer delimiter for interpolation 2019-09-12 22:59:04 +09:00
test Support :plain filter in truffleruby 2019-09-08 16:56:42 +09:00
.gitignore Ignore truffleruby-specific binary 2019-09-08 17:03:41 +09:00
.travis.yml Add truffleruby to test matrix 2019-09-08 14:55:29 +09:00
CHANGELOG.md Version 2.9.5 2019-09-08 16:58:29 +09:00
Gemfile Pass bundle install for truffleruby 2019-09-08 15:08:19 +09:00
hamlit.gemspec Drop obsoleted gemspec switch [ci skip] 2019-09-08 16:29:27 +09:00
LICENSE.txt
Rakefile Drop C extension requirement from JRuby 2018-11-01 14:54:34 +09:00
README.md Explain the coding style 2019-01-23 19:09:09 +09:00
REFERENCE.md Mention hamlit-boolean_attributes in the reference 2019-07-20 00:17:11 +09:00

Hamlit

Gem Version Build Status

Hamlit is a high performance Haml implementation.

Introduction

What is Hamlit?

Hamlit is another implementation of Haml. With some limitations by design for performance, Hamlit is 2.39x times faster than original haml gem in this benchmark, which is an HTML-escaped version of slim-template/slim's one for fairness. (Result on Travis)

Hamlit Benchmark
       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

Why is Hamlit faster?

Less string concatenation by design

As written in 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 is static. And Hamlit can also compile string literal with string interpolation to reduce string allocation and concatenation on runtime.

C extension to build attributes

While Hamlit has static analyzer and static attributes are rendered on compilation, dynamic attributes must be rendered on runtime. So Hamlit optimizes rendering on runtime with C extension.

Usage

Hamlit currently supports Ruby 2.1 and higher. See REFERENCE.md for detail features of Hamlit.

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.

gem 'hamlit'

If you want to use view generator, consider using 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:

set :haml, { escape_html: false }

Command line interface

You can see compiled code or rendering result with "hamlit" command.

$ gem install hamlit
$ 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
  hamlit temple HAML     # Show temple intermediate expression

$ cat in.haml
- user_id = 123
%a{ href: "/users/#{user_id}" }

# Show compiled code
$ hamlit compile in.haml
_buf = [];  user_id = 123;
; _buf << ("<a href='/users/".freeze); _buf << (::Hamlit::Utils.escape_html((user_id))); _buf << ("'></a>\n".freeze); _buf = _buf.join

# Render html
$ hamlit render in.haml
<a href='/users/123'></a>

Contributing

Test latest version

# Gemfile
gem 'hamlit', github: 'k0kubun/hamlit', submodules: true

Development

Contributions are welcomed. It'd be good to see Temple's EXPRESSIONS.md to learn Temple which is a template engine framework used in Hamlit.

$ git clone --recursive https://github.com/k0kubun/hamlit
$ 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

Coding styles

Please follow the existing coding styles and do not send patches including cosmetic changes.

License

Copyright (c) 2015 Takashi Kokubun