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
2015-11-28 01:42:53 +09:00
benchmark Share millisecond extension 2015-11-27 01:51:04 +09:00
bin Share millisecond extension 2015-11-27 01:51:04 +09:00
exe Create hamlit CLI 2015-10-24 20:32:19 +09:00
ext/hamlit Compile extension with houdini 2015-11-28 01:42:53 +09:00
lib Replace build_id implementation with C 2015-11-27 00:39:24 +09:00
sample Change sinatra sample to work 2015-11-21 06:42:31 +09:00
test Add test for static_analyzer_test 2015-11-24 02:16:05 +09:00
.gitignore Prepare extension in C 2015-11-26 01:07:06 +09:00
.gitmodules Add houdini as submodule 2015-11-28 01:39:51 +09:00
.travis.yml bundle gem hamlit 2015-10-06 21:21:22 +09:00
CHANGELOG.md Update CHANGELOG.md 2015-11-23 05:34:32 +09:00
Gemfile Fix Gemfile to run with Ruby 2.0 2015-11-23 02:53:56 +09:00
hamlit.gemspec Prepare extension in C 2015-11-26 01:07:06 +09:00
LICENSE.txt Vendoring Haml modules for parser 2015-11-19 16:21:54 +09:00
Rakefile Prepare extension in C 2015-11-26 01:07:06 +09:00
README.md Fix wording in README.md 2015-11-24 02:14:58 +09:00
REFERENCE.md Fix REFERENCE.md wording 2015-11-24 02:18:32 +09:00
wercker.yml Update bundler 2015-11-27 02:10:27 +09:00

Hamlit

Hamlit is a high performance Haml implementation.

Introduction

What is Hamlit?

Hamlit is another implementation of Haml and designed to make Haml language faster than Slim. With some limitations by design for performance, Hamlit is 7.16x times faster than original haml gem in this benchmark, which is an HTML-escaped version of slim-template/slim's one for fairness.

Hamlit Benchmark

    erubis:   114501.6 i/s
    hamlit:   112888.1 i/s - 1.01x slower
      slim:   103298.5 i/s - 1.11x slower
      faml:    88675.4 i/s - 1.29x slower
      haml:    15750.6 i/s - 7.27x 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.

Temple optimizers

Hamlit is built with Temple, which is a framework to build template engines and also used in Slim. By using the framework and its optimizers, Hamlit can reduce string allocation and concatenation easily.

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

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". See sample/sinatra for working sample.

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

'hamlit' command is available if you install thor gem with gem install thor.

$ gem install hamlit thor
$ 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
.foo#bar

# Show compiled code
$ hamlit compile in.haml
_buf = "<div class='foo' id='bar'></div>\n"

# Render html
$ hamlit render in.haml
<div class='foo' id='bar'></div>

Contributing

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

License

MIT License

Copyright (c) 2015 Takashi Kokubun

Parser and Haml tests

lib/hamlit/parser/*.rb and test/haml/* are:

Copyright (c) 2006-2009 Hampton Catlin and Natalie Weizenbaum

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.