Skip to content

Commit

Permalink
Simplified tags -> hyperlinks conversion hack
Browse files Browse the repository at this point in the history
The longer I use publish.vim the more I start to push the boundaries of
its current implementation. This necessitated the following changes:

 * Recently the plug-in started crashing Vim with a segmentation fault.
   It turned out the plug-in was generating a corrupt regular expression
   because of tags containing unescaped [ characters:
   http://groups.google.com/group/vim_dev/browse_frm/thread/3c705d4ab811c979

 * After fixing the above bug Vim started throwing "E339: Pattern too
   long" errors which were apparently caused by the complexity (not the
   length) of the generated regular expression so I had to remove the
   ignore_html() hack and replace it with a much simpler hack: Slightly
   tweak Vim's syntax highlighting before running 2html.vim so that all
   tags appear literally in the output of 2html.vim.
  • Loading branch information
xolox committed Sep 5, 2010
1 parent 5df4c3d commit 459b829
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
39 changes: 16 additions & 23 deletions autoload.vim
Expand Up @@ -92,43 +92,36 @@ function! publish#create_subst_cmd(tags_to_publish) " {{{1
" Generate a :substitute command that, when executed, replaces tags with
" hyperlinks using a callback. This is complicated somewhat by the fact that
" tag names won't always appear literally in the output of 2html.vim, for
" example the names of Vim autoload functions appear as:
" example the names of Vim autoload functions can appear as:
"
" foo#bar#<span class=Normal>baz</span>
"
let patterns = []
let slfunctions = []
for name in keys(a:tags_to_publish)
let tokens = []
for token in split(name, '\W\@=\|\W\@<=')
let escaped = xolox#escape#pattern(token)
call add(tokens, s:ignore_html(token))
endfor
let entry = a:tags_to_publish[name]
if g:publish_viml_sl_hack && get(entry, 'language') == 'Vim'
let subpattern = '\s\(s:\|<[Ss][Ii][Dd]>\)' . xolox#escape#pattern(name) . '\s*('
if get(entry, 'cmd') =~ subpattern
if !exists('s:viml_sl_prefix')
let s:viml_sl_prefix = s:nasty()
endif
call insert(tokens, s:viml_sl_prefix)
if get(entry, 'language') == 'Vim'
let is_slfunc = '\s\(s:\|<[Ss][Ii][Dd]>\)' . xolox#escape#pattern(name) . '\s*('
if get(entry, 'cmd') =~ is_slfunc
call add(slfunctions, xolox#escape#pattern(name))
continue
endif
endif
call add(patterns, join(tokens, ''))
call add(patterns, xolox#escape#pattern(name))
endfor
call insert(patterns, '\%(\%(&lt;[Ss][Ii][Dd]&gt;\|s:\)\%(' . join(slfunctions, '\|') . '\)\)')
let tag_names_pattern = escape(join(patterns, '\|'), '/')
" Gotcha: Use \w\@<! and \w\@! here instead of \< and \> which won't work.
return '%s/[A-Za-z0-9_]\@<!\%(' . tag_names_pattern . '\)[A-Za-z0-9_]\@!/\=s:ConvertTagToLink(submatch(0))/eg'
endfunction

function! s:ignore_html(s)
return printf('\%%(<[^/][^>]*>%s</[^>]\+>\|%s\)', a:s, a:s)
endfunction

function! s:nasty()
" return '\%(s:\|&lt;[Ss][Ii][Dd]&gt;\)'
let short = s:ignore_html('s') . s:ignore_html(':')
let long = s:ignore_html('&lt;') . s:ignore_html('[Ss][Ii][Dd]') . s:ignore_html('&gt;')
return '\%(' . short . '\|' . long . '\)'
function! publish#munge_syntax_items() " {{{1
" Tag to hyperlink conversion only works when tag names appear literally in
" the output of 2html.vim while this isn't always the case in Vim scripts.
if &filetype == 'vim'
syntax match vimFuncName /\<s:\w\+\>/ containedin=vim.*
syntax match vimFuncName /\c<Sid>\w\+\>/ containedin=vim.*
endif
endfunction

function! publish#rsync_check(target) " {{{1
Expand Down
7 changes: 2 additions & 5 deletions publish.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: August 31, 2010
" Last Change: September 5, 2010
" URL: http://peterodding.com/code/vim/publish/
" License: MIT
" Version: 1.6
Expand All @@ -21,10 +21,6 @@ if !exists('g:publish_plaintext')
let g:publish_plaintext = 0
endif

if !exists('g:publish_viml_sl_hack')
let g:publish_viml_sl_hack = 1
endif

function! Publish(source, target, files) abort
let start = xolox#timer#start()
call xolox#message("Preparing to publish file%s ..", len(a:files) == 1 ? '' : 's')
Expand Down Expand Up @@ -58,6 +54,7 @@ function! Publish(source, target, files) abort
endif
silent execute 'doautocmd User PublishPre'
let highlight_start = xolox#timer#start()
call publish#munge_syntax_items()
runtime syntax/2html.vim
let msg = "publish.vim: The 2html.vim script took %s to highlight %s."
call xolox#timer#stop(msg, highlight_start, pathname)
Expand Down

0 comments on commit 459b829

Please sign in to comment.