Mercurial > vim-piecrust
annotate plugin/piecrust.vim @ 3:af8514b79c04 default tip
Fix for PieCrust 1.0-rc1.
Added very simple `Pcprepare` command.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 14 Mar 2013 22:15:58 -0700 |
parents | 6185e60279c0 |
children |
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 | |
3
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
116 let l:chef_command = g:piecrust_chef_executable . ' --root=' . shellescape(s:stripslash(self.root_dir)) |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
117 let l:chef_command = l:chef_command . ' ' . a:command . ' ' . join(l:arg_list, ' ') |
0 | 118 call s:trace("Running Chef command: " . l:chef_command) |
119 return system(l:chef_command) | |
120 endfunction | |
121 | |
122 " Website cache map | |
123 let s:buffer_websites = {} | |
124 | |
125 " Get a cached website | |
126 function! s:piecrust_website(...) abort | |
127 " Use the given path, or the website directory of the current buffer. | |
128 if a:0 == 0 | |
129 if exists('b:piecrust_dir') | |
130 let l:path = b:piecrust_dir | |
131 else | |
132 let l:path = s:find_website_root(expand('%:p')) | |
133 endif | |
134 else | |
135 let l:path = a:1 | |
136 endif | |
137 " Find a cache website instance, or make a new one. | |
138 if has_key(s:buffer_websites, l:path) | |
139 return get(s:buffer_websites, l:path) | |
140 else | |
141 let l:website = s:PieCrust.New(l:path) | |
142 let s:buffer_websites[l:path] = l:website | |
143 return l:website | |
144 endif | |
145 endfunction | |
146 | |
147 " Sets up the current buffer with PieCrust commands if it contains a file from a PieCrust website. | |
148 " If the file is not in a PieCrust website, just exit silently. | |
149 function! s:setup_buffer_commands() abort | |
150 call s:trace("Scanning buffer '" . bufname('%') . "' for PieCrust setup...") | |
151 let l:do_setup = 1 | |
152 if exists('b:piecrust_dir') | |
153 if b:piecrust_dir =~# '\v^\s*$' | |
154 unlet b:piecrust_dir | |
155 else | |
156 let l:do_setup = 0 | |
157 endif | |
158 endif | |
159 try | |
160 let l:website = s:piecrust_website() | |
161 catch /^piecrust\:/ | |
162 return | |
163 endtry | |
164 let b:piecrust_dir = l:website.root_dir | |
165 if exists('b:piecrust_dir') && l:do_setup | |
166 call s:trace("Setting PieCrust commands for buffer '" . bufname('%')) | |
167 call s:trace(" with website : " . expand(b:piecrust_dir)) | |
168 silent doautocmd User PieCrust | |
169 endif | |
170 endfunction | |
171 | |
172 augroup piecrust_detect | |
173 autocmd! | |
174 autocmd BufNewFile,BufReadPost * call s:setup_buffer_commands() | |
175 autocmd VimEnter * if expand('<amatch>')==''|call s:setup_buffer_commands()|endif | |
176 augroup end | |
177 | |
178 " }}} | |
179 | |
180 " Buffer Commands Management {{{ | |
181 | |
182 " Store the commands for PieCrust-enabled buffers so that we can add them in | |
183 " batch when we need to. | |
184 let s:main_commands = [] | |
185 | |
186 function! s:AddMainCommand(command) abort | |
187 let s:main_commands += [a:command] | |
188 endfunction | |
189 | |
190 function! s:DefineMainCommands() | |
191 for l:command in s:main_commands | |
192 execute 'command! -buffer ' . l:command | |
193 endfor | |
194 endfunction | |
195 | |
196 augroup piecrust_main | |
197 autocmd! | |
198 autocmd User PieCrust call s:DefineMainCommands() | |
199 augroup end | |
200 | |
201 " }}} | |
202 | |
203 " Pcedit {{{ | |
204 | |
205 function! s:PcEdit(bang, filename) abort | |
206 let l:full_path = s:piecrust_website().GetFullPath(a:filename) | |
207 if a:bang | |
208 execute "edit! " . l:full_path | |
209 else | |
210 execute "edit " . l:full_path | |
211 endif | |
212 endfunction | |
213 | |
214 function! s:FindWebsiteFiles(ArgLead, CmdLine, CursorPos) abort | |
215 let l:website = s:piecrust_website() | |
216 let l:output = l:website.RunCommand('find', a:ArgLead) | |
217 let l:matches = split(l:output, '\n') | |
218 call map(l:matches, 's:normalizepath(v:val)') | |
219 return l:matches | |
220 endfunction | |
221 | |
222 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:FindWebsiteFiles Pcedit :call s:PcEdit(<bang>0, <f-args>)") | |
223 | |
224 " }}} | |
225 | |
3
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
226 " Pcprepare {{{ |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
227 |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
228 function! s:PcPrepare(type, slug) abort |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
229 let l:website = s:piecrust_website() |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
230 let l:output = l:website.RunCommand('prepare', type, slug) |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
231 call s:trace(l:output) |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
232 endfunction |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
233 |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
234 call s:AddMainCommand("-nargs=* Pcprepare :call s:PcPrepare(<f-args>)") |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
235 |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
236 " }}} |
af8514b79c04
Fix for PieCrust 1.0-rc1.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
237 |
1
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
238 " Pcposturl {{{ |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
239 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
240 function! s:PcPostUrl(path) abort |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
241 " Get information about the given path. |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
242 let l:website = s:piecrust_website() |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
243 let l:fullPath = l:website.GetFullPath('_content/posts/' . a:path) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
244 let l:output = l:website.RunCommand('find', '--exact', '--components', l:fullPath) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
245 call s:trace(l:output) |
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 " Get each bit of info from the command output. |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
248 let l:lines = split(l:output, '\n') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
249 let l:components = {} |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
250 for line in l:lines |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
251 let l:kv = split(line, ': ') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
252 if len(l:kv) != 2 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
253 continue |
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 let l:components[l:kv[0]] = l:kv[1] |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
256 endfor |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
257 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
258 " Check for errors |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
259 if !has_key(l:components, 'year') || |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
260 \!has_key(l:components, 'month') || |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
261 \!has_key(l:components, 'day') || |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
262 \!has_key(l:components, 'slug') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
263 call s:error(join(l:lines, '\n')) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
264 return |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
265 endif |
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 " Print the `pcposturl` command. |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
268 let l:posturl = "{{ pcposturl(" |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
269 let l:posturl .= l:components['year'] . ", " |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
270 let l:posturl .= l:components['month'] . ", " |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
271 let l:posturl .= l:components['day'] . ", " |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
272 let l:posturl .= "'" . l:components['slug'] . "'" |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
273 let l:posturl .= ") }}" |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
274 call setreg(v:register, l:posturl) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
275 execute 'normal "' . v:register . 'p' |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
276 endfunction |
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 function! s:FindWebsitePosts(ArgLead, CmdLine, CursorPos) abort |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
279 let l:website = s:piecrust_website() |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
280 let l:output = l:website.RunCommand('find', '--posts', a:ArgLead) |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
281 let l:matches = split(l:output, '\n') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
282 let l:length = strlen('_content/posts/') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
283 call map(l:matches, 'strpart(v:val, ' . l:length . ')') |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
284 return l:matches |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
285 endfunction |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
286 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
287 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
|
288 |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
289 " }}} |
6185e60279c0
Added `Pcposturl` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
290 |
0 | 291 " Autoload Functions {{{ |
292 | |
293 " Rescans the current buffer for setting up PieCrust commands. | |
294 " Passing '1' as the parameter enables debug traces temporarily. | |
295 function! piecrust#rescan(...) | |
296 if exists('b:piecrust_dir') | |
297 unlet b:piecrust_dir | |
298 endif | |
299 if a:0 && a:1 | |
300 let l:trace_backup = g:piecrust_trace | |
301 let g:piecrust_trace = 1 | |
302 endif | |
303 call s:setup_buffer_commands() | |
304 if a:0 && a:1 | |
305 let g:piecrust_trace = l:trace_backup | |
306 endif | |
307 endfunction | |
308 | |
309 " Enables/disables the debug trace. | |
310 function! piecrust#debugtrace(...) | |
311 let g:piecrust_trace = (a:0 == 0 || (a:0 && a:1)) | |
312 echom "PieCrust debug trace is now " . (g:piecrust_trace ? "enabled." : "disabled.") | |
313 endfunction | |
314 | |
315 " }}} |