Refactor HTML conversion into separate function

This commit is contained in:
Reiner Herrmann 2020-07-05 17:53:56 +02:00 committed by Tinmarino
parent e006da5aa0
commit 0543b46ffd

View File

@ -1574,32 +1574,17 @@ function! vimwiki#html#CustomWiki2HTML(path, wikifile, force) abort
endif endif
endfunction endfunction
function s:convert_file_to_lines(wikifile, current_html_file) abort
function! s:convert_file(path_html, wikifile) abort let result = {}
let done = 0
let wikifile = fnamemodify(a:wikifile, ':p')
let path_html = expand(a:path_html).vimwiki#vars#get_bufferlocal('subdir')
let htmlfile = fnamemodify(wikifile, ':t:r').'.html'
" the currently processed file name is needed when processing links " the currently processed file name is needed when processing links
" yeah yeah, shame on me for using (quasi-) global variables " yeah yeah, shame on me for using (quasi-) global variables
let s:current_wiki_file = wikifile let s:current_wiki_file = a:wikifile
let s:current_html_file = path_html . htmlfile let s:current_html_file = a:current_html_file
if s:use_custom_wiki2html() let lsource = readfile(a:wikifile)
let force = 1
call vimwiki#html#CustomWiki2HTML(path_html, wikifile, force)
let done = 1
endif
if s:syntax_supported() && done == 0
let lsource = readfile(wikifile)
let ldest = [] let ldest = []
call vimwiki#path#mkdir(path_html)
" nohtml placeholder -- to skip html generation. " nohtml placeholder -- to skip html generation.
let nohtml = 0 let nohtml = 0
@ -1663,9 +1648,10 @@ function! s:convert_file(path_html, wikifile) abort
endfor endfor
let result['nohtml'] = nohtml
if nohtml if nohtml
echon "\r".'%nohtml placeholder found' echon "\r".'%nohtml placeholder found'
return '' return result
endif endif
call s:remove_blank_lines(ldest) call s:remove_blank_lines(ldest)
@ -1683,18 +1669,29 @@ function! s:convert_file(path_html, wikifile) abort
call s:close_tag_table(state.table, lines, state.header_ids) call s:close_tag_table(state.table, lines, state.header_ids)
call extend(ldest, lines) call extend(ldest, lines)
let title = s:process_title(placeholders, fnamemodify(a:wikifile, ':t:r')) let result['html'] = ldest
let date = s:process_date(placeholders, strftime('%Y-%m-%d'))
let wiki_path = strpart(s:current_wiki_file, strlen(vimwiki#vars#get_wikilocal('path')))
let html_lines = s:get_html_template(template_name) let result['template_name'] = template_name
let result['title'] = s:process_title(placeholders, fnamemodify(a:wikifile, ':t:r'))
let result['date'] = s:process_date(placeholders, strftime('%Y-%m-%d'))
let result['wiki_path'] = strpart(s:current_wiki_file, strlen(vimwiki#vars#get_wikilocal('path')))
return result
endfunction
function s:convert_file_to_lines_template(wikifile, current_html_file) abort
let converted = s:convert_file_to_lines(a:wikifile, a:current_html_file)
if converted['nohtml'] == 1
return []
endif
let html_lines = s:get_html_template(converted['template_name'])
" processing template variables (refactor to a function) " processing template variables (refactor to a function)
call map(html_lines, 'substitute(v:val, "%title%", "'. title .'", "g")') call map(html_lines, 'substitute(v:val, "%title%", "'. converted['title'] .'", "g")')
call map(html_lines, 'substitute(v:val, "%date%", "'. date .'", "g")') call map(html_lines, 'substitute(v:val, "%date%", "'. converted['date'] .'", "g")')
call map(html_lines, 'substitute(v:val, "%root_path%", "'. call map(html_lines, 'substitute(v:val, "%root_path%", "'.
\ s:root_path(vimwiki#vars#get_bufferlocal('subdir')) .'", "g")') \ s:root_path(vimwiki#vars#get_bufferlocal('subdir')) .'", "g")')
call map(html_lines, 'substitute(v:val, "%wiki_path%", "'. wiki_path .'", "g")') call map(html_lines, 'substitute(v:val, "%wiki_path%", "'. converted['wiki_path'] .'", "g")')
let css_name = expand(vimwiki#vars#get_wikilocal('css_name')) let css_name = expand(vimwiki#vars#get_wikilocal('css_name'))
let css_name = substitute(css_name, '\', '/', 'g') let css_name = substitute(css_name, '\', '/', 'g')
@ -1706,11 +1703,31 @@ function! s:convert_file(path_html, wikifile) abort
endif endif
call map(html_lines, 'substitute(v:val, "%encoding%", "'. enc .'", "g")') call map(html_lines, 'substitute(v:val, "%encoding%", "'. enc .'", "g")')
let html_lines = s:html_insert_contents(html_lines, ldest) " %contents% let html_lines = s:html_insert_contents(html_lines, converted['html']) " %contents%
return html_lines
endfunction
function! s:convert_file(path_html, wikifile) abort
let done = 0
let wikifile = fnamemodify(a:wikifile, ':p')
let path_html = expand(a:path_html).vimwiki#vars#get_bufferlocal('subdir')
if s:use_custom_wiki2html()
let force = 1
call vimwiki#html#CustomWiki2HTML(path_html, wikifile, force)
let done = 1
endif
if s:syntax_supported() && done == 0
let htmlfile = fnamemodify(wikifile, ':t:r').'.html'
let html_lines = s:convert_file_to_lines_template(wikifile, path_html . htmlfile)
if html_lines == []
return ''
endif
call vimwiki#path#mkdir(path_html)
call writefile(html_lines, path_html.htmlfile) call writefile(html_lines, path_html.htmlfile)
let done = 1 let done = 1
endif endif
if done == 0 if done == 0