Skip to content

Commit

Permalink
feat: improve toc filetype spec
Browse files Browse the repository at this point in the history
refer: #346
  • Loading branch information
lervag committed Apr 18, 2024
1 parent 928ae30 commit 795944a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
13 changes: 0 additions & 13 deletions autoload/wiki/paths.vim
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,6 @@ endfunction

" }}}1

function! wiki#paths#get_filetype(path) abort " {{{1
return get(#{
\ md: 'markdown',
\ adoc: 'asciidoc',
\ wiki: 'wiki',
\ org: 'org',
\},
\ fnamemodify(a:path, ':e'),
\ !empty(&filetype) ? &filetype : 'wiki')
endfunction

" }}}1

let s:cd = haslocaldir()
\ ? 'lcd'
\ : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'
Expand Down
20 changes: 13 additions & 7 deletions autoload/wiki/toc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"

function! wiki#toc#create(local) abort " {{{1
let l:filetype = s:get_filetype()

try
let l:filetype = !empty(&filetype) ? &filetype : 'wiki'
call s:toc_create_{l:filetype}(a:local)
catch /E117:/
call wiki#log#error("No TOC support for filetype: " . &filetype . "!")
call wiki#log#error("No TOC support for filetype: " . l:filetype . "!")
endtry
endfunction

Expand Down Expand Up @@ -49,7 +50,7 @@ function! wiki#toc#gather_entries(...) abort " {{{1
\ url: '',
\ path: '',
\ lines: [],
\ filetype: !empty(&filetype) ? &filetype : 'wiki',
\ filetype: s:get_filetype(),
\ first_only: v:false,
\ at_lnum: -1,
\}, a:0 > 0 ? a:1 : {})
Expand All @@ -75,7 +76,7 @@ function! wiki#toc#gather_entries(...) abort " {{{1
let l:current.ftime = l:ftime
let l:current.toc = wiki#toc#gather_entries_from_lines(
\ readfile(l:path),
\ wiki#paths#get_filetype(l:path))
\ s:get_filetype(l:path))
endif
call l:cache.write()

Expand Down Expand Up @@ -368,12 +369,17 @@ function! s:toc_create_markdown(local) abort " {{{1
endfunction

" }}}1
function! s:toc_create_wiki(local) abort " {{{1
call s:toc_create_markdown(a:local)

function! s:get_filetype(...) abort " {{{1
let l:ext = a:0 > 0 ? fnamemodify(a:1, ':e') : expand('%:e')
if empty(l:ext)
let l:ext = g:wiki_filetypes[0]
endif

return get(g:wiki_toc_filetypes, l:ext, g:wiki_toc_filetypes._)
endfunction

" }}}1

function! s:get_local_toc(entries, lnum_current) abort " {{{1
" Get ToC for the section for lnum_current
"
Expand Down
53 changes: 39 additions & 14 deletions doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ the root is set to the same directory as the current file. In addition, one
may specify a "main wiki" with the |g:wiki_root| option. This allows
convenient mappings and commands for opening this main wiki from anywhere.

*wiki-filetype-specification*
`wiki.vim` uses the filename extension to specify a filetype. Notice that
a file extension is not always named the same as the corresponding 'filetype'
option value. For example, the common extension for Markdown is `.md`, whereas
the corresponding 'filetype' is `markdown`.

------------------------------------------------------------------------------
REQUIREMENTS *wiki-intro-requirements*

Expand Down Expand Up @@ -772,10 +778,8 @@ a reference of all available options.
\}

*g:wiki_filetypes*
List of filetype extensions for which `wiki.vim` should be enabled. Notice
that file extensions are not always the same as the filetype. For example,
the common extension for Markdown is `.md`, whereas the filetype is called
`markdown`. The option should list the file extensions.
List of filetypes for which `wiki.vim` should be enabled. As explained in
|wiki-filetype-spec|, the filetypes are specified by the file extension.

The first element of the list is considered the default filetype where that
is relevant.
Expand Down Expand Up @@ -1032,10 +1036,8 @@ a reference of all available options.
\}

*g:wiki_link_creation*
A dictionary to configure how links are created from text for any given
filetype. Filetypes are specified by file extension so as to correspond with
|g:wiki_filetypes|, that is, we use `md` and not `markdown`.

A dictionary to configure how links are created from text for a given
filetype. The keys are filetype extensions (|wiki-filetype-specification|).
The corresponding value is a dictionary of options. If there is no key for
the current filetype, then the fallback key `"_"` is used.

Expand Down Expand Up @@ -1109,8 +1111,8 @@ a reference of all available options.
definition of URLs and schemes.

The keys represent the link type and the values are either strings, in which
case they specify the default scheme, or dictionaries between filetype
extensions and default schemes.
case they specify the default scheme, or dictionaries between filetypes
(notice |wiki-filetype-specification|) and default schemes.

Note: Reference-style links are special and do not allow to override the
scheme. See |wiki-link-reference|.
Expand Down Expand Up @@ -1316,11 +1318,33 @@ a reference of all available options.

Default: `''`

*g:wiki_toc_filetypes*
A dictionary to map a filetype extension to a filetype used to parse the TOC.
The supported filetype formats are currently:

* markdown
* asciidoc
* org

Default: >vim

let g:wiki_toc_filetypes = {
\ '_' : 'markdown',
\ 'md' : 'markdown',
\ 'adoc' : 'asciidoc',
\ 'org' : 'org',
\}

*g:wiki_toc_title*
The title of TOC listings.

Default: `'Contents'`

*g:wiki_toc_depth*
The number of sections to parse for the TOC.

Default: `6

*g:wiki_tag_list*
A dictionary that specifies the default options for |WikiTagList|.

Expand Down Expand Up @@ -1521,10 +1545,11 @@ a reference of all available options.
\}

*g:wiki_viewer*
A dictionary that specifies which viewer to use for a given filetype. The
entry `_` specifies the fallback or generic viewer. This option thus allows
one to setup different viewers for different file types that are used by the
generic link scheme handler (|g:wiki_link_schemes|) and by |WikiExport|.
A dictionary that specifies which viewer to use for any given filetype, see
|wiki-filetype-specification|. The entry `_` specifies the fallback or
generic viewer. This option thus allows one to setup different viewers for
different file types that are used by the generic link scheme handler
(|g:wiki_link_schemes|) and by |WikiExport|.

The special value `':edit'` indicates to open the file in the current Vim or
neovim session as with |:edit|.
Expand Down
6 changes: 6 additions & 0 deletions plugin/wiki.vim
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ call wiki#init#option('wiki_template_title_month',
\ '# Summary, %(year) %(month-name)')
call wiki#init#option('wiki_template_title_week',
\ '# Summary, %(year) week %(week)')
call wiki#init#option('wiki_toc_filetypes', {
\ '_' : 'markdown',
\ 'md' : 'markdown',
\ 'adoc' : 'asciidoc',
\ 'org' : 'org',
\})
call wiki#init#option('wiki_toc_title', 'Contents')
call wiki#init#option('wiki_toc_depth', 6)
call wiki#init#option('wiki_ui_method', {
Expand Down

0 comments on commit 795944a

Please sign in to comment.