]> git.r.bdr.sh - rbdr/dotfiles/blob - vim/ftplugin/textile.vim
Ignore vim/.netrwhist
[rbdr/dotfiles] / vim / ftplugin / textile.vim
1 " textile.vim
2 "
3 " Tim Harper (tim.theenchanter.com)
4
5 command! -nargs=0 TextileRenderFile call TextileRenderBufferToFile()
6 command! -nargs=0 TextileRenderTab call TextileRenderBufferToTab()
7 command! -nargs=0 TextilePreview call TextileRenderBufferToPreview()
8 noremap <buffer> <Leader>rp :TextilePreview<CR>
9 noremap <buffer> <Leader>rf :TextileRenderFile<CR>
10 noremap <buffer> <Leader>rt :TextileRenderTab<CR>
11 setlocal ignorecase
12 setlocal wrap
13 setlocal lbr
14
15 function! TextileRender(lines)
16 if (system('which ruby') == "")
17 throw "Could not find ruby!"
18 end
19
20 let text = join(a:lines, "\n")
21 let html = system("ruby -e \"def e(msg); puts msg; exit 1; end; begin; require 'rubygems'; rescue LoadError; e('rubygems not found'); end; begin; require 'redcloth'; rescue LoadError; e('RedCloth gem not installed. Run this from the terminal: sudo gem install RedCloth'); end; puts(RedCloth.new(\\$stdin.read).to_html(:textile))\"", text)
22 return html
23 endfunction
24
25 function! TextileRenderFile(lines, filename)
26 let html = TextileRender(getbufline(bufname("%"), 1, '$'))
27 let html = "<html><head><title>" . bufname("%") . "</title><body>\n" . html . "\n</body></html>"
28 return writefile(split(html, "\n"), a:filename)
29 endfunction
30
31 function! TextileRenderBufferToPreview()
32 let filename = "/tmp/textile-preview.html"
33 call TextileRenderFile(getbufline(bufname("%"), 1, '$'), filename)
34 " Verify if browser was set
35 if !exists("g:TextileBrowser")
36 let g:TextileBrowser='Safari'
37 endif
38 " call configured browser according OS
39 if !exists("g:TextileOS") || g:TextileOS == 'mac'
40 call system("open -a \"".g:TextileBrowser."\" ".filename)
41 else
42 echo g:TextileBrowser." ".filename
43 call system(g:TextileBrowser." ".filename)
44 endif
45 endfunction
46
47 function! TextileRenderBufferToFile()
48 let filename = input("Filename:", substitute(bufname("%"), "textile$", "html", ""), "file")
49 call TextileRenderFile(getbufline(bufname("%"), 1, '$'), filename)
50 echo "Rendered to '" . filename . "'"
51 endfunction
52
53 function! TextileRenderBufferToTab()
54 let html = TextileRender(getbufline(bufname("%"), 1, '$'))
55 tabnew
56 call append("^", split(html, "\n"))
57 set syntax=html
58 endfunction
59