Class: Packcr::Stream
- Inherits:
-
Object
- Object
- Packcr::Stream
- Defined in:
- lib/packcr/stream.rb
Instance Method Summary collapse
- #get_code_block(code, indent, fname) ⇒ Object
-
#initialize(io, name, line) ⇒ Stream
constructor
A new instance of Stream.
- #write(s, rewrite_line_directive: false) ⇒ Object
- #write_code_block(code, indent, fname) ⇒ Object
- #write_line_directive(fname, lineno) ⇒ Object
- #write_output_line_directive ⇒ Object
- #write_text(s) ⇒ Object
Constructor Details
#initialize(io, name, line) ⇒ Stream
Returns a new instance of Stream.
6 7 8 9 10 11 |
# File 'lib/packcr/stream.rb', line 6 def initialize(io, name, line) @io = io @name = name @line = line @line_directive_tag = nil end |
Instance Method Details
#get_code_block(code, indent, fname) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/packcr/stream.rb', line 93 def get_code_block(code, indent, fname) buf = StringIO.new line, io, @io, @line = @line, @io, buf, @line && :uuid write_code_block(code, indent, fname) return buf.string ensure @line, @io = line, io end |
#write(s, rewrite_line_directive: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/packcr/stream.rb', line 13 def write(s, rewrite_line_directive: false) if rewrite_line_directive && @line_directive_tag && @line.respond_to?(:+) s.gsub!(@line_directive_tag) { (@line + $`.count("\n") + 1).to_s } @line_directive_tag = nil end @io.write(s) if @line.respond_to?(:+) @line += s.count("\n") end 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 88 89 90 91 |
# File 'lib/packcr/stream.rb', line 45 def write_code_block(code, indent, fname) text = code.text ptr = text.b len = code.len lineno = code.line if len == nil return # for safety end ptr.sub!(/\A\n+/) do lineno += $&.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 = $1 char = $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}#{$1}" end end return if ptr.empty? write_line_directive(fname, lineno) ptr.scan(/^(.+?)[ \v\f\t\r]*$|^\r?\n/) do write $1 if $1 write "\n" end write_output_line_directive end |
#write_line_directive(fname, lineno) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/packcr/stream.rb', line 28 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_directive ⇒ Object
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
24 25 26 |
# File 'lib/packcr/stream.rb', line 24 def write_text(s) write s.gsub(/\r\n/, "\n") end |