Module: Germinate::TextTransforms

Defined in:
lib/germinate/text_transforms.rb

Overview

A library of text transforms. Each public module method returns a callable object which can be used to transform an array of lines.

Constant Summary collapse

IDENTITY_TRANSFORM =
lambda {|hunk| hunk}

Class Method Summary collapse

Class Method Details

.bracket(open_bracket = nil, close_bracket = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/germinate/text_transforms.rb', line 78

def self.bracket(open_bracket=nil, close_bracket=nil)
  lambda { |hunk|
    result = hunk.dup
    result.clear
    result << (open_bracket || hunk.code_open_bracket)
    result.push(*Array(hunk))
    result << (close_bracket || hunk.code_close_bracket)
    result.compact!
    result
  }
end

.erase_comments(comment_prefix = "") ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/germinate/text_transforms.rb', line 39

def self.erase_comments(comment_prefix="")
  lambda { |hunk|
    hunk.dup.map! do |line|
      if comment_prefix && String === line
        if match_data = /^\s*(#{comment_prefix})+\s*/.match(line)
          offset = match_data.begin(0)
          length = match_data[0].length
          line_copy = line.dup
          line_copy[offset, length] = (" " * length)
          line_copy
        else
          line
        end
      else
        line
      end
    end
  }
end

.expand_insertionsObject



97
98
99
100
101
# File 'lib/germinate/text_transforms.rb', line 97

def self.expand_insertions
  lambda do |hunk|
    hunk.resolve_insertions
  end
end

.flatten_nestedObject



103
104
105
106
107
108
109
# File 'lib/germinate/text_transforms.rb', line 103

def self.flatten_nested
  lambda do |hunk|
    result = hunk.flatten
    result.copy_shared_style_attributes_from(hunk)
    result
  end
end

.join_linesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/germinate/text_transforms.rb', line 6

def self.join_lines
  lambda { |hunk|
    paragraphs = hunk.inject([[]]) { |result, line|
      case line
      when /\S/
        result.last << line.strip
      when String
        result << [line]
        result << []
      else
        result << line
      end
      result
    }
    paragraphs.delete_if{|p| p.empty?}
    hunk.dup.replace(paragraphs.map {|paragraph|
      case paragraph
      when Germinate::Hunk then paragraph
      else paragraph.join(" ") 
      end
    })
  }
end

.pipeline(pipeline = nil) ⇒ Object



90
91
92
93
94
95
# File 'lib/germinate/text_transforms.rb', line 90

def self.pipeline(pipeline=nil)
  lambda do |hunk|
    pipeline ||= hunk.pipeline
    pipeline.call(hunk)
  end
end

.rstrip_linesObject



72
73
74
75
76
# File 'lib/germinate/text_transforms.rb', line 72

def self.rstrip_lines
  lambda { |hunk|
    hunk.dup.map!{|line| String === line ? line.to_s.rstrip : line}
  }
end

.strip_blanksObject



30
31
32
33
34
35
36
37
# File 'lib/germinate/text_transforms.rb', line 30

def self.strip_blanks
  lambda { |hunk|
    result = hunk.dup
    result.shift while result.first =~ /^\s*$/ && !result.empty?
    result.pop while result.last =~ /^\s*$/ && !result.empty?
    result
  }
end

.uncomment(comment_prefix = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/germinate/text_transforms.rb', line 59

def self.uncomment(comment_prefix=nil)
  lambda { |hunk|
    comment_prefix ||= hunk.comment_prefix
    hunk.dup.map! do |line|
      if comment_prefix && line.respond_to?(:sub)
        line.sub(/^#{Regexp.escape(comment_prefix.rstrip)}\s*/,"")
      else
        line
      end
    end
  }
end