aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-12-30 17:21:06 +1300
committerTom Ryder <tom@sanctum.geek.nz>2019-12-30 17:21:06 +1300
commit3c1b52b2667668f3bc1a0acb45b901fcd3bcf090 (patch)
tree29377c36d3083db4bcf80363f48092ae3394291e
parentFix and refactor HTML timestamp updating (diff)
downloaddotfiles-3c1b52b2667668f3bc1a0acb45b901fcd3bcf090.tar.gz
dotfiles-3c1b52b2667668f3bc1a0acb45b901fcd3bcf090.zip
Factor out HTML timestamp update into sep file
-rw-r--r--vim/after/ftplugin/html.vim2
-rw-r--r--vim/autoload/html/timestamp.vim45
2 files changed, 46 insertions, 1 deletions
diff --git a/vim/after/ftplugin/html.vim b/vim/after/ftplugin/html.vim
index 7866f31b..b4e55462 100644
--- a/vim/after/ftplugin/html.vim
+++ b/vim/after/ftplugin/html.vim
@@ -26,7 +26,7 @@ let b:undo_ftplugin .= '|nunmap <buffer> <Leader>='
augroup html_timestamp
autocmd BufWritePre <buffer>
\ if exists('b:html_timestamp_check')
- \| call html#TimestampUpdate()
+ \| call html#timestamp#Update()
\|endif
augroup END
let b:undo_ftplugin .= '|execute ''autocmd! html_timestamp'''
diff --git a/vim/autoload/html/timestamp.vim b/vim/autoload/html/timestamp.vim
new file mode 100644
index 00000000..af9b0d44
--- /dev/null
+++ b/vim/autoload/html/timestamp.vim
@@ -0,0 +1,45 @@
+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#timestamp#Update() abort
+ if !&modified
+ return
+ endif
+ let lnum = search(s:pattern, 'nw')
+ if !lnum
+ return
+ endif
+ 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