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},