Class: VimColor

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/idl/command/vimcolor.rb,
lib/msgpack/idl/command/vimcolor.rb

Overview

VimColor for Ruby

Copyright © 2008-2011 FURUHASHI Sadayuki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: Format_ansi, Format_array, Format_html, Format_xml

Constant Summary collapse

VIM_COMMAND =
"vim"
VIM_OPTIONS =
%w[-R -X -Z -i NONE -u NONE -N]
VIM_PRESET =

+set shiftwidth

["+set nomodeline", '+set expandtab']
VIM_POSTSET =
[":let b:is_bash=1", ":filetype on"]
VIM_MARK_SCRIPT =
File.join(File.dirname(__FILE__), 'vimcolor', 'mark.vim')
VIM_UNESCAPE =
{'&l' => '<', '&g' => '>', '&a' => '&'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil, options = nil, preset = nil, postset = nil) ⇒ VimColor

Returns a new instance of VimColor.



33
34
35
36
37
38
39
# File 'lib/msgpack/idl/command/vimcolor.rb', line 33

def initialize(command=nil, options=nil, preset=nil, postset=nil)
  require 'tempfile'
  @command = VIM_COMMAND.dup
  @options = VIM_OPTIONS + (options || [])
  @preset  = VIM_PRESET  + (preset  || [])
  @postset = VIM_POSTSET + (postset || [])
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



41
42
43
# File 'lib/msgpack/idl/command/vimcolor.rb', line 41

def command
  @command
end

#optionsObject

Returns the value of attribute options.



41
42
43
# File 'lib/msgpack/idl/command/vimcolor.rb', line 41

def options
  @options
end

#postsetObject

Returns the value of attribute postset.



41
42
43
# File 'lib/msgpack/idl/command/vimcolor.rb', line 41

def postset
  @postset
end

#presetObject

Returns the value of attribute preset.



41
42
43
# File 'lib/msgpack/idl/command/vimcolor.rb', line 41

def preset
  @preset
end

Class Method Details

._escape_xml!(text) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/msgpack/idl/command/vimcolor.rb', line 180

def self._escape_xml!(text)
  text.gsub!("&", "&amp;")
  text.gsub!("<", "&lt;")
  text.gsub!(">", "&gt;")
  text.gsub!("'", "&#39;")
  text.gsub!('"', "&quot;")
  text
end

Instance Method Details

#run(str, options, formatter_class, *formatter_args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/msgpack/idl/command/vimcolor.rb', line 123

def run(str, options, formatter_class, *formatter_args)
  tmp_in = Tempfile.new('ruby-vimcolor-input')
  begin
    tmp_in.write(str)
    tmp_in.flush
    run_file(tmp_in.path, options, formatter_class, *formatter_args)
  ensure
    tmp_in.close
  end
end

#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 ":syntax on\n\#{postset.join(\"\\n\")}\n:source \#{VIM_MARK_SCRIPT}\n:write! \#{tmp_path}\n:qall!\n\n"
    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

#run_stream(stream, options, formatter_class, *formatter_args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/msgpack/idl/command/vimcolor.rb', line 111

def run_stream(stream, options, formatter_class, *formatter_args)
  tmp_in = Tempfile.new('ruby-vimcolor-input')
  begin
    tmp_in.write(stream.read)
    tmp_in.flush
    run_file(tmp_in.path, options, formatter_class, *formatter_args)
  ensure
    tmp_in.close
  end
end