From 88e97b7d6476c9a08b007bb70baa81c3105bc705 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquemin Date: Sat, 22 Aug 2020 17:08:55 +0200 Subject: [PATCH] First commit. --- COPYING | 19 +++++++++ README.md | 10 +++++ jcm/pagination.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 COPYING create mode 100644 README.md create mode 100644 jcm/pagination.py diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..29d056a --- /dev/null +++ b/COPYING @@ -0,0 +1,19 @@ +Copyright © 2020 Guillaume Jacquemin + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7cd992 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# jcm-pagination + +jcm-pagination is a Pelican plugin designed to be used with the [m.css](https://mcss.mosra.cz/) theme. + +## Installation instructions + +1. Clone the repository somewhere, like in your Pelican site's folder. +2. Add the folder to `PLUGIN_PATHS` in your site's configuration. +3. Add `'jcm.pagination'` to `PLUGINS`. +4. Done! You can then start using the `jcm-pagination` reST directive in your documents according to the [documentation](https://williamjcm.ovh/coding/jcm-pagination/). \ No newline at end of file diff --git a/jcm/pagination.py b/jcm/pagination.py new file mode 100644 index 0000000..b89a8e8 --- /dev/null +++ b/jcm/pagination.py @@ -0,0 +1,105 @@ +# +# This file is part of jcm-pagination. +# +# Copyright © 2020 Guillaume Jacquemin +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# + +from docutils.parsers import rst +from docutils.parsers.rst import Directive +from docutils.parsers.rst import directives +from docutils import nodes + +class Pagination(Directive): + required_arguments = 1 + optional_arguments = 0 + final_argument_whitespace = False + option_spec = {'page_number': directives.unchanged_required, + 'total_pages': directives.unchanged_required, + 'previous_text': directives.unchanged, + 'next_text': directives.unchanged, + 'style': directives.unchanged} + has_content = False + + def run(self): + container = nodes.container() + container['classes'].append('m-article-pagination') + + base_url = self.arguments[0] + url_format_string = base_url + "/{}/" + + page_number = int(self.options['page_number'].strip()) + if page_number < 1: + return None + + total_pages = int(self.options['total_pages'].strip()) + if total_pages < 1: + return None + + if 'previous_text' in self.options: + previous_text = self.options['previous_text'] + else: + previous_text = 'previous' + + if 'next_text' in self.options: + next_text = self.options['next_text'] + else: + next_text = 'next' + + if 'style' in self.options: + style = self.options['style'] if self.options['style'] in ['default', 'x_out_of_y', 'mcss'] else 'default' + else: + style = 'default' + + if page_number == 1: + if style != 'mcss': + container.append(nodes.inline(text="« {}".format(previous_text))) + else: + container.append(nodes.reference(text="« {}".format(previous_text), + refuri=(base_url if page_number == 2 else url_format_string.format(page_number - 1)))) + + if style != 'mcss' or page_number != 1: + container.append(nodes.inline(text=' | ')) + + if style == 'default': + for i in range(1, total_pages + 1): + if i == page_number: + container.append(nodes.inline(text="{}".format(i))) + else: + container.append(nodes.reference(text="{}".format(i), + refuri=(base_url if i == 1 else url_format_string.format(i)))) + container.append(nodes.inline(text=' | ')) + else: + container.append(nodes.inline(text="page {}{}".format(page_number, " out of {}".format(total_pages) if style == 'x_out_of_y' else ''))) + + if style != 'mcss' or page_number != total_pages: + container.append(nodes.inline(text=' | ')) + + if page_number == total_pages: + if style != 'mcss': + container.append(nodes.inline(text="{} »".format(next_text))) + else: + container.append(nodes.reference(text="{} »".format(next_text), + refuri=url_format_string.format(page_number + 1))) + + return [container] + +def register(): + rst.directives.register_directive('jcm-pagination', Pagination)