10BC0 Added optional extra caption levels to diary index. · vimwiki/vimwiki@8f5d383 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f5d383

Browse files
edlangloishq6
authored andcommitted
Added optional extra caption levels to diary index.
Added the option 'diary_caption_level', which controls the presence of captions in the diary index linking to headers within the diary pages. Possible values: -1: No headers are read from the diary page. 0: The first header from the diary page is used as the caption. There are no sub-captions. 1: Captions are created for headers of level 1 in the diary page. 2: Captions are created for headers up to level 2 in the diary page. etc. When the value is >= 1, the primary caption of each diary page is set to the first header read from that page if it is the unique lowest-level header.
1 parent 7d82c75 commit 8f5d383

File tree

3 files changed

+131
-21
lines changed

3 files changed

+131
-21
lines changed

autoload/vimwiki/diary.vim

Lines changed: 105 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,98 @@ function! s:get_month_name(month)
6363
return vimwiki#vars#get_global('diary_months')[str2nr(a:month)]
6464
endfunction
6565

66+
function! s:get_first_header(fl)
67+
" Get the first header in the file within the first s:vimwiki_max_scan_for_caption lines.
68+
let header_rx = vimwiki#vars#get_syntaxlocal('rxHeader')
69+
70+
for line in readfile(a:fl, '', s:vimwiki_max_scan_for_caption)
71+
if line =~# header_rx
72+
return vimwiki#u#trim(matchstr(line, header_rx))
73+
endif
74+
endfor
75+
return ''
76+
endfunction
77+
78+
function! s:get_all_headers(fl, maxlevel)
79+
" Get a list of all headers in a file up to a given level.
80+
" Returns a list whose elements are pairs [level, title]
81+
let headers_rx = {}
82+
for i in range(1, a:maxlevel)
83+
let headers_rx[i] = vimwiki#vars#get_syntaxlocal('rxH'.i.'_Text')
84+
endfor
85+
86+
let headers = []
87+
for line in readfile(a:fl, '')
88+
for [i, header_rx] in items(headers_rx)
89+
if line =~# header_rx
90+
call add(headers, [i, vimwiki#u#trim(matchstr(line, header_rx))])
91+
break
92+
endif
93+
endfor
94+
endfor
95+
return headers
96+
endfunction
97+
98+
function! s:count_headers_level_less_equal(headers, maxlevel)
99+
" Count headers with level <= maxlevel in a list of [level, title] pairs.
100+
let l:count = 0
101+
for [header_level, _] in a:headers
102+
if header_level <= a:maxlevel
103+
let l:count += 1
104+
endif
105+
endfor
106+
return l:count
107+
endfunction
108+
109+
function! s:get_min_header_level(headers)
110+
" The minimum level of any header in a list of [level, title] pairs.
111+
if len(a:headers) == 0
112+
return 0
113+
endif
114+
let minlevel = a:headers[0][0]
115+
for [level, _] in a:headers
116+
let minlevel = min([minlevel, level])
117+
endfor
118+
return minlevel
119+
endfunction
120+
66121

67122
function! s:read_captions(files)
68123
let result = {}
69-
let rx_header = vimwiki#vars#get_syntaxlocal('rxHeader')
124+
let caption_level = vimwiki#vars#get_wikilocal('diary_caption_level')
125+
70126
for fl in a:files
71127
" remove paths and extensions
72-
let fl_key = substitute(fnamemodify(fl, ':t'), vimwiki#vars#get_wikilocal('ext').'$', '', '')
128+
let fl_captions = {}
129+
130+
" Default; no captions from the file.
131+
let fl_captions['top'] = ''
132+
let fl_captions['rest'] = []
133+
134+
if caption_level >= 0 && filereadable(fl)
135+
if caption_level == 0
136+
" Take first header of any level as the top caption.
137+
let fl_captions['top'] = s:get_first_header(fl)
138+
else
139+
let headers = s:get_all_headers(fl, caption_level)
140+
if len(headers) > 0
141+
" If first header is the only one at its level or less, then make it the top caption.
142+
let [first_level, first_header] = headers[0]
143+
if s:count_headers_level_less_equal(headers, first_level) == 1
144+
let fl_captions['top'] = first_header
145+
call remove(headers, 0)
146+
endif
73147

74-
if filereadable(fl)
75-
for line in readfile(fl, '', s:vimwiki_max_scan_for_caption)
76-
if line =~# rx_header && !has_key(result, fl_key)
77-
let result[fl_key] = vimwiki#u#trim(matchstr(line, rx_header))
148+
let min_header_level = s:get_min_header_level(headers)
149+
for [level, header] in headers
150+
call add(fl_captions['rest'], [level - min_header_level, header])
151+
endfor
78152
endif
79-
endfor
80-
endif
81-
82-
if !has_key(result, fl_key)
83-
let result[fl_key] = ''
153+
endif
84154
endif
85155

156+
let fl_key = substitute(fnamemodify(fl, ':t'), vimwiki#vars#get_wikilocal('ext').'$', '', '')
157+
let result[fl_key] = fl_captions
86158
endfor
87159
return result
88160
endfunction
@@ -149,22 +221,36 @@ function! s:format_diary()
149221
call add(result, substitute(vimwiki#vars#get_syntaxlocal('rxH3_Template'),
150222
\ '__Header__', s:get_month_name(month), ''))
151223

152-
for [fl, cap] in s:sort(items(g_files[year][month]))
224+
for [fl, captions] in s:sort(items(g_files[year][month]))
225+
let topcap = captions['top']
153226
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate2')
154227

155228
if vimwiki#vars#get_wikilocal('syntax') == 'markdown'
156229
let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template')
157230

158-
if empty(cap) " When using markdown syntax, we should ensure we always have a link description.
159-
let cap = fl
231+
if empty(topcap) " When using markdown syntax, we should ensure we always have a link description.
232+
let topcap = fl
160233
endif
161-
elseif empty(cap)
162-
let link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
163234
endif
164235

165-
let entry = substitute(link_tpl, '__LinkUrl__', fl, '')
166-
let entry = substitute(entry, '__LinkDescription__', cap, '')
236+
if empty(topcap)
237+
let top_link_tpl = vimwiki#vars#get_global('WikiLinkTemplate1')
238+
else
239+
let top_link_tpl = link_tpl
240+
endif
241+
242+
let entry = substitute(top_link_tpl, '__LinkUrl__', fl, '')
243+
let entry = substitute(entry, '__LinkDescription__', topcap, '')
167244
call add(result, repeat(' ', vimwiki#lst#get_list_margin()).'* '.entry)
245+
246+
for [depth, subcap] in captions['rest']
247+
if empty(subcap)
248+
continue
249+
endif
250+
let entry = substitute(link_tpl, '__LinkUrl__', fl.'#'.subcap, '')
251+
let entry = substitute(entry, '__LinkDescription__', subcap, '')
252+
call add(result, repeat(' ', vimwiki#lst#get_list_margin() * (2 + depth)).'- '.entry)
253+
endfor
168254
endfor
169255

170256
endfor
@@ -285,7 +371,7 @@ function! vimwiki#diary#generate_diary_section()
285371
let current_file = vimwiki#path#path_norm(expand("%:p"))
286372
let diary_file = vimwiki#path#path_norm(s:diary_index())
287373
if vimwiki#path#is_equal(current_file, diary_file)
288-
let content_rx = '^\%(\s*\* \)\|\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxHeader').'\)'
374+
let content_rx = '^\%(\s*[*-] \)\|\%(^\s*$\)\|\%('.vimwiki#vars#get_syntaxlocal('rxHeader').'\)'
289375
call vimwiki#base#update_listing_in_buffer(s:format_diary(),
290376
\ vimwiki#vars#get_wikilocal('diary_header'), content_rx, line('$')+1, 1, 1)
291377
else
@@ -324,4 +410,3 @@ function vimwiki#diary#calendar_sign(day, month, year)
324410
\ a:year.'-'.month.'-'.day.vimwiki#vars#get_wikilocal('ext')
325411
return filereadable(expand(sfile))
326412
endfunction
327-

autoload/vimwiki/vars.vim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ function! s:populate_wikilocal_options()
254254
\ 'diary_header': {'type': type(''), 'default': 'Diary', 'min_length': 1},
255255
\ 'diary_index': {'type': type(''), 'default': 'diary', 'min_length': 1},
256256
\ 'diary_rel_path': {'type': type(''), 'default': 'diary/', 'min_length': 1},
257+
\ 'diary_caption_level': {'type': type(0), 'default': 0, 'min': -1, 'max': 6},
257258
\ 'diary_sort': {'type': type(''), 'default': 'desc', 'possible_values': ['asc', 'desc']},
258259
\ 'ext': {'type': type(''), 'default': '.wik C02E i', 'min_length': 1},
259260
\ 'index': {'type': type(''), 'default': 'index', 'min_length': 1},
@@ -444,6 +445,9 @@ function! vimwiki#vars#populate_syntax_vars(syntax)
444445
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i] =
445446
\ '^\s*'.header_symbol.'\{'.i.'}[^'.header_symbol.'].*[^'.header_symbol.']'
446447
\ .header_symbol.'\{'.i.'}\s*$'
448+
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i.'_Text'] =
449+
\ '^\s*'.header_symbol.'\{'.i.'}\zs[^'.header_symbol.'].*[^'.header_symbol.']\ze'
450+
\ .header_symbol.'\{'.i.'}\s*$'
447451
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i.'_Start'] =
448452
\ '^\s*'.header_symbol.'\{'.i.'}[^'.header_symbol.'].*[^'.header_symbol.']'
449453
\ .header_symbol.'\{'.i.'}\s*$'
@@ -460,6 +464,8 @@ function! vimwiki#vars#populate_syntax_vars(syntax)
460464
\ repeat(header_symbol, i).' __Header__'
461465
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i] =
462466
\ '^\s*'.header_symbol.'\{'.i.'}[^'.header_symbol.'].*$'
467+
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i.'_Text'] =
468+
\ '^\s*'.header_symbol.'\{'.i.'}\zs[^'.header_symbol.'].*\ze$'
463469
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i.'_Start'] =
464470
\ '^\s*'.header_symbol.'\{'.i.'}[^'.header_symbol.'].*$'
465471
let g:vimwiki_syntax_variables[a:syntax]['rxH'.i.'_End'] =
@@ -857,4 +863,3 @@ endfunction
857863
function! vimwiki#vars#number_of_wikis()
858864
return len(g:vimwiki_wikilocal_vars) - 1
859865
endfunction
860-

doc/vimwiki.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,26 @@ Description~
22442244
Sort links in a diary index page.
22452245

22462246

2247+
*vimwiki-option-diary_caption_level*
2248+
------------------------------------------------------------------------------
2249+
Key Default value~
2250+
diary_caption_level 0
2251+
2252+
Description~
2253+
Controls the presence of captions in the diary index linking to headers within
2254+
the diary pages.
2255+
2256+
Possible values:
2257+
-1: No headers are read from the diary page.
2258+
0: The first header from the diary page is used as the caption.
2259+
There are no sub-captions.
2260+
1: Captions are created for headers of level 1 in the diary page.
2261+
2: Captions are created for headers up to level 2 in the diary page.
2262+
etc.
2263+
2264+
When the value is >= 1, the primary caption of each diary page is set to the
2265+
first header read from that page if it is the unique lowest-level header.
2266+
22472267
*vimwiki-option-custom_wiki2html*
22482268
------------------------------------------------------------------------------
22492269
Key Default value~

0 commit comments

Comments
 (0)
0