Sha256: 4844ce2e0540acd0625e0cbd4adfec65898a2fad605e96903ffac6c5813081dd

Contents?: true

Size: 1.62 KB

Versions: 10

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require_relative '../transformer'

module Epuber
  class Transformer
    class TextTransformer < Transformer
      # @return [String]
      #
      attr_accessor :text

      # @return [String]
      #
      attr_accessor :file_path


      # @param [String] file_path  path to transforming file
      # @param [String] text  text file content
      # @param [CompilationContext] compilation_context
      #
      # @return [String] new transformed text
      #
      def call(file_path, text, compilation_context)
        @file_path = file_path
        @text = text.dup

        @block.call(self, @text, compilation_context)

        new_text = @text

        @text = nil
        @file_path = nil

        new_text
      end

      # Shortcut for performing substitutions in text
      #
      # @param [Regexp, String] pattern
      # @param [String, nil] replacement
      # @param [Bool] multiple_times  run the replacement multiple times, while there is something to replace
      # @param [Proc] block  optional block for creating replacements, see String#gsub!
      #
      # @return [String, nil] see String#gsub!
      #
      def replace_all(pattern, replacement = nil, multiple_times: false, &block)
        result = if replacement.nil?
                   @text.gsub!(pattern, &block)
                 else
                   @text.gsub!(pattern, replacement, &block)
                 end

        if multiple_times && !result.nil?
          result = replace_all(pattern, replacement, multiple_times: multiple_times,
                               &block)
        end
        result
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
epuber-0.11.1 lib/epuber/transformer/text_transformer.rb
epuber-0.11.0 lib/epuber/transformer/text_transformer.rb
epuber-0.10.3 lib/epuber/transformer/text_transformer.rb
epuber-0.10.2 lib/epuber/transformer/text_transformer.rb
epuber-0.10.1 lib/epuber/transformer/text_transformer.rb
epuber-0.9.3 lib/epuber/transformer/text_transformer.rb
epuber-0.9.2 lib/epuber/transformer/text_transformer.rb
epuber-0.9.1 lib/epuber/transformer/text_transformer.rb
epuber-0.9.0 lib/epuber/transformer/text_transformer.rb
epuber-0.8.0 lib/epuber/transformer/text_transformer.rb