File: html_linewise.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (22 lines) | stat: -rw-r--r-- 631 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Formatters
    class HTMLLinewise < Formatter
      def initialize(formatter, opts={})
        @formatter = formatter
        @tag_name = opts.fetch(:tag_name, 'div')
        @class_format = opts.fetch(:class, 'line-%i')
      end

      def stream(tokens, &b)
        token_lines(tokens).with_index(1) do |line_tokens, lineno|
          yield %(<#{@tag_name} class="#{sprintf @class_format, lineno}">)
          @formatter.stream(line_tokens) {|formatted| yield formatted }
          yield %(\n</#{@tag_name}>)
        end
      end
    end
  end
end