diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2019-12-28 23:43:08 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2019-12-28 23:43:08 +1300 |
commit | bb5cb79bd1736fc33f5959fafed207ca1dd0db6d (patch) | |
tree | 4722dae9d004a5e7ccff89f99b1f5a36b014efe2 | |
parent | Centralize default 'foldlevel' (diff) | |
download | dotfiles-bb5cb79bd1736fc33f5959fafed207ca1dd0db6d.tar.gz dotfiles-bb5cb79bd1736fc33f5959fafed207ca1dd0db6d.zip |
Fix and refactor HTML timestamp updating
-rw-r--r-- | vim/autoload/html.vim | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/vim/autoload/html.vim b/vim/autoload/html.vim index 7ac4f9d3..72f29bb4 100644 --- a/vim/autoload/html.vim +++ b/vim/autoload/html.vim @@ -21,19 +21,48 @@ function! html#TidyBuffer() abort call winrestview(view) endfunction +let s:formats = { + \ 'human': '%a, %d %b %Y %T %Z', + \ 'machine': '%Y-%m-%dT%H:%M:%S.000Z', + \} + +function! s:Timestamp(time) abort + if exists('$TZ') + let tz = $TZ + endif + let $TZ = 'UTC' + let time = localtime() + let timestamp = {} + for key in keys(s:formats) + let timestamp[key] = strftime(s:formats[key], time) + endfor + if exists('tz') + let $TZ = tz + endif + return timestamp +endfunction + +let s:pattern = '\m\C' + \.'Last updated: ' + \.'<time datetime="[^"]\+">' + \.'[^<]\+' + \.'</time>' + " Update a timestamp function! html#TimestampUpdate() abort if !&modified return endif - let cv = winsaveview() - call cursor(1,1) - let li = search('\m\C^\s*<em>Last updated: .\+</em>$', 'n') - if li - let date = substitute(system('date -u'), '\n$', '', '') - let line = getline(li) - call setline(li, substitute(line, '\S.*', - \ '<em>Last updated: '.date.'</em>', '')) + let lnum = search(s:pattern, 'nw') + if !lnum + return endif - call winrestview(cv) + let timestamp = s:Timestamp(localtime()) + let update = 'Last updated: ' + \.'<time datetime="'.timestamp['machine'].'">' + \.timestamp['human'] + \.'</time>' + let line = getline(lnum) + let line = substitute(line, s:pattern, update, '') + call setline(lnum, line) endfunction |