Skip to content

Commit 50d722f

Browse files
authored
Merge pull request #28 from gyptazy/feature/27-blog-list-pagination
feature: Add pagination support to manpageblog
2 parents 88992fa + aa83b7c commit 50d722f

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The configuration file (default `blog.conf`) provides the following configuratio
6464
| author | admin | A default name for the author of blog posts |
6565
| copyright | manpageblog | name of a copyright holder |
6666
| preview_words | 150 (default) | How many words should be displayed within the blog index page |
67+
| pagination | 10 | How many words should be displayed within the blog index page |
6768
| logo_site | https://cdn.gyptazy.ch/images/manpageblog.jpg | URL to a default site logo (e.g. used for RSS) |
6869
| logo_favicon | https://cdn.gyptazy.ch/images/manpageblog.jpg | URL to a favicon image |
6970
| logo_apple_touch | https://cdn.gyptazy.ch/images/manpageblog.jpg | URL to an image for Apple Touch Icon (Webapp) |

_templates/list.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<b>{{ title }}</b>
22
<p></p>
33
{{ content }}
4+
5+
{{ pagination }}

blog.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ subtitle: manpageblog - a small and lightweight blog engine.
88
author: admin
99
copyright: manpageblog
1010
preview_words: 150
11+
pagination: 10
1112
logo_site: https://cdn.gyptazy.ch/images/manpageblog.jpg
1213
logo_favicon: https://cdn.gyptazy.ch/images/manpageblog.jpg
1314
logo_apple_touch: https://cdn.gyptazy.ch/images/manpageblog.jpg

manpageblog

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,48 @@ def make_list(config, posts, dst, list_layout, item_layout, **params):
227227
fwrite(dst_path, output)
228228

229229

230+
def paginate_list(config, posts, dst_template, list_layout, item_layout, per_page, **params):
231+
"""Generate paginated blog list pages"""
232+
total_pages = (len(posts) + per_page - 1) // per_page
233+
234+
for page_num in range(total_pages):
235+
start_idx = page_num * per_page
236+
end_idx = start_idx + per_page
237+
page_posts = posts[start_idx:end_idx]
238+
239+
items = []
240+
for post in page_posts:
241+
item_params = dict(params, **post)
242+
item_params['summary'] = truncate(post['content'], int(config['general'].get('preview_words', 150)))
243+
items.append(render(item_layout, **item_params))
244+
245+
page_params = dict(params)
246+
page_params['content'] = ''.join(items)
247+
page_params['current_page'] = page_num + 1
248+
page_params['total_pages'] = total_pages
249+
page_params['prev_page'] = page_num if page_num > 0 else None
250+
page_params['next_page'] = page_num + 2 if page_num + 1 < total_pages else None
251+
252+
253+
pagination_html = ""
254+
if page_params['prev_page']:
255+
prev_link = "index.html" if page_params['prev_page'] == 1 else f"page{page_params['prev_page']}.html"
256+
pagination_html += f'<a href="{prev_link}">Previous</a> '
257+
if page_params['next_page']:
258+
pagination_html += f'<a href="page{page_params["next_page"]}.html">Next</a>'
259+
260+
page_params['pagination'] = pagination_html
261+
262+
if page_num == 0:
263+
dst_path = render(dst_template, **page_params)
264+
else:
265+
dst_path = dst_template.replace("index.html", f"page{page_num+1}.html")
266+
267+
output = render(list_layout, **page_params)
268+
log('Rendering list page {} => {} ...', page_num + 1, dst_path)
269+
fwrite(dst_path, output)
270+
271+
230272
def main():
231273
""" Run manpageblog """
232274
arguments = parse_arguments()
@@ -289,8 +331,10 @@ def main():
289331
# Create blogs
290332
blog_posts = make_pages('content/blog/*.md', config['processing']['output_path']+'/blog/{{ slug }}/index.html', post_layout, blog='blog', **params)
291333

292-
# Create blog list pages
293-
make_list(config, blog_posts, config['processing']['output_path']+'/blog/index.html', list_layout, item_layout, blog='blog', title='Blog', **params)
334+
# Create paginated blog list pages (3 posts per page)
335+
paginate_list(config, blog_posts, config['processing']['output_path']+'/blog/index.html',
336+
list_layout, item_layout, per_page=int(config['general'].get('pagination', 10)), blog='blog', title='Blog', **params)
337+
294338

295339
# Create RSS feeds
296340
make_list(config, blog_posts, config['processing']['output_path']+'/blog/rss.xml', feed_xml, item_xml, blog='blog', title='Blog', **params)

0 commit comments

Comments
 (0)