~dricottone/vim-markdown-preview

cf2cc781caf7075f0e4b089f47a494a48b7b8ad3 — JamshedVesuna 9 years ago 2be6693
Include Safari on OSX
2 files changed, 58 insertions(+), 20 deletions(-)

M README.md
M plugin/vim-markdown-preview.vim
M README.md => README.md +19 -8
@@ 1,7 1,7 @@
Vim Markdown Preview
====================

A Vim plugin for previewing markdown files in a browser. This plugin was designed to maintain focus on vim but use Google Chrome to preview rendered markdown. Thus, everything is done in the background so you never have to leave Vim.
A Vim plugin for previewing markdown files in a browser. This plugin was designed to maintain focus on vim but use Google Chrome or Safari to preview rendered markdown. Thus, everything is done in the background so you never have to leave Vim.

![Screenshot](images/screenshot.gif?raw=true "Screenshot")



@@ 9,26 9,37 @@ Installation
============

1. With Pathogen: Place `vim-markdown-preview/` in `.vim/bundle/`.
2. Default browsers:
    * OS X: Safari
    * Unix: Google Chrome

Support and Requirements
========================

## OS X:

* [Markdown](http://daringfireball.net/projects/markdown/)
* [Safari](https://www.apple.com/safari/)

## Unix:

Requirements
============
* [Markdown](http://daringfireball.net/projects/markdown/)
* [xdotool](https://github.com/jordansissel/xdotool)
* [Google Chrome](https://www.google.com/chrome/browser/)

Usage
=====
When in a *.markdown or *.md file, vim-markdown-preview does the following when you type `Ctrl-p`:
When in a *.markdown or *.md file, vim-markdown-preview does the following when you type `Ctrl-p` (can be remapped):

* If you are not previewing the current file:
    * Open an html rendered version of your file in Google Chrome in the background.
    * Open an html rendered version of your file in Google Chrome or Safari in the background.
* Otherwise:
    * Refresh your preview of the current markdown file in Google Chrome.
    * Refresh your preview of the current markdown file in Google Chrome or Safari.

Uncomment the last line in `plugin/vim-markdown-preview.vim` to map a buffer write to the above instead of `Ctrl-p`.

Behind The Scenes
=================
1. First, vim-markdown-preview renders your markdown as html and creates a temporary file `vim-markdown-preview.html` in `/tmp/`.
2. Next, vim-markdown-preview then either opens the html file or refreshes the Google Chrome tab.
3. If you would like vim-markdown-preview to remove the temporary file so nothing left behind, uncomment lines 20 and 21.
2. Next, vim-markdown-preview then either opens the html file or refreshes the Google Chrome or Safari tab.
3. If you would like vim-markdown-preview to remove the temporary file so nothing left behind, set `REMOVE_TEMP_FILE` to 1.

M plugin/vim-markdown-preview.vim => plugin/vim-markdown-preview.vim +39 -12
@@ 4,21 4,48 @@
"============================================================

function! Vim_Markdown_Preview()

  let BROWSER = 'google-chrome'
  let OSNAME = 'Unidentified'
  let REMOVE_TEMP_FILE = 0  "To remove the temp file, set to 1

  if has('win32')
    " Not yet used
    let OSNAME = 'win32'
  endif
  if has('unix')
    let OSNAME = 'unix'
  endif
  if has('mac')
    let OSNAME = 'mac'
    let BROWSER = 'safari'
  endif

  let curr_file = expand('%:p')
  call system('markdown ' . curr_file . ' > /tmp/vim-markdown-preview.html')
  let chrome_wid = system("xdotool search --name 'vim-markdown-preview.html - Google Chrome'")
  if !chrome_wid
    "sleep 300m
    call system('see /tmp/vim-markdown-preview.html & > /dev/null &')
  else
    let curr_wid = system('xdotool getwindowfocus')
    call system('xdotool windowmap ' . chrome_wid)
    call system('xdotool windowactivate ' . chrome_wid)
    call system("xdotool key 'ctrl+r'")
    call system('xdotool windowactivate ' . curr_wid)

  if OSNAME == 'unix' && BROWSER == 'google-chrome'
    let chrome_wid = system("xdotool search --name 'vim-markdown-preview.html - Google Chrome'")
    if !chrome_wid
      "sleep 300m
      call system('see /tmp/vim-markdown-preview.html & > /dev/null &')
    else
      let curr_wid = system('xdotool getwindowfocus')
      call system('xdotool windowmap ' . chrome_wid)
      call system('xdotool windowactivate ' . chrome_wid)
      call system("xdotool key 'ctrl+r'")
      call system('xdotool windowactivate ' . curr_wid)
    endif
    "sleep 700m
  endif

  if OSNAME == 'mac' && BROWSER == 'safari'
    call system('open -g /tmp/vim-markdown-preview.html')
  endif

  if REMOVE_TEMP_FILE
    call system('rm /tmp/vim-markdown-preview.html')
  endif
  "sleep 700m
  "call system('rm /tmp/vim-markdown-preview.html')
endfunction

autocmd Filetype markdown,md map <buffer> <C-p> :call Vim_Markdown_Preview()<CR>