Skip to content

Commit

Permalink
Improved handling of VimL script-local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 15, 2010
1 parent 2323f33 commit 537fe77
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -69,4 +69,4 @@ This software is licensed under the [MIT license] [license].<br>
[license]: http://en.wikipedia.org/wiki/MIT_License
[vim]: http://www.vim.org/
[vim_scripts_entry]: http://www.vim.org/scripts/script.php?script_id=2252
[zip]: http://github.com/downloads/xolox/vim-publish/publish-latest.zip
[zip]: http://peterodding.com/code/vim/download.php?script=publish
17 changes: 15 additions & 2 deletions TODO.md
Expand Up @@ -7,5 +7,18 @@ Here's some things in the nice-to-have department:

* I once wrote a PHP script that created fancy looking dynamic directory
listings for trees of source code published using a previous incarnation of
this plug-in. Reincarnate that script as an add-on for the Vim plug-in that
generates static index pages which don't need PHP but work just as well?
this plug-in. You can [see it in action][autoindex]. Should I reincarnate
that script as an add-on for the Vim plug-in that generates static index
pages which don't need PHP but work just as well?

* Automatically generate a temporary tags file up front? (So I don't have to
rerun the plug-in when I break my [easytags.vim][easytags] plug-in which
also breaks the hyperlinking feature of `publish.vim`.

* Create an option to publish to a temporary local directory, create a tarball
from the published files, upload the tarball to a remote location and unpack
it there because establishing SFTP connections has quite a lot of overhead?


[autoindex]: http://peterodding.com/code/vim/profile
[easytags]: http://peterodding.com/code/vim/easytags
67 changes: 45 additions & 22 deletions autoload.vim
@@ -1,6 +1,6 @@
" Vim script
" Maintainer: Peter Odding <peter@peterodding.com>
" Last Change: June 6, 2010
" Last Change: June 15, 2010
" URL: http://peterodding.com/code/vim/publish

function! publish#resolve_files(directory, pathnames) " {{{1
Expand All @@ -25,22 +25,23 @@ function! publish#find_tags(files_to_publish) " {{{1
let s:cached_contents = {}
for entry in taglist('.')
let pathname = xolox#path#absolute(entry.filename)
if has_key(a:files_to_publish, pathname)
call s:pattern_to_lnum(entry, pathname)
if entry.cmd =~ '^\d\+$'
if !has_key(tags_to_publish, entry.name)
let tags_to_publish[entry.name] = entry
else
let num_duplicates += 1
if num_duplicates <= 3
let tag_name = string(entry.name)
let this_path = string(entry.filename)
let other_path = string(tags_to_publish[entry.name].filename)
let msg = "publish.vim: Ignoring duplicate tag %s! (duplicate is in %s, first was in %s)"
echohl warningmsg
echomsg printf(msg, tag_name, this_path, other_path)
echohl none
if has_key(a:files_to_publish, pathname) && s:pattern_to_lnum(entry, pathname)
if !has_key(tags_to_publish, entry.name)
let tags_to_publish[entry.name] = entry
else
let num_duplicates += 1
if num_duplicates <= 3
let other = tags_to_publish[entry.name]
if entry.filename == other.filename && entry.lnum < other.lnum
let tags_to_publish[entry.name] = entry
endif
let tag_name = string(entry.name)
let this_path = string(entry.filename)
let other_path = string(other.filename)
let msg = "publish.vim: Ignoring duplicate tag %s! (duplicate is in %s, first was in %s)"
echohl warningmsg
echomsg printf(msg, tag_name, this_path, other_path)
echohl none
endif
endif
endif
Expand All @@ -61,7 +62,10 @@ function! s:pattern_to_lnum(entry, pathname) " {{{2
" search patterns. Since search patterns are more flexible I use those, but
" the plug-in needs absolute line numbers, so this function converts search
" patterns into line numbers.
if a:entry.cmd !~ '^\d\+$'
if a:entry.cmd =~ '^\d\+$'
let a:entry.lnum = a:entry.cmd + 0
return 1
else
if !has_key(s:cached_contents, a:pathname)
let contents = readfile(a:pathname)
let s:cached_contents[a:pathname] = contents
Expand All @@ -76,8 +80,8 @@ function! s:pattern_to_lnum(entry, pathname) " {{{2
throw "Failed pattern: " . string(pattern)
endtry
if index >= 0
let lnum = index + 1
let a:entry.cmd = string(lnum)
let a:entry.lnum = index + 1
return 1
endif
endif
endfunction
Expand All @@ -91,19 +95,38 @@ function! publish#create_subst_cmd(tags_to_publish) " {{{1
" foo#bar#<span class=Normal>baz</span>
"
let patterns = []
let ignore_html = '\%%(<[^/][^>]*>%s</[^>]\+>\|%s\)'
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, printf(ignore_html, token, 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]>\)' . 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)
endif
endif
call add(patterns, join(tokens, ''))
endfor
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'
return '%s/\w\@<!\%(' . tag_names_pattern . '\)\w\@!/\=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 . '\)'
endfunction

function! publish#create_dirs(target_path) " {{{1
Expand Down
16 changes: 13 additions & 3 deletions publish.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Maintainer: Peter Odding <peter@peterodding.com>
" Last Change: June 6, 2010
" Last Change: June 15, 2010
" URL: http://peterodding.com/code/vim/publish
" License: MIT
" Version: 1.5
Expand All @@ -21,6 +21,10 @@ 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
call s:Message("Preparing to publish file%s ..", len(a:files) == 1 ? '' : 's')
let s:files_to_publish = publish#resolve_files(a:source, a:files)
Expand Down Expand Up @@ -82,13 +86,19 @@ function! s:ConvertTagToLink(name) " {{{1
" work on the local file system just as well as on a web server.
try
" Strip HTML from matched text and use result to find tag info.
let entry = s:tags_to_publish[substitute(a:name, '<[^>]\+>', '', 'g')]
let text = substitute(a:name, '<[^>]\+>', '', 'g')
if has_key(s:tags_to_publish, text)
let entry = s:tags_to_publish[text]
else
let text = substitute(text, '^\(s:\|&lt;[Ss][Ii][Dd]&gt;\)', '', 'g')
let entry = s:tags_to_publish[text]
endif
" Convert the fully resolved pathname back into the one given by the user.
let pathname = s:FindOriginalPath(entry.filename)
" Now convert that pathname into a relative hyperlink with an anchor.
let relative = xolox#path#relative(pathname, s:current_source_directory)
let suffix = g:publish_omit_dothtml ? '' : '.html'
return '<a href="' . relative . suffix . '#l' . entry.cmd . '">' . a:name . '</a>'
return '<a href="' . relative . suffix . '#l' . entry.lnum . '">' . a:name . '</a>'
catch
return a:name
endtry
Expand Down

0 comments on commit 537fe77

Please sign in to comment.