Sha256: b8fb2df5c4596b0c2df400974ae4b35c5ec977beb4abc320282939550d7fb73c

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

module Typedown

  class Shorthand

    def self.process body
      # Find all occurences mathcing the shorthand syntax
      # 'action/param1[/param2]
      offset = 0
      while(match = body[offset..-1].match(/\'\w+[\/\w+]+/)) do
        m = match[0]
        res = self.resolve m
        body.gsub!(m, res) if res
        offset += match.begin(0) + (res || m).length
      end
      body
    end


    private

    def self.resolve m
      begin
        params = m.tr("'", "").split("/")
        action = params.shift

        if self.shorthands[ action.downcase ]
          self.shorthands[ action.downcase ].resolve action, params
        else
          nil
        end
      rescue
        # Something wrong with parameters
        nil
      end
    end

    def self.shorthands
      @shorthands = {} unless @shorthands
      @shorthands
    end


    def self.add_shorthand name, obj
      self.shorthands[ name.downcase ] = obj
    end

    def initialize name
      Shorthand.add_shorthand(name, self)
    end

    def resolve action, params
      raise "Please subclass and override this method."
      nil
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
typedown-0.0.14 lib/typedown/shorthand.rb
typedown-0.0.13 lib/typedown/shorthand.rb
typedown-0.0.12 lib/typedown/shorthand.rb
typedown-0.0.11 lib/typedown/shorthand.rb
typedown-0.0.10 lib/typedown/shorthand.rb
typedown-0.0.9 lib/typedown/shorthand.rb
typedown-0.0.8 lib/typedown/shorthand.rb
typedown-0.0.7 lib/typedown/shorthand.rb