Class: Slimi::Parser::Factory
- Inherits:
-
Object
- Object
- Slimi::Parser::Factory
- 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
- #attribute_delimiters ⇒ Hash readonly
- #ruby_attribute_delimiters ⇒ Hash readonly
Instance Method Summary collapse
-
#additional_attributes ⇒ Hash
E.g.
-
#attribute_delimiter_regexp ⇒ Regexp
Pattern that matches to attribute delimiter.
- #attribute_name_regexp ⇒ Regexp
-
#attribute_shortcut_regexp ⇒ Regexp
Pattern that matches to attribute shortcuts part.
-
#attribute_shortcuts ⇒ Hash
E.g.
- #embedded_template_regexp ⇒ Regexp
-
#initialize(attribute_delimiters:, default_tag:, ruby_attribute_delimiters:, shortcut:) ⇒ Factory
constructor
A new instance of Factory.
- #quoted_attribute_regexp ⇒ Regexp
- #ruby_attribute_delimiter_regexp ⇒ Regexp
- #ruby_attribute_regexp ⇒ Regexp
-
#tag_name_regexp ⇒ Regexp
Pattern that matches to tag header part.
-
#tag_shortcuts ⇒ Hash
E.g.
Constructor Details
#initialize(attribute_delimiters:, default_tag:, ruby_attribute_delimiters:, shortcut:) ⇒ Factory
Returns a new instance of Factory.
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_delimiters ⇒ Hash (readonly)
595 596 597 |
# File 'lib/slimi/parser.rb', line 595 def attribute_delimiters @attribute_delimiters end |
#ruby_attribute_delimiters ⇒ Hash (readonly)
598 599 600 |
# File 'lib/slimi/parser.rb', line 598 def ruby_attribute_delimiters @ruby_attribute_delimiters end |
Instance Method Details
#additional_attributes ⇒ Hash
Returns 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_regexp ⇒ Regexp
Returns 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_regexp ⇒ 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_regexp ⇒ Regexp
Returns 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_shortcuts ⇒ Hash
Returns 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_regexp ⇒ Regexp
669 670 671 |
# File 'lib/slimi/parser.rb', line 669 def /(#{::Regexp.union(EMBEDDED_TEMPLATE_ENGINE_NAMES)})(?:[ \t]*(?:(.*)))?:([ \t]*)/ end |
#quoted_attribute_regexp ⇒ Regexp
674 675 676 |
# File 'lib/slimi/parser.rb', line 674 def quoted_attribute_regexp /#{attribute_name_regexp}[ \t]*=(=?)[ \t]*("|')/ end |
#ruby_attribute_delimiter_regexp ⇒ 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_regexp ⇒ Regexp
664 665 666 |
# File 'lib/slimi/parser.rb', line 664 def ruby_attribute_regexp /#{attribute_name_regexp}[ \t]*=(=?)[ \t]*/ end |
#tag_name_regexp ⇒ Regexp
Returns 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_shortcuts ⇒ Hash
Returns 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 |