w2tags is the way to tags, a short cut / macros. when it do it use some patern define in files (HOT file) to produce tags from minimal code to become full fledge tags
JavaScript Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Run on MRI Ruby : 1.8.6 / 1.9.x

gem install w2tags
w2tags -irb
ARGV : -irb
Your W2Tags instance object stored in WH...
Ex: puts WH.parse_line("#content\n  %span Hello")
Shortcut command => wh("#content\n  %span Hello")
irb(main):001:0> wh <<END
irb(main):002:0" #wow
irb(main):003:0"   #kereen
irb(main):004:0"     .greeting Hello World
irb(main):005:0" END
<div id="wow">
  <div id="kereen">
    <div class="greeting">Hello World</div>
=> ["<div id="wow">\n", "  <div id="kereen">\n", "    <div class="greeting">Hello World</div>\n", "  </div>\n", "</div>\n"]


W2Tags is the way to tags, a short cut / macros. When it do, it use some patern define in HOT file to produce tags from minimal code to become full fledge tags.


Before it release it use some patern, but I reliaze, today web development have some popular patern in view engine, so I change the patern. Now syntax mimics HAML to easy transisition, and proven to be useable and best.

Extended Syntax are the remain patern before I choose HAML-like syntax like “^” or “*” - NextTag (next tag will be use after using a tag like “ul” next tag “li”), Redefinition NextTag command using “~^”, define variable “&var=”, define uniq variable “@var=”, Key-Variable

"$:","*:" for attribute NAME, 
"$#","*#" for attribute ID, 
"$.","*." for attribute CLASS, 
"$$" for all Key-Variable, 
"$@","*@" for custome attribute,
"$=" for ruby code ("=" or "==" last Key-Variable).

This Constanta will be ready every time it parse the W2TAGS HOT (% / -).

HOT file is the powerfull features in w2tags, it contains some patern that you can call it later using command “%” or “-”, it contains one or more line of html/w2tags/hot command, if you detect in you source that not DRY you can migrate the patern to HOT files and you can call it later

The rest of the command you can see in each RDOC Documentation or by reading the source of W2Tags::Parser or see in Test folder, So you can code less typing.


Demo files

Some of Demo not yet convert to new patern so please not to tray it, I'll do it if I have some time to do it.

Auto closing for Code

Closing tags for HAML code like:

- if <condition>

does not have automatically close so you must closing it your self with

- end

for w2erb, the HOT have definition for autoclosing and the help from method on parser “shortcut_exec(regex)”, now it can auto intelligent closing

-if index>100
  .bigger bigger than 100
-elsif index>50
  .bigger bigger than 50 but less than 100
  .less less than 50
  Common Tag

work around to have a closing code, you can use feature to save some patern in a hot file and use it in your code, example: “common.hot”

- if $*
- end

and use it in your code:

-if <condition>

and it do automatic closing


HAML Like Syntax

just use it like haml basic, and if alredy comfort with that syntax you can extend the syntax and combine it with patern definition created in hot file or you can create your own hot file.

Example 1 - HAML:

  %h1 menu
    %li menu 1
    %li menu 2

will produce

<div id="content">
    <li>menu 1</li>
    <li>menu 2</li>

Extended syntax that produce the same result

  %h1 menu
    ^ menu 1; menu 2

or if the patern is so common, it can be save in a hot file and use it as hot command. Example: inside “common.hot”


and code in source:

  %h1 menu
  -ul menu 1; menu 2

Example 2 - Form input

HAML format:

%h1 New user
= error_messages_for :user 
-form_for @user do |f| 
    %b Name
    = f.text_field :name 
    %b Email
    = f.text_field :email 
    %b Last login
    = f.datetime_select :last_login 
    = f.submit "Create"
= link_to 'Back', users_path

You can see that it not DRY, some patern will go to HOT files “rails_basic.hot”

  %b $0.capitalize.gsub('_',' ')-
  = f.text_field :$0
  = f.submit "$0"

and code in source:

%h1 New user
= error_messages_for :user
-form_for @user do |f|
  -input name;email;last_login
  -submit Create
= link_to 'Back', users_path


no requirement for w2tags (the parser), since it use only regular expression to do parsing.

converting .erb or .html need hpricot engine to do search tags (see below on command no.10)


sudo gem install w2tags


get it from commandline “w2tags”

 How to WaytoTags:
 w2tags [*.w2erb] [-a] [-d:*opt*]
 w2tags [file1,file2,file3] [-b] [-d:*opt*]

 1. w2tags -a             #=> translate all file with ext: w2erb
 2. w2tags *.w2erb -a     #=> translate all file with ext: w2erb
 3. w2tags file1.w2erb    #=> translate a file
 4. w2tags file1.w2erb -b #=> translate a file with no initialize/finalize
 5. w2tags file1.w2erb -d:parser #=> translate with debug parser
 6. w2tags file1.w2erb -d:constanta #=> translate with debug constanta
 7. w2tags file1.w2erb -d:stack #=> translate with debug stack indentation
 8. w2tags -try           #=> Test W2tags using Sinatra http://localhost:4567/
 9. w2tags -irb           #=> Start IRB with WH as instance object of W2Tags
10. w2tags -rails         #=> Copy w2scaffold Rails and set environment.rb
11.w2tags -tags2w         #=> Parsing erb to w2tags



add inside file “config/environment.rb” in the bottom:

# if using rails 2.x
require 'w2tags'
require 'w2tags/rails_hook-2.x' 
# if using rails 3.x
require 'w2tags'
require 'w2tags/rails_hook-3.x'

You can automatically add by using commandline “w2tags -rails”


add inside file “config/init.rb” in the bottom:

require 'w2tags'
require 'w2tags/merb_hook'


sinatra usualy consist only one file “main.rb”, so just add below sinatra require:

require 'w2tags'
require 'w2tags/sinatra_hook'


Copyright © 2008 - 2009 Widi Harsojo. See LICENSE for details.