Class: Epuber::Transformer::TextTransformer

Inherits:
Epuber::Transformer show all
Defined in:
lib/epuber/transformer/text_transformer.rb

Instance Attribute Summary collapse

Attributes inherited from CheckerTransformerBase

#block, #options, #source_type

Instance Method Summary collapse

Methods inherited from Epuber::Transformer

map_source_type__class

Methods inherited from CheckerTransformerBase

class_for_source_type, #initialize, map_source_type__class, #valid_options

Constructor Details

This class inherits a constructor from Epuber::CheckerTransformerBase

Instance Attribute Details

#file_pathString

Returns:

  • (String)


14
15
16
# File 'lib/epuber/transformer/text_transformer.rb', line 14

def file_path
  @file_path
end

#textString

Returns:

  • (String)


10
11
12
# File 'lib/epuber/transformer/text_transformer.rb', line 10

def text
  @text
end

Instance Method Details

#call(file_path, text, compilation_context) ⇒ String

Returns new transformed text.

Parameters:

  • file_path (String)

    path to transforming file

  • text (String)

    text file content

  • compilation_context (CompilationContext)

Returns:

  • (String)

    new transformed text



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/epuber/transformer/text_transformer.rb', line 23

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

#replace_all(pattern, replacement = nil, multiple_times: false, &block) ⇒ String?

Shortcut for performing substitutions in text

Parameters:

  • pattern (Regexp, String)
  • replacement (String, nil) (defaults to: nil)
  • multiple_times (Bool) (defaults to: false)

    run the replacement multiple times, while there is something to replace

  • block (Proc)

    optional block for creating replacements, see String#gsub!

Returns:

  • (String, nil)

    see String#gsub!



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/epuber/transformer/text_transformer.rb', line 46

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