public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Search Repo:
nex3 (author)
Tue Dec 11 02:03:44 -0800 2007
commit  9a848a7eff92c6f98eb50b5a45a766e560199d76
tree    09a26fd0fcb90886c66861ff54bc87cee037ec50
parent  a4c5b79dfb5b6ccdf639b3df925e519dafbb8945
haml / lib / haml.rb
100644 727 lines (725 sloc) 19.418 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
dir = File.dirname(__FILE__)
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
 
# = Haml (XHTML Abstraction Markup Language)
#
# Haml is a markup language
# that's used to cleanly and simply describe the XHTML of any web document,
# without the use of inline code.
# Haml functions as a replacement
# for inline page templating systems such as PHP, ERB, and ASP.
# However, Haml avoids the need for explicitly coding XHTML into the template,
# because it is actually an abstract description of the XHTML,
# with some code to generate dynamic content.
#
# == Features
#
# * Whitespace active
# * Well-formatted markup
# * DRY
# * Follows CSS conventions
# * Integrates Ruby code
# * Implements Rails templates with the .haml extension
#
# == Using Haml
#
# Haml can be used in two ways:
# as a plugin for Ruby on Rails,
# and as a standalone Ruby module.
#
# Sass can be used in several ways:
# As a template engine for Ruby on Rails or Merb,
# or as a standalone engine.
# The first step for all of these is to install the Haml gem:
#
# gem install haml
#
# To enable it as a Rails plugin,
# then run
#
# haml --rails path/to/rails/app
#
# Haml is enabled in Merb by default,
# so Merb users don't have to do anything more.
#
# Once it's installed, all view files with the ".haml" extension
# (or ".html.haml" for Merb or edge Rails)
# will be compiled using Haml.
#
# You can access instance variables in Haml templates
# the same way you do in ERb templates.
# Helper methods are also available in Haml templates.
# For example (this example uses Rails, but the principle for Merb is the same):
#
# # file: app/controllers/movies_controller.rb
#
# class MoviesController < ApplicationController
# def index
# @title = "Teen Wolf"
# end
# end
#
# -# file: app/views/movies/index.haml
#
# #content
# .title
# %h1= @title
# = link_to 'Home', home_url
#
# may be compiled to:
#
# <div id='content'>
# <div class='title'>
# <h1>Teen Wolf</h1>
# <a href='/'>Home</a>
# </div>
# </div>
#
# === Ruby Module
#
# Haml can also be used completely separately from Rails and ActionView.
# To do this, install the gem with RubyGems:
#
# gem install haml
#
# You can then use it by including the "haml" gem in Ruby code,
# and using Haml::Engine like so:
#
# engine = Haml::Engine.new("%p Haml code!")
# engine.render #=> "<p>Haml code!</p>\n"
#
# == Characters with meaning to Haml
#
# Various characters, when placed at a certain point in a line,
# instruct Haml to render different types of things.
#
# === XHTML Tags
#
# These characters render XHTML tags.
#
# ==== %
#
#
# The percent character is placed at the beginning of a line.
# It's followed immediately by the name of an element,
# then optionally by modifiers (see below), a space,
# and text to be rendered inside the element.
# It creates an element in the form of <tt><element></element></tt>.
# For example:
#
# %one
# %two
# %three Hey there
#
# is compiled to:
#
# <one>
# <two>
# <three>Hey there</three>
# </two>
# </one>
#
# Any string is a valid element name;
# Haml will automatically generate opening and closing tags for any element.
#
# ==== {}
#
# Brackets represent a Ruby hash
# that is used for specifying the attributes of an element.
# It is literally evaluated as a Ruby hash,
# so logic will work in it and local variables may be used.
# Quote characters within the attribute
# will be replaced by appropriate escape sequences.
# The hash is placed after the tag is defined.
# For example:
#
# %head{ :name => "doc_head" }
# %script{ 'type' => "text/" + "javascript",
# :src => "javascripts/script_#{2 + 7}" }
#
# is compiled to:
#
# <head name="doc_head">
# <script src='javascripts/script_9' type='text/javascript'>
# </script>
# </head>
#
# ==== []
#
# Square brackets follow a tag definition and contain a Ruby object
# that is used to set the class and id of that tag.
# The class is set to the object's class
# (transformed to use underlines rather than camel case)
# and the id is set to the object's class, followed by its id.
# Because the id of an object is normally an obscure implementation detail,
# this is most useful for elements that represent instances of Models.
# For example:
#
# # file: app/controllers/users_controller.rb
#
# def show
# @user = CrazyUser.find(15)
# end
#
# -# file: app/views/users/show.haml
#
# %div[@user]
# %bar[290]/
# Hello!
#
# is compiled to:
#
# <div class="crazy_user" id="crazy_user_15">
# <bar class="fixnum" id="fixnum_581" />
# Hello!
# </div>
#
# This is based off of DHH's SimplyHelpful syntax,
# as presented at RailsConf Europe 2006.
#
# ==== /
#
# The forward slash character, when placed at the end of a tag definition,
# causes the tag to be self-closed.
# For example:
#
# %br/
# %meta{'http-equiv' => 'Content-Type', :content => 'text/html'}/
#
# is compiled to:
#
# <br />
# <meta http-equiv='Content-Type' content='text/html' />
#
# Some tags are automatically closed, as long as they have no content.
# +meta+, +img+, +link+, +script+, +br+, and +hr+ tags are closed by default.
# This list can be customized by setting the <tt>:autoclose</tt> option (see below).
# For example:
#
# %br
# %meta{'http-equiv' => 'Content-Type', :content => 'text/html'}
#
# is also compiled to:
#
# <br />
# <meta http-equiv='Content-Type' content='text/html' />
#
# ==== . and #
#
# The period and pound sign are borrowed from CSS.
# They are used as shortcuts to specify the <tt>class</tt>
# and <tt>id</tt> attributes of an element, respectively.
# Multiple class names can be specified in a similar way to CSS,
# by chaining the class names together with periods.
# They are placed immediately after the tag and before an attributes hash.
# For example:
#
# %div#things
# %span#rice Chicken Fried
# %p.beans{ :food => 'true' } The magical fruit
# %h1.class.otherclass#id La La La
#
# is compiled to:
#
# <div id='things'>
# <span id='rice'>Chicken Fried</span>
# <p class='beans' food='true'>The magical fruit</p>
# <h1 class='class otherclass' id='id'>La La La</h1>
# </div>
#
# And,
#
# #content
# .articles
# .article.title
# Doogie Howser Comes Out
# .article.date
# 2006-11-05
# .article.entry
# Neil Patrick Harris would like to dispel any rumors that he is straight
#
# is compiled to:
#
# <div id="content">
# <div class="articles">
# <div class="article title">Doogie Howser Comes Out</div>
# <div class="article date">2006-11-05</div>
# <div class="article entry">
# Neil Patrick Harris would like to dispel any rumors that he is straight
# </div>
# </div>
# </div>
#
# ==== Implicit Div Elements
#
# Because the div element is used so often, it is the default element.
# If you only define a class and/or id using the <tt>.</tt> or <tt>#</tt> syntax,
# a div element is automatically used.
# For example:
#
# #collection
# .item
# .description What a cool item!
#
# is the same as:
#
# %div{:id => collection}
# %div{:class => 'item'}
# %div{:class => 'description'} What a cool item!
#
# and is compiled to:
#
# <div id='collection'>
# <div class='item'>
# <div class='description'>What a cool item!</div>
# </div>
# </div>
#
# ==== =
#
# <tt>=</tt> is placed at the end of a tag definition,
# after class, id, and attribute declarations.
# It's just a shortcut for inserting Ruby code into an element.
# It works the same as <tt>=</tt> without a tag:
# it inserts the result of the Ruby code into the template.
# However, if the result is short enough,
# it is displayed entirely on one line.
# For example:
#
# %p= "hello"
#
# is not quite the same as:
#
# %p
# = "hello"
#
# It's compiled to:
#
# <p>hello</p>
#
# === XHTML Helpers
#
# ==== No Special Character
#
# If no special character appears at the beginning of a line,
# the line is rendered as plain text.
# For example:
#
# %gee
# %whiz
# Wow this is cool!
#
# is compiled to:
#
# <gee>
# <whiz>
# Wow this is cool!
# </whiz>
# </gee>
#
# ==== !!!
#
# When describing XHTML documents with Haml,
# you can have a document type or XML prolog generated automatically
# by including the characters <tt>!!!</tt>.
# For example:
#
# !!! XML
# !!!
# %html
# %head
# %title Myspace
# %body
# %h1 I am the international space station
# %p Sign my guestbook
#
# is compiled to:
#
# <?xml version="1.0" encoding="utf-8" ?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
# <html>
# <head>
# <title>Myspace</title>
# </head>
# <body>
# <h1>I am the international space station</h1>
# <p>Sign my guestbook</p>
# </body>
# </html>
#
# You can also specify the version and type of XHTML after the <tt>!!!</tt>.
# XHTML 1.0 Strict, Transitional, and Frameset and XHTML 1.1 are supported.
# The default version is 1.0 and the default type is Transitional.
# For example:
#
# !!! 1.1
#
# is compiled to:
#
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
#
# and
#
# !!! Strict
#
# is compiled to:
#
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
#
# If you're not using the UTF-8 character set for your document,
# you can specify which encoding should appear
# in the XML prolog in a similar way.
# For example:
#
# !!! XML iso-8859-1
#
# is compiled to:
#
# <?xml version="1.0" encoding="iso-8859-1" ?>
#
# ==== /
#
# The forward slash character, when placed at the beginning of a line,
# wraps all text after it in an HTML comment.
# For example:
#
# %peanutbutterjelly
# / This is the peanutbutterjelly element
# I like sandwiches!
#
# is compiled to:
#
# <peanutbutterjelly>
# <!-- This is the peanutbutterjelly element -->
# I like sandwiches!
# </peanutbutterjelly>
#
# The forward slash can also wrap indented sections of code. For example:
#
# /
# %p This doesn't render...
# %div
# %h1 Because it's commented out!
#
# is compiled to:
#
# <!--
# <p>This doesn't render...</p>
# <div>
# <h1>Because it's commented out!</h1>
# </div>
# -->
#
# You can also use Internet Explorer conditional comments
# (about)[http://www.quirksmode.org/css/condcom.html]
# by enclosing the condition in square brackets after the <tt>/</tt>.
# For example:
#
# /[if IE]
# %a{ :href => 'http://www.mozilla.com/en-US/firefox/' }
# %h1 Get Firefox
#
# is compiled to:
#
# <!--[if IE]>
# <a href='http://www.mozilla.com/en-US/firefox/'>
# <h1>Get Firefox</h1>
# </a>
# <![endif]-->
#
# ==== \
#
# The backslash character escapes the first character of a line,
# allowing use of otherwise interpreted characters as plain text.
# For example:
#
# %title
# = @title
# \- MySite
#
# is compiled to:
#
# <title>
# MyPage
# - MySite
# </title>
#
# ==== |
#
# The pipe character designates a multiline string.
# It's placed at the end of a line
# and means that all following lines that end with <tt>|</tt>
# will be evaluated as though they were on the same line.
# For example:
#
# %whoo
# %hoo I think this might get |
# pretty long so I should |
# probably make it |
# multiline so it doesn't |
# look awful. |
# %p This is short.
#
# is compiled to:
#
# <whoo>
# <hoo>
# I think this might get pretty long so I should probably make it multiline so it doesn't look awful.
# </hoo>
# </whoo>
#
# ==== :
#
# The colon character designates a filter.
# This allows you to pass an indented block of text as input
# to another filtering program and add the result to the output of Haml.
# The syntax is simply a colon followed by the name of the filter.
# For example,
#
# %p
# :markdown
# Textile
# =======
#
# Hello, *World*
#
# is compiled to
#
# <p>
# <h1>Textile</h1>
#
# <p>Hello, <em>World</em></p>
# </p>
#
# Haml has the following filters defined:
#
# [plain] Does not parse the filtered text.
# This is useful for large blocks of text without HTML tags,
# when you don't want lines starting with <tt>.</tt> or <tt>-</tt>
# to be parsed.
#
# [ruby] Parses the filtered text with the normal Ruby interpreter.
# All output sent to <tt>$stdout</tt>, like with +puts+,
# is output into the Haml document.
# Not available if the <tt>suppress_eval</tt> option is set to true.
#
# [preserve] Inserts the filtered text into the template with whitespace preserved.
# <tt>preserve</tt>d blocks of text aren't indented,
# and newlines are replaced with the HTML escape code for newlines,
# to preserve nice-looking output.
#
# [erb] Parses the filtered text with ERB, like an RHTML template.
# Not available if the <tt>suppress_eval</tt> option is set to true.
# At the moment, this doesn't support access to variables
# defined by Ruby on Rails or Haml code.
#
# [sass] Parses the filtered text with Sass to produce CSS output.
#
# [redcloth] Parses the filtered text with RedCloth (http://whytheluckystiff.net/ruby/redcloth),
# which uses both Textile and Markdown syntax.
# Only works if RedCloth is installed.
#
# [textile] Parses the filtered text with Textile (http://www.textism.com/tools/textile).
# Only works if RedCloth is installed.
#
# [markdown] Parses the filtered text with Markdown (http://daringfireball.net/projects/markdown).
# Only works if RedCloth or BlueCloth (http://www.deveiate.org/projects/BlueCloth)
# is installed
# (BlueCloth takes precedence if both are installed).
#
# You can also define your own filters (see Setting Options, below).
#
# === Ruby evaluators
#
# ==== =
#
# The equals character is followed by Ruby code,
# which is evaluated and the output inserted into the document as plain text.
# For example:
#
# %p
# = ['hi', 'there', 'reader!'].join " "
# = "yo"
#
# is compiled to:
#
# <p>
# hi there reader!
# yo
# </p>
#
# You can also use two equal signs, <tt>==</tt>,
# along with conventional Ruby string-embedding syntax
# to easily embed Ruby code in otherwise static text.
# For example:
#
# %p
# == 1 + 1 = #{1 + 1}
#
# is compiled to:
#
# <p>
# 1 + 1 = 2
# </p>
#
# ==== -
#
# The hyphen character makes the text following it into "silent script":
# Ruby script that is evaluated, but not output.
#
# <b>It is not recommended that you use this widely;
# almost all processing code and logic should be restricted
# to the Controller, the Helper, or partials.</b>
#
# For example:
#
# - foo = "hello"
# - foo << " there"
# - foo << " you!"
# %p= foo
#
# is compiled to:
#
# <p>
# hello there you!
# </p>
#
# ===== Blocks
#
# Ruby blocks, like XHTML tags, don't need to be explicitly closed in Haml.
# Rather, they're automatically closed, based on indentation.
# A block begins whenever the indentation is increased
# after a silent script command.
# It ends when the indentation decreases
# (as long as it's not an +else+ clause or something similar).
# For example:
#
# - (42...47).each do |i|
# %p= i
# %p See, I can count!
#
# is compiled to:
#
# <p>
# 42
# </p>
# <p>
# 43
# </p>
# <p>
# 44
# </p>
# <p>
# 45
# </p>
# <p>
# 46
# </p>
#
# Another example:
#
# %p
# - case 2
# - when 1
# = "1!"
# - when 2
# = "2?"
# - when 3
# = "3."
#
# is compiled to:
#
# <p>
# 2?
# </p>
#
# ==== -#
#
# The hyphen followed immediately by the pound sign
# signifies a silent comment.
# Any text following this isn't rendered in the resulting document
# at all.
#
# For example:
#
# %p foo
# -# This is a comment
# %p bar
#
# is compiled to:
#
# <p>foo</p>
# <p>bar</p>
#
# You can also nest text beneath a silent comment.
# None of this text will be rendered.
# For example:
#
# %p foo
# -#
# This won't be displayed
# Nor will this
# %p bar
#
# is compiled to:
#
# <p>foo</p>
# <p>bar</p>
#
# == Other Useful Things
#
# === Helpers
#
# Haml offers a bunch of helpers that are useful
# for doing stuff like preserving whitespace,
# creating nicely indented output for user-defined helpers,
# and other useful things.
# The helpers are all documented in the Haml::Helpers and Haml::Helpers::ActionViewExtensions modules.
#
# === Haml Options
#
# Options can be set by setting the hash <tt>Haml::Template.options</tt>
# from <tt>environment.rb</tt> in Rails,
# or by passing an options hash to Haml::Engine.
# Available options are:
#
# [<tt>:suppress_eval</tt>] Whether or not attribute hashes and Ruby scripts
# designated by <tt>=</tt> or <tt>~</tt> should be
# evaluated. If this is true, said scripts are
# rendered as empty strings. Defaults to false.
#
# [<tt>:attr_wrapper</tt>] The character that should wrap element attributes.
# This defaults to <tt>'</tt> (an apostrophe). Characters
# of this type within the attributes will be escaped
# (e.g. by replacing them with <tt>&apos;</tt>) if
# the character is an apostrophe or a quotation mark.
#
# [<tt>:filename</tt>] The name of the Haml file being parsed.
# This is only used as information when exceptions are raised.
# This is automatically assigned when working through ActionView,
# so it's really only useful for the user to assign
# when dealing with Haml programatically.
#
# [<tt>:filters</tt>] A hash of filters that can be applied to Haml code.
# The keys are the string names of the filters;
# the values are references to the classes of the filters.
# User-defined filters should always have lowercase keys,
# and should have:
# * An +initialize+ method that accepts one parameter,
# the text to be filtered.
# * A +render+ method that returns the result of the filtering.
#
# [<tt>:autoclose</tt>] A list of tag names that should be automatically self-closed
# if they have no content.
# Defaults to <tt>['meta', 'img', 'link', 'script', 'br', 'hr']</tt>.
#
module Haml
  # This method is called by init.rb,
  # which is run by Rails on startup.
  # We use it rather than putting stuff straight into init.rb
  # so we can change the initialization behavior
  # without modifying the file itself.
  def self.init_rails(binding)
    %w[haml/template sass sass/plugin].each(&:require)
  end
end
 
require 'haml/engine'