Unify path handling -- part 3

This commit is contained in:
EinfachToll 2018-02-22 08:21:23 +01:00
parent 12d6265193
commit f76e75d117
4 changed files with 96 additions and 43 deletions

View File

@ -49,8 +49,8 @@ function! s:is_img_link(lnk) "{{{
return 0
endfunction "}}}
function! s:default_CSS_full_name(target_dir) " {{{
return vimwiki#path#join(path, vimwiki#vars#get_wikilocal('css_name'))
function! s:default_CSS_full_name(path) " {{{
return vimwiki#path#join(a:path, vimwiki#vars#get_wikilocal('css_name'))
endfunction "}}}
" Returns: 1 if it was created, 0 if it already existed
@ -72,7 +72,7 @@ function! s:template_full_name(name) "{{{
let name = a:name
endif
let filename = vimwiki#path#from_segment_file(name . vimwiki#vars#get_wikilocal('template_ext'))
let filename = vimwiki#path#file_segment(name . vimwiki#vars#get_wikilocal('template_ext'))
let template_file = vimwiki#path#to_string(vimwiki#path#join(vimwiki#vars#get_wikilocal('template_path'), filename))

View File

@ -169,19 +169,28 @@ endif
"----------------------------------------------------------
" Path manipulation, i.e. do stuff with the paths of (not necessarily existing) files
" Path manipulation, i.e. functions which do stuff with the paths of (not necessarily existing) files
"----------------------------------------------------------
" The used data types are (with their internal representation)
" - directory object: a dictionary with the following entries:
" The used data types are
"
" - directory object:
" - used for an absolute path to a directory
" - internally, it is a dictionary with the following entries:
" - 'protocoll' -- how to access the file. Supported are 'scp' or 'file'
" - 'is_unix' -- 1 if it's supposed to be a unix-like path
" - 'path' -- a list containing the directory names starting at the root
" - file object: a list [dir_obj, file name]
" - file segment: representing e.g. a relative path. Is a list where the first
" element is a list of directory names and the second the file name
" - directory segment: a list of directory names
" - file object:
" - for an absolute path to a file
" - internally a list [dir_obj, file name]
" - file segment:
" - for a relative path to a file or a part of an absolute path
" - internally a list where the first element is a list of directory names and the second the
" file name
" - directory segment:
" - for a relative path to a directory or a part of an absolute path
" - internally a list of directory names
" create and return a file object from a string. It is assumed that the given
@ -214,6 +223,21 @@ function! vimwiki#path#dir_obj(dirpath)
endfunction
" Assume it is not an absolute path
function! vimwiki#path#file_segment(path_segment)
let filename = fnamemodify(a:path_segment, ':t')
let path = fnamemodify(a:path_segment, ':h')
let path_list = (path ==# '.' ? [] : split(path, '\m[/\\]', 1))
return [path_list, filename]
endfunction
" Assume it is not an absolute path
function! vimwiki#path#dir_segment(path_segment)
return split(a:path_segment, '\m[/\\]', 1)
endfunction
function! vimwiki#path#extension(file_object)
return fnamemodify(a:file_object[1], ':e')
endfunction
@ -231,16 +255,26 @@ function! vimwiki#path#filename(file_object)
endfunction
" Returns: the dir_obj or file_obj as string, ready to be used with the regular
" path handling functions in Vim
" Returns: the dir_obj, file_obj, file segment or dir segment as string, ready
" to be used with the regular path handling functions in Vim
function! vimwiki#path#to_string(obj)
let dir_obj = type(a:obj) == 4 ? a:obj : a:obj[0]
let separator = dir_obj.is_unix ? '/' : '\'
if type(a:obj) == 4 " dir object
let separator = a:obj.is_unix ? '/' : '\'
let address = join(dir_obj.path, separator) . separator
if type(a:obj) == 3
let address .= a:path[1]
endif
return address
elseif type(a:obj[0]) == 4 " file object
let dir_obj = type(a:obj) == 4 ? a:obj : a:obj[0]
let separator = a:obj[0].is_unix ? '/' : '\'
let address = join(a:obj[0].path, separator) . separator . a:obj[1]
return address
elseif type(a:obj[0]) == 3 " file segment
" XXX: what about the separator?
return join(a:obj[0], '/') . '/' . a:obj[1]
elseif type(a:obj[0]) == 1 " directory segment
return join(a:obj, '/') . '/'
else
call vimwiki#u#error('Invalid argument ' . string(a:obj))
endif
endfunction
@ -251,38 +285,30 @@ function! vimwiki#path#append_to_dirname(dir_obj, str)
endfunction
" Assume it is not an absolute path
function! vimwiki#path#from_segment_file(path_segment)
let filename = fnamemodify(a:path_segment, ':t')
let path = fnamemodify(a:path_segment, ':h')
let path_list = (path ==# '.' ? [] : split(path, '\m[/\\]', 1))
return [path_list, filename]
endfunction
" Assume it is not an absolute path
function! vimwiki#path#from_segment_dir(path_segment)
return split(a:path_segment, '\m[/\\]', 1)
endfunction
function! vimwiki#path#join(dir_path, file_segment)
let new_dir_object = copy(a:dir_path)
" Returns a file object made from a dir object plus a file semgent
function! vimwiki#path#join(dir_obje, file_segment)
let new_dir_object = copy(a:dir_obj)
let new_dir_object.path += a:file_segment[0]
return [new_dir_object, a:file_segment[1]]
endfunction
" Returns a dir object made from a dir object plus a dir semgent
function! vimwiki#path#join_dir(dir_path, dir_segment)
let new_dir_object = copy(a:dir_path)
let new_dir_object.path += a:dir_segment
return new_dir_object
endfunction
" returns the segment s, so that join(dir, s) == file
" returns the file segment fs, so that join(dir, fs) == file
" we just assume the file is somewhere in dir
function! vimwiki#path#subtract(dir_object, file_object)
let path_rest = a:file_object[0].path[len(a:dir_object.path):]
return [path_rest, file_object[1]]
endfunction
" Returns: the relative path from a:dir to a:file
function! vimwiki#path#relpath(dir1_object, dir2_object)
let dir1_path = copy(a:dir1_object.path)
@ -331,7 +357,7 @@ let s:vimwiki_autoload_dir = expand('<sfile>:p:h')
function! vimwiki#path#find_autoload_file(filename)
let autoload_dir = vimwiki#path#dir_obj(s:vimwiki_autoload_dir)
let filename_obj = vimwiki#path#from_segment_file(a:filename)
let filename_obj = vimwiki#path#file_segment(a:filename)
let file = vimwiki#path#join(autoload_dir, filename_obj)
if !vimwiki#path#exists(file)
echom 'Vimwiki Error: ' . vimwiki#path#to_string(file) . ' not found'

View File

@ -145,7 +145,7 @@ endfunction " }}}
" vimwiki#tags#metadata_file_path
" Returns tags metadata file path object
function! vimwiki#tags#metadata_file_path() abort "{{{
return vimwiki#path#join(vimwiki#vars#get_wikilocal('path'), vimwiki#path#from_segment_file(s:TAGS_METADATA_FILE_NAME))
return vimwiki#path#join(vimwiki#vars#get_wikilocal('path'), vimwiki#path#file_segment(s:TAGS_METADATA_FILE_NAME))
endfunction " }}}
" s:load_tags_metadata

View File

@ -48,6 +48,8 @@ function! s:populate_global_variables()
endif
endfor
call s:validate_global_settings()
" non-configurable global variables
" Scheme regexes should be defined even if syntax file is not loaded yet cause users should be
@ -152,6 +154,27 @@ function! s:populate_global_variables()
endfunction
function! s:validate_global_settings()
let g:vimwiki_global_vars.dir_link =
\ vimwiki#path#file_segment(g:vimwiki_global_vars.dir_link)
for extension in g:vimwiki_global_vars.ext2syntax
if extension[0] != '.'
let g:vimwiki_global_vars.ext2syntax['.'.extension] =
g:vimwiki_global_vars.ext2syntax[extension]
call remove(g:vimwiki_global_vars.ext2syntax, extension)
endif
endfor
let new_user_htmls = []
for file_name in split(g:vimwiki_global_vars.user_htmls, ',')
let trimmed = vimwiki#u#trim(file_name)
call add(new_user_htmls, vimwiki#path#file_segment(trimmed))
endfor
let g:vimwiki_global_vars.user_htmls = new_user_htmls
endfunction
" g:vimwiki_wikilocal_vars is a list of dictionaries: one dict for every registered wiki plus one
" (the last in the list) which contains the default values (used for temporary wikis).
function! s:populate_wikilocal_options()
@ -213,12 +236,16 @@ function! s:populate_wikilocal_options()
let temporary_options_dict.temp = 1
call add(g:vimwiki_wikilocal_vars, temporary_options_dict)
call s:validate_settings()
call s:validate_wikilocal_settings()
endfunction
function! s:validate_settings()
function! s:validate_wikilocal_settings()
for wiki_settings in g:vimwiki_wikilocal_vars
let wiki_settings.css_name = vimwiki#path#file_segment(wiki_settings.css_name)
let wiki_settings.custom_wiki2html = vimwiki#path#file_object(wiki_settings.custom_wiki2html)
let wiki_settings['path'] = vimwiki#path#dir_obj(wiki_settings['path'])
let path_html = wiki_settings['path_html']
@ -231,7 +258,7 @@ function! s:validate_settings()
let wiki_settings['template_path'] = vimwiki#path#dir_obj(wiki_settings['template_path'])
let wiki_settings['diary_path'] = vimwiki#path#join_dir(wiki_settings['path'],
\ vimwiki#path#from_segment_dir(wiki_settings['diary_rel_path']))
\ vimwiki#path#dir_segment(wiki_settings['diary_rel_path']))
endfor
endfunction
@ -606,7 +633,7 @@ function! vimwiki#vars#add_temporary_wiki(settings)
let new_temp_wiki_settings[key] = value
endfor
call insert(g:vimwiki_wikilocal_vars, new_temp_wiki_settings, -1)
call s:validate_settings()
call s:validate_wikilocal_settings()
endfunction
" number of registered wikis + temporary