Sha256: 639a9d1a628d035ee04c5a7618ff5e51137d4cbafe1d312e8f7ffa1b57d038e0

Contents?: true

Size: 817 Bytes

Versions: 13

Compression:

Stored size: 817 Bytes

Contents

# -*- coding: utf-8 -*- #

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

      # @yield the html output.
      def stream(tokens, &b)
        tokens.each { |tok, val| yield span(tok, val) }
      end

      def span(tok, val)
        safe_span(tok, val.gsub(/[&<>]/, TABLE_FOR_ESCAPE_HTML))
      end

      def safe_span(tok, safe_val)
        if tok == Token::Tokens::Text
          safe_val
        else
          shortname = tok.shortname \
            or raise "unknown token: #{tok.inspect} for #{safe_val.inspect}"

          "<span class=\"#{shortname}\">#{safe_val}</span>"
        end
      end


      TABLE_FOR_ESCAPE_HTML = {
        '&' => '&amp;',
        '<' => '&lt;',
        '>' => '&gt;',
      }
    end
  end
end

Version data entries

13 entries across 13 versions & 5 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/rouge-2.2.1/lib/rouge/formatters/html.rb
rouge-3.2.1 lib/rouge/formatters/html.rb
rouge-3.2.0 lib/rouge/formatters/html.rb
rouge_ecl-1.0.0 lib/rouge/formatters/html.rb
rouge_ecl-0.0.1 lib/rouge/formatters/html.rb
rouge-3.1.1 lib/rouge/formatters/html.rb
rouge-3.1.0 lib/rouge/formatters/html.rb
rouge-3.0.0 lib/rouge/formatters/html.rb
rouge-2.2.1 lib/rouge/formatters/html.rb
rouge-2.2.0 lib/rouge/formatters/html.rb
rougegal-2.1.2 lib/rouge/formatters/html.rb
tdiary-5.0.5 vendor/bundle/gems/rouge-2.1.1/lib/rouge/formatters/html.rb
rouge-2.1.1 lib/rouge/formatters/html.rb