Generate RSS feed for diary entries

This commit is contained in:
Reiner Herrmann 2020-07-05 17:54:46 +02:00 committed by Tinmarino
parent 0543b46ffd
commit e3501dcdd0
6 changed files with 141 additions and 3 deletions

View File

@ -2,6 +2,7 @@
<html>
<head>
<link rel="Stylesheet" type="text/css" href="%root_path%%css%">
<link rel="alternate" type="application/rss+xml" title="RSS" href="%root_path%%rss%">
<title>%title%</title>
<meta http-equiv="Content-Type" content="text/html; charset=%encoding%">
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

@ -262,6 +262,9 @@ function! s:sort(lst) abort
endif
endfunction
function! vimwiki#diary#diary_sort(lst) abort
return s:sort(a:lst)
endfunction
" Create note
" The given wiki number a:wnum is 1 for the first wiki, 2 for the second and so on. This is in
@ -504,3 +507,7 @@ function! vimwiki#diary#calendar_sign(day, month, year) abort
\ a:year.'-'.month.'-'.day.vimwiki#vars#get_wikilocal('ext')
return filereadable(expand(sfile))
endfunction
function! vimwiki#diary#diary_file_captions() abort
return s:read_captions(vimwiki#diary#get_diary_files())
endfunction

View File

@ -1574,7 +1574,7 @@ function! vimwiki#html#CustomWiki2HTML(path, wikifile, force) abort
endif
endfunction
function s:convert_file_to_lines(wikifile, current_html_file) abort
function! s:convert_file_to_lines(wikifile, current_html_file) abort
let result = {}
" the currently processed file name is needed when processing links
@ -1679,7 +1679,7 @@ function s:convert_file_to_lines(wikifile, current_html_file) abort
return result
endfunction
function s:convert_file_to_lines_template(wikifile, current_html_file) abort
function! s:convert_file_to_lines_template(wikifile, current_html_file) abort
let converted = s:convert_file_to_lines(a:wikifile, a:current_html_file)
if converted['nohtml'] == 1
return []
@ -1697,6 +1697,10 @@ function s:convert_file_to_lines_template(wikifile, current_html_file) abort
let css_name = substitute(css_name, '\', '/', 'g')
call map(html_lines, 'substitute(v:val, "%css%", "'. css_name .'", "g")')
let rss_name = expand(vimwiki#vars#get_wikilocal('rss_name'))
let rss_name = substitute(rss_name, '\', '/', 'g')
call map(html_lines, 'substitute(v:val, "%rss%", "'. rss_name .'", "g")')
let enc = &fileencoding
if enc ==? ''
let enc = &encoding
@ -1838,3 +1842,91 @@ endfunction
function! vimwiki#html#CatUrl(wikifile) abort
execute '!echo file://'.s:get_wikifile_url(a:wikifile)
endfunction
function! s:rss_header() abort
let title = vimwiki#vars#get_wikilocal('diary_header')
let rss_url = vimwiki#vars#get_wikilocal('base_url') . vimwiki#vars#get_wikilocal('rss_name')
let link = vimwiki#vars#get_wikilocal('base_url')
\ . vimwiki#vars#get_wikilocal('diary_rel_path')
\ . vimwiki#vars#get_wikilocal('diary_index') . '.html'
let description = title
let pubdate = strftime('%a, %d %b %Y %T %z')
let header = [
\ '<?xml version="1.0" encoding="UTF-8" ?>',
\ '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">',
\ '<channel>',
\ ' <title>' . title . '</title>',
\ ' <link>' . link . '</link>',
\ ' <description>' . description . '</description>',
\ ' <pubDate>' . pubdate . '</pubDate>',
\ ' <atom:link href="' . rss_url . '" rel="self" type="application/rss+xml" />'
\ ]
return header
endfunction
function! s:rss_footer() abort
let footer = ['</channel>', '</rss>']
return footer
endfunction
function! s:rss_item(path, title) abort
let diary_rel_path = vimwiki#vars#get_wikilocal('diary_rel_path')
let full_path = vimwiki#vars#get_wikilocal('path')
\ . diary_rel_path . a:path . vimwiki#vars#get_wikilocal('ext')
let fname_base = fnamemodify(a:path, ':t:r')
let htmlfile = fname_base . '.html'
let converted = s:convert_file_to_lines(full_path, htmlfile)
if converted['nohtml'] == 1
return []
endif
let link = vimwiki#vars#get_wikilocal('base_url')
\ . diary_rel_path
\ . fname_base . '.html'
let pubdate = strftime('%a, %d %b %Y %T %z', getftime(full_path))
let item_pre = [' <item>',
\ ' <title>' . a:title . '</title>',
\ ' <link>' . link . '</link>',
\ ' <guid isPermaLink="false">' . fname_base . '</guid>',
\ ' <description><![CDATA[']
let item_post = [']]></description>',
\ ' <pubDate>' . pubdate . '</pubDate>',
\ ' </item>'
\]
return item_pre + converted['html'] + item_post
endfunction
function! s:generate_rss(path) abort
let rss_path = a:path . vimwiki#vars#get_wikilocal('rss_name')
let max_items = vimwiki#vars#get_wikilocal('rss_max_items')
let rss_lines = []
call extend(rss_lines, s:rss_header())
let captions = vimwiki#diary#diary_file_captions()
let i = 0
for diary in vimwiki#diary#diary_sort(keys(captions))
if i >= max_items
break
endif
let title = captions[diary]['top']
if title ==? ''
let title = diary
endif
call extend(rss_lines, s:rss_item(diary, title))
let i += 1
endfor
call extend(rss_lines, s:rss_footer())
call writefile(rss_lines, rss_path)
endfunction
function! vimwiki#html#diary_rss() abort
echomsg 'Vimwiki: Saving RSS feed ...'
let path_html = expand(vimwiki#vars#get_wikilocal('path_html'))
call vimwiki#path#mkdir(path_html)
call s:generate_rss(path_html)
endfunction

View File

@ -360,6 +360,7 @@ function! s:populate_wikilocal_options() abort
\ 'auto_tags': {'type': type(0), 'default': 0, 'min': 0, 'max': 1},
\ 'auto_toc': {'type': type(0), 'default': 0, 'min': 0, 'max': 1},
\ 'automatic_nested_syntaxes': {'type': type(0), 'default': 1, 'min': 0, 'max': 1},
\ 'base_url': {'type': type(''), 'default': '', 'min_length': 1},
\ 'css_name': {'type': type(''), 'default': 'style.css', 'min_length': 1},
\ 'custom_wiki2html': {'type': type(''), 'default': ''},
\ 'custom_wiki2html_args': {'type': type(''), 'default': ''},
@ -380,6 +381,8 @@ function! s:populate_wikilocal_options() abort
\ 'nested_syntaxes': {'type': type({}), 'default': {}},
\ 'path': {'type': type(''), 'default': $HOME . '/vimwiki/', 'min_length': 1},
\ 'path_html': {'type': type(''), 'default': ''},
\ 'rss_max_items': {'type': type(0), 'default': 10, 'min': 0},
\ 'rss_name': {'type': type(''), 'default': 'rss.xml', 'min_length': 1},
\ 'syntax': {'type': type(''), 'default': 'default',
\ 'possible_values': ['default', 'markdown', 'media', 'mediawiki']},
\ 'template_default': {'type': type(''), 'default': 'default', 'min_length': 1},

View File

@ -829,6 +829,9 @@ Vimwiki file.
converted or have been modified since their last conversion. With !,
convert all pages, regardless of whether or not they are out-of-date.
*:VimwikiRss*
Generate an RSS feed for the latest diary entries.
*:VimwikiToggleListItem*
Toggle checkbox of a list item on/off.
See |vimwiki-todo-lists|.
@ -1617,7 +1620,7 @@ If you do not want a wiki page to be converted to HTML, place:
%nohtml
into it.
into it. This will also prevent inclusion in the RSS feed.
------------------------------------------------------------------------------
@ -2693,6 +2696,34 @@ regeneration of HTML files.
This setting also turns off the automatic deletion of files
in the site_html directory which don't match existing wiki files.
*vimwiki-option-rss_name*
------------------------------------------------------------------------------
Key Default value~
rss_name rss.xml
Description~
Setup RSS file name: >
let g:vimwiki_list = [{'path': '~/my_pages/', 'rss_name': 'feed.rss'}]
<
Vimwiki will generate an RSS feed for the diary in this file.
*vimwiki-option-rss_max_items*
------------------------------------------------------------------------------
Key Default value~
rss_max_items 10
Description~
This setting limits the number of diary entries exported into the RSS feed.
*vimwiki-option-base_url*
------------------------------------------------------------------------------
Key Default value~
base_url ''
Description~
This setting specifies the URL where the generated VimWiki HTML pages can
be reached. It is used for the link to the RSS feed and for links to the
diary entries inside the feed.
------------------------------------------------------------------------------
12.4 Global Options *vimwiki-global-options*
@ -3642,6 +3673,7 @@ Contributors and their Github usernames in roughly chronological order:
- Marius Lopez (@PtitCaius)
- Edward Bassett (@ebassett)
- Rafael Castillo (@eltrufas)
- Reiner Herrmann (@reinerh)
==============================================================================
16. Changelog *vimwiki-changelog*
@ -3658,6 +3690,7 @@ New:~
* PR #907: Cycle bullets
* PR #900: conceallevel is now setted locally for vimwiki buffers
* PR #901: adds multiparagraph blockquotes using email style syntax
* PR #934: RSS feed generation for diary with :VimwikiRss.
2.5 (2020-05-26)~

View File

@ -242,6 +242,8 @@ command! -buffer Vimwiki2HTMLBrowse
command! -buffer -bang VimwikiAll2HTML
\ call vimwiki#html#WikiAll2HTML(expand(vimwiki#vars#get_wikilocal('path_html')), <bang>0)
command! -buffer VimwikiRss call vimwiki#html#diary_rss()
command! -buffer VimwikiTOC call vimwiki#base#table_of_contents(1)
command! -buffer VimwikiNextTask call vimwiki#base#find_next_task()