Update 2024-12-16 19:13 OpenBSD/amd64-t14
This commit is contained in:
21
.vim/pack/plugins/start/vim-hugo-helper/LICENSE
Normal file
21
.vim/pack/plugins/start/vim-hugo-helper/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Robert Basic
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
79
.vim/pack/plugins/start/vim-hugo-helper/README.md
Normal file
79
.vim/pack/plugins/start/vim-hugo-helper/README.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# vim-hugo-helper
|
||||
|
||||
A small Vim plugin to help me with writing posts with [Hugo](https://gohugo.io).
|
||||
|
||||
# Helpers
|
||||
|
||||
The following helpers are available:
|
||||
|
||||
## Draft
|
||||
|
||||
`:HugoHelperDraft` drafts the current post.
|
||||
|
||||
## Undraft
|
||||
|
||||
`:HugoHelperUndraft` undrafts the current post.
|
||||
|
||||
## Date is now
|
||||
|
||||
`:HugoHelperDateIsNow` sets the date and time of the current post to current date and time.
|
||||
|
||||
## Highlight
|
||||
|
||||
`:HugoHelperHighlight language` inserts the `highlight` block for "language" in to the current post.
|
||||
|
||||
## Link
|
||||
|
||||
`:HugoHelperLink` helps with adding links to a document. Visually select the word(s) you want to be a link, enter `:HugoHelperLink https://gohugo.io` and it will turn the selected word(s) to `[selected words](https://gohugo.io)`.
|
||||
|
||||

|
||||
|
||||
Probably works only for markdown documents.
|
||||
|
||||
## Spell check
|
||||
|
||||
`:HugoHelperSpellCheck` toggles the spell check for the current language. Running it once, turns the spell check on. Running it again, turns it off.
|
||||
|
||||
You can set the language by setting the following in your `.vimrc` file:
|
||||
|
||||
```
|
||||
let g:hugohelper_spell_check_lang = 'en_us'
|
||||
```
|
||||
|
||||
By default it is set to `en_us`.
|
||||
|
||||
## Title to slug
|
||||
|
||||
`:HugoHelperTitleToSlug` turns the title of the Hugo post to the slug. It assumes the following two lines are present in the frontmatter:
|
||||
|
||||
```
|
||||
+++
|
||||
// frontmatter
|
||||
title = "Title of the post"
|
||||
// frontmatter
|
||||
slug = ""
|
||||
// frontmatter
|
||||
+++
|
||||
```
|
||||
|
||||
It will turn it into:
|
||||
|
||||
```
|
||||
+++
|
||||
// frontmatter
|
||||
title = "Title of the post"
|
||||
// frontmatter
|
||||
slug = "title-of-the-post"
|
||||
// frontmatter
|
||||
+++
|
||||
```
|
||||
|
||||
# Installation
|
||||
|
||||
## [vim-plug](https://github.com/junegunn/vim-plug)
|
||||
|
||||
`Plug 'robertbasic/vim-hugo-helper'`
|
||||
|
||||
## [Vundle](https://github.com/VundleVim/Vundle.vim)
|
||||
|
||||
`PluginInstall 'robertbasic/vim-hugo-helper'`
|
||||
@@ -0,0 +1,99 @@
|
||||
function! hugohelper#SpellCheck()
|
||||
exe "setlocal spell! spelllang=" . g:hugohelper_spell_check_lang
|
||||
endfun
|
||||
|
||||
function! hugohelper#TitleToSlug()
|
||||
normal gg
|
||||
exe '/^title'
|
||||
normal! vi"y
|
||||
exe '/^slug'
|
||||
normal! f"pVu
|
||||
" Not sure why I can't make \%V work here
|
||||
exe ':s/ /-/g'
|
||||
exe 'normal! f-r f-r '
|
||||
endfun
|
||||
|
||||
function! hugohelper#TitleCase()
|
||||
normal gg
|
||||
exe '/^title'
|
||||
normal! vi"u~
|
||||
endfun
|
||||
|
||||
function! hugohelper#Draft()
|
||||
call s:set_key('draft', 'true')
|
||||
endfun
|
||||
|
||||
function! hugohelper#Undraft()
|
||||
call s:set_key('draft', 'false')
|
||||
endfun
|
||||
|
||||
function! hugohelper#DateIsNow()
|
||||
call s:set_key('date', s:hugo_now())
|
||||
endfun
|
||||
|
||||
function! hugohelper#LastmodIsNow()
|
||||
call s:set_key('lastmod', s:hugo_now())
|
||||
endfun
|
||||
|
||||
function! hugohelper#Highlight(language)
|
||||
normal! I{{< highlight language_placeholder >}}
|
||||
exe 's/language_placeholder/' . a:language . '/'
|
||||
normal! o{{< /highlight >}}
|
||||
endfun
|
||||
|
||||
function! hugohelper#Link(link)
|
||||
let l:selection = s:get_visual_selection()
|
||||
exe ':s/\%V\(.*\)\%V\(.\)/[\0]/'
|
||||
exe "normal! gv\ef]a(link_placeholder)\e"
|
||||
exe 's/link_placeholder/' . escape(a:link, '\\/.*^~[]') . '/'
|
||||
exe "normal! gv\ef)"
|
||||
endfun
|
||||
|
||||
function! hugohelper#HasFrontMatter()
|
||||
try
|
||||
call s:front_matter_format()
|
||||
return 1
|
||||
catch
|
||||
endtry
|
||||
return 0
|
||||
endfun
|
||||
|
||||
function! s:get_visual_selection()
|
||||
" From http://stackoverflow.com/a/6271254/794380
|
||||
let [lnum1, col1] = getpos("'<")[1:2]
|
||||
let [lnum2, col2] = getpos("'>")[1:2]
|
||||
let lines = getline(lnum1, lnum2)
|
||||
let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
|
||||
let lines[0] = lines[0][col1 - 1:]
|
||||
return join(lines, "\n")
|
||||
endfun
|
||||
|
||||
function! s:front_matter_format()
|
||||
let l:line = getline(1)
|
||||
if l:line =~ '+++'
|
||||
return 'toml'
|
||||
elseif l:line =~ '---'
|
||||
return 'yaml'
|
||||
else
|
||||
throw "Could not determine Hugo front matter format. Looking for +++ or ---. JSON not supported."
|
||||
endif
|
||||
endfun
|
||||
|
||||
function! s:set_key(key, value)
|
||||
let l:format = s:front_matter_format()
|
||||
if l:format == 'toml'
|
||||
exe '1;/+++/substitute/^' . a:key . '\s*=.*/' . a:key . ' = ' . a:value
|
||||
elseif l:format == 'yaml'
|
||||
exe '1;/---/substitute/^' . a:key . '\s*:.*/' . a:key . ': ' . a:value
|
||||
else
|
||||
throw "Can't set key, value pair for unknown format " . l:format
|
||||
endif
|
||||
endfun
|
||||
|
||||
function! s:hugo_now()
|
||||
" Contrust a time string using the format: 2018-09-18T19:41:32-07:00
|
||||
let l:time = strftime("%FT%T%z")
|
||||
return l:time[:-3] . ':' . l:time[-2:]
|
||||
endfun
|
||||
|
||||
" vim: expandtab shiftwidth=4
|
||||
111
.vim/pack/plugins/start/vim-hugo-helper/plugin/hugohelper.vim
Normal file
111
.vim/pack/plugins/start/vim-hugo-helper/plugin/hugohelper.vim
Normal file
@@ -0,0 +1,111 @@
|
||||
if exists('g:hugohelper_plugin_loaded') || &cp
|
||||
finish
|
||||
endif
|
||||
let g:hugohelper_plugin_loaded = 1
|
||||
|
||||
let g:hugohelper_plugin_path = expand('<sfile>:p:h')
|
||||
|
||||
if !exists('g:hugohelper_spell_check_lang')
|
||||
let g:hugohelper_spell_check_lang = 'en_us'
|
||||
endif
|
||||
|
||||
if !exists('g:hugohelper_update_lastmod_on_write')
|
||||
let g:hugohelper_update_lastmod_on_write = 0
|
||||
endif
|
||||
|
||||
if !exists('g:hugohelper_content_dir')
|
||||
let g:hugohelper_content_dir = 'content'
|
||||
endif
|
||||
|
||||
if !exists('g:hugohelper_site_config')
|
||||
" List of site configuration files vim-hugo-helper uses to detemine
|
||||
" the root of the hugo site.
|
||||
" For more information, see: https://gohugo.io/getting-started/configuration/
|
||||
let g:hugohelper_site_config = [ 'config.toml', 'config.yaml', 'config.json' ]
|
||||
endif
|
||||
|
||||
|
||||
|
||||
command! -nargs=0 HugoHelperSpellCheck call hugohelper#SpellCheck()
|
||||
command! -nargs=0 HugoHelperDraft call hugohelper#Draft()
|
||||
command! -nargs=0 HugoHelperUndraft call hugohelper#Undraft()
|
||||
command! -nargs=0 HugoHelperDateIsNow call hugohelper#DateIsNow()
|
||||
command! -nargs=0 HugoHelperLastmodIsNow call hugohelper#LastmodIsNow()
|
||||
command! -nargs=0 HugoHelperTitleToSlug call hugohelper#TitleToSlug()
|
||||
command! -nargs=0 HugoHelperTitleCase call hugohelper#TitleCase()
|
||||
command! -nargs=1 HugoHelperHighlight call hugohelper#Highlight(<f-args>)
|
||||
command! -range -nargs=1 HugoHelperLink call hugohelper#Link(<f-args>)
|
||||
|
||||
function! HugoHelperFrontMatterReorder()
|
||||
exe 'g/^draft/m 1'
|
||||
exe 'g/^date/m 2'
|
||||
exe 'g/^title/m 3'
|
||||
exe 'g/^slug/m 4'
|
||||
exe 'g/^description/m 5'
|
||||
exe 'g/^tags/m 6'
|
||||
exe 'g/^categories/m 7'
|
||||
" create date taxonomy
|
||||
exe 'g/^date/co 8'
|
||||
exe ':9'
|
||||
exe ':s/.*\(\d\{4\}\)-\(\d\{2\}\).*/\1 = ["\2"]'
|
||||
endfun
|
||||
|
||||
augroup vim-hugo-helper
|
||||
autocmd BufWritePre *.md call s:UpdateLastMod()
|
||||
augroup end
|
||||
|
||||
" Update lastmod on save.
|
||||
function! s:UpdateLastMod()
|
||||
if s:ShouldUpdateLastMod()
|
||||
let save_cursor = getcurpos()
|
||||
call hugohelper#LastmodIsNow()
|
||||
call setpos('.', save_cursor)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:ShouldUpdateLastMod()
|
||||
if !g:hugohelper_update_lastmod_on_write
|
||||
return v:false
|
||||
endif
|
||||
|
||||
if hugohelper#HasFrontMatter() == 0
|
||||
return v:false
|
||||
endif
|
||||
|
||||
" Only update lastmod in markdown in the content directory. In particular, archetypes
|
||||
" should not be automatically updated.
|
||||
return s:IsFileInHugoContentDirectory(expand('<afile>'))
|
||||
endfunction
|
||||
|
||||
function! s:IsFileInHugoContentDirectory(filepath)
|
||||
let l:mods = ':p:h'
|
||||
let l:dirname = 'dummy'
|
||||
while !empty(l:dirname)
|
||||
let l:path = fnamemodify(a:filepath, l:mods)
|
||||
let l:mods .= ':h'
|
||||
let l:dirname = fnamemodify(l:path, ':t')
|
||||
if l:dirname == g:hugohelper_content_dir
|
||||
" Check if the parent of the content directory contains a config file.
|
||||
let l:parent = fnamemodify(l:path, ":h")
|
||||
if s:HasHugoConfigFile(l:parent)
|
||||
return v:true
|
||||
endif
|
||||
endif
|
||||
endwhile
|
||||
|
||||
return v:false
|
||||
endfunction
|
||||
|
||||
function! s:HasHugoConfigFile(dir)
|
||||
" :p adds the final path separator if a:dir is a directory.
|
||||
let l:dirpath = fnamemodify(a:dir, ':p')
|
||||
for config in g:hugohelper_site_config
|
||||
let l:file = l:dirpath . config
|
||||
if filereadable(l:file)
|
||||
return v:true
|
||||
endif
|
||||
endfor
|
||||
return v:false
|
||||
endfunction
|
||||
|
||||
" vim: expandtab shiftwidth=4
|
||||
Reference in New Issue
Block a user