Version 2.0.stu

This release is partly incompatible with 1.2.

You should delete previous version of vimwiki before install.

= Summary =

    * Quick page-link creation.
    * Redesign of link syntaxes (!)
        * No more CamelCase links. Check the ways to convert them http://goo.gl/15ctX
        * No more [[link][desc]] links.
        * No more [http://link description] links.
        * No more plain image links. Use transclusions.
        * No more image links identified by extension. Use transclusions.
    * Interwiki links.
    * Link schemes.
    * Transclusions.
    * Normalize link command.
    * Improved diary organization and generation.
    * List manipulation.
    * Markdown support.
    * Mathjax support.
    * Improved handling of special characters and punctuation in filenames and urls.
    * Back links command: list links referring to the current page.
    * Highlighting nonexisted links are off by default.
    * Table syntax change. Row separator uses | instead of +.
    * Fold multilined list items.
    * Custom wiki to HTML converters.
    * Conceal long weblinks.
    * Option to disable table mappings.

For detailed information see issues list on
http://code.google.com/p/vimwiki/issues/list
This commit is contained in:
Maxim Kim
2012-06-07 00:00:00 +00:00
committed by Able Scraper
parent 84297c9051
commit d5a6d097da
17 changed files with 4161 additions and 2011 deletions

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,9 @@ endif
let g:loaded_vimwiki_diary_auto = 1
"}}}
let s:vimwiki_max_scan_for_caption = 5
" Helpers {{{
function! s:prefix_zero(num) "{{{
if a:num < 10
return '0'.a:num
@ -18,10 +21,6 @@ function! s:prefix_zero(num) "{{{
return a:num
endfunction "}}}
function! s:desc(d1, d2) "{{{
return a:d1 == a:d2 ? 0 : a:d1 < a:d2 ? 1 : -1
endfunction "}}}
function! s:get_date_link(fmt) "{{{
return strftime(a:fmt)
endfunction "}}}
@ -37,76 +36,26 @@ function! s:link_exists(lines, link) "{{{
return link_exists
endfunction "}}}
function! s:diary_path() "{{{
return VimwikiGet('path').VimwikiGet('diary_rel_path')
function! s:diary_path(...) "{{{
let idx = a:0 == 0 ? g:vimwiki_current_idx : a:1
return VimwikiGet('path', idx).VimwikiGet('diary_rel_path', idx)
endfunction "}}}
function! s:diary_index() "{{{
return s:diary_path().VimwikiGet('diary_index').VimwikiGet('ext')
function! s:diary_index(...) "{{{
let idx = a:0 == 0 ? g:vimwiki_current_idx : a:1
return s:diary_path(idx).VimwikiGet('diary_index', idx).VimwikiGet('ext', idx)
endfunction "}}}
function! s:get_diary_range(lines, header) "{{{
let rx = '\[\[\d\{4}-\d\d-\d\d\]\]'
let idx = 0
let ln_start = -1
let ln_end = -1
for line in a:lines
if ln_start != -1
if line =~ '^\s*\(=\)\+.*\1\s*$' || (line !~ rx && line !~ '^\s*$')
break
endif
endif
if line =~ '^\s*\(=\)\+\s*'.a:header.'\s*\1\s*$'
let ln_start = idx + 1
endif
let idx += 1
endfor
let ln_end = idx
return [ln_start, ln_end]
endfunction "}}}
function! s:diary_date_link() "{{{
return s:get_date_link(VimwikiGet('diary_link_fmt'))
endfunction "}}}
function! s:get_file_contents(file_name) "{{{
let lines = []
let bufnr = bufnr(expand(a:file_name))
if bufnr != -1
let lines = getbufline(bufnr, 1, '$')
else
try
let lines = readfile(expand(a:file_name))
catch
endtry
endif
return [lines, bufnr]
endfunction "}}}
function! s:get_links() "{{{
let rx = '\d\{4}-\d\d-\d\d'
let s_links = glob(VimwikiGet('path').VimwikiGet('diary_rel_path').
\ '*'.VimwikiGet('ext'))
let s_links = substitute(s_links, '\'.VimwikiGet('ext'), "", "g")
let links = split(s_links, '\n')
" remove backup files (.wiki~)
call filter(links, 'v:val !~ ''.*\~$''')
" remove paths
call map(links, 'fnamemodify(v:val, ":t")')
call filter(links, 'v:val =~ "'.escape(rx, '\').'"')
return links
function! s:diary_date_link(...) "{{{
let idx = a:0 == 0 ? g:vimwiki_current_idx : a:1
return s:get_date_link(VimwikiGet('diary_link_fmt', idx))
endfunction "}}}
function! s:get_position_links(link) "{{{
let idx = -1
let links = []
if a:link =~ '\d\{4}-\d\d-\d\d'
let links = s:get_links()
if a:link =~ '^\d\{4}-\d\d-\d\d'
let links = keys(s:get_diary_links())
" include 'today' into links
if index(links, s:diary_date_link()) == -1
call add(links, s:diary_date_link())
@ -117,103 +66,263 @@ function! s:get_position_links(link) "{{{
return [idx, links]
endfunction "}}}
function! s:format_links(links) "{{{
let lines = []
let line = '| '
let idx = 0
let trigger = 0
while idx < len(a:links)
if idx/VimwikiGet('diary_link_count') > trigger
let trigger = idx/VimwikiGet('diary_link_count')
call add(lines, substitute(line, '\s\+$', '', ''))
let line = '| '
endif
let line .= a:links[idx].' | '
let idx += 1
endwhile
call add(lines, substitute(line, '\s\+$', '', ''))
call extend(lines, [''])
fun! s:get_month_name(month) "{{{
return g:vimwiki_diary_months[str2nr(a:month)]
endfun "}}}
return lines
endfunction "}}}
" Helpers }}}
function! s:add_link(page, header, link) "{{{
let [lines, bufnr] = s:get_file_contents(a:page)
" Diary index stuff {{{
fun! s:read_captions(files) "{{{
let result = {}
for fl in a:files
" remove paths and extensions
let fl_key = fnamemodify(fl, ':t:r')
let [ln_start, ln_end] = s:get_diary_range(lines, a:header)
let link = '[['.a:link.']]'
let link_exists = s:link_exists(lines[ln_start : ln_end], link)
if !link_exists
if ln_start == -1
call insert(lines, '= '.a:header.' =')
let ln_start = 1
let ln_end = 1
if filereadable(fl)
for line in readfile(fl, '', s:vimwiki_max_scan_for_caption)
if line =~ g:vimwiki_rxHeader && !has_key(result, fl_key)
let result[fl_key] = vimwiki#u#trim(matchstr(line, g:vimwiki_rxHeader))
endif
endfor
endif
" removing 'old' links
let idx = ln_end - ln_start
while idx > 0
call remove(lines, ln_start)
let idx -= 1
endwhile
" get all diary links from filesystem
let links = s:get_links()
call map(links, '"[[".v:val."]]"')
" add current link
if index(links, link) == -1
call add(links, link)
if !has_key(result, fl_key)
let result[fl_key] = ''
endif
let links = sort(links, 's:desc')
call extend(lines, s:format_links(links), ln_start)
endfor
return result
endfun "}}}
if bufnr != -1
exe 'buffer '.bufnr
if !&readonly
1,$delete _
call append(1, lines)
1,1delete _
endif
fun! s:get_diary_links(...) "{{{
let rx = '^\d\{4}-\d\d-\d\d'
let s_files = glob(VimwikiGet('path').VimwikiGet('diary_rel_path').'*'.VimwikiGet('ext'))
let files = split(s_files, '\n')
call filter(files, 'fnamemodify(v:val, ":t") =~ "'.escape(rx, '\').'"')
" remove backup files (.wiki~)
call filter(files, 'v:val !~ ''.*\~$''')
if a:0
call add(files, a:1)
endif
let links_with_captions = s:read_captions(files)
return links_with_captions
endfun "}}}
fun! s:group_links(links) "{{{
let result = {}
let p_year = 0
let p_month = 0
for fl in sort(keys(a:links))
let year = strpart(fl, 0, 4)
let month = strpart(fl, 5, 2)
if p_year != year
let result[year] = {}
let p_month = 0
endif
if p_month != month
let result[year][month] = {}
endif
let result[year][month][fl] = a:links[fl]
let p_year = year
let p_month = month
endfor
return result
endfun "}}}
fun! s:sort(lst) "{{{
if VimwikiGet("diary_sort") == 'desc'
return reverse(sort(a:lst))
else
return sort(a:lst)
endif
endfun "}}}
fun! s:format_diary(...) "{{{
let result = []
call add(result, substitute(g:vimwiki_rxH1_Template, '__Header__', VimwikiGet('diary_header'), ''))
if a:0
let g_files = s:group_links(s:get_diary_links(a:1))
else
let g_files = s:group_links(s:get_diary_links())
endif
" for year in s:rev(sort(keys(g_files)))
for year in s:sort(keys(g_files))
call add(result, '')
call add(result, substitute(g:vimwiki_rxH2_Template, '__Header__', year , ''))
" for month in s:rev(sort(keys(g_files[year])))
for month in s:sort(keys(g_files[year]))
call add(result, '')
call add(result, substitute(g:vimwiki_rxH3_Template, '__Header__', s:get_month_name(month), ''))
" for [fl, cap] in s:rev(sort(items(g_files[year][month])))
for [fl, cap] in s:sort(items(g_files[year][month]))
if empty(cap)
let entry = substitute(g:vimwiki_WikiLinkTemplate1, '__LinkUrl__', fl, '')
let entry = substitute(entry, '__LinkDescription__', cap, '')
call add(result, repeat(' ', &sw).'* '.entry)
else
let entry = substitute(g:vimwiki_WikiLinkTemplate2, '__LinkUrl__', fl, '')
let entry = substitute(entry, '__LinkDescription__', cap, '')
call add(result, repeat(' ', &sw).'* '.entry)
endif
endfor
endfor
endfor
call add(result, '')
return result
endfun "}}}
function! s:delete_diary_section() "{{{
" remove diary section
let old_pos = getpos('.')
let ln_start = -1
let ln_end = -1
call cursor(1, 1)
if search(substitute(g:vimwiki_rxH1_Template, '__Header__', VimwikiGet('diary_header'), ''), 'Wc')
let ln_start = line('.')
if search(g:vimwiki_rxH1, 'W')
let ln_end = line('.') - 1
else
call writefile(lines, expand(a:page))
let ln_end = line('$')
endif
endif
if ln_start < 0 || ln_end < 0
call setpos('.', old_pos)
return
endif
if !&readonly
exe ln_start.",".ln_end."delete _"
endif
call setpos('.', old_pos)
endfunction "}}}
function! s:insert_diary_section() "{{{
if !&readonly
let ln = line('.')
call append(ln, s:format_diary())
if ln == 1 && getline(ln) == ''
1,1delete
endif
endif
endfunction "}}}
function! s:make_date_link(...) "{{{
if a:0
let link = a:1
else
let link = s:diary_date_link()
" Diary index stuff }}}
function! vimwiki#diary#make_note(wnum, ...) "{{{
if a:wnum > len(g:vimwiki_list)
echom "vimwiki: Wiki ".a:wnum." is not registered in g:vimwiki_list!"
return
endif
let header = VimwikiGet('diary_header')
call s:add_link(s:diary_index(), header, link)
return VimwikiGet('diary_rel_path').link
endfunction "}}}
function! vimwiki#diary#make_note(index, ...) "{{{
call vimwiki#base#select(a:index)
call vimwiki#base#mkdir(VimwikiGet('path').VimwikiGet('diary_rel_path'))
if a:0
let link = s:make_date_link(a:1)
" TODO: refactor it. base#goto_index uses the same
if a:wnum > 0
let idx = a:wnum - 1
else
let link = s:make_date_link()
let idx = 0
endif
call vimwiki#base#open_link(':e ', link, s:diary_index())
call vimwiki#base#validate_wiki_options(idx)
call vimwiki#base#mkdir(VimwikiGet('path', idx).VimwikiGet('diary_rel_path', idx))
if a:0
let cmd = 'tabedit'
else
let cmd = 'edit'
endif
if len(a:0)>1
let link = 'diary:'.a:2
else
let link = 'diary:'.s:diary_date_link(idx)
endif
call vimwiki#base#open_link(cmd, link, s:diary_index(idx))
call vimwiki#base#setup_buffer_state(idx)
endfunction "}}}
function! vimwiki#diary#goto_index(index) "{{{
call vimwiki#base#select(a:index)
call vimwiki#base#edit_file(':e', s:diary_index())
function! vimwiki#diary#goto_diary_index(wnum) "{{{
if a:wnum > len(g:vimwiki_list)
echom "vimwiki: Wiki ".a:wnum." is not registered in g:vimwiki_list!"
return
endif
" TODO: refactor it. base#goto_index uses the same
if a:wnum > 0
let idx = a:wnum - 1
else
let idx = 0
endif
call vimwiki#base#validate_wiki_options(idx)
call vimwiki#base#edit_file('e', s:diary_index(idx))
call vimwiki#base#setup_buffer_state(idx)
endfunction "}}}
" Calendar.vim callback function.
function! vimwiki#diary#goto_next_day() "{{{
let link = ''
let [idx, links] = s:get_position_links(expand('%:t:r'))
if idx == (len(links) - 1)
return
endif
if idx != -1 && idx < len(links) - 1
let link = 'diary:'.links[idx+1]
else
" goto today
let link = 'diary:'.s:diary_date_link()
endif
if len(link)
call vimwiki#base#open_link(':e ', link)
endif
endfunction "}}}
function! vimwiki#diary#goto_prev_day() "{{{
let link = ''
let [idx, links] = s:get_position_links(expand('%:t:r'))
if idx == 0
return
endif
if idx > 0
let link = 'diary:'.links[idx-1]
else
" goto today
let link = 'diary:'.s:diary_date_link()
endif
if len(link)
call vimwiki#base#open_link(':e ', link)
endif
endfunction "}}}
function! vimwiki#diary#generate_diary_section() "{{{
let current_file = vimwiki#u#path_norm(expand("%:p"))
let diary_file = vimwiki#u#path_norm(s:diary_index())
if current_file == diary_file
call s:delete_diary_section()
call s:insert_diary_section()
else
echom "vimwiki: You can generate diary links only in a diary index page!"
endif
endfunction "}}}
" Calendar.vim {{{
" Callback function.
function! vimwiki#diary#calendar_action(day, month, year, week, dir) "{{{
let day = s:prefix_zero(a:day)
let month = s:prefix_zero(a:month)
@ -233,10 +342,10 @@ function! vimwiki#diary#calendar_action(day, month, year, week, dir) "{{{
endif
" Create diary note for a selected date in default wiki.
call vimwiki#diary#make_note(1, link)
call vimwiki#diary#make_note(1, 0, link)
endfunction "}}}
" Calendar.vim sign function.
" Sign function.
function vimwiki#diary#calendar_sign(day, month, year) "{{{
let day = s:prefix_zero(a:day)
let month = s:prefix_zero(a:month)
@ -245,42 +354,5 @@ function vimwiki#diary#calendar_sign(day, month, year) "{{{
return filereadable(expand(sfile))
endfunction "}}}
function! vimwiki#diary#goto_next_day() "{{{
let link = ''
let [idx, links] = s:get_position_links(expand('%:t:r'))
" Calendar.vim }}}
if idx == (len(links) - 1)
return
endif
if idx != -1 && idx < len(links) - 1
let link = VimwikiGet('diary_rel_path').links[idx+1]
else
" goto today
let link = VimwikiGet('diary_rel_path').s:diary_date_link()
endif
if len(link)
call vimwiki#base#open_link(':e ', link)
endif
endfunction "}}}
function! vimwiki#diary#goto_prev_day() "{{{
let link = ''
let [idx, links] = s:get_position_links(expand('%:t:r'))
if idx == 0
return
endif
if idx > 0
let link = VimwikiGet('diary_rel_path').links[idx-1]
else
" goto today
let link = VimwikiGet('diary_rel_path').s:diary_date_link()
endif
if len(link)
call vimwiki#base#open_link(':e ', link)
endif
endfunction "}}}

View File

@ -14,6 +14,15 @@ let g:loaded_vimwiki_html_auto = 1
"}}}
" UTILITY "{{{
function s:get_completion_index(sym) "{{{
for idx in range(1, 5)
if match(g:vimwiki_listsyms, '\C\%'.idx.'v'.a:sym) != -1
return (idx-1)
endif
endfor
return 0
endfunction "}}}
function! s:root_path(subdir) "{{{
return repeat('../', len(split(a:subdir, '[/\\]')))
endfunction "}}}
@ -36,7 +45,7 @@ function! s:is_web_link(lnk) "{{{
endfunction "}}}
function! s:is_img_link(lnk) "{{{
if a:lnk =~ '\.\%(png\|jpg\|gif\|jpeg\)$'
if tolower(a:lnk) =~ '\.\%(png\|jpg\|gif\|jpeg\)$'
return 1
endif
return 0
@ -59,9 +68,14 @@ function! s:find_autoload_file(name) " {{{
return ''
endfunction " }}}
function! s:create_default_CSS(path) " {{{
function! s:default_CSS_full_name(path) " {{{
let path = expand(a:path)
let css_full_name = path.VimwikiGet('css_name')
return css_full_name
endfunction "}}}
function! s:create_default_CSS(path) " {{{
let css_full_name = s:default_CSS_full_name(a:path)
if glob(css_full_name) == ""
call vimwiki#base#mkdir(fnamemodify(css_full_name, ':p:h'))
let default_css = s:find_autoload_file('style.css')
@ -81,8 +95,7 @@ function! s:template_full_name(name) "{{{
endif
let fname = expand(VimwikiGet('template_path').
\name.
\VimwikiGet('template_ext'))
\ name.VimwikiGet('template_ext'))
if filereadable(fname)
return fname
@ -95,8 +108,8 @@ function! s:get_html_template(wikifile, template) "{{{
" TODO: refactor it!!!
let lines=[]
let template_name = s:template_full_name(a:template)
if template_name != ''
if a:template != ''
let template_name = s:template_full_name(a:template)
try
let lines = readfile(template_name)
return lines
@ -106,27 +119,40 @@ function! s:get_html_template(wikifile, template) "{{{
endtry
endif
" if no VimwikiGet('html_template') set up or error while reading template
" file -- use default one.
let default_tpl = s:find_autoload_file('default.tpl')
if default_tpl != ''
let lines = readfile(default_tpl)
let default_tpl = s:template_full_name('')
if default_tpl == ''
let default_tpl = s:find_autoload_file('default.tpl')
endif
let lines = readfile(default_tpl)
return lines
endfunction "}}}
function! s:safe_html_tags(line) "{{{
let line = substitute(a:line,'<','\&lt;', 'g')
let line = substitute(line,'>','\&gt;', 'g')
return line
endfunction "}}}
function! s:safe_html(line) "{{{
" escape & < > when producing HTML text
" uses variables s:lt_pattern, s:gt_pattern that are
" set in vimwiki#html#Wiki2HTML() according to g:vimwiki_valid_html_tags
"" htmlize symbols: < > &
let line = substitute(a:line, '&', '\&amp;', 'g')
" the following depends on g:vimwiki_valid_html_tags
let line = substitute(line,s:lt_pattern,'\&lt;', 'g')
let line = substitute(line,s:gt_pattern,'\&gt;', 'g')
let tags = join(split(g:vimwiki_valid_html_tags, '\s*,\s*'), '\|')
let line = substitute(line,'<\%(/\?\%('
\.tags.'\)\%(\s\{-1}\S\{-}\)\{-}/\?>\)\@!',
\'\&lt;', 'g')
let line = substitute(line,'\%(</\?\%('
\.tags.'\)\%(\s\{-1}\S\{-}\)\{-}/\?\)\@<!>',
\'\&gt;', 'g')
"let tags = join(split(g:vimwiki_valid_html_tags, '\s*,\s*'), '\|')
"let line = substitute(line,'<\%(/\?\%('
" \.tags.'\)\%(\s\{-1}\S\{-}\)\{-}/\?>\)\@!',
" \'\&lt;', 'g')
"let line = substitute(line,'\%(</\?\%('
" \.tags.'\)\%(\s\{-1}\S\{-}\)\{-}/\?\)\@<!>',
" \'\&gt;', 'g')
return line
endfunction "}}}
@ -140,8 +166,8 @@ function! s:delete_html_files(path) "{{{
" delete if there is no corresponding wiki file
let subdir = vimwiki#base#subdir(VimwikiGet('path_html'), fname)
let wikifile = VimwikiGet("path").subdir.
\fnamemodify(fname, ":t:r").VimwikiGet("ext")
let wikifile = VimwikiGet('path').subdir.
\fnamemodify(fname, ":t:r").VimwikiGet('ext')
if filereadable(wikifile)
continue
endif
@ -182,12 +208,6 @@ function! s:save_vimwiki_buffer() "{{{
endif
endfunction "}}}
function! s:trim(string) "{{{
let res = substitute(a:string, '^\s\+', '', '')
let res = substitute(res, '\s\+$', '', '')
return res
endfunction "}}}
function! s:get_html_toc(toc_list) "{{{
" toc_list is list of [level, header_text, header_id]
" ex: [[1, "Header", "toc1"], [2, "Header2", "toc2"], ...]
@ -265,8 +285,7 @@ function! s:is_html_uptodate(wikifile) "{{{
endif
let wikifile = fnamemodify(a:wikifile, ":p")
let subdir = vimwiki#base#subdir(VimwikiGet('path'), wikifile)
let htmlfile = expand(VimwikiGet('path_html').subdir.
let htmlfile = expand(VimwikiGet('path_html').VimwikiGet('subdir').
\fnamemodify(wikifile, ":t:r").".html")
if getftime(wikifile) <= getftime(htmlfile) && tpl_time <= getftime(htmlfile)
@ -299,6 +318,11 @@ endfunction "}}}
"}}}
" INLINE TAGS "{{{
function! s:tag_eqin(value) "{{{
" mathJAX wants \( \) for inline maths
return '\('.s:mid(a:value, 1).'\)'
endfunction "}}}
function! s:tag_em(value) "{{{
return '<em>'.s:mid(a:value, 1).'</em>'
endfunction "}}}
@ -324,141 +348,113 @@ function! s:tag_sub(value) "{{{
endfunction "}}}
function! s:tag_code(value) "{{{
return '<code>'.s:mid(a:value, 1).'</code>'
return '<code>'.s:safe_html_tags(s:mid(a:value, 1)).'</code>'
endfunction "}}}
function! s:tag_pre(value) "{{{
return '<code>'.s:mid(a:value, 3).'</code>'
"function! s:tag_pre(value) "{{{
" return '<code>'.s:mid(a:value, 3).'</code>'
"endfunction "}}}
"FIXME dead code?
"function! s:tag_math(value) "{{{
" return '\['.s:mid(a:value, 3).'\]'
"endfunction "}}}
"{{{ v2.0 links
" match n-th ARG within {{URL[|ARG1|ARG2|...]}} " {{{
" *c,d,e),...
function! vimwiki#html#incl_match_arg(nn_index)
let rx = g:vimwiki_rxWikiInclPrefix. g:vimwiki_rxWikiInclUrl
let rx = rx. repeat(g:vimwiki_rxWikiInclSeparator. g:vimwiki_rxWikiInclArg, a:nn_index-1)
if a:nn_index > 0
let rx = rx. g:vimwiki_rxWikiInclSeparator. '\zs'. g:vimwiki_rxWikiInclArg. '\ze'
endif
let rx = rx. g:vimwiki_rxWikiInclArgs. g:vimwiki_rxWikiInclSuffix
return rx
endfunction
"}}}
function! vimwiki#html#linkify_link(src, descr) "{{{
let src_str = ' href="'.a:src.'"'
let descr = substitute(a:descr,'^\s*\(.*\)\s*$','\1','')
let descr = (descr == "" ? a:src : descr)
let descr_str = (descr =~ g:vimwiki_rxWikiIncl
\ ? s:tag_wikiincl(descr)
\ : descr)
return '<a'.src_str.'>'.descr_str.'</a>'
endfunction "}}}
function! s:tag_internal_link(value) "{{{
" Make <a href="This is a link">This is a link</a>
" from [[This is a link]]
" Make <a href="link">This is a link</a>
" from [[link|This is a link]]
" Make <a href="link">This is a link</a>
" from [[link][This is a link]]
" TODO: rename function -- it makes not only internal links.
" TODO: refactor it.
function! vimwiki#html#linkify_image(src, descr, verbatim_str) "{{{
let src_str = ' src="'.a:src.'"'
let descr_str = (a:descr != '' ? ' alt="'.a:descr.'"' : '')
let verbatim_str = (a:verbatim_str != '' ? ' '.a:verbatim_str : '')
return '<img'.src_str.descr_str.verbatim_str.' />'
endfunction "}}}
function! s:linkify(src, caption, style) "{{{
if a:style == ''
let style_str = ''
else
let style_str = ' style="'.a:style.'"'
endif
if s:is_img_link(a:caption)
let link = '<a href="'.a:src.'"><img src="'.a:caption.'"'.style_str.' />'.
\ '</a>'
elseif vimwiki#base#is_non_wiki_link(a:src)
let link = '<a href="'.a:src.'">'.a:caption.'</a>'
elseif s:is_img_link(a:src)
let link = '<img src="'.a:src.'" alt="'.a:caption.'"'. style_str.' />'
elseif vimwiki#base#is_link_to_dir(a:src)
if g:vimwiki_dir_link == ''
let link = '<a href="'.vimwiki#base#safe_link(a:src).'">'.a:caption.'</a>'
else
let link = '<a href="'.vimwiki#base#safe_link(a:src).
\ g:vimwiki_dir_link.'.html">'.a:caption.'</a>'
endif
else
let link = '<a href="'.vimwiki#base#safe_link(a:src).
\ '.html">'.a:caption.'</a>'
endif
return link
endfunction "}}}
let value = s:mid(a:value, 2)
let line = ''
if value =~ '|'
let link_parts = split(value, "|", 1)
else
let link_parts = split(value, "][", 1)
endif
if len(link_parts) > 1
if len(link_parts) < 3
let style = ""
else
let style = link_parts[2]
endif
let line = s:linkify(link_parts[0], link_parts[1], style)
else
let line = s:linkify(value, value, '')
endif
function! s:tag_weblink(value) "{{{
" Weblink Template -> <a href="url">descr</a>
let str = a:value
let url = matchstr(str, g:vimwiki_rxWeblinkMatchUrl)
let descr = matchstr(str, g:vimwiki_rxWeblinkMatchDescr)
let line = vimwiki#html#linkify_link(url, descr)
return line
endfunction "}}}
function! s:tag_external_link(value) "{{{
"" Make <a href="link">link desc</a>
"" from [link link desc]
let value = s:mid(a:value, 1)
let line = ''
if s:is_web_link(value)
let lnkElements = split(value)
let head = lnkElements[0]
let rest = join(lnkElements[1:])
if rest==""
let rest=head
function! s:tag_wikiincl(value) "{{{
" {{imgurl|arg1|arg2}} -> ???
" {{imgurl}} -> <img src="imgurl"/>
" {{imgurl|descr|style="A"}} -> <img src="imgurl" alt="descr" style="A" />
" {{imgurl|descr|class="B"}} -> <img src="imgurl" alt="descr" class="B" />
let str = a:value
" custom transclusions
let line = VimwikiWikiIncludeHandler(str)
" otherwise, assume image transclusion
if line == ''
let url_0 = matchstr(str, g:vimwiki_rxWikiInclMatchUrl)
let descr = matchstr(str, vimwiki#html#incl_match_arg(1))
let verbatim_str = matchstr(str, vimwiki#html#incl_match_arg(2))
" resolve url
let [idx, scheme, path, subdir, lnk, ext, url] =
\ vimwiki#base#resolve_scheme(url_0, 1)
" generate html output
" TODO: migrate non-essential debugging messages into g:VimwikiLog
if g:vimwiki_debug > 1
echom '{{idx='.idx.', scheme='.scheme.', path='.path.', subdir='.subdir.', lnk='.lnk.', ext='.ext.'}}'
endif
if s:is_img_link(rest)
if rest!=head
let line = '<a href="'.head.'"><img src="'.rest.'" /></a>'
else
let line = '<img src="'.rest.'" />'
endif
else
let line = '<a href="'.head.'">'.rest.'</a>'
endif
elseif s:is_img_link(value)
let line = '<img src="'.value.'" />'
else
" [alskfj sfsf] shouldn't be a link. So return it as it was --
" enclosed in [...]
let line = '['.value.']'
endif
return line
endfunction "}}}
function! s:tag_wikiword_link(value) "{{{
" Make <a href="WikiWord">WikiWord</a> from WikiWord
if a:value[0] == '!'
return a:value[1:]
elseif g:vimwiki_camel_case
let line = '<a href="'.a:value.'.html">'.a:value.'</a>'
let url = escape(url, '#')
let line = vimwiki#html#linkify_image(url, descr, verbatim_str)
return line
else
return a:value
endif
endfunction "}}}
function! s:tag_barebone_link(value) "{{{
"" Make <a href="http://habamax.ru">http://habamax.ru</a>
"" from http://habamax.ru
if s:is_img_link(a:value)
let line = '<img src="'.a:value.'" />'
else
let line = '<a href="'.a:value.'">'.a:value.'</a>'
endif
return line
endfunction "}}}
function! s:tag_no_wikiword_link(value) "{{{
if a:value[0] == '!'
return a:value[1:]
else
return a:value
function! s:tag_wikilink(value) "{{{
" [[url]] -> <a href="url.html">url</a>
" [[url|descr]] -> <a href="url.html">descr</a>
" [[url|{{...}}]] -> <a href="url.html"> ... </a>
" [[fileurl.ext|descr]] -> <a href="fileurl.ext">descr</a>
" [[dirurl/|descr]] -> <a href="dirurl/index.html">descr</a>
let str = a:value
let url = matchstr(str, g:vimwiki_rxWikiLinkMatchUrl)
let descr = matchstr(str, g:vimwiki_rxWikiLinkMatchDescr)
let descr = (substitute(descr,'^\s*\(.*\)\s*$','\1','') != '' ? descr : url)
" resolve url
let [idx, scheme, path, subdir, lnk, ext, url] =
\ vimwiki#base#resolve_scheme(url, 1)
" generate html output
" TODO: migrate non-essential debugging messages into g:VimwikiLog
if g:vimwiki_debug > 1
echom '[[idx='.idx.', scheme='.scheme.', path='.path.', subdir='.subdir.', lnk='.lnk.', ext='.ext.']]'
endif
let url = escape(url, '#')
let line = vimwiki#html#linkify_link(url, descr)
return line
endfunction "}}}
"}}} v1.3 links
function! s:tag_remove_internal_link(value) "{{{
let value = s:mid(a:value, 2)
@ -508,10 +504,16 @@ endfunction "}}}
function! s:make_tag(line, regexp, func) "{{{
" Make tags for a given matched regexp.
" Exclude preformatted text and href links.
" FIXME
let patt_splitter = '\(`[^`]\+`\)\|'.
\ '\('.g:vimwiki_rxPreStart.'.\+'.g:vimwiki_rxPreEnd.'\)\|'.
\ '\(<a href.\{-}</a>\)\|'.
\ '\(<img src.\{-}/>\)\|'.
\ '\('.g:vimwiki_rxEqIn.'\)'
let patt_splitter = '\(`[^`]\+`\)\|\({{{.\+}}}\)\|'.
\ '\(<a href.\{-}</a>\)\|\(<img src.\{-}/>\)'
if '`[^`]\+`' == a:regexp || '{{{.\+}}}' == a:regexp
"FIXME FIXME !!! these can easily occur on the same line!
"XXX {{{ }}} ??? obsolete
if '`[^`]\+`' == a:regexp || '{{{.\+}}}' == a:regexp || g:vimwiki_rxEqIn == a:regexp
let res_line = s:subst_func(a:line, a:regexp, a:func)
else
let pos = 0
@ -541,7 +543,6 @@ endfunction " }}}
function! s:process_tags_typefaces(line) "{{{
let line = a:line
let line = s:make_tag(line, g:vimwiki_rxNoWikiWord, 's:tag_no_wikiword_link')
let line = s:make_tag(line, g:vimwiki_rxItalic, 's:tag_em')
let line = s:make_tag(line, g:vimwiki_rxBold, 's:tag_strong')
let line = s:make_tag(line, g:vimwiki_rxTodo, 's:tag_todo')
@ -549,15 +550,15 @@ function! s:process_tags_typefaces(line) "{{{
let line = s:make_tag(line, g:vimwiki_rxSuperScript, 's:tag_super')
let line = s:make_tag(line, g:vimwiki_rxSubScript, 's:tag_sub')
let line = s:make_tag(line, g:vimwiki_rxCode, 's:tag_code')
let line = s:make_tag(line, g:vimwiki_rxEqIn, 's:tag_eqin')
return line
endfunction " }}}
function! s:process_tags_links(line) " {{{
let line = a:line
let line = s:make_tag(line, '\[\[.\{-}\]\]', 's:tag_internal_link')
let line = s:make_tag(line, '\[.\{-}\]', 's:tag_external_link')
let line = s:make_tag(line, g:vimwiki_rxWeblink, 's:tag_barebone_link')
let line = s:make_tag(line, g:vimwiki_rxWikiWord, 's:tag_wikiword_link')
let line = s:make_tag(line, g:vimwiki_rxWikiLink, 's:tag_wikilink')
let line = s:make_tag(line, g:vimwiki_rxWikiIncl, 's:tag_wikiincl')
let line = s:make_tag(line, g:vimwiki_rxWeblink, 's:tag_weblink')
return line
endfunction " }}}
@ -577,6 +578,14 @@ function! s:close_tag_pre(pre, ldest) "{{{
return a:pre
endfunction "}}}
function! s:close_tag_math(math, ldest) "{{{
if a:math[0]
call insert(a:ldest, "\\\]")
return 0
endif
return a:math
endfunction "}}}
function! s:close_tag_quote(quote, ldest) "{{{
if a:quote
call insert(a:ldest, "</blockquote>")
@ -745,11 +754,15 @@ endfunction! "}}}
function! s:process_tag_pre(line, pre) "{{{
" pre is the list of [is_in_pre, indent_of_pre]
"XXX always outputs a single line or empty list!
let lines = []
let pre = a:pre
let processed = 0
if !pre[0] && a:line =~ '^\s*{{{[^\(}}}\)]*\s*$'
"XXX huh?
"if !pre[0] && a:line =~ '^\s*{{{[^\(}}}\)]*\s*$'
if !pre[0] && a:line =~ '^\s*{{{'
let class = matchstr(a:line, '{{{\zs.*$')
"FIXME class cannot contain arbitrary strings
let class = substitute(class, '\s\+$', '', 'g')
if class != ""
call add(lines, "<pre ".class.">")
@ -764,11 +777,48 @@ function! s:process_tag_pre(line, pre) "{{{
let processed = 1
elseif pre[0]
let processed = 1
call add(lines, substitute(a:line, '^\s\{'.pre[1].'}', '', ''))
"XXX destroys indent in general!
"call add(lines, substitute(a:line, '^\s\{'.pre[1].'}', '', ''))
call add(lines, s:safe_html_tags(a:line))
endif
return [processed, lines, pre]
endfunction "}}}
function! s:process_tag_math(line, math) "{{{
" math is the list of [is_in_math, indent_of_math]
let lines = []
let math = a:math
let processed = 0
if !math[0] && a:line =~ '^\s*{{\$[^\(}}$\)]*\s*$'
let class = matchstr(a:line, '{{$\zs.*$')
"FIXME class cannot be any string!
let class = substitute(class, '\s\+$', '', 'g')
" Check the math placeholder (default: displaymath)
let b:vimwiki_mathEnv = matchstr(class, '^%\zs\S\+\ze%')
if b:vimwiki_mathEnv != ""
call add(lines, substitute(class, '^%\(\S\+\)%','\\begin{\1}', ''))
elseif class != ""
call add(lines, "\\\[".class)
else
call add(lines, "\\\[")
endif
let math = [1, len(matchstr(a:line, '^\s*\ze{{\$'))]
let processed = 1
elseif math[0] && a:line =~ '^\s*}}\$\s*$'
let math = [0, 0]
if b:vimwiki_mathEnv != ""
call add(lines, "\\end{".b:vimwiki_mathEnv."}")
else
call add(lines, "\\\]")
endif
let processed = 1
elseif math[0]
let processed = 1
call add(lines, substitute(a:line, '^\s\{'.math[1].'}', '', ''))
endif
return [processed, lines, math]
endfunction "}}}
function! s:process_tag_quote(line, quote) "{{{
let lines = []
let quote = a:quote
@ -800,7 +850,8 @@ function! s:process_tag_list(line, lists) "{{{
if chk[1] =~ '[.*\\^$~]'
let chk[1] ='\'.chk[1]
endif
let completion = match(g:vimwiki_listsyms, '\C' . chk[1])
" let completion = match(g:vimwiki_listsyms, '\C' . chk[1])
let completion = s:get_completion_index(chk[1])
if completion >= 0 && completion <=4
let st_tag = '<li class="done'.completion.'">'
endif
@ -935,18 +986,9 @@ function! s:process_tag_h(line, id) "{{{
let h_level = 0
let h_text = ''
let h_id = ''
if a:line =~ g:vimwiki_rxH6
let h_level = 6
elseif a:line =~ g:vimwiki_rxH5
let h_level = 5
elseif a:line =~ g:vimwiki_rxH4
let h_level = 4
elseif a:line =~ g:vimwiki_rxH3
let h_level = 3
elseif a:line =~ g:vimwiki_rxH2
let h_level = 2
elseif a:line =~ g:vimwiki_rxH1
let h_level = 1
if a:line =~ g:vimwiki_rxHeader
let h_level = vimwiki#u#count_first_sym(a:line)
endif
if h_level > 0
let a:id[h_level] += 1
@ -960,8 +1002,6 @@ function! s:process_tag_h(line, id) "{{{
let centered = 1
endif
let line = s:trim(line)
let h_number = ''
for l in range(1, h_level-1)
let h_number .= a:id[l].'.'
@ -978,7 +1018,8 @@ function! s:process_tag_h(line, id) "{{{
let h_part .= '>'
endif
let h_text = s:trim(strpart(line, h_level, len(line) - h_level * 2))
let h_text = vimwiki#u#trim(matchstr(line, g:vimwiki_rxHeader))
if g:vimwiki_html_header_numbering
let num = matchstr(h_number,
\ '^\(\d.\)\{'.(g:vimwiki_html_header_numbering-1).'}\zs.*')
@ -1046,14 +1087,15 @@ function! s:process_tag_table(line, table) "{{{
let lines = []
let processed = 0
if a:line =~ '^\s*|[-+]\+|\s*$'
if vimwiki#tbl#is_separator(a:line)
call extend(table, s:table_add_row(a:table, a:line))
let processed = 1
elseif a:line =~ '^\s*|.\+|\s*$'
elseif vimwiki#tbl#is_table(a:line)
call extend(table, s:table_add_row(a:table, a:line))
let processed = 1
let cells = split(a:line, '\s*|\s*', 1)[1: -2]
" let cells = split(a:line, vimwiki#tbl#cell_splitter(), 1)[1: -2]
let cells = vimwiki#tbl#get_cells(a:line)
call map(cells, 's:table_empty_cell(v:val)')
call extend(table[-1], cells)
else
@ -1072,6 +1114,7 @@ function! s:parse_line(line, state) " {{{
let state.para = a:state.para
let state.quote = a:state.quote
let state.pre = a:state.pre[:]
let state.math = a:state.math[:]
let state.table = a:state.table[:]
let state.lists = a:state.lists[:]
let state.deflist = a:state.deflist
@ -1135,6 +1178,9 @@ function! s:parse_line(line, state) " {{{
" if processed && len(state.lists)
" call s:close_tag_list(state.lists, lines)
" endif
if !processed
let [processed, lines, state.math] = s:process_tag_math(line, state.math)
endif
if processed && len(state.table)
let state.table = s:close_tag_table(state.table, lines)
endif
@ -1160,6 +1206,9 @@ function! s:parse_line(line, state) " {{{
if processed && state.pre[0]
let state.pre = s:close_tag_pre(state.pre, lines)
endif
if processed && state.math[0]
let state.math = s:close_tag_math(state.math, lines)
endif
if processed && len(state.table)
let state.table = s:close_tag_table(state.table, lines)
endif
@ -1183,6 +1232,7 @@ function! s:parse_line(line, state) " {{{
call s:close_tag_list(state.lists, res_lines)
let state.table = s:close_tag_table(state.table, res_lines)
let state.pre = s:close_tag_pre(state.pre, res_lines)
let state.math = s:close_tag_math(state.math, res_lines)
let state.quote = s:close_tag_quote(state.quote, res_lines)
let state.para = s:close_tag_para(state.para, res_lines)
@ -1218,6 +1268,9 @@ function! s:parse_line(line, state) " {{{
if processed && state.pre[0]
let state.pre = s:close_tag_pre(state.pre, lines)
endif
if processed && state.math[0]
let state.math = s:close_tag_math(state.math, lines)
endif
if processed && state.para
let state.para = s:close_tag_para(state.para, lines)
endif
@ -1235,6 +1288,7 @@ function! s:parse_line(line, state) " {{{
call s:close_tag_list(state.lists, res_lines)
let state.table = s:close_tag_table(state.table, res_lines)
let state.pre = s:close_tag_pre(state.pre, res_lines)
let state.math = s:close_tag_math(state.math, res_lines)
call add(res_lines, line)
endif
endif
@ -1262,6 +1316,9 @@ function! s:parse_line(line, state) " {{{
if processed && state.pre[0]
let state.pre = s:close_tag_pre(state.pre, res_lines)
endif
if processed && state.math[0]
let state.math = s:close_tag_math(state.math, res_lines)
endif
if processed && len(state.table)
let state.table = s:close_tag_table(state.table, res_lines)
endif
@ -1281,128 +1338,174 @@ function! s:parse_line(line, state) " {{{
endfunction " }}}
function! vimwiki#html#Wiki2HTML(path, wikifile) "{{{
function! s:use_custom_wiki2html() "{{{
let custom_wiki2html = VimwikiGet('custom_wiki2html')
return !empty(custom_wiki2html) && s:file_exists(custom_wiki2html)
endfunction " }}}
function! vimwiki#html#CustomWiki2HTML(path, wikifile, force) "{{{
call vimwiki#base#mkdir(a:path)
execute '!'.VimwikiGet('custom_wiki2html'). ' '
\ a:force. ' '.
\ VimwikiGet('syntax'). ' '.
\ strpart(VimwikiGet('ext'), 1). ' '.
\ a:path. ' '.
\ a:wikifile. ' '.
\ s:default_CSS_full_name(a:path)
endfunction " }}}
function! vimwiki#html#Wiki2HTML(path_html, wikifile) "{{{
let starttime = reltime() " start the clock
echo 'Generating HTML ... '
if !s:syntax_supported()
echomsg 'vimwiki: Only vimwiki_default syntax supported!!!'
return
endif
let done = 0
let wikifile = fnamemodify(a:wikifile, ":p")
let subdir = vimwiki#base#subdir(VimwikiGet('path'), wikifile)
let path = expand(a:path).subdir
let path_html = expand(a:path_html).VimwikiGet('subdir')
let htmlfile = fnamemodify(wikifile, ":t:r").'.html'
let lsource = readfile(wikifile)
let ldest = []
if s:use_custom_wiki2html()
let force = 1
call vimwiki#html#CustomWiki2HTML(path_html, wikifile, force)
let done = 1
endif
call vimwiki#base#mkdir(path)
if s:syntax_supported() && done == 0
let lsource = readfile(wikifile)
let ldest = []
" nohtml placeholder -- to skip html generation.
let nohtml = 0
"if g:vimwiki_debug
" echo 'Generating HTML ... '
"endif
" template placeholder
let template_name = ''
call vimwiki#base#mkdir(path_html)
" for table of contents placeholders.
let placeholders = []
" nohtml placeholder -- to skip html generation.
let nohtml = 0
" current state of converter
let state = {}
let state.para = 0
let state.quote = 0
let state.pre = [0, 0] " [in_pre, indent_pre]
let state.table = []
let state.deflist = 0
let state.lists = []
let state.placeholder = []
let state.toc = []
let state.toc_id = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 }
" template placeholder
let template_name = ''
for line in lsource
let oldquote = state.quote
let [lines, state] = s:parse_line(line, state)
" for table of contents placeholders.
let placeholders = []
" Hack: There could be a lot of empty strings before s:process_tag_quote
" find out `quote` is over. So we should delete them all. Think of the way
" to refactor it out.
if oldquote != state.quote
call s:remove_blank_lines(ldest)
" current state of converter
let state = {}
let state.para = 0
let state.quote = 0
let state.pre = [0, 0] " [in_pre, indent_pre]
let state.math = [0, 0] " [in_math, indent_math]
let state.table = []
let state.deflist = 0
let state.lists = []
let state.placeholder = []
let state.toc = []
let state.toc_id = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 }
" prepare constants for s:safe_html()
let s:lt_pattern = '<'
let s:gt_pattern = '>'
if g:vimwiki_valid_html_tags != ''
let tags = join(split(g:vimwiki_valid_html_tags, '\s*,\s*'), '\|')
let s:lt_pattern = '<\%(/\?\%('.tags.'\)\%(\s\{-1}\S\{-}\)\{-}/\?>\)\@!'
let s:gt_pattern = '\%(</\?\%('.tags.'\)\%(\s\{-1}\S\{-}\)\{-}/\?\)\@<!>'
endif
if !empty(state.placeholder)
if state.placeholder[0] == 'nohtml'
let nohtml = 1
break
elseif state.placeholder[0] == 'template'
let template_name = state.placeholder[1]
else
call add(placeholders, [state.placeholder, len(ldest), len(placeholders)])
for line in lsource
let oldquote = state.quote
let [lines, state] = s:parse_line(line, state)
" Hack: There could be a lot of empty strings before s:process_tag_quote
" find out `quote` is over. So we should delete them all. Think of the way
" to refactor it out.
if oldquote != state.quote
call s:remove_blank_lines(ldest)
endif
let state.placeholder = []
if !empty(state.placeholder)
if state.placeholder[0] == 'nohtml'
let nohtml = 1
break
elseif state.placeholder[0] == 'template'
let template_name = state.placeholder[1]
else
call add(placeholders, [state.placeholder, len(ldest), len(placeholders)])
endif
let state.placeholder = []
endif
call extend(ldest, lines)
endfor
if nohtml
echon "\r"."%nohtml placeholder found"
return
endif
let toc = s:get_html_toc(state.toc)
call s:process_toc(ldest, placeholders, toc)
call s:remove_blank_lines(ldest)
"" process end of file
"" close opened tags if any
let lines = []
call s:close_tag_quote(state.quote, lines)
call s:close_tag_para(state.para, lines)
call s:close_tag_pre(state.pre, lines)
call s:close_tag_math(state.math, lines)
call s:close_tag_list(state.lists, lines)
call s:close_tag_def_list(state.deflist, lines)
call s:close_tag_table(state.table, lines)
call extend(ldest, lines)
endfor
let title = s:process_title(placeholders, fnamemodify(a:wikifile, ":t:r"))
if nohtml
echon "\r"."%nohtml placeholder found"
let html_lines = s:get_html_template(a:wikifile, template_name)
" processing template variables (refactor to a function)
call map(html_lines, 'substitute(v:val, "%title%", "'. title .'", "g")')
call map(html_lines, 'substitute(v:val, "%root_path%", "'.
\ s:root_path(VimwikiGet('subdir')) .'", "g")')
let css_name = expand(VimwikiGet('css_name'))
let css_name = substitute(css_name, '\', '/', 'g')
call map(html_lines, 'substitute(v:val, "%css%", "'. css_name .'", "g")')
let enc = &fileencoding
if enc == ''
let enc = &encoding
endif
call map(html_lines, 'substitute(v:val, "%encoding%", "'. enc .'", "g")')
let html_lines = s:html_insert_contents(html_lines, ldest) " %contents%
"" make html file.
call writefile(html_lines, path_html.htmlfile)
let done = 1
endif
if done == 0
echomsg 'vimwiki: conversion to HTML is not supported for this syntax!!!'
return
endif
let toc = s:get_html_toc(state.toc)
call s:process_toc(ldest, placeholders, toc)
call s:remove_blank_lines(ldest)
" measure the elapsed time
let time1 = vimwiki#u#time(starttime) "XXX
call VimwikiLog_extend('html',[htmlfile,time1])
"if g:vimwiki_debug
" echon "\r".htmlfile.' written (time: '.time1.'s)'
"endif
"" process end of file
"" close opened tags if any
let lines = []
call s:close_tag_quote(state.quote, lines)
call s:close_tag_para(state.para, lines)
call s:close_tag_pre(state.pre, lines)
call s:close_tag_list(state.lists, lines)
call s:close_tag_def_list(state.deflist, lines)
call s:close_tag_table(state.table, lines)
call extend(ldest, lines)
let title = s:process_title(placeholders, fnamemodify(a:wikifile, ":t:r"))
let html_lines = s:get_html_template(a:wikifile, template_name)
" processing template variables (refactor to a function)
call map(html_lines, 'substitute(v:val, "%title%", "'. title .'", "g")')
call map(html_lines, 'substitute(v:val, "%root_path%", "'.
\ s:root_path(subdir) .'", "g")')
let css_name = expand(VimwikiGet('css_name'))
let css_name = substitute(css_name, '\', '/', 'g')
call map(html_lines, 'substitute(v:val, "%css%", "'. css_name .'", "g")')
let enc = &fileencoding
if enc == ''
let enc = &encoding
endif
call map(html_lines, 'substitute(v:val, "%encoding%", "'. enc .'", "g")')
let html_lines = s:html_insert_contents(html_lines, ldest) " %contents%
"" make html file.
call writefile(html_lines, path.htmlfile)
" measure the elapsed time and cut away miliseconds and smaller
let elapsedtimestr = matchstr(reltimestr(reltime(starttime)),'\d\+\(\.\d\d\)\=')
echon "\r".htmlfile.' written (time: '.elapsedtimestr.'s)'
return path.htmlfile
return path_html.htmlfile
endfunction "}}}
function! vimwiki#html#WikiAll2HTML(path) "{{{
if !s:syntax_supported()
echomsg 'vimwiki: Only vimwiki_default syntax supported!!!'
function! vimwiki#html#WikiAll2HTML(path_html) "{{{
if !s:syntax_supported() && !s:use_custom_wiki2html()
echomsg 'vimwiki: conversion to HTML is not supported for this syntax!!!'
return
endif
@ -1414,28 +1517,63 @@ function! vimwiki#html#WikiAll2HTML(path) "{{{
exe 'buffer '.cur_buf
let &eventignore = save_eventignore
let path = expand(a:path)
call vimwiki#base#mkdir(path)
let path_html = expand(a:path_html)
call vimwiki#base#mkdir(path_html)
echomsg 'Deleting non-wiki html files...'
call s:delete_html_files(path)
call s:delete_html_files(path_html)
echomsg 'Converting wiki to html files...'
let setting_more = &more
setlocal nomore
" temporarily adjust current_subdir global state variable
let current_subdir = VimwikiGet('subdir')
let current_invsubdir = VimwikiGet('invsubdir')
let wikifiles = split(glob(VimwikiGet('path').'**/*'.VimwikiGet('ext')), '\n')
for wikifile in wikifiles
let wikifile = fnamemodify(wikifile, ":p")
" temporarily adjust 'subdir' and 'invsubdir' state variables
let subdir = vimwiki#base#subdir(VimwikiGet('path'), wikifile)
call VimwikiSet('subdir', subdir)
call VimwikiSet('invsubdir', vimwiki#base#invsubdir(subdir))
if !s:is_html_uptodate(wikifile)
echomsg 'Processing '.wikifile
call vimwiki#html#Wiki2HTML(path, wikifile)
call vimwiki#html#Wiki2HTML(path_html, wikifile)
else
echomsg 'Skipping '.wikifile
endif
endfor
call s:create_default_CSS(path)
" reset 'subdir' state variable
call VimwikiSet('subdir', current_subdir)
call VimwikiSet('invsubdir', current_invsubdir)
call s:create_default_CSS(path_html)
echomsg 'Done!'
let &more = setting_more
endfunction "}}}
function! s:file_exists(fname) "{{{
return !empty(getftype(a:fname))
endfunction "}}}
" uses VimwikiGet('path')
function! vimwiki#html#get_wikifile_url(wikifile) "{{{
return VimwikiGet('path_html').
\ vimwiki#base#subdir(VimwikiGet('path'), a:wikifile).
\ fnamemodify(a:wikifile, ":t:r").'.html'
endfunction "}}}
function! vimwiki#html#PasteUrl(wikifile) "{{{
execute 'r !echo file://'.vimwiki#html#get_wikifile_url(a:wikifile)
endfunction "}}}
function! vimwiki#html#CatUrl(wikifile) "{{{
execute '!echo file://'.vimwiki#html#get_wikifile_url(a:wikifile)
endfunction "}}}
"}}}

View File

@ -15,24 +15,36 @@ let s:rx_li_box = '\[.\?\]'
" Script functions {{{
" Get unicode string symbol at index
function! s:str_idx(str, idx) "{{{
" Unfortunatly vimscript cannot get symbol at index in unicode string such as
" '✗○◐●✓'
return matchstr(a:str, '\%'.a:idx.'v.')
endfunction "}}}
" Get checkbox regexp
function! s:rx_li_symbol(rate) "{{{
let result = ''
if a:rate == 100
let result = g:vimwiki_listsyms[4]
let result = s:str_idx(g:vimwiki_listsyms, 5)
elseif a:rate == 0
let result = g:vimwiki_listsyms[0]
let result = s:str_idx(g:vimwiki_listsyms, 1)
elseif a:rate >= 67
let result = g:vimwiki_listsyms[3]
let result = s:str_idx(g:vimwiki_listsyms, 4)
elseif a:rate >= 34
let result = g:vimwiki_listsyms[2]
let result = s:str_idx(g:vimwiki_listsyms, 3)
else
let result = g:vimwiki_listsyms[1]
let result = s:str_idx(g:vimwiki_listsyms, 2)
endif
return '\['.result.'\]'
endfunction "}}}
" Get blank checkbox
function! s:blank_checkbox() "{{{
return '['.s:str_idx(g:vimwiki_listsyms, 1).'] '
endfunction "}}}
" Get regexp of the list item.
function! s:rx_list_item() "{{{
return '\('.g:vimwiki_rxListBullet.'\|'.g:vimwiki_rxListNumber.'\)'
@ -46,7 +58,7 @@ endfunction "}}}
" Get level of the list item.
function! s:get_level(lnum) "{{{
if VimwikiGet('syntax') == 'media'
let level = vimwiki#base#count_first_sym(getline(a:lnum))
let level = vimwiki#u#count_first_sym(getline(a:lnum))
else
let level = indent(a:lnum)
endif
@ -222,7 +234,7 @@ function! s:create_cb_list_item(lnum) "{{{
let m = matchstr(line, s:rx_list_item())
if m != ''
let li_content = substitute(strpart(line, len(m)), '^\s*', '', '')
let line = substitute(m, '\s*$', ' ', '').'[ ] '.li_content
let line = substitute(m, '\s*$', ' ', '').s:blank_checkbox().li_content
call setline(a:lnum, line)
endif
endfunction "}}}
@ -320,7 +332,7 @@ function! vimwiki#lst#kbd_cr() "{{{
" This function is heavily relies on proper 'set comments' option.
let cr = "\<CR>"
if getline('.') =~ s:rx_cb_list_item()
let cr .= '[ ] '
let cr .= s:blank_checkbox()
endif
return cr
endfunction "}}}
@ -341,11 +353,10 @@ function! vimwiki#lst#kbd_oO(cmd) "{{{
let lnum = line('.')
endif
" let line = substitute(m, '\s*$', ' ', '').'[ ] '.li_content
let m = matchstr(line, s:rx_list_item())
let res = ''
if line =~ s:rx_cb_list_item()
let res = substitute(m, '\s*$', ' ', '').'[ ] '
let res = substitute(m, '\s*$', ' ', '').s:blank_checkbox()
elseif line =~ s:rx_list_item()
let res = substitute(m, '\s*$', ' ', '')
elseif &autoindent || &smartindent
@ -367,3 +378,178 @@ function! vimwiki#lst#kbd_oO(cmd) "{{{
endfunction "}}}
function! vimwiki#lst#default_symbol() "{{{
" TODO: initialize default symbol from syntax/vimwiki_xxx.vim
if VimwikiGet('syntax') == 'default'
return '-'
else
return '*'
endif
endfunction "}}}
function vimwiki#lst#get_list_margin() "{{{
if VimwikiGet('list_margin') < 0
return &sw
else
return VimwikiGet('list_margin')
endif
endfunction "}}}
function s:get_list_sw() "{{{
if VimwikiGet('syntax') == 'media'
return 1
else
return &sw
endif
endfunction "}}}
function s:get_list_nesting_level(lnum) "{{{
if VimwikiGet('syntax') == 'media'
if getline(a:lnum) !~ s:rx_list_item()
let level = 0
else
let level = vimwiki#u#count_first_sym(getline(a:lnum)) - 1
let level = level < 0 ? 0 : level
endif
else
let level = indent(a:lnum)
endif
return level
endfunction "}}}
function s:get_list_indent(lnum) "{{{
if VimwikiGet('syntax') == 'media'
return indent(a:lnum)
else
return 0
endif
endfunction "}}}
function! s:compose_list_item(n_indent, n_nesting, sym_nest, sym_bullet, li_content, ...) "{{{
if a:0
let sep = a:1
else
let sep = ''
endif
let li_indent = repeat(' ', max([0,a:n_indent])).sep
let li_nesting = repeat(a:sym_nest, max([0,a:n_nesting])).sep
if len(a:sym_bullet) > 0
let li_bullet = a:sym_bullet.' '.sep
else
let li_bullet = ''.sep
endif
return li_indent.li_nesting.li_bullet.a:li_content
endfunction "}}}
function s:compose_cb_bullet(prev_cb_bullet, sym) "{{{
return a:sym.matchstr(a:prev_cb_bullet, '\S*\zs\s\+.*')
endfunction "}}}
function! vimwiki#lst#change_level(...) "{{{
let default_sym = vimwiki#lst#default_symbol()
let cmd = '>>'
let sym = default_sym
" parse argument
if a:0
if a:1 != '<<' && a:1 != '>>'
let cmd = '--'
let sym = a:1
else
let cmd = a:1
endif
endif
" is symbol valid
if sym.' ' !~ s:rx_cb_list_item() && sym.' ' !~ s:rx_list_item()
return
endif
" parsing setup
let lnum = line('.')
let line = getline('.')
let list_margin = vimwiki#lst#get_list_margin()
let list_sw = s:get_list_sw()
let n_nesting = s:get_list_nesting_level(lnum)
let n_indent = s:get_list_indent(lnum)
" remove indent and nesting
let li_bullet_and_content = strpart(line, n_nesting + n_indent)
" list bullet and checkbox
let cb_bullet = matchstr(li_bullet_and_content, s:rx_list_item()).
\ matchstr(li_bullet_and_content, s:rx_cb_list_item())
" XXX: it could be not unicode proof --> if checkboxes are set up with unicode syms
" content
let li_content = strpart(li_bullet_and_content, len(cb_bullet))
" trim
let cb_bullet = vimwiki#u#trim(cb_bullet)
let li_content = vimwiki#u#trim(li_content)
" nesting symbol
if VimwikiGet('syntax') == 'media'
if len(cb_bullet) > 0
let sym_nest = cb_bullet[0]
else
let sym_nest = sym
endif
else
let sym_nest = ' '
endif
if g:vimwiki_debug
echomsg "PARSE: Sw [".list_sw."]"
echomsg s:compose_list_item(n_indent, n_nesting, sym_nest, cb_bullet, li_content, '|')
endif
" change level
if cmd == '--'
let cb_bullet = s:compose_cb_bullet(cb_bullet, sym)
if VimwikiGet('syntax') == 'media'
let sym_nest = sym
endif
elseif cmd == '>>'
if cb_bullet == ''
let cb_bullet = sym
else
let n_nesting = n_nesting + list_sw
endif
elseif cmd == '<<'
let n_nesting = n_nesting - list_sw
if VimwikiGet('syntax') == 'media'
if n_nesting < 0
let cb_bullet = ''
endif
else
if n_nesting < list_margin
let cb_bullet = ''
endif
endif
endif
let n_nesting = max([0, n_nesting])
if g:vimwiki_debug
echomsg "SHIFT:"
echomsg s:compose_list_item(n_indent, n_nesting, sym_nest, cb_bullet, li_content, '|')
endif
" XXX: this is the code that adds the initial indent
let add_nesting = VimwikiGet('syntax') != 'media'
if n_indent + n_nesting*(add_nesting) < list_margin
let n_indent = list_margin - n_nesting*(add_nesting)
endif
if g:vimwiki_debug
echomsg "INDENT:"
echomsg s:compose_list_item(n_indent, n_nesting, sym_nest, cb_bullet, li_content, '|')
endif
let line = s:compose_list_item(n_indent, n_nesting, sym_nest, cb_bullet, li_content)
" replace
call setline(lnum, line)
call cursor(lnum, match(line, '\S') + 1)
endfunction "}}}

View File

@ -0,0 +1,111 @@
" vim:tabstop=2:shiftwidth=2:expandtab:foldmethod=marker:textwidth=79
" Vimwiki autoload plugin file
" Desc: Link functions for markdown syntax
" Author: Maxim Kim <habamax@gmail.com>
" Home: http://code.google.com/p/vimwiki/
function! s:normalize_link_syntax_n() " {{{
let lnum = line('.')
" try WikiIncl
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiIncl)
if !empty(lnk)
" NO-OP !!
if g:vimwiki_debug > 1
echomsg "WikiIncl: ".lnk." Sub: ".lnk
endif
return
endif
" try WikiLink0: replace with WikiLink1
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink0)
if !empty(lnk)
let sub = vimwiki#base#normalize_link_helper(lnk,
\ g:vimwiki_rxWikiLinkMatchUrl, g:vimwiki_rxWikiLinkMatchDescr,
\ g:vimwiki_WikiLink1Template2)
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWikiLink0, sub)
if g:vimwiki_debug > 1
echomsg "WikiLink: ".lnk." Sub: ".sub
endif
return
endif
" try WikiLink1: replace with WikiLink0
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWikiLink1)
if !empty(lnk)
let sub = vimwiki#base#normalize_link_helper(lnk,
\ g:vimwiki_rxWikiLinkMatchUrl, g:vimwiki_rxWikiLinkMatchDescr,
\ g:vimwiki_WikiLinkTemplate2)
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWikiLink1, sub)
if g:vimwiki_debug > 1
echomsg "WikiLink: ".lnk." Sub: ".sub
endif
return
endif
" try Weblink
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWeblink)
if !empty(lnk)
let sub = vimwiki#base#normalize_link_helper(lnk,
\ g:vimwiki_rxWeblinkMatchUrl, g:vimwiki_rxWeblinkMatchDescr,
\ g:vimwiki_Weblink1Template)
call vimwiki#base#replacestr_at_cursor(g:vimwiki_rxWeblink, sub)
if g:vimwiki_debug > 1
echomsg "WebLink: ".lnk." Sub: ".sub
endif
return
endif
" try Word (any characters except separators)
" rxWord is less permissive than rxWikiLinkUrl which is used in
" normalize_link_syntax_v
let lnk = vimwiki#base#matchstr_at_cursor(g:vimwiki_rxWord)
if !empty(lnk)
let sub = vimwiki#base#normalize_link_helper(lnk,
\ g:vimwiki_rxWord, '',
\ g:vimwiki_WikiLinkTemplate1)
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
if g:vimwiki_debug > 1
echomsg "Word: ".lnk." Sub: ".sub
endif
return
endif
endfunction " }}}
function! s:normalize_link_syntax_v() " {{{
let lnum = line('.')
let sel_save = &selection
let &selection = "old"
let rv = @"
let rt = getregtype('"')
let done = 0
try
norm! gvy
let visual_selection = @"
let visual_selection = substitute(g:vimwiki_WikiLinkTemplate1, '__LinkUrl__', '\='."'".visual_selection."'", '')
call setreg('"', visual_selection, 'v')
" paste result
norm! `>pgvd
finally
call setreg('"', rv, rt)
let &selection = sel_save
endtry
endfunction " }}}
" normalize_link
function! vimwiki#markdown_base#normalize_link(is_visual_mode) "{{{
if !a:is_visual_mode
call s:normalize_link_syntax_n()
elseif visualmode() ==# 'v' && line("'<") == line("'>")
" action undefined for 'line-wise' or 'multi-line' visual mode selections
call s:normalize_link_syntax_v()
endif
endfunction "}}}

View File

@ -24,16 +24,56 @@ del {text-decoration: line-through; color: #777777;}
.justright {text-align: right;}
.justcenter {text-align: center;}
.center {margin-left: auto; margin-right: auto;}
/* classes for items of todo lists */
.done0:before {content: "\2592\2592\2592\2592"; color: SkyBlue;}
.done1:before {content: "\2588\2592\2592\2592"; color: SkyBlue;}
.done2:before {content: "\2588\2588\2592\2592"; color: SkyBlue;}
.done3:before {content: "\2588\2588\2588\2592"; color: SkyBlue;}
.done4:before {content: "\2588\2588\2588\2588"; color: SkyBlue;}
/* comment the next four or five lines out *
* if you do not want color-coded todo lists */
.done0 {color: #c00000;}
.done1 {color: #c08000;}
.done2 {color: #80a000;}
.done3 {color: #00c000;}
.done4 {color: #7f7f7f; text-decoration: line-through;}
.done0 {
/* list-style: none; */
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAxQAAAMUBHc26qAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAA7SURBVCiR7dMxEgAgCANBI3yVRzF5KxNbW6wsuH7LQ2YKQK1mkswBVERYF5Os3UV3gwd/jF2SkXy66gAZkxS6BniubAAAAABJRU5ErkJggg==);
background-repeat: no-repeat;
background-position: 0 .2em;
margin-left: -2em;
padding-left: 1.5em;
}
.done1 {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAxQAAAMUBHc26qAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAABtSURBVCiR1ZO7DYAwDER9BDmTeZQMFXmUbGYpOjrEryA0wOvO8itOslFrJYAug5BMM4BeSkmjsrv3aVTa8p48Xw1JSkSsWVUFwD05IqS1tmYzk5zzae9jnVVVzGyXb8sALjse+euRkEzu/uirFomVIdDGOLjuAAAAAElFTkSuQmCC);
background-repeat: no-repeat;
background-position: 0 .15em;
margin-left: -2em;
padding-left: 1.5em;
}
.done2 {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAxQAAAMUBHc26qAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAB1SURBVCiRzdO5DcAgDAVQGxjAYgTvxlDIu1FTIRYAp8qlFISkSH7l5kk+ZIwxKiI2mIyqWoeILYRgZ7GINDOLjnmF3VqklKCUMgTee2DmM661Qs55iI3Zm/1u5h9sm4ig9z4ERHTFzLyd4G4+nFlVrYg8+qoF/c0kdpeMsmcAAAAASUVORK5CYII=);
background-repeat: no-repeat;
background-position: 0 .15em;
margin-left: -2em;
padding-left: 1.5em;
}
.done3 {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAxQAAAMUBHc26qAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAABoSURBVCiR7dOxDcAgDATA/0DtUdiKoZC3YhLkHjkVKF3idJHiztKfvrHZWnOSE8Fx95RJzlprimJVnXktvXeY2S0SEZRSAAAbmxnGGKH2I5T+8VfxPhIReQSuuY3XyYWa3T2p6quvOgGrvSFGlewuUAAAAABJRU5ErkJggg==);
background-repeat: no-repeat;
background-position: 0 .15em;
margin-left: -2em;
padding-left: 1.5em;
}
.done4 {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAQCAYAAAAbBi9cAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAzgAAAM4BlP6ToAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAIISURBVDiNnZQ9SFtRFMd/773kpTaGJoQk1im4VDpWQcTNODhkFBcVTCNCF0NWyeDiIIiCm82QoIMIUkHUxcFBg1SEQoZszSat6cdTn1qNue92CMbEr9Sey+XC/Z/zu+f8h6ukUil3sVg0+M+4cFxk42/jH2wAqqqKSCSiPQdwcHHAnDHH9s/tN1h8V28ETdP+eU8fT9Nt62ancYdIPvJNtsu87bmjrJlrTDVM4RROJs1JrHPrD4Bar7A6cpc54iKOaTdJXCUI2UMVrQZ0Js7YPN18ECKkYNQcJe/OE/4dZsw7VqNXQMvHy3QZXQypQ6ycrtwDjf8aJ+PNEDSCzLpn7+m2pD8ZKHlKarYhy6XjEoCYGcN95qansQeA3fNdki+SaJZGTMQIOoL3W/Z89rxv+tokubNajlvk/vm+LFpF2XnUKZHI0I+QrI7Dw0OZTqdzUkpsM7mZTyfy5OPGyw1tK7AFSvmB/Ks8w8YwbUYbe6/3QEKv0vugfxWPnMLJun+d/kI/WLdizpNjMbAIKrhMF4OuwadBALqqs+RfInwUvuNi+fBd+wjogfogAFVRmffO02q01mZZ0HHdgXIzdz0QQLPezIQygX6llxNKKgOFARYCC49CqhoHIUTlss/Vx2phlYwjw8j1CAlfAiwQiJpiy7o1VHnsG5FISkoJu7Q/2YmmaV+i0ei7v38L2CBguSi5AAAAAElFTkSuQmCC);
background-repeat: no-repeat;
background-position: 0 .15em;
margin-left: -2em;
padding-left: 1.5em;
}
code {
font-family: Monaco,"Courier New","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;
-webkit-border-radius: 1px;
-moz-border-radius: 1px;
border-radius: 1px;
-moz-background-clip: padding;
-webkit-background-clip: padding-box;
background-clip: padding-box;
padding: 0px 3px;
display: inline-block;
color: #52595d;
border: 1px solid #ccc;
background-color: #f9f9f9;
}

View File

@ -2,7 +2,7 @@
" Vimwiki autoload plugin file
" Desc: Tables
" | Easily | manageable | text | tables | ! |
" |--------+------------+-------+--------+---------|
" |--------|------------|-------|--------|---------|
" | Have | fun! | Drink | tea | Period. |
"
" Author: Maxim Kim <habamax@gmail.com>
@ -16,6 +16,8 @@ let g:loaded_vimwiki_tbl_auto = 1
"}}}
let s:textwidth = &tw
let s:rxSep = g:vimwiki_rxTableSep
" Misc functions {{{
function! s:wide_len(str) "{{{
@ -40,21 +42,37 @@ function! s:wide_len(str) "{{{
return ret
endfunction "}}}
function! s:cell_splitter() "{{{
return '\s*'.s:rxSep.'\s*'
endfunction "}}}
function! s:sep_splitter() "{{{
return '-'.s:rxSep.'-'
endfunction "}}}
function! s:is_table(line) "{{{
return a:line =~ '^\s*\%(|[^|]\+\)\+|\s*$' || s:is_separator(a:line)
return s:is_separator(a:line) || (a:line !~ s:rxSep.s:rxSep && a:line =~ '^\s*'.s:rxSep.'.\+'.s:rxSep.'\s*$')
endfunction "}}}
function! s:is_separator(line) "{{{
return a:line =~ '^\s*[|+]\s*--[-|+]\+'
return a:line =~ '^\s*'.s:rxSep.'\(--\+'.s:rxSep.'\)\+\s*$'
endfunction "}}}
function! s:is_separator_tail(line) "{{{
return a:line =~ '^\{-1}\%(\s*\|-*\)\%('.s:rxSep.'-\+\)\+'.s:rxSep.'\s*$'
endfunction "}}}
function! s:is_last_column(lnum, cnum) "{{{
return strpart(getline(a:lnum), a:cnum - 1) =~ '^[^|]*|\s*$'
let line = strpart(getline(a:lnum), a:cnum - 1)
"echomsg "DEBUG is_last_column> ".(line =~ s:rxSep.'\s*$' && line !~ s:rxSep.'.*'.s:rxSep.'\s*$')
return line =~ s:rxSep.'\s*$' && line !~ s:rxSep.'.*'.s:rxSep.'\s*$'
endfunction "}}}
function! s:is_first_column(lnum, cnum) "{{{
let line = strpart(getline(a:lnum), 0, a:cnum - 1)
return line =~ '^\s*|[^|]*$' || line =~ '^\s*$'
"echomsg "DEBUG is_first_column> ".(line =~ '^\s*'.s:rxSep && line !~ '^\s*'.s:rxSep.'.*'.s:rxSep)
return line =~ '^\s*$' || (line =~ '^\s*'.s:rxSep && line !~ '^\s*'.s:rxSep.'.*'.s:rxSep)
endfunction "}}}
function! s:count_separators_up(lnum) "{{{
@ -82,11 +100,10 @@ function! s:count_separators_down(lnum) "{{{
endfunction "}}}
function! s:create_empty_row(cols) "{{{
let first_cell = "| |"
let cell = " |"
let row = first_cell
let row = s:rxSep
let cell = " ".s:rxSep
for c in range(a:cols - 1)
for c in range(a:cols)
let row .= cell
endfor
@ -94,36 +111,71 @@ function! s:create_empty_row(cols) "{{{
endfunction "}}}
function! s:create_row_sep(cols) "{{{
let first_cell = "|---+"
let cell = "---+"
let last_cell = "---|"
let row = s:rxSep
let cell = "---".s:rxSep
if a:cols < 2
return "|---|"
endif
let row = first_cell
for c in range(a:cols - 2)
for c in range(a:cols)
let row .= cell
endfor
let row .= last_cell
return row
endfunction "}}}
function! s:get_values(line) "{{{
return split(a:line, '\s*|\s*', 1)[1:-2]
function! vimwiki#tbl#get_cells(line) "{{{
let result = []
let cell = ''
let quote = ''
let state = 'NONE'
" 'Simple' FSM
for idx in range(strlen(a:line))
" The only way I know Vim can do Unicode...
let ch = a:line[idx]
if state == 'NONE'
if ch == '|'
let state = 'CELL'
endif
elseif state == 'CELL'
if ch == '[' || ch == '{'
let state = 'BEFORE_QUOTE_START'
let quote = ch
elseif ch == '|'
call add(result, vimwiki#u#trim(cell))
let cell = ""
else
let cell .= ch
endif
elseif state == 'BEFORE_QUOTE_START'
if ch == '[' || ch == '{'
let state = 'QUOTE'
let quote .= ch
else
let state = 'CELL'
let cell .= quote.ch
let quote = ''
endif
elseif state == 'QUOTE'
if ch == ']' || ch == '}'
let state = 'BEFORE_QUOTE_END'
endif
let quote .= ch
elseif state == 'BEFORE_QUOTE_END'
if ch == ']' || ch == '}'
let state = 'CELL'
endif
let cell .= quote.ch
let quote = ''
endif
endfor
if cell.quote != ''
call add(result, vimwiki#u#trim(cell.quote, '|'))
endif
return result
endfunction "}}}
function! s:col_count(lnum) "{{{
let line = getline(a:lnum)
if !s:is_separator(line)
return len(split(line, '\s*|\s*', 1)[1:-2])
else
return len(split(line, '-+-', 1))
endif
return len(vimwiki#tbl#get_cells(getline(a:lnum)))
endfunction "}}}
function! s:get_indent(lnum) "{{{
@ -155,7 +207,7 @@ function! s:get_rows(lnum) "{{{
let lower_rows = []
let lnum = a:lnum - 1
while lnum > 1
while lnum >= 1
let line = getline(lnum)
if s:is_table(line)
call add(upper_rows, [lnum, line])
@ -186,7 +238,7 @@ function! s:get_cell_max_lens(lnum) "{{{
if s:is_separator(row)
continue
endif
let cells = s:get_values(row)
let cells = vimwiki#tbl#get_cells(row)
for idx in range(len(cells))
let value = cells[idx]
if has_key(max_lens, idx)
@ -219,17 +271,13 @@ function! s:cur_column() "{{{
if !s:is_table(line)
return -1
endif
if s:is_separator(line)
let sep = '[+|]'
else
let sep = '|'
endif
" TODO: do we need conditional: if s:is_separator(line)
let curs_pos = col('.')
let mpos = match(line, '|', 0)
let mpos = match(line, s:rxSep, 0)
let col = -1
while mpos < curs_pos && mpos != -1
let mpos = match(line, sep, mpos+1)
let mpos = match(line, s:rxSep, mpos+1)
if mpos != -1
let col += 1
endif
@ -253,8 +301,8 @@ function! s:fmt_cell(cell, max_len) "{{{
endfunction "}}}
function! s:fmt_row(line, max_lens, col1, col2) "{{{
let new_line = '|'
let cells = s:get_values(a:line)
let new_line = s:rxSep
let cells = vimwiki#tbl#get_cells(a:line)
for idx in range(len(cells))
if idx == a:col1
let idx = a:col2
@ -262,12 +310,12 @@ function! s:fmt_row(line, max_lens, col1, col2) "{{{
let idx = a:col1
endif
let value = cells[idx]
let new_line .= s:fmt_cell(value, a:max_lens[idx]).'|'
let new_line .= s:fmt_cell(value, a:max_lens[idx]).s:rxSep
endfor
let idx = len(cells)
while idx < len(a:max_lens)
let new_line .= s:fmt_cell('', a:max_lens[idx]).'|'
let new_line .= s:fmt_cell('', a:max_lens[idx]).s:rxSep
let idx += 1
endwhile
return new_line
@ -282,17 +330,16 @@ function! s:fmt_cell_sep(max_len) "{{{
endfunction "}}}
function! s:fmt_sep(max_lens, col1, col2) "{{{
let sep = '|'
let new_line = s:rxSep
for idx in range(len(a:max_lens))
if idx == a:col1
let idx = a:col2
elseif idx == a:col2
let idx = a:col1
endif
let sep .= s:fmt_cell_sep(a:max_lens[idx]).'+'
let new_line .= s:fmt_cell_sep(a:max_lens[idx]).s:rxSep
endfor
let sep = substitute(sep, '+$', '|', '')
return sep
return new_line
endfunction "}}}
"}}}
@ -300,41 +347,94 @@ endfunction "}}}
function! s:kbd_create_new_row(cols, goto_first) "{{{
let cmd = "\<ESC>o".s:create_empty_row(a:cols)
let cmd .= "\<ESC>:call vimwiki#tbl#format(line('.'))\<CR>"
let cmd .= "\<ESC>0"
if a:goto_first
let cmd .= "\<ESC>0:call search('|', 'c', line('.'))\<CR>la"
let cmd .= ":call search('\\(".s:rxSep."\\)\\zs', 'c', line('.'))\<CR>"
else
let cmd .= "0".(col('.')-1)."lT|a"
let cmd .= (col('.')-1)."l"
let cmd .= ":call search('\\(".s:rxSep."\\)\\zs', 'bc', line('.'))\<CR>"
endif
let cmd .= "a"
return cmd
endfunction "}}}
function! s:kbd_goto_next_row() "{{{
let cmd = "\<ESC>jt|T|a"
let cmd = "\<ESC>j"
let cmd .= ":call search('.\\(".s:rxSep."\\)', 'c', line('.'))\<CR>"
let cmd .= ":call search('\\(".s:rxSep."\\)\\zs', 'bc', line('.'))\<CR>"
let cmd .= "a"
return cmd
endfunction "}}}
function! s:kbd_goto_prev_row() "{{{
let cmd = "\<ESC>jt|T|a"
let cmd = "\<ESC>k"
let cmd .= ":call search('.\\(".s:rxSep."\\)', 'c', line('.'))\<CR>"
let cmd .= ":call search('\\(".s:rxSep."\\)\\zs', 'bc', line('.'))\<CR>"
let cmd .= "a"
return cmd
endfunction "}}}
function! s:kbd_goto_next_col(last) "{{{
if a:last
" Used in s:kbd_goto_next_col
function! vimwiki#tbl#goto_next_col() "{{{
let curcol = virtcol('.')
let lnum = line('.')
let newcol = s:get_indent(lnum)
let max_lens = s:get_cell_max_lens(lnum)
for cell_len in values(max_lens)
if newcol >= curcol-1
break
endif
let newcol += cell_len + 3 " +3 == 2 spaces + 1 separator |<space>...<space>
endfor
let newcol += 2 " +2 == 1 separator + 1 space |<space
call vimwiki#u#cursor(lnum, newcol)
endfunction "}}}
function! s:kbd_goto_next_col(jumpdown) "{{{
let cmd = "\<ESC>"
if a:jumpdown
let seps = s:count_separators_down(line('.'))
let cmd = "\<ESC>".seps."j0:call search('|', 'c', line('.'))\<CR>la"
else
let cmd = "\<ESC>:call search('|', 'c', line('.'))\<CR>la"
let cmd .= seps."j0"
endif
let cmd .= ":call vimwiki#tbl#goto_next_col()\<CR>a"
return cmd
endfunction "}}}
function! s:kbd_goto_prev_col(first) "{{{
if a:first
" Used in s:kbd_goto_prev_col
function! vimwiki#tbl#goto_prev_col() "{{{
let curcol = virtcol('.')
let lnum = line('.')
let newcol = s:get_indent(lnum)
let max_lens = s:get_cell_max_lens(lnum)
let prev_cell_len = 0
echom string(max_lens)
for cell_len in values(max_lens)
let delta = cell_len + 3 " +3 == 2 spaces + 1 separator |<space>...<space>
if newcol + delta > curcol-1
let newcol -= (prev_cell_len + 3) " +3 == 2 spaces + 1 separator |<space>...<space>
break
elseif newcol + delta == curcol-1
break
endif
let prev_cell_len = cell_len
let newcol += delta
endfor
let newcol += 2 " +2 == 1 separator + 1 space |<space
call vimwiki#u#cursor(lnum, newcol)
endfunction "}}}
function! s:kbd_goto_prev_col(jumpup) "{{{
let cmd = "\<ESC>"
if a:jumpup
let seps = s:count_separators_up(line('.'))
let cmd = "\<ESC>".seps."k$:call search('|', 'b', line('.'))\<CR>la"
else
let cmd = "\<ESC>2F|la"
let cmd .= seps."k"
let cmd .= "$"
endif
let cmd .= ":call vimwiki#tbl#goto_prev_col()\<CR>a"
" let cmd .= ":call search('\\(".s:rxSep."\\)\\zs', 'b', line('.'))\<CR>"
" let cmd .= "a"
"echomsg "DEBUG kbd_goto_prev_col> ".cmd
return cmd
endfunction "}}}
@ -348,7 +448,7 @@ function! vimwiki#tbl#kbd_cr() "{{{
endif
if s:is_separator(getline(lnum+1)) || !s:is_table(getline(lnum+1))
let cols = len(s:get_values(getline(lnum)))
let cols = len(vimwiki#tbl#get_cells(getline(lnum)))
return s:kbd_create_new_row(cols, 0)
else
return s:kbd_goto_next_row()
@ -362,11 +462,13 @@ function! vimwiki#tbl#kbd_tab() "{{{
endif
let last = s:is_last_column(lnum, col('.'))
if last && !s:is_table(getline(lnum+1))
let cols = len(s:get_values(getline(lnum)))
let is_sep = s:is_separator_tail(getline(lnum))
"echomsg "DEBUG kbd_tab> last=".last.", is_sep=".is_sep
if (is_sep || last) && !s:is_table(getline(lnum+1))
let cols = len(vimwiki#tbl#get_cells(getline(lnum)))
return s:kbd_create_new_row(cols, 1)
endif
return s:kbd_goto_next_col(last)
return s:kbd_goto_next_col(is_sep || last)
endfunction "}}}
function! vimwiki#tbl#kbd_shift_tab() "{{{
@ -376,10 +478,12 @@ function! vimwiki#tbl#kbd_shift_tab() "{{{
endif
let first = s:is_first_column(lnum, col('.'))
if first && !s:is_table(getline(lnum-1))
let is_sep = s:is_separator_tail(getline(lnum))
"echomsg "DEBUG kbd_tab> ".first
if (is_sep || first) && !s:is_table(getline(lnum-1))
return ""
endif
return s:kbd_goto_prev_col(first)
return s:kbd_goto_prev_col(is_sep || first)
endfunction "}}}
function! vimwiki#tbl#format(lnum, ...) "{{{
@ -462,7 +566,12 @@ endfunction "}}}
" TODO: move_column_left and move_column_right are good candidates to be
" refactored.
function! vimwiki#tbl#move_column_left() "{{{
if !s:is_table(getline('.'))
"echomsg "DEBUG move_column_left: "
let line = getline('.')
if !s:is_table(line)
return
endif
@ -474,16 +583,28 @@ function! vimwiki#tbl#move_column_left() "{{{
if cur_col > 0
call vimwiki#tbl#format(line('.'), cur_col-1, cur_col)
call cursor(line('.'), 1)
if !s:is_separator(getline('.'))
call search('\%(|[^|]\+\)\{'.(cur_col-1).'}| .', 'eW')
else
call search('|\%([^+]\++\)\{'.(cur_col-1).'}--', 'eW')
endif
let sep = '\('.s:rxSep.'\).\zs'
let mpos = -1
let col = -1
while col < cur_col-1
let mpos = match(line, sep, mpos+1)
if mpos != -1
let col += 1
else
break
endif
endwhile
endif
endfunction "}}}
function! vimwiki#tbl#move_column_right() "{{{
if !s:is_table(getline('.'))
let line = getline('.')
if !s:is_table(line)
return
endif
@ -495,16 +616,41 @@ function! vimwiki#tbl#move_column_right() "{{{
if cur_col < s:col_count(line('.'))-1
call vimwiki#tbl#format(line('.'), cur_col, cur_col+1)
call cursor(line('.'), 1)
if !s:is_separator(getline('.'))
call search('\%(|[^|]\+\)\{'.(cur_col+1).'}| .', 'eW')
else
call search('|\%([^+]\++\)\{'.(cur_col+1).'}--', 'eW')
endif
let sep = '\('.s:rxSep.'\).\zs'
let mpos = -1
let col = -1
while col < cur_col+1
let mpos = match(line, sep, mpos+1)
if mpos != -1
let col += 1
else
break
endif
endwhile
endif
endfunction "}}}
function! vimwiki#tbl#get_rows(lnum) "{{{
return s:get_rows(a:lnum)
endfunction "}}}
function! vimwiki#tbl#is_table(line) "{{{
return s:is_table(a:line)
endfunction "}}}
function! vimwiki#tbl#is_separator(line) "{{{
return s:is_separator(a:line)
endfunction "}}}
function! vimwiki#tbl#cell_splitter() "{{{
return s:cell_splitter()
endfunction "}}}
function! vimwiki#tbl#sep_splitter() "{{{
return s:sep_splitter()
endfunction "}}}
"}}}

77
autoload/vimwiki/u.vim Normal file
View File

@ -0,0 +1,77 @@
" vim:tabstop=2:shiftwidth=2:expandtab:foldmethod=marker:textwidth=79
" Vimwiki autoload plugin file
" Utility functions
" Author: Maxim Kim <habamax@gmail.com>
" Home: http://code.google.com/p/vimwiki/
function! vimwiki#u#trim(string, ...) "{{{
let chars = ''
if a:0 > 0
let chars = a:1
endif
let res = substitute(a:string, '^[[:space:]'.chars.']\+', '', '')
let res = substitute(res, '[[:space:]'.chars.']\+$', '', '')
return res
endfunction "}}}
" Builtin cursor doesn't work right with unicode characters.
function! vimwiki#u#cursor(lnum, cnum) "{{{
exe a:lnum
exe 'normal! 0'.a:cnum.'|'
endfunction "}}}
function! vimwiki#u#is_windows() "{{{
return has("win32") || has("win64") || has("win95") || has("win16")
endfunction "}}}
function! vimwiki#u#chomp_slash(str) "{{{
return substitute(a:str, '[/\\]\+$', '', '')
endfunction "}}}
function! vimwiki#u#time(starttime) "{{{
" measure the elapsed time and cut away miliseconds and smaller
return matchstr(reltimestr(reltime(a:starttime)),'\d\+\(\.\d\d\)\=')
endfunction "}}}
function! vimwiki#u#path_norm(path) "{{{
" /-slashes
let path = substitute(a:path, '\', '/', 'g')
" treat multiple consecutive slashes as one path separator
let path = substitute(path, '/\+', '/', 'g')
" ensure that we are not fooled by a symbolic link
return resolve(path)
endfunction "}}}
function! vimwiki#u#is_link_to_dir(link) "{{{
" Check if link is to a directory.
" It should be ended with \ or /.
if a:link =~ '.\+[/\\]$'
return 1
endif
return 0
endfunction " }}}
function! vimwiki#u#count_first_sym(line) "{{{
let first_sym = matchstr(a:line, '\S')
return len(matchstr(a:line, first_sym.'\+'))
endfunction "}}}
" return longest common path prefix of 2 given paths.
" '~/home/usrname/wiki', '~/home/usrname/wiki/shmiki' => '~/home/usrname/wiki'
function! vimwiki#u#path_common_pfx(path1, path2) "{{{
let p1 = split(a:path1, '[/\\]', 1)
let p2 = split(a:path2, '[/\\]', 1)
let idx = 0
let minlen = min([len(p1), len(p2)])
while (idx < minlen) && (p1[idx] ==? p2[idx])
let idx = idx + 1
endwhile
if idx == 0
return ''
else
return join(p1[: idx-1], '/')
endif
endfunction "}}}