Module: Insta::SnapshotContent

Defined in:
lib/insta/snapshot_content.rb,
sig/insta/snapshot_content.rbs

Class Method Summary collapse

Class Method Details

.indent(content, level) ⇒ String

: (String, Integer) -> String

Parameters:

  • (String)
  • (Integer)

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
# File 'lib/insta/snapshot_content.rb', line 15

def self.indent(content, level)
  prefix = " " * level
  content.each_line.map { |line|
    if line.strip.empty?
      "\n"
    else
      "#{prefix}#{line}"
    end
  }.join
end

.normalize(content) ⇒ String

: (String) -> String

Parameters:

  • (String)

Returns:

  • (String)


6
7
8
9
10
11
12
# File 'lib/insta/snapshot_content.rb', line 6

def self.normalize(content)
  content
    .gsub("\r\n", "\n")
    .gsub("\r", "\n")
    .sub(/\n+\z/, "")
    .concat("\n")
end

.strip_indent(content) ⇒ String

: (String) -> String

Parameters:

  • (String)

Returns:

  • (String)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/insta/snapshot_content.rb', line 27

def self.strip_indent(content)
  lines = content.split("\n", -1)
  non_empty = lines.reject { |l| l.strip.empty? }
  return content if non_empty.empty?

  min_indent = non_empty.map { |l| l.match(/^(\s*)/).to_a[1].to_s.length }.min || 0
  return content if min_indent.zero?

  lines.map { |l|
    if l.strip.empty?
      ""
    else
      l[min_indent..] || ""
    end
  }.join("\n")
end