haml 5.2.2 → 6.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.github/workflows/test.yml +13 -9
  4. data/.gitignore +16 -16
  5. data/CHANGELOG.md +13 -3
  6. data/Gemfile +18 -11
  7. data/MIT-LICENSE +1 -1
  8. data/README.md +13 -19
  9. data/Rakefile +95 -93
  10. data/bin/bench +66 -0
  11. data/bin/console +11 -0
  12. data/bin/ruby +3 -0
  13. data/bin/setup +7 -0
  14. data/bin/stackprof +27 -0
  15. data/bin/test +24 -0
  16. data/exe/haml +6 -0
  17. data/ext/haml/extconf.rb +10 -0
  18. data/ext/haml/haml.c +537 -0
  19. data/ext/haml/hescape.c +108 -0
  20. data/ext/haml/hescape.h +20 -0
  21. data/haml.gemspec +39 -37
  22. data/lib/haml/ambles.rb +20 -0
  23. data/lib/haml/attribute_builder.rb +135 -179
  24. data/lib/haml/attribute_compiler.rb +85 -194
  25. data/lib/haml/attribute_parser.rb +86 -126
  26. data/lib/haml/cli.rb +154 -0
  27. data/lib/haml/compiler/children_compiler.rb +126 -0
  28. data/lib/haml/compiler/comment_compiler.rb +39 -0
  29. data/lib/haml/compiler/doctype_compiler.rb +46 -0
  30. data/lib/haml/compiler/script_compiler.rb +116 -0
  31. data/lib/haml/compiler/silent_script_compiler.rb +24 -0
  32. data/lib/haml/compiler/tag_compiler.rb +76 -0
  33. data/lib/haml/compiler.rb +63 -296
  34. data/lib/haml/dynamic_merger.rb +67 -0
  35. data/lib/haml/engine.rb +42 -227
  36. data/lib/haml/error.rb +3 -52
  37. data/lib/haml/escapable.rb +6 -70
  38. data/lib/haml/filters/base.rb +12 -0
  39. data/lib/haml/filters/cdata.rb +20 -0
  40. data/lib/haml/filters/coffee.rb +17 -0
  41. data/lib/haml/filters/css.rb +33 -0
  42. data/lib/haml/filters/erb.rb +10 -0
  43. data/lib/haml/filters/escaped.rb +22 -0
  44. data/lib/haml/filters/javascript.rb +33 -0
  45. data/lib/haml/filters/less.rb +20 -0
  46. data/lib/haml/filters/markdown.rb +11 -0
  47. data/lib/haml/filters/plain.rb +29 -0
  48. data/lib/haml/filters/preserve.rb +22 -0
  49. data/lib/haml/filters/ruby.rb +10 -0
  50. data/lib/haml/filters/sass.rb +15 -0
  51. data/lib/haml/filters/scss.rb +15 -0
  52. data/lib/haml/filters/text_base.rb +25 -0
  53. data/lib/haml/filters/tilt_base.rb +49 -0
  54. data/lib/haml/filters.rb +54 -378
  55. data/lib/haml/force_escapable.rb +29 -0
  56. data/lib/haml/haml_error.rb +66 -0
  57. data/lib/haml/helpers.rb +3 -697
  58. data/lib/haml/html.rb +22 -0
  59. data/lib/haml/identity.rb +13 -0
  60. data/lib/haml/object_ref.rb +30 -0
  61. data/lib/haml/parser.rb +179 -49
  62. data/lib/haml/rails_helpers.rb +51 -0
  63. data/lib/haml/rails_template.rb +55 -0
  64. data/lib/haml/railtie.rb +7 -45
  65. data/lib/haml/ruby_expression.rb +32 -0
  66. data/lib/haml/string_splitter.rb +20 -0
  67. data/lib/haml/template.rb +15 -34
  68. data/lib/haml/temple_line_counter.rb +2 -1
  69. data/lib/haml/util.rb +17 -15
  70. data/lib/haml/version.rb +1 -2
  71. data/lib/haml.rb +8 -20
  72. metadata +211 -57
  73. data/.gitmodules +0 -3
  74. data/.yardopts +0 -22
  75. data/TODO +0 -24
  76. data/benchmark.rb +0 -70
  77. data/bin/haml +0 -9
  78. data/lib/haml/.gitattributes +0 -1
  79. data/lib/haml/buffer.rb +0 -182
  80. data/lib/haml/exec.rb +0 -347
  81. data/lib/haml/generator.rb +0 -42
  82. data/lib/haml/helpers/action_view_extensions.rb +0 -60
  83. data/lib/haml/helpers/action_view_mods.rb +0 -132
  84. data/lib/haml/helpers/action_view_xss_mods.rb +0 -60
  85. data/lib/haml/helpers/safe_erubi_template.rb +0 -20
  86. data/lib/haml/helpers/safe_erubis_template.rb +0 -33
  87. data/lib/haml/helpers/xss_mods.rb +0 -114
  88. data/lib/haml/options.rb +0 -273
  89. data/lib/haml/plugin.rb +0 -54
  90. data/lib/haml/sass_rails_filter.rb +0 -47
  91. data/lib/haml/template/options.rb +0 -27
  92. data/lib/haml/temple_engine.rb +0 -124
  93. data/yard/default/.gitignore +0 -1
  94. data/yard/default/fulldoc/html/css/common.sass +0 -15
  95. data/yard/default/layout/html/footer.erb +0 -12
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Haml
4
- module Helpers
5
- @@action_view_defined = true
6
-
7
- # This module contains various useful helper methods
8
- # that either tie into ActionView or the rest of the ActionPack stack,
9
- # or are only useful in that context.
10
- # Thus, the methods defined here are only available
11
- # if ActionView is installed.
12
- module ActionViewExtensions
13
- # Returns a value for the "class" attribute
14
- # unique to this controller/action pair.
15
- # This can be used to target styles specifically at this action or controller.
16
- # For example, if the current action were `EntryController#show`,
17
- #
18
- # %div{:class => page_class} My Div
19
- #
20
- # would become
21
- #
22
- # <div class="entry show">My Div</div>
23
- #
24
- # Then, in a stylesheet (shown here as [Sass](http://sass-lang.com)),
25
- # you could refer to this specific action:
26
- #
27
- # .entry.show
28
- # font-weight: bold
29
- #
30
- # or to all actions in the entry controller:
31
- #
32
- # .entry
33
- # color: #00f
34
- #
35
- # @return [String] The class name for the current page
36
- def page_class
37
- "#{controller.controller_name} #{controller.action_name}"
38
- end
39
- alias_method :generate_content_class_names, :page_class
40
-
41
- # Treats all input to \{Haml::Helpers#haml\_concat} within the block
42
- # as being HTML safe for Rails' XSS protection.
43
- # This is useful for wrapping blocks of code that concatenate HTML en masse.
44
- #
45
- # This has no effect if Rails' XSS protection isn't enabled.
46
- #
47
- # @yield A block in which all input to `#haml_concat` is treated as raw.
48
- # @see Haml::Util#rails_xss_safe?
49
- def with_raw_haml_concat
50
- old = instance_variable_defined?(:@_haml_concat_raw) ? @_haml_concat_raw : false
51
- @_haml_concat_raw = true
52
- yield
53
- ensure
54
- @_haml_concat_raw = old
55
- end
56
- end
57
-
58
- include ActionViewExtensions
59
- end
60
- end
@@ -1,132 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Haml
4
- module Helpers
5
- module ActionViewMods
6
- def render(*args, &block)
7
- options = args.first
8
-
9
- # If render :layout is used with a block, it concats rather than returning
10
- # a string so we need it to keep thinking it's Haml until it hits the
11
- # sub-render.
12
- if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
13
- return non_haml { super }
14
- end
15
- super
16
- end
17
-
18
- def output_buffer
19
- return haml_buffer.buffer if is_haml?
20
- super
21
- end
22
-
23
- def output_buffer=(new_buffer)
24
- if is_haml?
25
- if Haml::Util.rails_xss_safe? && new_buffer.is_a?(ActiveSupport::SafeBuffer)
26
- new_buffer = String.new(new_buffer)
27
- end
28
- haml_buffer.buffer = new_buffer
29
- else
30
- super
31
- end
32
- end
33
- end
34
- ActionView::Base.send(:prepend, ActionViewMods)
35
- end
36
- end
37
-
38
- module ActionView
39
- module Helpers
40
- module CaptureHelper
41
- def capture_with_haml(*args, &block)
42
- if Haml::Helpers.block_is_haml?(block)
43
- #double assignment is to avoid warnings
44
- _hamlout = _hamlout = eval('_hamlout', block.binding) # Necessary since capture_haml checks _hamlout
45
-
46
- capture_haml(*args, &block)
47
- else
48
- capture_without_haml(*args, &block)
49
- end
50
- end
51
- alias_method :capture_without_haml, :capture
52
- alias_method :capture, :capture_with_haml
53
- end
54
-
55
- module TagHelper
56
- DEFAULT_PRESERVE_OPTIONS = %w(textarea pre code).freeze
57
-
58
- def content_tag_with_haml(name, *args, &block)
59
- return content_tag_without_haml(name, *args, &block) unless is_haml?
60
-
61
- preserve = haml_buffer.options.fetch(:preserve, DEFAULT_PRESERVE_OPTIONS).include?(name.to_s)
62
-
63
- if block_given? && block_is_haml?(block) && preserve
64
- return content_tag_without_haml(name, *args) do
65
- haml_buffer.fix_textareas!(Haml::Helpers.preserve(&block)).html_safe
66
- end
67
- end
68
-
69
- content = content_tag_without_haml(name, *args, &block)
70
- if preserve && content
71
- content = haml_buffer.fix_textareas!(Haml::Helpers.preserve(content)).html_safe
72
- end
73
- content
74
- end
75
-
76
- alias_method :content_tag_without_haml, :content_tag
77
- alias_method :content_tag, :content_tag_with_haml
78
- end
79
-
80
- module HamlSupport
81
- include Haml::Helpers
82
-
83
- def haml_buffer
84
- @template_object.send :haml_buffer
85
- end
86
-
87
- def is_haml?
88
- @template_object.send :is_haml?
89
- end
90
- end
91
-
92
- module Tags
93
- class TextArea
94
- include HamlSupport
95
- end
96
- end
97
-
98
- class InstanceTag
99
- include HamlSupport
100
-
101
- def content_tag(*args, &block)
102
- html_tag = content_tag_with_haml(*args, &block)
103
- return html_tag unless respond_to?(:error_wrapping)
104
- return error_wrapping(html_tag) if method(:error_wrapping).arity == 1
105
- return html_tag unless object.respond_to?(:errors) && object.errors.respond_to?(:on)
106
- return error_wrapping(html_tag, object.errors.on(@method_name))
107
- end
108
- end
109
-
110
- module FormTagHelper
111
- def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
112
- if is_haml?
113
- wrap_block = block_given? && block_is_haml?(proc)
114
- if wrap_block
115
- oldproc = proc
116
- proc = haml_bind_proc do |*args|
117
- concat "\n"
118
- with_tabs(1) {oldproc.call(*args)}
119
- end
120
- end
121
- res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) << "\n"
122
- res << "\n" if wrap_block
123
- res
124
- else
125
- form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
126
- end
127
- end
128
- alias_method :form_tag_without_haml, :form_tag
129
- alias_method :form_tag, :form_tag_with_haml
130
- end
131
- end
132
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActionView
4
- module Helpers
5
- module CaptureHelper
6
- def with_output_buffer_with_haml_xss(*args, &block)
7
- res = with_output_buffer_without_haml_xss(*args, &block)
8
- case res
9
- when Array; res.map {|s| Haml::Util.html_safe(s)}
10
- when String; Haml::Util.html_safe(res)
11
- else; res
12
- end
13
- end
14
- alias_method :with_output_buffer_without_haml_xss, :with_output_buffer
15
- alias_method :with_output_buffer, :with_output_buffer_with_haml_xss
16
- end
17
-
18
- module FormTagHelper
19
- def form_tag_with_haml_xss(*args, &block)
20
- res = form_tag_without_haml_xss(*args, &block)
21
- res = Haml::Util.html_safe(res) unless block_given?
22
- res
23
- end
24
- alias_method :form_tag_without_haml_xss, :form_tag
25
- alias_method :form_tag, :form_tag_with_haml_xss
26
- end
27
-
28
- module FormHelper
29
- def form_for_with_haml_xss(*args, &block)
30
- res = form_for_without_haml_xss(*args, &block)
31
- return Haml::Util.html_safe(res) if res.is_a?(String)
32
- return res
33
- end
34
- alias_method :form_for_without_haml_xss, :form_for
35
- alias_method :form_for, :form_for_with_haml_xss
36
- end
37
-
38
- module TextHelper
39
- def concat_with_haml_xss(string)
40
- if is_haml?
41
- haml_buffer.buffer.concat(haml_xss_html_escape(string))
42
- else
43
- concat_without_haml_xss(string)
44
- end
45
- end
46
- alias_method :concat_without_haml_xss, :concat
47
- alias_method :concat, :concat_with_haml_xss
48
-
49
- def safe_concat_with_haml_xss(string)
50
- if is_haml?
51
- haml_buffer.buffer.concat(string)
52
- else
53
- safe_concat_without_haml_xss(string)
54
- end
55
- end
56
- alias_method :safe_concat_without_haml_xss, :safe_concat
57
- alias_method :safe_concat, :safe_concat_with_haml_xss
58
- end
59
- end
60
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'action_view'
4
-
5
- module Haml
6
- class ErubiTemplateHandler < ActionView::Template::Handlers::ERB::Erubi
7
-
8
- def initialize(*args, &blk)
9
- @newline_pending = 0
10
- super
11
- end
12
- end
13
-
14
- class SafeErubiTemplate < Tilt::ErubiTemplate
15
- def prepare
16
- @options.merge! engine_class: Haml::ErubiTemplateHandler
17
- super
18
- end
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'action_view'
4
-
5
- module Haml
6
-
7
- class ErubisTemplateHandler < ActionView::Template::Handlers::Erubis
8
-
9
- def initialize(*args, &blk)
10
- @newline_pending = 0
11
- super
12
- end
13
- end
14
-
15
- class SafeErubisTemplate < Tilt::ErubisTemplate
16
-
17
- def initialize_engine
18
- end
19
-
20
- def prepare
21
- @options.merge! :engine_class => Haml::ErubisTemplateHandler
22
- super
23
- end
24
-
25
- def precompiled_preamble(locals)
26
- [super, "@output_buffer = ActionView::OutputBuffer.new;"].join("\n")
27
- end
28
-
29
- def precompiled_postamble(locals)
30
- [super, '@output_buffer.to_s'].join("\n")
31
- end
32
- end
33
- end
@@ -1,114 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Haml
4
- module Helpers
5
- # This module overrides Haml helpers to work properly
6
- # in the context of ActionView.
7
- # Currently it's only used for modifying the helpers
8
- # to work with Rails' XSS protection methods.
9
- module XssMods
10
- def self.included(base)
11
- %w[find_and_preserve preserve list_of surround
12
- precede succeed capture_haml haml_concat haml_internal_concat haml_indent].each do |name|
13
- base.send(:alias_method, "#{name}_without_haml_xss", name)
14
- base.send(:alias_method, name, "#{name}_with_haml_xss")
15
- end
16
- # Those two always have _without_haml_xss
17
- %w[html_escape escape_once].each do |name|
18
- base.send(:alias_method, name, "#{name}_with_haml_xss")
19
- end
20
- end
21
-
22
- # Don't escape text that's already safe,
23
- # output is always HTML safe
24
- def html_escape_with_haml_xss(text)
25
- str = text.to_s
26
- return text if str.html_safe?
27
- Haml::Util.html_safe(html_escape_without_haml_xss(str))
28
- end
29
-
30
- # Output is always HTML safe
31
- def find_and_preserve_with_haml_xss(*args, &block)
32
- Haml::Util.html_safe(find_and_preserve_without_haml_xss(*args, &block))
33
- end
34
-
35
- # Output is always HTML safe
36
- def preserve_with_haml_xss(*args, &block)
37
- Haml::Util.html_safe(preserve_without_haml_xss(*args, &block))
38
- end
39
-
40
- # Output is always HTML safe
41
- def list_of_with_haml_xss(*args, &block)
42
- Haml::Util.html_safe(list_of_without_haml_xss(*args, &block))
43
- end
44
-
45
- # Input is escaped, output is always HTML safe
46
- def surround_with_haml_xss(front, back = front, &block)
47
- Haml::Util.html_safe(
48
- surround_without_haml_xss(
49
- haml_xss_html_escape(front),
50
- haml_xss_html_escape(back),
51
- &block))
52
- end
53
-
54
- # Input is escaped, output is always HTML safe
55
- def precede_with_haml_xss(str, &block)
56
- Haml::Util.html_safe(precede_without_haml_xss(haml_xss_html_escape(str), &block))
57
- end
58
-
59
- # Input is escaped, output is always HTML safe
60
- def succeed_with_haml_xss(str, &block)
61
- Haml::Util.html_safe(succeed_without_haml_xss(haml_xss_html_escape(str), &block))
62
- end
63
-
64
- # Output is always HTML safe
65
- def capture_haml_with_haml_xss(*args, &block)
66
- Haml::Util.html_safe(capture_haml_without_haml_xss(*args, &block))
67
- end
68
-
69
- # Input will be escaped unless this is in a `with_raw_haml_concat`
70
- # block. See #Haml::Helpers::ActionViewExtensions#with_raw_haml_concat.
71
- def haml_concat_with_haml_xss(text = "")
72
- raw = instance_variable_defined?(:@_haml_concat_raw) ? @_haml_concat_raw : false
73
- if raw
74
- haml_internal_concat_raw text
75
- else
76
- haml_internal_concat text
77
- end
78
- ErrorReturn.new("haml_concat")
79
- end
80
-
81
- # Input is escaped
82
- def haml_internal_concat_with_haml_xss(text="", newline=true, indent=true)
83
- haml_internal_concat_without_haml_xss(haml_xss_html_escape(text), newline, indent)
84
- end
85
- private :haml_internal_concat_with_haml_xss
86
-
87
- # Output is always HTML safe
88
- def haml_indent_with_haml_xss
89
- Haml::Util.html_safe(haml_indent_without_haml_xss)
90
- end
91
-
92
- # Output is always HTML safe
93
- def escape_once_with_haml_xss(*args)
94
- Haml::Util.html_safe(escape_once_without_haml_xss(*args))
95
- end
96
-
97
- private
98
-
99
- # Escapes the HTML in the text if and only if
100
- # Rails XSS protection is enabled *and* the `:escape_html` option is set.
101
- def haml_xss_html_escape(text)
102
- return text unless Haml::Util.rails_xss_safe? && haml_buffer.options[:escape_html]
103
- html_escape(text)
104
- end
105
- end
106
-
107
- class ErrorReturn
108
- # Any attempt to treat ErrorReturn as a string should cause it to blow up.
109
- alias_method :html_safe, :to_s
110
- alias_method :html_safe?, :to_s
111
- alias_method :html_safe!, :to_s
112
- end
113
- end
114
- end
data/lib/haml/options.rb DELETED
@@ -1,273 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Haml
4
- # This class encapsulates all of the configuration options that Haml
5
- # understands. Please see the {file:REFERENCE.md#options Haml Reference} to
6
- # learn how to set the options.
7
- class Options
8
- @valid_formats = [:html4, :html5, :xhtml]
9
- @buffer_option_keys = [:autoclose, :preserve, :attr_wrapper, :format,
10
- :encoding, :escape_html, :escape_filter_interpolations, :escape_attrs, :hyphenate_data_attrs, :cdata]
11
-
12
- class << self
13
- # The default option values.
14
- # @return Hash
15
- def defaults
16
- @defaults ||= Haml::TempleEngine.options.to_hash.merge(encoding: 'UTF-8')
17
- end
18
-
19
- # An array of valid values for the `:format` option.
20
- # @return Array
21
- attr_reader :valid_formats
22
-
23
- # An array of keys that will be used to provide a hash of options to
24
- # {Haml::Buffer}.
25
- # @return Hash
26
- attr_reader :buffer_option_keys
27
-
28
- # Returns a subset of defaults: those that {Haml::Buffer} cares about.
29
- # @return [{Symbol => Object}] The options hash
30
- def buffer_defaults
31
- @buffer_defaults ||= buffer_option_keys.inject({}) do |hash, key|
32
- hash.merge(key => defaults[key])
33
- end
34
- end
35
-
36
- def wrap(options)
37
- if options.is_a?(Options)
38
- options
39
- else
40
- Options.new(options)
41
- end
42
- end
43
- end
44
-
45
- # The character that should wrap element attributes. This defaults to `'`
46
- # (an apostrophe). Characters of this type within the attributes will be
47
- # escaped (e.g. by replacing them with `&apos;`) if the character is an
48
- # apostrophe or a quotation mark.
49
- attr_reader :attr_wrapper
50
-
51
- # A list of tag names that should be automatically self-closed if they have
52
- # no content. This can also contain regular expressions that match tag names
53
- # (or any object which responds to `#===`). Defaults to `['meta', 'img',
54
- # 'link', 'br', 'hr', 'input', 'area', 'param', 'col', 'base']`.
55
- attr_accessor :autoclose
56
-
57
- # The encoding to use for the HTML output.
58
- # This can be a string or an `Encoding` Object. Note that Haml **does not**
59
- # automatically re-encode Ruby values; any strings coming from outside the
60
- # application should be converted before being passed into the Haml
61
- # template. Defaults to `Encoding.default_internal`; if that's not set,
62
- # defaults to the encoding of the Haml template; if that's `US-ASCII`,
63
- # defaults to `"UTF-8"`.
64
- attr_reader :encoding
65
-
66
- # Sets whether or not to escape HTML-sensitive characters in attributes. If
67
- # this is true, all HTML-sensitive characters in attributes are escaped. If
68
- # it's set to false, no HTML-sensitive characters in attributes are escaped.
69
- # If it's set to `:once`, existing HTML escape sequences are preserved, but
70
- # other HTML-sensitive characters are escaped.
71
- #
72
- # Defaults to `true`.
73
- attr_accessor :escape_attrs
74
-
75
- # Sets whether or not to escape HTML-sensitive characters in script. If this
76
- # is true, `=` behaves like {file:REFERENCE.md#escaping_html `&=`};
77
- # otherwise, it behaves like {file:REFERENCE.md#unescaping_html `!=`}. Note
78
- # that if this is set, `!=` should be used for yielding to subtemplates and
79
- # rendering partials. See also {file:REFERENCE.md#escaping_html Escaping HTML} and
80
- # {file:REFERENCE.md#unescaping_html Unescaping HTML}.
81
- #
82
- # Defaults to false.
83
- attr_accessor :escape_html
84
-
85
- # Sets whether or not to escape HTML-sensitive characters in interpolated strings.
86
- # See also {file:REFERENCE.md#escaping_html Escaping HTML} and
87
- # {file:REFERENCE.md#unescaping_html Unescaping HTML}.
88
- #
89
- # Defaults to the current value of `escape_html`.
90
- attr_accessor :escape_filter_interpolations
91
-
92
- # The name of the Haml file being parsed.
93
- # This is only used as information when exceptions are raised. This is
94
- # automatically assigned when working through ActionView, so it's really
95
- # only useful for the user to assign when dealing with Haml programatically.
96
- attr_accessor :filename
97
-
98
- # If set to `true`, Haml will convert underscores to hyphens in all
99
- # {file:REFERENCE.md#html5_custom_data_attributes Custom Data Attributes} As
100
- # of Haml 4.0, this defaults to `true`.
101
- attr_accessor :hyphenate_data_attrs
102
-
103
- # The line offset of the Haml template being parsed. This is useful for
104
- # inline templates, similar to the last argument to `Kernel#eval`.
105
- attr_accessor :line
106
-
107
- # Determines the output format. The default is `:html5`. The other options
108
- # are `:html4` and `:xhtml`. If the output is set to XHTML, then Haml
109
- # automatically generates self-closing tags and wraps the output of the
110
- # Javascript and CSS-like filters inside CDATA. When the output is set to
111
- # `:html5` or `:html4`, XML prologs are ignored. In all cases, an appropriate
112
- # doctype is generated from `!!!`.
113
- #
114
- # If the mime_type of the template being rendered is `text/xml` then a
115
- # format of `:xhtml` will be used even if the global output format is set to
116
- # `:html4` or `:html5`.
117
- attr :format
118
-
119
- # The mime type that the rendered document will be served with. If this is
120
- # set to `text/xml` then the format will be overridden to `:xhtml` even if
121
- # it has set to `:html4` or `:html5`.
122
- attr_accessor :mime_type
123
-
124
- # A list of tag names that should automatically have their newlines
125
- # preserved using the {Haml::Helpers#preserve} helper. This means that any
126
- # content given on the same line as the tag will be preserved. For example,
127
- # `%textarea= "Foo\nBar"` compiles to `<textarea>Foo&#x000A;Bar</textarea>`.
128
- # Defaults to `['textarea', 'pre']`. See also
129
- # {file:REFERENCE.md#whitespace_preservation Whitespace Preservation}.
130
- attr_accessor :preserve
131
-
132
- # If set to `true`, all tags are treated as if both
133
- # {file:REFERENCE.md#whitespace_removal__and_ whitespace removal} options
134
- # were present. Use with caution as this may cause whitespace-related
135
- # formatting errors.
136
- #
137
- # Defaults to `false`.
138
- attr_accessor :remove_whitespace
139
-
140
- # Whether or not attribute hashes and Ruby scripts designated by `=` or `~`
141
- # should be evaluated. If this is `true`, said scripts are rendered as empty
142
- # strings.
143
- #
144
- # Defaults to `false`.
145
- attr_accessor :suppress_eval
146
-
147
- # Whether to include CDATA sections around javascript and css blocks when
148
- # using the `:javascript` or `:css` filters.
149
- #
150
- # This option also affects the `:sass`, `:scss`, `:less` and `:coffeescript`
151
- # filters.
152
- #
153
- # Defaults to `false` for html, `true` for xhtml. Cannot be changed when using
154
- # xhtml.
155
- attr_accessor :cdata
156
-
157
- # The parser class to use. Defaults to Haml::Parser.
158
- attr_accessor :parser_class
159
-
160
- # The compiler class to use. Defaults to Haml::Compiler.
161
- attr_accessor :compiler_class
162
-
163
- # Enable template tracing. If true, it will add a 'data-trace' attribute to
164
- # each tag generated by Haml. The value of the attribute will be the
165
- # source template name and the line number from which the tag was generated,
166
- # separated by a colon. On Rails applications, the path given will be a
167
- # relative path as from the views directory. On non-Rails applications,
168
- # the path will be the full path.
169
- attr_accessor :trace
170
-
171
- # Key is filter name in String and value is Class to use. Defaults to {}.
172
- attr_accessor :filters
173
-
174
- def initialize(values = {})
175
- defaults.each {|k, v| instance_variable_set :"@#{k}", v}
176
- values.each {|k, v| send("#{k}=", v) if defaults.has_key?(k) && !v.nil?}
177
- yield if block_given?
178
- end
179
-
180
- # Retrieve an option value.
181
- # @param key The value to retrieve.
182
- def [](key)
183
- send key
184
- end
185
-
186
- # Set an option value.
187
- # @param key The key to set.
188
- # @param value The value to set for the key.
189
- def []=(key, value)
190
- send "#{key}=", value
191
- end
192
-
193
- [:escape_attrs, :hyphenate_data_attrs, :remove_whitespace, :suppress_eval].each do |method|
194
- class_eval(<<-END)
195
- def #{method}?
196
- !! @#{method}
197
- end
198
- END
199
- end
200
-
201
- # @return [Boolean] Whether or not the format is XHTML.
202
- def xhtml?
203
- not html?
204
- end
205
-
206
- # @return [Boolean] Whether or not the format is any flavor of HTML.
207
- def html?
208
- html4? or html5?
209
- end
210
-
211
- # @return [Boolean] Whether or not the format is HTML4.
212
- def html4?
213
- format == :html4
214
- end
215
-
216
- # @return [Boolean] Whether or not the format is HTML5.
217
- def html5?
218
- format == :html5
219
- end
220
-
221
- def attr_wrapper=(value)
222
- @attr_wrapper = value || self.class.defaults[:attr_wrapper]
223
- end
224
-
225
- # Undef :format to suppress warning. It's defined above with the `:attr`
226
- # macro in order to make it appear in Yard's list of instance attributes.
227
- undef :format
228
- def format
229
- mime_type == "text/xml" ? :xhtml : @format
230
- end
231
-
232
- def format=(value)
233
- unless self.class.valid_formats.include?(value)
234
- raise Haml::Error, "Invalid output format #{value.inspect}"
235
- end
236
- @format = value
237
- end
238
-
239
- undef :cdata
240
- def cdata
241
- xhtml? || @cdata
242
- end
243
-
244
- def encoding=(value)
245
- return unless value
246
- @encoding = value.is_a?(Encoding) ? value.name : value.to_s
247
- @encoding = "UTF-8" if @encoding.upcase == "US-ASCII"
248
- end
249
-
250
- # Returns a non-default subset of options: those that {Haml::Buffer} cares about.
251
- # All of the values here are such that when `#inspect` is called on the hash,
252
- # it can be `Kernel#eval`ed to get the same result back.
253
- #
254
- # See {file:REFERENCE.md#options the Haml options documentation}.
255
- #
256
- # @return [{Symbol => Object}] The options hash
257
- def for_buffer
258
- self.class.buffer_option_keys.inject({}) do |hash, key|
259
- value = public_send(key)
260
- if self.class.buffer_defaults[key] != value
261
- hash[key] = value
262
- end
263
- hash
264
- end
265
- end
266
-
267
- private
268
-
269
- def defaults
270
- self.class.defaults
271
- end
272
- end
273
- end