Module: Token::Handler
- Defined in:
- lib/token/handler.rb,
lib/code_object/function.rb
Overview
TODO:
rewrite as default-handler, because this looks a little distracting
Constant Summary collapse
- ALL =
/./m- NO_BR =
/((?!\n)\s)/- IDENTIFIER =
/(?:[^\s])*/- TYPELIST =
/\[(?<types>#{IDENTIFIER}(?:,#{NO_BR}*#{IDENTIFIER})*)\]/- TOKEN_W_TYPE =
Token with type and content
/#{NO_BR}*#{TYPELIST}#{NO_BR}*(?<content>#{ALL}*)/- TOKEN_W_TYPE_NAME =
Token with type, name and content
/^#{NO_BR}* #{TYPELIST}#{NO_BR}* (?<name>#{IDENTIFIER}) #{NO_BR}* (?<content>#{ALL}*) /x- @@defaults =
{ :text_only => ->(tokenklass, content) { self.add_token tokenklass.new(:content => content) }, :typed => ->(tokenklass, content) { typestring, content = TOKEN_W_TYPE.match(content).captures types = typestring.split /,\s*/ self.add_token tokenklass.new(:types => types, :content => content) }, :typed_with_name => ->(tokenklass, content) { typestring, name, content = TOKEN_W_TYPE_NAME.match(content).captures types = typestring.split /,\s*/ self.add_token tokenklass.new(:name => name, :types => types, :content => content) }, :named_multiline => ->(tokenklass, content) { rows = content.split(/\n/) # use first row as name name = rows.shift.strip content = rows.join("\n") self.add_token tokenklass.new(:name => name, :content => content) }, :noop => ->(tokenklass, content) {} }
- @@handlers =
{}
Class Method Summary collapse
- .add_default_handler(name, &block) ⇒ Object
-
.handlers ⇒ Hash<Symbol, Block>
Attribute-Reader for all registered ‘@@handlers`.
-
.register(tokenname, options = {}, &handler) ⇒ Object
Registering a new Tokenhandler ============================== It is possible to register your own Tokenhandlers and therefore extend the capabilities of this documentation-program.
-
.unregister(tokenname) ⇒ Object
Remove a registered handler from the list.
Class Method Details
.add_default_handler(name, &block) ⇒ Object
237 238 239 |
# File 'lib/token/handler.rb', line 237 def self.add_default_handler(name, &block) @@defaults[name] = block; end |
.handlers ⇒ Hash<Symbol, Block>
Attribute-Reader for all registered ‘@@handlers`
106 107 108 |
# File 'lib/token/handler.rb', line 106 def self.handlers @@handlers end |
.self.register(tokenname, type = nil) ⇒ Object .self.register(tokenname) {|tokenklass, stringcontent| ... } ⇒ Object
Registering a new Tokenhandler
It is possible to register your own Tokenhandlers and therefore extend the capabilities of this documentation-program.
There are different types of handlers which can be used:
1. Default-handler
2. A handler for Typed-Token
3. A handler for Named-Typed-Tokens
4. Your custom handler (see second overload)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/token/handler.rb', line 198 def self.register(tokenname, = {}, &handler) tokenname = tokenname.to_sym # search matching handler if block_given? # handler is already defined elsif [:handler] and @@defaults.include?([:handler]) handler = @@defaults[[:handler]] elsif [:handler] raise Exception, "#{type} has no registered Tokenhandler" else handler = @@defaults[:text_only] end # Dynamically create Class named TokennameToken klass = Token.const_set "#{tokenname.to_s.capitalize}Token", Class.new(Token) klass. .merge({ :token => tokenname, :handler => handler }); @@handlers[tokenname] = klass end |
.unregister(tokenname) ⇒ Object
Remove a registered handler from the list
233 234 235 |
# File 'lib/token/handler.rb', line 233 def self.unregister(tokenname) @@handlers.delete(tokenname.to_sym) end |