Mercurial > vim-piecrust
annotate plugin/piecrust.vim @ 1:6185e60279c0
Added `Pcposturl` command.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 04 May 2012 11:51:22 -0700 |
parents | d09875b9f100 |
children | af8514b79c04 |
rev | line source |
---|---|
0 | 1 " piecrust.vim - PieCrust plugin for Vim |
2 " Maintainer: Ludovic Chabant <http://ludovic.chabant.com> | |
3 " Version: 0.1 | |
4 | |
5 " Globals {{{ | |
6 | |
7 if !exists('g:piecrust_debug') | |
8 let g:piecrust_debug = 0 | |
9 endif | |
10 | |
11 if (exists('g:loaded_piecrust') || &cp) && !g:piecrust_debug | |
12 finish | |
13 endif | |
14 if (exists('g:loaded_piecrust') && g:piecrust_debug) | |
15 echom "Reloaded PieCrust." | |
16 endif | |
17 let g:loaded_piecrust = 1 | |
18 | |
19 if !exists('g:piecrust_chef_executable') | |
20 let g:piecrust_chef_executable = 'chef' | |
21 endif | |
22 | |
23 if !exists('g:piecrust_trace') | |
24 let g:piecrust_trace = 0 | |
25 endif | |
26 | |
27 " }}} | |
28 | |
29 " Utility {{{ | |
30 | |
31 " Strips the ending slash in a path. | |
32 function! s:stripslash(path) | |
33 return fnamemodify(a:path, ':s?[/\\]$??') | |
34 endfunction | |
35 | |
36 " Normalizes the slashes in a path. | |
37 function! s:normalizepath(path) | |
38 if exists('+shellslash') && &shellslash | |
39 return substitute(a:path, '\\', '/', '') | |
40 elseif has('win32') | |
41 return substitute(a:path, '/', '\\', '') | |
42 else | |
43 return a:path | |
44 endif | |
45 endfunction | |
46 | |
47 " Prints a message if debug tracing is enabled. | |
48 function! s:trace(message, ...) | |
49 if g:piecrust_trace || (a:0 && a:1) | |
50 let l:message = "piecrust: " . a:message | |
51 echom l:message | |
52 endif | |
53 endfunction | |
54 | |
55 " Prints an error message with 'piecrust error' prefixed to it. | |
56 function! s:error(message) | |
57 echom "piecrust error: " . a:message | |
58 endfunction | |
59 | |
60 " Throw a PieCrust exception message. | |
61 function! s:throw(message) | |
62 let v:errmsg = "piecrust: " . a:message | |
63 throw v:errmsg | |
64 endfunction | |
65 | |
66 " Finds the website root given a path inside that website. | |
67 " Throw an error if not repository is found. | |
68 function! s:find_website_root(path) | |
69 let l:path = s:stripslash(a:path) | |
70 let l:previous_path = "" | |
71 while l:path != l:previous_path | |
72 if filereadable(l:path . '/_content/config.yml') | |
73 return simplify(fnamemodify(l:path, ':p')) | |
74 endif | |
75 let l:previous_path = l:path | |
76 let l:path = fnamemodify(l:path, ':h') | |
77 endwhile | |
78 call s:throw("No PieCrust website found above: " . a:path) | |
79 endfunction | |
80 | |
81 " }}} | |
82 | |
83 " PieCrust website {{{ | |
84 | |
85 " Let's define a PieCrust website 'class' using prototype-based object-oriented | |
86 " programming. | |
87 " | |
88 " The prototype dictionary. | |
89 let s:PieCrust = {} | |
90 | |
91 " Constructor | |
92 function! s:PieCrust.New(path) abort | |
93 let l:newSite = copy(self) | |
94 let l:newSite.root_dir = s:find_website_root(a:path) | |
95 call s:trace("Built new PieCrust website object at : " . l:newSite.root_dir) | |
96 return l:newSite | |
97 endfunction | |
98 | |
99 " Gets a full path given a repo-relative path | |
100 function! s:PieCrust.GetFullPath(path) abort | |
101 let l:root_dir = self.root_dir | |
102 if a:path =~# '\v^[/\\]' | |
103 let l:root_dir = s:stripslash(l:root_dir) | |
104 endif | |
105 return l:root_dir . a:path | |
106 endfunction | |
107 | |
108 " Runs a Chef command in the website | |
109 function! s:PieCrust.RunCommand(command, ...) abort | |
110 " If there's only one argument, and it's a list, then use that as the | |
111 " argument list. | |
112 let l:arg_list = a:000 | |
113 if a:0 == 1 && type(a:1) == type([]) | |
114 let l:arg_list = a:1 | |
115 endif | |
116 let l:chef_command = g:piecrust_chef_executable . ' ' . a:command | |
117 let l:chef_command = l:chef_command . ' --root=' . shellescape(s:stripslash(self.root_dir)) | |
118 let l:chef_command = l:chef_command . ' ' . join(l:arg_list, ' ') | |
119 call s:trace("Running Chef command: " . l:chef_command) | |
120 return system(l:chef_command) | |
121 endfunction | |
122 | |
123 " Website cache map | |
124 let s:buffer_websites = {} | |
125 | |
126 " Get a cached website | |
127 function! s:piecrust_website(...) abort | |
128 " Use the given path, or the website directory of the current buffer. | |
129 if a:0 == 0 | |
130 if exists('b:piecrust_dir') | |
131 let l:path = b:piecrust_dir | |
132 else | |
133 let l:path = s:find_website_root(expand('%:p')) | |
134 endif | |
135 else | |
136 let l:path = a:1 | |
137 endif | |
138 " Find a cache website instance, or make a new one. | |
139 if has_key(s:buffer_websites, l:path) | |
140 return get(s:buffer_websites, l:path) | |
141 else | |
142 let l:website = s:PieCrust.New(l:path) | |
143 let s:buffer_websites[l:path] = l:website | |
144 return l:website | |
145 endif | |
146 endfunction | |
147 | |
148 " Sets up the current buffer with PieCrust commands if it contains a file from a PieCrust website. | |
149 " If the file is not in a PieCrust website, just exit silently. | |
150 function! s:setup_buffer_commands() abort | |
151 call s:trace("Scanning buffer '" . bufname('%') . "' for PieCrust setup...") | |
152 let l:do_setup = 1 | |
153 if exists('b:piecrust_dir') | |
154 if b:piecrust_dir =~# '\v^\s*$' | |
155 unlet b:piecrust_dir | |
156 else | |
157 let l:do_setup = 0 | |
158 endif | |
159 endif | |
160 try | |
161 let l:website = s:piecrust_website() | |
162 catch /^piecrust\:/ | |
163 return | |
164 endtry | |
165 let b:piecrust_dir = l:website.root_dir | |
166 if exists('b:piecrust_dir') && l:do_setup | |
167 call s:trace("Setting PieCrust commands for buffer '" . bufname('%')) | |
168 call s:trace(" with website : " . expand(b:piecrust_dir)) | |
169 silent doautocmd User PieCrust | |
170 endif | |
171 endfunction | |
172 | |
173 augroup piecrust_detect | |
174 autocmd! | |
175 autocmd BufNewFile,BufReadPost * call s:setup_buffer_commands() | |
176 autocmd VimEnter * if expand('<amatch>')==''|call s:setup_buffer_commands()|endif | |
177 augroup end | |
178 | |
179 " }}} | |
180 | |
181 " Buffer Commands Management {{{ | |
182 | |
183 " Store the commands for PieCrust-enabled buffers so that we can add them in | |
184 " batch when we need to. | |
185 let s:main_commands = [] | |
186 | |
187 function! s:AddMainCommand(command) abort | |
188 let s:main_commands += [a:command] | |
189 endfunction | |
190 | |
191 function! s:DefineMainCommands() | |
192 for l:command in s:main_commands | |
193 execute 'command! -buffer ' . l:command | |
194 endfor | |
195 endfunction | |
196 | |
197 augroup piecrust_main | |
198 autocmd! | |
199 autocmd User PieCrust call s:DefineMainCommands() | |
200 augroup end | |
201 | |
202 " }}} | |
203 | |
204 " Pcedit {{{ | |
205 | |
206 function! s:PcEdit(bang, filename) abort | |
207 let l:full_path = s:piecrust_website().GetFullPath(a:filename) | |
208 if a:bang | |
209 execute "edit! " . l:full_path | |
210 else | |
211 execute "edit " . l:full_path | |
212 endif | |
213 endfunction | |
214 | |
215 function! s:FindWebsiteFiles(ArgLead, CmdLine, CursorPos) abort | |
216 let l:website = s:piecrust_website() | |
217 let l:output = l:website.RunCommand('find', a:ArgLead) | |
218 let l:matches = split(l:output, '\n') | |
219 call map(l:matches, 's:normalizepath(v:val)') | |
220 return l:matches | |
221 endfunction | |
222 | |
223 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:FindWebsiteFiles Pcedit :call s:PcEdit(<bang>0, <f-args>)") | |
224 | |
225 " }}} | |
226 | |
1
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
227 " Pcposturl {{{ |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
228 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
229 function! s:PcPostUrl(path) abort |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
230 " Get information about the given path. |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
231 let l:website = s:piecrust_website() |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
232 let l:fullPath = l:website.GetFullPath('_content/posts/' . a:path) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
233 let l:output = l:website.RunCommand('find', '--exact', '--components', l:fullPath) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
234 call s:trace(l:output) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
235 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
236 " Get each bit of info from the command output. |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
237 let l:lines = split(l:output, '\n') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
238 let l:components = {} |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
239 for line in l:lines |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
240 let l:kv = split(line, ': ') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
241 if len(l:kv) != 2 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
242 continue |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
243 endif |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
244 let l:components[l:kv[0]] = l:kv[1] |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
245 endfor |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
246 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
247 " Check for errors |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
248 if !has_key(l:components, 'year') || |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
249 \!has_key(l:components, 'month') || |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
250 \!has_key(l:components, 'day') || |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
251 \!has_key(l:components, 'slug') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
252 call s:error(join(l:lines, '\n')) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
253 return |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
254 endif |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
255 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
256 " Print the `pcposturl` command. |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
257 let l:posturl = "{{ pcposturl(" |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
258 let l:posturl .= l:components['year'] . ", " |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
259 let l:posturl .= l:components['month'] . ", " |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
260 let l:posturl .= l:components['day'] . ", " |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
261 let l:posturl .= "'" . l:components['slug'] . "'" |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
262 let l:posturl .= ") }}" |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
263 call setreg(v:register, l:posturl) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
264 execute 'normal "' . v:register . 'p' |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
265 endfunction |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
266 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
267 function! s:FindWebsitePosts(ArgLead, CmdLine, CursorPos) abort |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
268 let l:website = s:piecrust_website() |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
269 let l:output = l:website.RunCommand('find', '--posts', a:ArgLead) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
270 let l:matches = split(l:output, '\n') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
271 let l:length = strlen('_content/posts/') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
272 call map(l:matches, 'strpart(v:val, ' . l:length . ')') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
273 return l:matches |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
274 endfunction |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
275 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
276 call s:AddMainCommand("-nargs=? -complete=customlist,s:FindWebsitePosts Pcposturl :call s:PcPostUrl(<f-args>)") |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
277 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
278 " }}} |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
279 |
0 | 280 " Autoload Functions {{{ |
281 | |
282 " Rescans the current buffer for setting up PieCrust commands. | |
283 " Passing '1' as the parameter enables debug traces temporarily. | |
284 function! piecrust#rescan(...) | |
285 if exists('b:piecrust_dir') | |
286 unlet b:piecrust_dir | |
287 endif | |
288 if a:0 && a:1 | |
289 let l:trace_backup = g:piecrust_trace | |
290 let g:piecrust_trace = 1 | |
291 endif | |
292 call s:setup_buffer_commands() | |
293 if a:0 && a:1 | |
294 let g:piecrust_trace = l:trace_backup | |
295 endif | |
296 endfunction | |
297 | |
298 " Enables/disables the debug trace. | |
299 function! piecrust#debugtrace(...) | |
300 let g:piecrust_trace = (a:0 == 0 || (a:0 && a:1)) | |
301 echom "PieCrust debug trace is now " . (g:piecrust_trace ? "enabled." : "disabled.") | |
302 endfunction | |
303 | |
304 " }}} |