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)


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

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)


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

def attribute_delimiters
  @attribute_delimiters
end

#ruby_attribute_delimitersHash (readonly)

Returns:

  • (Hash)


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

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
    


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

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.



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

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

#attribute_name_regexpRegexp

Returns:

  • (Regexp)


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

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.



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

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
    


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

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)


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

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

#quoted_attribute_regexpRegexp

Returns:

  • (Regexp)


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

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

#ruby_attribute_delimiter_regexpRegexp

Returns:

  • (Regexp)


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

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

#ruby_attribute_regexpRegexp

Returns:

  • (Regexp)


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

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.



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

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
    


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

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