Method: VimColor#run_file

Defined in:
lib/msgpack/idl/command/vimcolor.rb

#run_file(path, options, formatter_class, *formatter_args) ⇒ Object



43
44
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/msgpack/idl/command/vimcolor.rb', line 43

def run_file(path, options, formatter_class, *formatter_args)
	preset = @preset.dup
	postset = @postset.dup

	if formatter_class.class == Symbol
		formatter_class = self.class.const_get("Format_#{formatter_class}")
	end

	if options.is_a? Hash
		if options.include? :filetype
			postset.unshift(":set filetype=#{options[:filetype]}")
		end
		if options.include? :encoding
			preset.unshift("+set encoding=#{options[:encoding]}")
		end
	else
		postset.unshift(":set filetype=#{options}")
	end

	vimout = nil
	tmp_stream = Tempfile.new('ruby-vimcolor')
	begin
		tmp_path = tmp_stream.path
		tmp_stream.puts <<SCRIPT
:syntax on
#{postset.join("\n")}
:source #{VIM_MARK_SCRIPT}
:write! #{tmp_path}
:qall!

SCRIPT
		tmp_stream.flush
		pid = Process.fork {
			STDIN.reopen "/dev/null"
			STDOUT.reopen "/dev/null", "a"
			STDERR.reopen "/dev/null", "a"
			args = []
			args.concat @options
			args.concat ['-s', tmp_path]
			args.push path
			args.concat preset
			exec(@command, *args)
			exit 127
		}
		Process.waitpid(pid)
		tmp_stream.seek(0)
		vimout = tmp_stream.read
	ensure
		tmp_stream.close
	end

	require 'strscan'
	s = StringScanner.new(vimout)

	formatter = formatter_class.new(*formatter_args)
	while s.scan_until(/(.*?)>(.*?)>(.*?)<\2</m)
		formatter.push('', s[1]) unless s[1].empty?
		type = s[2]
		text = s[3]
		text.gsub!(/&[agl]/) do
			VIM_UNESCAPE[$&]
		end
		formatter.push(type, text)
	end
	formatter.result
end