Module | Haml::Util |
In: |
lib/haml/util.rb
lib/haml/template.rb |
A module containing various useful functions.
Returns an ActionView::Template* class. In pre-3.0 versions of Rails, most of these classes were of the form `ActionView::TemplateFoo`, while afterwards they were of the form `ActionView;:Template::Foo`.
@param name [to_s] The name of the class to get.
For example, `:Error` will return `ActionView::TemplateError` or `ActionView::Template::Error`.
Moves a scanner through a balanced pair of characters. For example:
Foo (Bar (Baz bang) bop) (Bang (bop bip)) ^ ^ from to
@param scanner [StringScanner] The string scanner to move @param start [Character] The character opening the balanced pair.
A `Fixnum` in 1.8, a `String` in 1.9
@param finish [Character] The character closing the balanced pair.
A `Fixnum` in 1.8, a `String` in 1.9
@param count [Fixnum] The number of opening characters matched
before calling this method
@return [(String, String)] The string matched within the balanced pair
and the rest of the string. `["Foo (Bar (Baz bang) bop)", " (Bang (bop bip))"]` in the example above.
Returns information about the caller of the previous method.
@param entry [String] An entry in the `caller` list, or a similarly formatted string @return [[String, Fixnum, (String, nil)]] An array containing the filename, line, and method name of the caller.
The method name may be nil
Like {\check_encoding}, but also checks for a Ruby-style `-# coding:` comment at the beginning of the template and uses that encoding if it exists.
The Haml encoding rules are simple. If a `-# coding:` comment exists, we assume that that‘s the original encoding of the document. Otherwise, we use whatever encoding Ruby has.
Haml uses the same rules for parsing coding comments as Ruby. This means that it can understand Emacs-style comments (e.g. `-*- encoding: "utf-8" -*-`), and also that it cannot understand non-ASCII-compatible encodings such as `UTF-16` and `UTF-32`.
@param str [String] The Haml template of which to check the encoding @yield [msg] A block in which an encoding error can be raised.
Only yields if there is an encoding error
@yieldparam msg [String] The error message to be raised @return [String] The original string encoded properly @raise [ArgumentError] if the document declares an unknown encoding
This is used for methods in {Haml::Buffer} that need to be very fast, and take a lot of boolean parameters that are known at compile-time. Instead of passing the parameters in normally, a separate method is defined for every possible combination of those parameters; these are then called using \{static_method_name}.
To define a static method, an ERB template for the method is provided. All conditionals based on the static parameters are done as embedded Ruby within this template. For example:
def_static_method(Foo, :my_static_method, [:foo, :bar], :baz, :bang, <<RUBY) <% if baz && bang %> return foo + bar <% elsif baz || bang %> return foo - bar <% else %> return 17 <% end %> RUBY
\{static_method_name} can be used to call static methods.
@overload def_static_method(klass, name, args, *vars, erb) @param klass [Module] The class on which to define the static method @param name [to_s] The (base) name of the static method @param args [Array<Symbol>] The names of the arguments to the defined methods
(**not** to the ERB template)
@param vars [Array<Symbol>] The names of the static boolean variables
to be made available to the ERB template
Scans through a string looking for the interoplation-opening `#{` and, when it‘s found, yields the scanner to the calling code so it can handle it properly.
The scanner will have any backslashes immediately in front of the `#{` as the second capture group (`scan[2]`), and the text prior to that as the first (`scan[1]`).
@yieldparam scan [StringScanner] The scanner scanning through the string @return [String] The text remaining in the scanner after all `#{`s have been processed
Returns the given text, marked as being HTML-safe. With older versions of the Rails XSS-safety mechanism, this destructively modifies the HTML-safety of `text`.
@param text [String, nil] @return [String, nil] `text`, marked as HTML-safe
Formats a string for use in error messages about indentation.
@param indentation [String] The string used for indentation @return [String] The name of the indentation (e.g. `"12 spaces"`, `"1 tab"`)
Computes the powerset of the given array. This is the set of all subsets of the array.
@example
powerset([1, 2, 3]) #=> Set[Set[], Set[1], Set[2], Set[3], Set[1, 2], Set[2, 3], Set[1, 3], Set[1, 2, 3]]
@param arr [Enumerable] @return [Set<Set>] The subsets of `arr`
Whether or not ActionView‘s XSS protection is available and enabled, as is the default for Rails 3.0+, and optional for version 2.3.5+. Overridden in haml/template.rb if this is the case.
@return [Boolean]
Silence all output to STDERR within a block.
@yield A block in which no output will be printed to STDERR