Class: Hamlit::HamlOptions

Inherits:
Object show all
Defined in:
lib/hamlit/parser/haml_options.rb

Overview

This class encapsulates all of the configuration options that Haml understands. Please see the Haml Reference to learn how to set the options.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}, &block) ⇒ HamlOptions

Returns a new instance of HamlOptions.



184
185
186
187
188
# File 'lib/hamlit/parser/haml_options.rb', line 184

def initialize(values = {}, &block)
  defaults.each {|k, v| instance_variable_set :"@#{k}", v}
  values.each {|k, v| send("#{k}=", v) if defaults.has_key?(k) && !v.nil?}
  yield if block_given?
end

Instance Attribute Details

#attr_wrapperObject

The character that should wrap element attributes. This defaults to ‘’‘ (an apostrophe). Characters of this type within the attributes will be escaped (e.g. by replacing them with `'`) if the character is an apostrophe or a quotation mark.



62
63
64
# File 'lib/hamlit/parser/haml_options.rb', line 62

def attr_wrapper
  @attr_wrapper
end

#autocloseObject

A list of tag names that should be automatically self-closed if they have no content. This can also contain regular expressions that match tag names (or any object which responds to ‘#===`). Defaults to `[’meta’, ‘img’, ‘link’, ‘br’, ‘hr’, ‘input’, ‘area’, ‘param’, ‘col’, ‘base’]‘.



68
69
70
# File 'lib/hamlit/parser/haml_options.rb', line 68

def autoclose
  @autoclose
end

#cdataObject

Whether to include CDATA sections around javascript and css blocks when using the ‘:javascript` or `:css` filters.

This option also affects the ‘:sass`, `:scss`, `:less` and `:coffeescript` filters.

Defaults to ‘false` for html, `true` for xhtml. Cannot be changed when using xhtml.



168
169
170
# File 'lib/hamlit/parser/haml_options.rb', line 168

def cdata
  @cdata
end

#compiler_classObject

The compiler class to use. Defaults to Haml::Compiler.



174
175
176
# File 'lib/hamlit/parser/haml_options.rb', line 174

def compiler_class
  @compiler_class
end

#encodingObject

The encoding to use for the HTML output. This can be a string or an ‘Encoding` Object. Note that Haml **does not** automatically re-encode Ruby values; any strings coming from outside the application should be converted before being passed into the Haml template. Defaults to `Encoding.default_internal`; if that’s not set, defaults to the encoding of the Haml template; if that’s ‘US-ASCII`, defaults to `“UTF-8”`.



77
78
79
# File 'lib/hamlit/parser/haml_options.rb', line 77

def encoding
  @encoding
end

#escape_attrsObject

Sets whether or not to escape HTML-sensitive characters in attributes. If this is true, all HTML-sensitive characters in attributes are escaped. If it’s set to false, no HTML-sensitive characters in attributes are escaped. If it’s set to ‘:once`, existing HTML escape sequences are preserved, but other HTML-sensitive characters are escaped.

Defaults to ‘true`.



86
87
88
# File 'lib/hamlit/parser/haml_options.rb', line 86

def escape_attrs
  @escape_attrs
end

#escape_htmlObject

Sets whether or not to escape HTML-sensitive characters in script. If this is true, ‘=` behaves like `&=`; otherwise, it behaves like `!=`. Note that if this is set, `!=` should be used for yielding to subtemplates and rendering partials. See also Escaping HTML and Unescaping HTML.

Defaults to false.



96
97
98
# File 'lib/hamlit/parser/haml_options.rb', line 96

def escape_html
  @escape_html
end

#filenameObject

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.



102
103
104
# File 'lib/hamlit/parser/haml_options.rb', line 102

def filename
  @filename
end

#formatObject

Determines the output format. The default is ‘:html5`. The other options are `:html4` and `:xhtml`. If the output is set to XHTML, then Haml automatically generates self-closing tags and wraps the output of the Javascript and CSS-like filters inside CDATA. When the output is set to `:html5` or `:html4`, XML prologs are ignored. In all cases, an appropriate doctype is generated from `!!!`.

If the mime_type of the template being rendered is ‘text/xml` then a format of `:xhtml` will be used even if the global output format is set to `:html4` or `:html5`.



123
124
125
# File 'lib/hamlit/parser/haml_options.rb', line 123

def format
  @format
end

#hyphenate_data_attrsObject

If set to ‘true`, Haml will convert underscores to hyphens in all Custom Data Attributes As of Haml 4.0, this defaults to `true`.



107
108
109
# File 'lib/hamlit/parser/haml_options.rb', line 107

def hyphenate_data_attrs
  @hyphenate_data_attrs
end

#lineObject

The line offset of the Haml template being parsed. This is useful for inline templates, similar to the last argument to ‘Kernel#eval`.



111
112
113
# File 'lib/hamlit/parser/haml_options.rb', line 111

def line
  @line
end

#mime_typeObject

The mime type that the rendered document will be served with. If this is set to ‘text/xml` then the format will be overridden to `:xhtml` even if it has set to `:html4` or `:html5`.



128
129
130
# File 'lib/hamlit/parser/haml_options.rb', line 128

def mime_type
  @mime_type
end

#parser_classObject

The parser class to use. Defaults to Haml::Parser.



171
172
173
# File 'lib/hamlit/parser/haml_options.rb', line 171

def parser_class
  @parser_class
end

#preserveObject

A list of tag names that should automatically have their newlines preserved using the Haml::Helpers#preserve helper. This means that any content given on the same line as the tag will be preserved. For example, ‘%textarea= “FoonBar”` compiles to `<textarea>Foo&#x000A;Bar</textarea>`. Defaults to `[’textarea’, ‘pre’]‘. See also Whitespace Preservation.



136
137
138
# File 'lib/hamlit/parser/haml_options.rb', line 136

def preserve
  @preserve
end

#remove_whitespaceObject

If set to ‘true`, all tags are treated as if both whitespace removal options were present. Use with caution as this may cause whitespace-related formatting errors.

Defaults to ‘false`.



144
145
146
# File 'lib/hamlit/parser/haml_options.rb', line 144

def remove_whitespace
  @remove_whitespace
end

#suppress_evalObject

Whether or not attribute hashes and Ruby scripts designated by ‘=` or `~` should be evaluated. If this is `true`, said scripts are rendered as empty strings.

Defaults to ‘false`.



151
152
153
# File 'lib/hamlit/parser/haml_options.rb', line 151

def suppress_eval
  @suppress_eval
end

#traceObject

Enable template tracing. If true, it will add a ‘data-trace’ attribute to each tag generated by Haml. The value of the attribute will be the source template name and the line number from which the tag was generated, separated by a colon. On Rails applications, the path given will be a relative path as from the views directory. On non-Rails applications, the path will be the full path.



182
183
184
# File 'lib/hamlit/parser/haml_options.rb', line 182

def trace
  @trace
end

#uglyObject

If set to ‘true`, Haml makes no attempt to properly indent or format the HTML output. This significantly improves rendering performance but makes viewing the source unpleasant.

Defaults to ‘true` in Rails production mode, and `false` everywhere else.



158
159
160
# File 'lib/hamlit/parser/haml_options.rb', line 158

def ugly
  @ugly
end

Class Method Details

.buffer_option_keysObject

An array of keys that will be used to provide a hash of options to Haml::Buffer.

Returns:

  • Hash



54
55
56
# File 'lib/hamlit/parser/haml_options.rb', line 54

def self.buffer_option_keys
  @buffer_option_keys
end

.defaultsObject

The default option values.

Returns:

  • Hash



41
42
43
# File 'lib/hamlit/parser/haml_options.rb', line 41

def self.defaults
  @defaults
end

.valid_formatsObject

An array of valid values for the ‘:format` option.

Returns:

  • Array



47
48
49
# File 'lib/hamlit/parser/haml_options.rb', line 47

def self.valid_formats
  @valid_formats
end

Instance Method Details

#[](key) ⇒ Object

Retrieve an option value.

Parameters:

  • key

    The value to retrieve.



192
193
194
# File 'lib/hamlit/parser/haml_options.rb', line 192

def [](key)
  send key
end

#[]=(key, value) ⇒ Object

Set an option value.

Parameters:

  • key

    The key to set.

  • value

    The value to set for the key.



199
200
201
# File 'lib/hamlit/parser/haml_options.rb', line 199

def []=(key, value)
  send "#{key}=", value
end

#for_buffer{Symbol => Object}

Returns a subset of options: those that Haml::Buffer cares about. All of the values here are such that when ‘#inspect` is called on the hash, it can be `Kernel#eval`ed to get the same result back.

See the Haml options documentation.

Returns:

  • ({Symbol => Object})

    The options hash



273
274
275
276
277
278
# File 'lib/hamlit/parser/haml_options.rb', line 273

def for_buffer
  self.class.buffer_option_keys.inject({}) do |hash, key|
    hash[key] = send(key)
    hash
  end
end

#html4?Boolean

Returns Whether or not the format is HTML4.

Returns:

  • (Boolean)

    Whether or not the format is HTML4.



223
224
225
# File 'lib/hamlit/parser/haml_options.rb', line 223

def html4?
  format == :html4
end

#html5?Boolean

Returns Whether or not the format is HTML5.

Returns:

  • (Boolean)

    Whether or not the format is HTML5.



228
229
230
# File 'lib/hamlit/parser/haml_options.rb', line 228

def html5?
  format == :html5
end

#html?Boolean

Returns Whether or not the format is any flavor of HTML.

Returns:

  • (Boolean)

    Whether or not the format is any flavor of HTML.



218
219
220
# File 'lib/hamlit/parser/haml_options.rb', line 218

def html?
  html4? or html5?
end

#xhtml?Boolean

Returns Whether or not the format is XHTML.

Returns:

  • (Boolean)

    Whether or not the format is XHTML.



213
214
215
# File 'lib/hamlit/parser/haml_options.rb', line 213

def xhtml?
  not html?
end