Module: RDoc::TokenStream

### Home Classes Methods

### In Files

- lib/rdoc/token_stream.rb

### Methods

- ::to_html
- #add_token
- #add_tokens
- #collect_tokens
- #pop_token
- #start_collecting_tokens
- #token_stream
- #tokens_to_s

### Files

- CVE-2013-0256.rdoc
- DEVELOPERS.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc
- RI.rdoc
- Rakefile
- TODO.rdoc

### Class/Module Index

Quicksearch

- RDoc
- RDoc::Alias
- RDoc::AnonClass
- RDoc::AnyMethod
- RDoc::Attr
- RDoc::ClassModule
- RDoc::CodeObject
- RDoc::Comment
- RDoc::Constant
- RDoc::Context
- RDoc::Context::Section
- RDoc::CrossReference
- RDoc::ERBIO
- RDoc::ERBPartial
- RDoc::Encoding
- RDoc::Error
- RDoc::Extend
- RDoc::Gauntlet
- RDoc::Generator
- RDoc::Generator::Darkfish
- RDoc::Generator::JsonIndex
- RDoc::Generator::Markup
- RDoc::Generator::RI
- RDoc::GhostMethod
- RDoc::Include
- RDoc::Markdown
- RDoc::Markdown::Literals
- RDoc::Markup
- RDoc::Markup::AttrChanger
- RDoc::Markup::AttrSpan
- RDoc::Markup::AttributeManager
- RDoc::Markup::Attributes
- RDoc::Markup::BlankLine
- RDoc::Markup::BlockQuote
- RDoc::Markup::Document
- RDoc::Markup::Formatter
- RDoc::Markup::FormatterTestCase
- RDoc::Markup::HardBreak
- RDoc::Markup::Include
- RDoc::Markup::IndentedParagraph
- RDoc::Markup::List
- RDoc::Markup::ListItem
- RDoc::Markup::Paragraph
- RDoc::Markup::Parser
- RDoc::Markup::Parser::Error
- RDoc::Markup::Parser::ParseError
- RDoc::Markup::PreProcess
- RDoc::Markup::Raw
- RDoc::Markup::Rule
- RDoc::Markup::Special
- RDoc::Markup::TextFormatterTestCase
- RDoc::Markup::ToAnsi
- RDoc::Markup::ToBs
- RDoc::Markup::ToHtml
- RDoc::Markup::ToHtmlCrossref
- RDoc::Markup::ToHtmlSnippet
- RDoc::Markup::ToJoinedParagraph
- RDoc::Markup::ToLabel
- RDoc::Markup::ToMarkdown
- RDoc::Markup::ToRdoc
- RDoc::Markup::ToTableOfContents
- RDoc::Markup::ToTest
- RDoc::Markup::ToTtOnly
- RDoc::Markup::Verbatim
- RDoc::MetaMethod
- RDoc::MethodAttr
- RDoc::NormalClass
- RDoc::NormalModule
- RDoc::Options
- RDoc::Parser
- RDoc::Parser::C
- RDoc::Parser::ChangeLog
- RDoc::Parser::Markdown
- RDoc::Parser::RD
- RDoc::Parser::Ruby
- RDoc::Parser::RubyTools
- RDoc::Parser::Simple
- RDoc::Parser::Text
- RDoc::RD
- RDoc::RD::BlockParser
- RDoc::RD::Inline
- RDoc::RD::InlineParser
- RDoc::RDoc
- RDoc::RI
- RDoc::RI::Driver
- RDoc::RI::Driver::Error
- RDoc::RI::Driver::NotFoundError
- RDoc::RI::Error
- RDoc::RI::Paths
- RDoc::Require
- RDoc::RubyLex
- RDoc::RubyLex::Error
- RDoc::RubyToken
- RDoc::RubygemsHook
- RDoc::Servlet
- RDoc::SingleClass
- RDoc::Stats
- RDoc::Stats::Normal
- RDoc::Stats::Quiet
- RDoc::Stats::Verbose
- RDoc::Store
- RDoc::Store::Error
- RDoc::Store::MissingFileError
- RDoc::Task
- RDoc::Task
- RDoc::TestCase
- RDoc::TokenStream
- RDoc::TomDoc
- RDoc::TopLevel
- Object

No matching classes.

# RDoc::TokenStream

A TokenStream is a list of tokens, gathered during the parse of some entity (say a method). Entities populate these streams by being registered with the lexer. Any class can collect tokens by including TokenStream. From the outside, you use such an object by calling the start_collecting_tokens method, followed by calls to add_token and pop_token.

### Public Class Methods

to_html(token_stream) click to toggle source

Converts `token_stream` to HTML wrapping various tokens with `<span>` elements. The following tokens types are wrapped in spans with the given class names:

TkCONSTANT | 'ruby-constant'
TkKW | 'ruby-keyword'
TkIVAR | 'ruby-ivar'
TkOp | 'ruby-operator'
TkId | 'ruby-identifier'
TkNode | 'ruby-node'
TkCOMMENT | 'ruby-comment'
TkREGEXP | 'ruby-regexp'
TkSTRING | 'ruby-string'
TkVal | 'ruby-value'

Other token types are not wrapped in spans.

    # File lib/rdoc/token_stream.rb, line 28
    def self.to_html token_stream
      token_stream.map do |t|
        next unless t

        style = case t
                when RDoc::RubyToken::TkCONSTANT then 'ruby-constant'
                when RDoc::RubyToken::TkKW       then 'ruby-keyword'
                when RDoc::RubyToken::TkIVAR     then 'ruby-ivar'
                when RDoc::RubyToken::TkOp       then 'ruby-operator'
                when RDoc::RubyToken::TkId       then 'ruby-identifier'
                when RDoc::RubyToken::TkNode     then 'ruby-node'
                when RDoc::RubyToken::TkCOMMENT  then 'ruby-comment'
                when RDoc::RubyToken::TkREGEXP   then 'ruby-regexp'
                when RDoc::RubyToken::TkSTRING   then 'ruby-string'
                when RDoc::RubyToken::TkVal      then 'ruby-value'
                end

        text = CGI.escapeHTML t.text

        if style then
          "<span class=\"#{style}\">#{text}</span>"
        else
          text
        end
      end.join
    end

### Public Instance Methods

add_token(*tokens) click to toggle source
Alias for: add_tokens
add_tokens(*tokens) click to toggle source

Adds `tokens` to the collected tokens

    # File lib/rdoc/token_stream.rb, line 58
    def add_tokens(*tokens)
      tokens.flatten.each { |token| @token_stream << token }
    end

Also aliased as: add_token
collect_tokens() click to toggle source

Starts collecting tokens

    # File lib/rdoc/token_stream.rb, line 67
    def collect_tokens
      @token_stream = []
    end

Also aliased as: start_collecting_tokens
pop_token() click to toggle source

Remove the last token from the collected tokens

    # File lib/rdoc/token_stream.rb, line 76
    def pop_token
      @token_stream.pop
    end

start_collecting_tokens() click to toggle source
Alias for: collect_tokens
token_stream() click to toggle source

Current token stream

    # File lib/rdoc/token_stream.rb, line 83
    def token_stream
      @token_stream
    end

tokens_to_s() click to toggle source

Returns a string representation of the token stream

    # File lib/rdoc/token_stream.rb, line 90
    def tokens_to_s
      token_stream.map { |token| token.text }.join ''
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.