Class: Packcr::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/packcr/stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream, name, line) ⇒ Stream

Returns a new instance of Stream.



5
6
7
8
9
10
# File 'lib/packcr/stream.rb', line 5

def initialize(stream, name, line)
  @stream = stream
  @name = name
  @line = line
  @line_directive_tag = nil
end

Instance Method Details

#get_code_block(code, indent, fname) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/packcr/stream.rb', line 89

def get_code_block(code, indent, fname)
  buf = +""
  line = @line
  stream = @stream
  @stream = buf
  @line &&= :uuid
  write_code_block(code, indent, fname)
  buf
ensure
  @line = line
  @stream = stream
end

#write(s, rewrite_line_directive: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/packcr/stream.rb', line 12

def write(s, rewrite_line_directive: false)
  if rewrite_line_directive && @line_directive_tag && @line.respond_to?(:+)
    s.gsub!(@line_directive_tag) { (@line + ::Regexp.last_match.pre_match.count("\n") + 1).to_s }
    @line_directive_tag = nil
  end
  @stream << s
  return unless @line.respond_to?(:+)

  @line += s.count("\n")
end

#write_code_block(code, indent, fname) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/packcr/stream.rb', line 45

def write_code_block(code, indent, fname)
  text = code.text
  ptr = text.b
  lineno = code.line

  ptr.sub!(/\A\n+/) do
    lineno += ::Regexp.last_match(0).length
    ""
  end
  ptr.sub!(/[ \v\f\t\r\n]*\z/, "")

  min_indent_spaces = nil
  ptr.gsub!(/^([ \v\f\t]*)([^ \v\f\t\r\n])/) do
    spaces = ::Regexp.last_match(1)
    char = ::Regexp.last_match(2)

    next char if char == "#"

    Packcr.unify_indent_spaces(spaces)

    if !min_indent_spaces || min_indent_spaces.length > spaces.length
      min_indent_spaces = spaces
    end

    spaces + char
  end

  if min_indent_spaces
    indent_spaces = " " * indent
    ptr.gsub!(/^#{min_indent_spaces}( *[^\n#])/) do
      "#{indent_spaces}#{::Regexp.last_match(1)}"
    end
  end

  return if ptr.empty?

  write_line_directive(fname, lineno)
  ptr.scan(/^(.+?)[ \v\f\t\r]*$|^\r?\n/) do
    write ::Regexp.last_match(1) if ::Regexp.last_match(1)
    write "\n"
  end
  write_output_line_directive
end

#write_line_directive(fname, lineno) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/packcr/stream.rb', line 27

def write_line_directive(fname, lineno)
  return unless @line

  if lineno.respond_to?(:+)
    write("#line #{lineno + 1} \"")
  else
    @line_directive_tag ||= "<#{SecureRandom.uuid}>"
    write("#line #{@line_directive_tag} \"")
  end

  write(Packcr.escape_string(fname))
  write("\"\n")
end

#write_output_line_directiveObject



41
42
43
# File 'lib/packcr/stream.rb', line 41

def write_output_line_directive
  write_line_directive(@name, @line)
end

#write_text(s) ⇒ Object



23
24
25
# File 'lib/packcr/stream.rb', line 23

def write_text(s)
  write s.gsub(/\r\n/, "\n")
end