forked from greyblake/vim-preview
/
preview.vim
269 lines (236 loc) · 6.53 KB
/
preview.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
" ============================================================================
" File: preview.vim
" Description: Vim global plugin to preview markup files(markdown,rdoc,textile)
" Author: Sergey Potapov (aka Blake) <blake131313 AT gmail DOT com>
" Version: 0.1
" Homepage: http://github.com/greyblake/vim-preview
" License: GPLv2+ -- look it up.
" Copyright: Copyright (C) 2010 Sergey Potapov (aka Blake)
"
" This program is free software; you can redistribute it and/or
" modify it under the terms of the GNU General Public License as
" published by the Free Software Foundation; either version 2 of
" the License, or (at your option) any later version.
"
" This program is distributed in the hope that it will be useful,
" but WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
" General Public License for more details.
"
" You should have received a copy of the GNU General Public License
" along with this program; if not, write to the Free Software
" Foundation, Inc., 59 Temple Place, Suite 330, Boston,
" MA 02111-1307 USA
" ============================================================================
function! s:load()
ruby << END_OF_RUBY
require 'singleton'
require 'tempfile'
require 'rubygems'
class Preview
include Singleton
OPTIONS = {
:browsers => "g:PreviewBrowsers",
:css_path => "g:PreviewCSSPath",
:markdown_ext => "g:PreviewMarkdownExt",
:textile_ext => "g:PreviewTextileExt",
:rdoc_ext => "g:PreviewRdocExt",
:html_ext => "g:PreviewHtmlExt"
}
DEPENDECIES = {
# :format => {:gem => 'name of gem' , :require => 'file to require'}
:markdown => {:gem => 'bluecloth' , :require => 'bluecloth' },
:textile => {:gem => 'RedCloth' , :require => 'redcloth' },
:rdoc => {:gem => 'github-markup', :require => 'github/markup' }
}
def show
update_fnames
ext_opts = OPTIONS.keys.find_all{|k| k.to_s =~ /_ext$/}
ext_opts.each do |opt|
if exts_match?(option(opt).split(','))
send "show_" + opt.to_s[/^(.*)_ext$/, 1]
return
end
end
error "don't know how to handle .#{@ftype} format"
end
def show_markdown
return unless load_dependencies(:markdown)
show_with(:browser) do
wrap_html BlueCloth.new(content).to_html
end
end
def show_html
show_with(:browser){content}
end
def show_rdoc
return unless load_dependencies(:rdoc)
show_with(:browser) do
wrap_html GitHub::Markup::RDoc.new(content).to_html
end
end
def show_textile
return unless load_dependencies(:textile)
show_with(:browser) do
wrap_html RedCloth.new(content).to_html
end
end
private
# TODO: handle errors when app can't be opened
def show_with(app_type, ext="html")
path = tmp_write(ext, yield)
app = get_apps_by_type(app_type).find{|app| system("which #{app.split()[0]} &> /dev/null")}
if app
pid = fork do
[STDOUT, STDERR].each { |io| io.reopen("/dev/null", "w") }
exec app, path
end
Process.detach pid # avoid zombies
else
error "any of apllications you specified in #{OPTIONS[app_type_to_opt(app_type)]} are not available"
end
end
def update_fnames
fname = VIM::Buffer.current.name
@base_name = File.basename(fname)
@ftype = fname[/\.([^.]+)$/, 1]
end
def get_apps_by_type(type)
option(app_type_to_opt(type)).split(',')
end
def app_type_to_opt(type)
case type
when :browser
:browsers
else
raise "Undefined application type #{type}"
end
end
def exts_match?(exts)
exts.find{|ext| ext.downcase == @ftype.downcase}
end
def tmp_write(ext, data)
tmp = File.open(File.join(Dir::tmpdir, [@base_name,ext].join('.')), 'w')
#tmp = Tempfile.new(@base_name)
tmp.write(data)
tmp.close
tmp.path
end
def content
text = ""
VIM::Buffer.current.count.times do |i|
text += VIM::Buffer.current[i+1] + "\n"
end
text
end
def error(msg)
puts("Preview: #{msg}")
end
def wrap_html(body)
<<-END_OF_HTML
<html>
<head>
<title>#{@base_name}</title>
#{css_tag}
</head>
<body>
<div id="main-container">
#{body}
<div>
</body>
</html>
END_OF_HTML
end
def css_tag
if option(:css_path).empty?
%Q(<style type="text/css">#{css}</style>)
else
%Q(<link rel="stylesheet" href="#{option(:css_path)}" type="text/css" />)
end
end
def option(name)
raise "Unknown option #{name.inspect}" unless OPTIONS.keys.include?(name)
VIM.evaluate(OPTIONS[name])
end
def load_dependencies(format)
require DEPENDECIES[format][:require]
true
rescue LoadError
error "To preview #{format} format you need to install #{DEPENDECIES[format][:gem]} gem"
false
end
def css
<<-END_OF_CSS
body{
background-color: #FFFFFF;
padding: 20px;
margin: 0px;
}
pre{
border: solid #DEDEDE 1px;
background-color: #F6F6F6;
padding: 4px;
}
code{
border: solid #DEDEDE 1px;
background-color: #F6F6F6;
padding: 1px;
font-family: monospace;
}
pre > code{
border: none;
padding: none;
}
blockquote{
border: dashed #AEAEAE 1px;
background-color: #F6F6F6;
padding: 4px 10px 4px 10px;
font-family: monospace;
}
div#main-container{
background-color: #F2F2F2;
padding: 20px;
margin: 0px;
border: solid #D0D0D0 1px;
}
END_OF_CSS
end
end
END_OF_RUBY
endfunction
function! s:init()
if(!(exists('s:loaded') && s:loaded))
call s:load()
let s:loaded = 1
endif
endfunction
function! preview#show()
call s:init()
ruby << END_OF_RUBY
Preview.instance.show
END_OF_RUBY
endfunction
function! preview#show_markdown()
call s:init()
ruby << END_OF_RUBY
Preview.instance.show_markdown
END_OF_RUBY
endfunction
function! preview#show_textile()
call s:init()
ruby << END_OF_RUBY
Preview.instance.show_textile
END_OF_RUBY
endfunction
function! preview#show_rdoc()
call s:init()
ruby << END_OF_RUBY
Preview.instance.show_rdoc
END_OF_RUBY
endfunction
function! preview#show_html()
call s:init()
ruby << END_OF_RUBY
Preview.instance.show_html
END_OF_RUBY
endfunction