Sha256: cb55528c97bfd01a9573aeaeb56df27fde6e6bf8fc22b7bb2ad4b7e6765f2f9a

Contents?: true

Size: 939 Bytes

Versions: 5

Compression:

Stored size: 939 Bytes

Contents

# stdlib
require 'cgi'

module Rouge
  module Formatters
    # Transforms a token stream into HTML output.
    class HTML < Formatter
      tag 'html'

      # @option opts :css_class
      # A css class to be used for the generated <pre> tag.
      def initialize(opts={})
        @css_class = opts[:css_class] || 'highlight'
      end

      # @yield the html output.
      def stream(tokens, &b)
        yield "<pre class=#{@css_class.inspect}>"
        tokens.each do |tok, val|
          # TODO: properly html-encode val
          val = CGI.escape_html(val)

          case tok.shortname
          when ''
            yield val
          when nil
            raise "unknown token: #{tok.inspect}"
          else
            yield '<span class='
            yield tok.shortname.inspect
            yield '>'
            yield val
            yield '</span>'
          end
        end
        yield '</pre>'
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rouge-0.2.5 lib/rouge/formatters/html.rb
rouge-0.2.4 lib/rouge/formatters/html.rb
rouge-0.2.3 lib/rouge/formatters/html.rb
rouge-0.2.2 lib/rouge/formatters/html.rb
rouge-0.2.1 lib/rouge/formatters/html.rb