Class: Slimi::Parser::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/slimi/parser.rb

Overview

Convert human-friendly options into machine-friendly objects.

Constant Summary collapse

EMBEDDED_TEMPLATE_ENGINE_NAMES =
%w[
  coffee
  css
  javascript
  less
  markdown
  rdoc
  ruby
  sass
  scss
  textile
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_delimiters:, default_tag:, ruby_attribute_delimiters:, shortcut:) ⇒ Factory

Returns a new instance of Factory.

Parameters:

  • attribute_delimiters (Hash)
  • default_tag (String)
  • ruby_attribute_delimiters (Hash)
  • shortcut (Hash)


604
605
606
607
608
609
# File 'lib/slimi/parser.rb', line 604

def initialize(attribute_delimiters:, default_tag:, ruby_attribute_delimiters:, shortcut:)
  @attribute_delimiters = attribute_delimiters
  @default_tag = default_tag
  @ruby_attribute_delimiters = ruby_attribute_delimiters
  @shortcut = shortcut
end

Instance Attribute Details

#attribute_delimitersHash (readonly)

Returns:

  • (Hash)


595
596
597
# File 'lib/slimi/parser.rb', line 595

def attribute_delimiters
  @attribute_delimiters
end

#ruby_attribute_delimitersHash (readonly)

Returns:

  • (Hash)


598
599
600
# File 'lib/slimi/parser.rb', line 598

def ruby_attribute_delimiters
  @ruby_attribute_delimiters
end

Instance Method Details

#additional_attributesHash

Returns e.g. ‘{ “.” => { “a” => “b” }}` ^^^ ^^^ ^^^

|        |       `- attribute value
|         `- attribute key
 `- marker.

Returns:

  • (Hash)

    e.g. ‘{ “.” => { “a” => “b” }}` ^^^ ^^^ ^^^

    |        |       `- attribute value
    |         `- attribute key
     `- marker
    


616
617
618
619
620
# File 'lib/slimi/parser.rb', line 616

def additional_attributes
  @additional_attributes ||= @shortcut.each_with_object({}) do |(marker, details), result|
    result[marker] = details[:additional_attrs] if details.key?(:additional_attrs)
  end
end

#attribute_delimiter_regexpRegexp

Returns Pattern that matches to attribute delimiter.

Returns:

  • (Regexp)

    Pattern that matches to attribute delimiter.



643
644
645
646
# File 'lib/slimi/parser.rb', line 643

def attribute_delimiter_regexp
  delimiters_regexp = ::Regexp.union(@attribute_delimiters.keys)
  /[ \t]*(#{delimiters_regexp})/
end

#attribute_name_regexpRegexp

Returns:

  • (Regexp)


649
650
651
652
653
654
# File 'lib/slimi/parser.rb', line 649

def attribute_name_regexp
  @attribute_name_regexp ||= begin
    characters = ::Regexp.escape(@attribute_delimiters.flatten.uniq.join)
    %r{[ \t]*([^\0 \t\r\n"'<>/=#{characters}]+)}
  end
end

#attribute_shortcut_regexpRegexp

Returns Pattern that matches to attribute shortcuts part.

Returns:

  • (Regexp)

    Pattern that matches to attribute shortcuts part.



657
658
659
660
661
# File 'lib/slimi/parser.rb', line 657

def attribute_shortcut_regexp
  markers = attribute_shortcuts.keys.sort_by { |marker| -marker.size }
  markers_regexp = ::Regexp.union(markers)
  %r{(#{markers_regexp}+)((?:\p{Word}|-|/\d+|:(\w|-)+)*)}
end

#attribute_shortcutsHash

Returns e.g. ‘{ “.” => [“class”] }` ^^^ ^^^^^^^

|            `- attribute name
 `- marker.

Returns:

  • (Hash)

    e.g. ‘{ “.” => [“class”] }` ^^^ ^^^^^^^

    |            `- attribute name
     `- marker
    


626
627
628
629
630
# File 'lib/slimi/parser.rb', line 626

def attribute_shortcuts
  @attribute_shortcuts ||= @shortcut.each_with_object({}) do |(marker, details), result|
    result[marker] = Array(details[:attr]) if details.key?(:attr)
  end
end

#embedded_template_regexpRegexp

Returns:

  • (Regexp)


669
670
671
# File 'lib/slimi/parser.rb', line 669

def embedded_template_regexp
  /(#{::Regexp.union(EMBEDDED_TEMPLATE_ENGINE_NAMES)})(?:[ \t]*(?:(.*)))?:([ \t]*)/
end

#quoted_attribute_regexpRegexp

Returns:

  • (Regexp)


674
675
676
# File 'lib/slimi/parser.rb', line 674

def quoted_attribute_regexp
  /#{attribute_name_regexp}[ \t]*=(=?)[ \t]*("|')/
end

#ruby_attribute_delimiter_regexpRegexp

Returns:

  • (Regexp)


679
680
681
# File 'lib/slimi/parser.rb', line 679

def ruby_attribute_delimiter_regexp
  ::Regexp.union(@ruby_attribute_delimiters.keys)
end

#ruby_attribute_regexpRegexp

Returns:

  • (Regexp)


664
665
666
# File 'lib/slimi/parser.rb', line 664

def ruby_attribute_regexp
  /#{attribute_name_regexp}[ \t]*=(=?)[ \t]*/
end

#tag_name_regexpRegexp

Returns Pattern that matches to tag header part.

Returns:

  • (Regexp)

    Pattern that matches to tag header part.



684
685
686
687
688
# File 'lib/slimi/parser.rb', line 684

def tag_name_regexp
  markers = tag_shortcuts.keys.sort_by { |marker| -marker.size }
  markers_regexp = ::Regexp.union(markers)
  /#{markers_regexp}|\*(?=[^ \t]+)|(\p{Word}(?:\p{Word}|:|-)*\p{Word}|\p{Word}+)/
end

#tag_shortcutsHash

Returns e.g. ‘{ “.” => “div” }` ^^^ ^^^^^

|         `- tag name
 `- marker.

Returns:

  • (Hash)

    e.g. ‘{ “.” => “div” }` ^^^ ^^^^^

    |         `- tag name
     `- marker
    


636
637
638
639
640
# File 'lib/slimi/parser.rb', line 636

def tag_shortcuts
  @tag_shortcuts ||= @shortcut.transform_values do |details|
    details[:tag] || @default_tag
  end
end