After spending several frustrating sessions trying to find a solution to posting sourcecode on this blog I think I’ve got something workable.
Most of the methods I tried, including the one in the WordPress FAQ, fail on some operator characters found in lisp or C++ (like e.g.<<) and although I don’t use vim much these days, tending to prefer nvi, it does have a handy htmlize function :TOhtml. This, with some tweaking, generates html acceptable to WordPress from a buffer of code (or some lines marked in Visual Mode).
I used some vim scripting given below to strip out the <pre> section from the generated html, wrap <code> just inside it and copy it to the clipboard ready for pasting into the browser. I have it bound to the keystroke Alt-l. One further complication was that WordPress, trying to be helpful, swaps out some characters even though they are in a <code> block, so I do a search/replace for apostrophes.
func Convert2HTMLClip(line1, line2)
if a:line2 >= a:line1
let g:html_start_line = a:line1
let g:html_end_line = a:line2
else
let g:html_start_line = a:line2
let g:html_end_line = a:line1
endif
runtime syntax/2html.vim
unlet g:html_start_line
unlet g:html_end_line
normal! gg
exe ":0,/<pre>/- d"
normal! A<code>
normal! G
exe ":?</pre>?+,$ d"
normal! I</code>
exe ":%s/'/\\'/eg"
exe ":%s/&#39;/\\&\\#39;/eg"
exe ":%s/&#35;/\\&\\#35;/eg"
normal! ggVG"+y
exe ":bd!"
endfunc
command -range=% TohtmlClip :call Convert2HTMLClip(<line1>, <line2>)
map <M-l> :TohtmlClip<CR>


