1 " Vim plug-in
  2 " Author: Peter Odding <peter@peterodding.com>
  3 " Last Change: September 6, 2010
  4 " URL: http://peterodding.com/code/vim/publish/
  5 " License: MIT
  6 " Version: 1.7.1
  7 
  8 " Support for automatic update using the GLVS plug-in.
  9 " GetLatestVimScripts: 2252 1 :AutoInstall: publish.zip
 10 
 11 " Don't source the plug-in when its already been loaded or &compatible is set.
 12 if &cp || exists('g:loaded_publish')
 13   finish
 14 endif
 15 
 16 if !exists('g:publish_omit_dothtml')
 17   let g:publish_omit_dothtml = 0
 18 endif
 19 
 20 if !exists('g:publish_plaintext')
 21   let g:publish_plaintext = 0
 22 endif
 23 
 24 function! Publish(source, target, files) abort
 25   let start = xolox#timer#start()
 26   call xolox#message("Preparing to publish file%s ..", len(a:files) == 1 ? '' : 's')
 27   let s:files_to_publish = publish#resolve_files(a:source, a:files)
 28   call publish#update_tags(values(s:files_to_publish))
 29   let s:tags_to_publish = publish#find_tags(s:files_to_publish)
 30   if s:tags_to_publish != {}
 31     let tags_to_links_command = publish#create_subst_cmd(s:tags_to_publish)
 32   endif
 33   let rsync_target = publish#rsync_check(a:target)
 34   if rsync_target != ''
 35     let rsync_dir = xolox#path#tempdir()
 36   endif
 37   let target_dir = rsync_target != '' ? rsync_dir : a:target
 38   call publish#prep_env(1)
 39   for pathname in a:files
 40     let source_path = xolox#path#merge(a:source, pathname)
 41     let suffix = g:publish_omit_dothtml ? '' : '.html'
 42     let target_path = xolox#path#merge(target_dir, pathname . suffix)
 43     call xolox#message("Publishing %s", string(pathname))
 44     if !publish#create_dirs(target_path)
 45       return
 46     endif
 47     " Save the pathname of the directory containing the source file in a
 48     " script-local variable so that s:ConvertTagToLink() has access to it.
 49     let given_source = s:FindOriginalPath(source_path)
 50     let s:current_source_directory = fnamemodify(given_source, ':h')
 51     silent execute 'edit!' fnameescape(source_path)
 52     if g:publish_plaintext
 53       let plaintext_path = xolox#path#merge(target_dir, pathname . '.txt')
 54       silent execute 'write!' fnameescape(plaintext_path)
 55     endif
 56     " Highlight tags in current buffer using easytags.vim?
 57     if exists('g:loaded_easytags')
 58       HighlightTags
 59     endif
 60     let highlight_start = xolox#timer#start()
 61     call publish#munge_syntax_items()
 62     runtime syntax/2html.vim
 63     let msg = "publish.vim: The 2html.vim script took %s to highlight %s."
 64     call xolox#timer#stop(msg, highlight_start, pathname)
 65     if exists('tags_to_links_command')
 66       let tags_to_links_start = xolox#timer#start()
 67       silent execute tags_to_links_command
 68       let msg = "publish.vim: Finished converting tags in %s to links in %s."
 69       call xolox#timer#stop(msg, pathname, tags_to_links_start)
 70     endif
 71     call publish#customize_html(pathname)
 72     silent execute 'write!' fnameescape(target_path)
 73     bwipeout!
 74   endfor
 75   unlet s:files_to_publish s:tags_to_publish
 76   if rsync_target != ''
 77     call publish#run_rsync(rsync_target, rsync_dir)
 78   endif
 79   let msg = "publish.vim: Published %i file%s to %s."
 80   call xolox#message(msg, len(a:files), len(a:files) == 1 ? '' : 's', a:target)
 81   call xolox#timer#stop("Finished publishing files in %s.", start)
 82   call publish#prep_env(0)
 83 endfunction
 84 
 85 function! s:FindOriginalPath(pathname) " {{{1
 86   let key = xolox#path#absolute(a:pathname)
 87   return get(s:files_to_publish, key, '')
 88 endfunction
 89 
 90 function! s:ConvertTagToLink(name) " {{{1
 91   " Convert each occurrence of every tag into a hyperlink that points to the
 92   " location where the tag is defined. Since the hyperlinks are relative they
 93   " work on the local file system just as well as on a web server.
 94   try
 95     " Strip HTML from matched text and use result to find tag info.
 96     let text = substitute(a:name, '<[^>]\+>', '', 'g')
 97     if has_key(s:tags_to_publish, text)
 98       let entry = s:tags_to_publish[text]
 99     else
100       let text = substitute(text, '^\(s:\|&lt;[Ss][Ii][Dd]&gt;\)', '', 'g')
101       let entry = s:tags_to_publish[text]
102     endif
103     " Convert the fully resolved pathname back into the one given by the user.
104     let pathname = s:FindOriginalPath(entry.filename)
105     " Now convert that pathname into a relative hyperlink with an anchor.
106     " TODO This is likely to be slow so cache the results?!
107     let relative = xolox#path#relative(pathname, s:current_source_directory)
108     let suffix = g:publish_omit_dothtml ? '' : '.html'
109     let href = publish#html_encode(relative . suffix . '#l' . entry.lnum)
110     return '<a href="' . href . '">' . a:name . '</a>'
111   catch
112     return a:name
113   endtry
114 endfunction
115 
116 let g:loaded_publish = 1
117 
118 " vim: ts=2 sw=2 et