commit 71b67ef710e93c5d1be31f5c293556e77d869ce0
parent abc8d6fb7a85667cb80e6e7f5d8cce070e572bd7
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Thu, 25 Mar 2021 11:22:00 +0100

Changed feed and HTML list generation

More information is gathered from the content files and the functions
have been united. Although they are two different features, since an
HTML list always has a feed (and viceversa), this makes the function
call clearer and the function code is not that complex anyway.

Diffstat:
Mcontent/blog/_index.md | 3+--
Mcontent/blog/categories/cryptography.md | 3++-
Mcontent/blog/categories/decentralization.md | 3++-
Mcontent/blog/categories/foss.md | 3++-
Mcontent/blog/categories/miscellany.md | 3++-
Mcontent/blog/categories/personal-domain.md | 3++-
Mcontent/blog/categories/privacy.md | 3++-
Mcontent/blog/categories/projects.md | 3++-
Mcontent/blog/categories/self-hosting.md | 3++-
Mgensite.py | 96++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlayouts/feed.xml | 2+-
Mlayouts/list.html | 1+
12 files changed, 67 insertions(+), 59 deletions(-)

diff --git a/content/blog/_index.md b/content/blog/_index.md @@ -1,7 +1,6 @@ <!-- title: Personal blog --> <!-- description: My personal blog. Subscribe to my web feed if you want to keep up to date! --> - -# Personal blog +<!-- feed_title: Oscar's Blog --> This is my personal blog. You can subscribe to [my feed][] or look through all the posts on [the archive][]. You can find links to other blogs I follow on [my diff --git a/content/blog/categories/cryptography.md b/content/blog/categories/cryptography.md @@ -1 +1,2 @@ -# Category: Cryptography +<!-- title: Category: Cryptography --> +<!-- feed_title: Cryptography on Oscar's Blog --> diff --git a/content/blog/categories/decentralization.md b/content/blog/categories/decentralization.md @@ -1 +1,2 @@ -# Category: Decentralization +<!-- title: Category: Decentralization --> +<!-- feed_title: Decentralization on Oscar's Blog --> diff --git a/content/blog/categories/foss.md b/content/blog/categories/foss.md @@ -1 +1,2 @@ -# Category: FOSS +<!-- title: Category: FOSS --> +<!-- feed_title: FOSS on Oscar's Blog --> diff --git a/content/blog/categories/miscellany.md b/content/blog/categories/miscellany.md @@ -1 +1,2 @@ -# Category: Miscellany +<!-- title: Category: Miscellany --> +<!-- feed_title: Miscellany on Oscar's Blog --> diff --git a/content/blog/categories/personal-domain.md b/content/blog/categories/personal-domain.md @@ -1 +1,2 @@ -# Category: Personal domain +<!-- title: Category: Personal domain --> +<!-- feed_title: Personal domain on Oscar's Blog --> diff --git a/content/blog/categories/privacy.md b/content/blog/categories/privacy.md @@ -1 +1,2 @@ -# Category: Privacy +<!-- title: Category: Privacy --> +<!-- feed_title: Privacy on Oscar's Blog --> diff --git a/content/blog/categories/projects.md b/content/blog/categories/projects.md @@ -1 +1,2 @@ -# Category: Projects +<!-- title: Category: Projects --> +<!-- feed_title: Projects on Oscar's Blog --> diff --git a/content/blog/categories/self-hosting.md b/content/blog/categories/self-hosting.md @@ -1 +1,2 @@ -# Category: Self-hosting +<!-- title: Category: Self-hosting --> +<!-- feed_title: Self-hosting on Oscar's Blog --> diff --git a/gensite.py b/gensite.py @@ -244,63 +244,69 @@ def make_pages(src, dst, layout, blog=False, **params): return items, categories -def make_lists(posts, dst, list_layout, item_layout, src=None, **params): - """Generate HTML lists for a blog.""" - item_per_page = 5 - items = [] - count = 1 - page_dst = dst - text = fread(src) if src else fread('content/' + dst + '_index.md') +def make_lists(posts, dst, l_html, l_html_item, l_feed, l_feed_item, **params): + """Generate HTML lists and Atom feed for a set of posts.""" + if os.path.isfile('content/' + dst + '_index.md'): + text = fread('content/' + dst + '_index.md') + else: + text = fread('content/' + dst[:-1] + '.md') end = 0 + for key, val, end in read_headers(text): params[key] = val + params['intro'] = markdown.markdown(text[end:], extensions=['footnotes', 'fenced_code']) + + # make HTML lists + ipp = 5 # items per page + params['content'] = '' + if dst != 'blog/': # blog feed appears on all pages already + params['extraheader'] = '<link rel="alternate" type="application/atom+xml" ' \ + 'title="{}" href="/{}index.xml"/>'.format(params['feed_title'], dst) + for i, post in enumerate(posts): item_params = dict(params, **post) # remove tags and truncate at 50 words item_params['summary'] = ' '.join(re.sub('(?s)<.*?>', '', post['content']).split()[:50]) + '...' - items.append(render(item_layout, **item_params)) - if i % item_per_page == item_per_page-1 and len(posts)-1 > i: - params['multiple_pages'] = '1' - params['content'] = ''.join(items) - params['next_url'] = dst + 'page/' + str(count+1) + '/' - if count != 1: - params['prev_url'] = dst + ('page/' + str(count-1) + '/' if count != 2 else '') - fwrite(page_dst, render(list_layout, **params)) - log('I', 'list => /{}', page_dst) - count = count+1 - page_dst = dst + 'page/' + str(count) + '/' - items = [] - - if count != 1: - del params['next_url'] - params['prev_url'] = dst + ('page/' + str(count-1) + '/' if count != 2 else '') - params['content'] = ''.join(items) - fwrite(page_dst, render(list_layout, **params)) - log('I', 'list => /{}', page_dst) + params['content'] += render(l_html_item, **item_params) - set_redirect(dst + 'page/1/', dst) + if i % ipp == ipp-1 or i == len(posts)-1: + page = i//ipp + 1 + curr_dst = dst + ('page/{}/'.format(page) if i >= ipp else '') + + if i != len(posts)-1: + params['multiple_pages'] = '1' + params['next_url'] = '{}page/{}/'.format(dst, page + 1) + elif page > 1: + params.pop('next_url') + + fwrite(curr_dst, render(l_html, **params)) + log('I', 'list => /{}', curr_dst) + params['prev_url'] = curr_dst + params['content'] = '' -def make_feed(posts, dst, list_layout, item_layout, **params): - """Generate feed for a blog.""" - max = 15 + set_redirect(dst + 'page/1/', dst) + + # make Atom feed + ipp = 15 # item per feed params['url'] = dst page_dst = dst + 'index.xml' - items = [] + params['content'] = '' for i, post in enumerate(posts): - if (i == max): + if (i == ipp): break item_params = dict(params, **post) + + # escape HTML content item_params['c_escaped'] = post['content'].replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;') - item = render(item_layout, **item_params) - items.append(item) - params['content'] = ''.join(items) + params['content'] += render(l_feed_item, **item_params) + params['updated'] = posts[0]['lastmod'] - fwrite(page_dst, render(list_layout, **params)) + fwrite(page_dst, render(l_feed, **params)) log('I', 'feed => /{}', page_dst) @@ -389,25 +395,19 @@ def main(): all_posts, categories = make_pages('content/blog/[!_]*.*', 'blog/{{ year }}/{{ month }}/{{ slug }}/', l_post, blog=True, **params) - # create HTML list pages - make_lists(all_posts, 'blog/', l_list, item_html, **params) + + # create HTML list pages and Atom feed + make_lists(all_posts, 'blog/', l_list, item_html, l_feed, item_xml, **params) + add_to_sitemap('blog/', lastmod=all_posts[0]['lastmod'], priority='1.0') - # create Atom feeds - make_feed(all_posts, 'blog/', l_feed, item_xml, title='Personal blog', - long_title='Oscar\'s Blog', **params) + # create blog archive make_archive(all_posts, categories, 'blog/', l_page, title='Blog archive', **params) # create blog categories for name, c_posts in categories.items(): dst = 'blog/categories/' + urlize(name) + '/' - src = 'content/blog/categories/' + urlize(name) + '.md' - lt = name + ' on Oscar\'s Blog' - eh = '<link rel="alternate" type="application/atom+xml" title="' + lt + '" href="/' + dst + 'index.xml"/>' - make_lists(c_posts, dst, l_list, item_html, src=src, title=name, - extraheader=eh, **params) - make_feed(c_posts, dst, l_feed, item_xml, title=name, long_title=lt, - **params) + make_lists(c_posts, dst, l_list, item_html, l_feed, item_xml, **params) # set redirections set_redirect('licenses/agpl-v3/', 'licenses/agpl-3.0.txt') diff --git a/layouts/feed.xml b/layouts/feed.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> - <title>{{ long_title }}</title> + <title>{{ feed_title }}</title> <link href="https://oscarbenedito.com/{{ url }}index.xml" rel="self"/> <link href="https://oscarbenedito.com/{{ url }}" rel="alternate"/> <updated>{{ updated }}</updated> diff --git a/layouts/list.html b/layouts/list.html @@ -1,5 +1,6 @@ <main class="list h-feed"> <header> + <h1>{{ title }}</h1> {{ intro }} </header>