aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-12-30 17:37:24 +1300
committerTom Ryder <tom@sanctum.geek.nz>2019-12-30 17:37:24 +1300
commit00782d5003c57936467dc5c945f8257562aeafa8 (patch)
treeb5876e2ea58a346cfa3b123253c22d8dad26ba19
parentFactor out HTML timestamp update into sep file (diff)
downloaddotfiles-00782d5003c57936467dc5c945f8257562aeafa8.tar.gz
dotfiles-00782d5003c57936467dc5c945f8257562aeafa8.zip
Add some comments
-rw-r--r--vim/autoload/html/timestamp.vim26
1 files changed, 26 insertions, 0 deletions
diff --git a/vim/autoload/html/timestamp.vim b/vim/autoload/html/timestamp.vim
index af9b0d44..871a800f 100644
--- a/vim/autoload/html/timestamp.vim
+++ b/vim/autoload/html/timestamp.vim
@@ -1,24 +1,39 @@
+" Keys and date formats for return value of s:Timestamp()
let s:formats = {
\ 'human': '%a, %d %b %Y %T %Z',
\ 'machine': '%Y-%m-%dT%H:%M:%S.000Z',
\}
+" Get UTC timestamp string dictionary with layout in s:formats
function! s:Timestamp(time) abort
+
+ " Force UTC, recording previous timezone, if any
if exists('$TZ')
let tz = $TZ
endif
let $TZ = 'UTC'
+
+ " Get current time
let time = localtime()
+
+ " Fill out timestamp dictionary
let timestamp = {}
for key in keys(s:formats)
let timestamp[key] = strftime(s:formats[key], time)
endfor
+
+ " Clear UTC and restore previous timezone, if any
+ unlet $TZ
if exists('tz')
let $TZ = tz
endif
+
+ " Return filled-out timestamp dictionary
return timestamp
+
endfunction
+" Define pattern to match date timestamps; no Z̷͖̟̩͕̊̇̈ͬ̆̄Á̩̺͙̫͇͔̓͛ͭ̓̈́L̟͘G̤̣̳̊̌O̻̝̣̠͇̥̠͗ͤ͐͘ please
let s:pattern = '\m\C'
\.'Last updated: '
\.'<time datetime="[^"]\+">'
@@ -27,19 +42,30 @@ let s:pattern = '\m\C'
" Update a timestamp
function! html#timestamp#Update() abort
+
+ " Do nothing if the buffer hasn't been modified
if !&modified
return
endif
+
+ " Find the first occurrence of the timestamp pattern, bail if none
let lnum = search(s:pattern, 'nw')
if !lnum
return
endif
+
+ " Get timestamp dictionary
let timestamp = s:Timestamp(localtime())
+
+ " Fill out updated timestamp string with dictionary values
let update = 'Last updated: '
\.'<time datetime="'.timestamp['machine'].'">'
\.timestamp['human']
\.'</time>'
+
+ " Apply the updated timestamp
let line = getline(lnum)
let line = substitute(line, s:pattern, update, '')
call setline(lnum, line)
+
endfunction