Coder Social home page Coder Social logo

zaru / hamlit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from k0kubun/hamlit

0.0 3.0 0.0 1.06 MB

High Performance Haml Implementation

Home Page: https://rubygems.org/gems/hamlit

License: Other

Ruby 83.05% HTML 13.09% Shell 0.10% C 3.17% JavaScript 0.12% CoffeeScript 0.04% CSS 0.36% Logos 0.06%

hamlit's Introduction

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 8.54x 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.0.1:   122622.3 i/s
         faml v0.7.0:    94239.1 i/s - 1.30x slower
         slim v3.0.6:    89143.0 i/s - 1.38x slower
       erubis v2.7.0:    65047.8 i/s - 1.89x slower
  haml v5.0.0.beta.2:    14363.6 i/s - 8.54x 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

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

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

License

Copyright (c) 2015 Takashi Kokubun

hamlit's People

Contributors

k0kubun avatar creasty avatar bsmith-optoro avatar connorshea avatar walf443 avatar eagletmt avatar shmargum avatar mibamur avatar timoschilling avatar mbrand12 avatar akelmanson avatar hfaulds avatar machu avatar myklclason avatar pushcx avatar raphaeleidus avatar rstacruz avatar

Watchers

zaru avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.