waylan / vim-markdown-extra-preview

A Python port of vim-markdown-preview that utilizes Python-Markdown's 'extra' extension.
17 stars 7 forks source link

Vim-Markdown-Extra-Preview

Preview Markdown documents in a Browser.

This plugin passes the current buffer through Python-Markdown (with the extra extension) and displays the resulting HTML in the system default browser.

The Python-Markdown library is included. However, Vim must be compiled with Python support (+python) and the matching version of Python must be installed on the system. The plugin checks this when called and will fail with an error message if Python is not available.

Usage

There are two commands available:

Options

Global Settings:

The following settings can be set globally in your .vimrc file and will effect the behavior of the plugin in all instances. Changing the global settings in one buffer will effect all buffers. See Local Settings if you want to change setting for a specific buffer.

Local Settings

Local settings are the same as the global settings except that they only apply to a single buffer. If a local setting is not set, the corresponding global setting will be used. To set a local setting, prepend the setting name with b: rather than g: (i.e., use b:VMEPstylesheet rather than g:VEMPstylesheet).

For example, to use a different template, when viewing the buffer, execute the following command:

:let b:VMEPtemplate = '/path/to/template.html'

You could also use autocommands in your .vimrc file to use a different setting for all files in a specific directory or of a specific type. Something like:

au BufNewFile,BufRead /path/to/project/* \
                let b:VMEPtemplate = '/path/to/template.html' | \
                let b:VMEPstylesheet = '/path/to/style.css'

Note that each command is separated by a |. The \ indicate line wraps as the command should all be one line.

You could even create a .vimrc file in your project directory and source it:

au BufNewFile,BufRead /path/to/project/* source /path/to/project/.vimrc

Just make sure your local .vimrc only defines buffer specific settings.

Templates

The template pointed to by the g:VMEPtemplate setting must be a file which is readable and contains the wrapper around which Markdown's output is inserted. The file extension of the template is used as the file extension of the output file. Templates use Python's string formatting to insert the following data:

If Python-Markdown's Meta-Data extension is used, any meta-data defined in the buffer will also be added to the context passed to the template. The meta-data will overwrite any of the existing data. This could be useful for setting a more verbose 'name' for the document or to set a document specific style. But it could also overwrite data if not used with care.

To enable the meta-data extension, it will need to included in the g:VMEPextensions setting (or its buffer specific counterpart) like this:

:let g:VMEPextensions = ['extra', 'meta']

A simple template may look something like this:

 <html>
   <head>
     <title>%(name)s</title>
      <link rel="stylesheet" href="https://github.com/waylan/vim-markdown-extra-preview/blob/master/%(style)s"></link>
    </head>
    <body>
      %(content)s
    </body>
 </html>