Class: Slim::Parser Private

Inherits:
Temple::Parser
  • Object
show all
Defined in:
lib/slim/parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Parses Slim code and transforms it to a Temple expression

Direct Known Subclasses

Smart::Parser

Defined Under Namespace

Classes: SyntaxError

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser



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
# File 'lib/slim/parser.rb', line 46

def initialize(opts = {})
  super
  @attr_list_delims = options[:attr_list_delims]
  @code_attr_delims = options[:code_attr_delims]
  tabsize = options[:tabsize]
  if tabsize > 1
    @tab_re = /\G((?: {#{tabsize}})*) {0,#{tabsize-1}}\t/
    @tab = '\1' + ' ' * tabsize
  else
    @tab_re = "\t"
    @tab = ' '
  end
  @tag_shortcut, @attr_shortcut, @additional_attrs = {}, {}, {}
  options[:shortcut].each do |k,v|
    raise ArgumentError, 'Shortcut requires :tag and/or :attr' unless (v[:attr] || v[:tag]) && (v.keys - [:attr, :tag, :additional_attrs]).empty?
    @tag_shortcut[k] = v[:tag] || options[:default_tag]
    if v.include?(:attr) || v.include?(:additional_attrs)
      raise ArgumentError, 'You can only use special characters for attribute shortcuts' if k =~ /(\p{Word}|-)/
    end
    if v.include?(:attr)
      @attr_shortcut[k] = [v[:attr]].flatten
    end
    if v.include?(:additional_attrs)
      @additional_attrs[k] = v[:additional_attrs]
    end
  end
  keys = Regexp.union @attr_shortcut.keys.sort_by {|k| -k.size }
  @attr_shortcut_re = /\A(#{keys}+)((?:\p{Word}|-)*)/
  keys = Regexp.union @tag_shortcut.keys.sort_by {|k| -k.size }
  @tag_re = /\A(?:#{keys}|\*(?=[^\s]+)|(\p{Word}(?:\p{Word}|:|-)*\p{Word}|\p{Word}+))/
  keys = Regexp.escape @code_attr_delims.keys.join
  @code_attr_delims_re = /\A[#{keys}]/
  keys = Regexp.escape @attr_list_delims.keys.join
  @attr_list_delims_re = /\A\s*([#{keys}])/
  @embedded_re = /\A(#{Regexp.union(Embedded.engines.keys.map(&:to_s))}):(\s*)/
  keys = Regexp.escape ('"\'></='.split(//) + @attr_list_delims.flatten + @code_attr_delims.flatten).uniq.join
  @attr_name = "\\A\\s*([^\0\s#{keys}]+)"
  @quoted_attr_re = /#{@attr_name}\s*=(=?)\s*("|')/
  @code_attr_re = /#{@attr_name}\s*=(=?)\s*/
end

Instance Method Details

#call(str) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compile string to Temple expression

Parameters:

  • str (String)

    Slim code

Returns:

  • (Array)

    Temple expression representing the code



91
92
93
94
95
96
97
98
99
# File 'lib/slim/parser.rb', line 91

def call(str)
  result = [:multi]
  reset(str.split(/\r?\n/), [result])

  parse_line while next_line

  reset
  result
end