Thursday, January 9, 2014

Re: convert markdown to html in new tab


On Jan 10, 2014 1:07 AM, "Rick Dooling" <rpdooling@gmail.com> wrote:
>
> On Thursday, January 9, 2014 2:29:41 PM UTC-6, ZyX wrote:
> > On Jan 9, 2014 11:50 PM, "Rick Dooling" <rpdo...@gmail.com> wrote:
> >
> > >
> >
> > > On Thursday, January 9, 2014 11:26:15 AM UTC-6, Rick Dooling wrote:
> >
> > > > On Thursday, January 9, 2014 10:42:47 AM UTC-6, Rick Dooling wrote:
> >
> > > > > Dear Vim Scripters:
> >
> > > > >
> >
> > > > > I know how to run external commands and send the output to new files and such, but I'm wondering if a Vim scripter can help me do something that has to be a common task.
> >
> > > > >
> >
> > > > > Assume I have a buffer open in Vim called file.markdown
> >
> > > > >
> >
> > > > > I want to run my external markdown processor of choice, say pandoc, on the contents of that buffer and have it appear in a new tab called file.html.
> >
> > > > >
> >
> > > > > In other words I don't want a filter to replace the markdown. I want to run the external command and have the output placed in a new appropriately named buffer in a new tab.
> >
> > > > >
> >
> > > > > THANK YOU
> >
> > > > >
> >
> > > > > Rick
> >
> > > >
> >
> > > > Thank you,
> >
> > > >
> >
> > > > I shall investigate execute!
> >
> > >
> >
> > > Quite an education! Still learning Vim. I mainly just write in it. Very little vim scripting.
> >
> > >
> >
> > > This works inside Vim on the command line
> >
> > >
> >
> > > :execute "!pandoc % -o html" | :tabe %:t:r.html
> >
> > >
> >
> > > But I could not map it. I would get weird errors about using :p:h.
> >
> > >
> >
> > > So I did this instead.
> >
> > >
> >
> > >
> >
> > > function! MD()
> >
> > >         exe "!pandoc % -o html"
> >
> > >         exe ":tabe %:t:r.html"
> >
> > > endfunction
> >
> > You do not need any :exe here in the current state. But you need :exe in the first statement: do not ever use % in shell commands as it is not doing any escaping. E.g. if name of currently edited file contains space first line of function MD will not do its job.
> >
> >
> > You should use
> >
> >     execute '!pandoc" shellescape(@%, 1) '-o ' shellescape(expand('%:t:r')) '.html'
> >
> > instead. This does not apply to vim commands: :tabe %:t:r.html is fine.
> >
> > >
> >
> > > --
> >
> > > --
> >
> > > You received this message from the "vim_use" maillist.
> >
> > > Do not top-post! Type your reply below the text you are replying to.
> >
> > > For more information, visit http://www.vim.org/maillist.php
> >
> > >
> >
> > > ---
> >
> > > You received this message because you are subscribed to the Google Groups "vim_use" group.
> >
> > > To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> >
> > > For more options, visit https://groups.google.com/groups/opt_out.
>
> THANK YOU!
>
> I probably would never have noticed the shellescape because I never use spaces in filenames. But always nice to be ready for cross platform.
>
> I'll leave this here for any other neophyte.
>
> function! MD()
>         exe '!pandoc' shellescape(@%, 1) ' -o ' shellescape(expand('%:t:r')).'.html'
>         :tabe %:t:r.html
> endfunction

Sorry, but I had a typo: both shellescape() calls should have second argument.

By the way, colon before tabe is optional. In mapping it is used to enter command mode, here it is just ignored (I usually use it to mark start of the body of :autocmd and :command: command -nargs=1 Foo :echo 'foo'). It does not harm in any case.

> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: