Skip to content

Commit

Permalink
Use reverse page numbering to allow permalinking
Browse files Browse the repository at this point in the history
  • Loading branch information
Aemiii91 committed May 18, 2024
1 parent 3077565 commit 0fa3973
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 241 deletions.
36 changes: 21 additions & 15 deletions .github/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import json
import math
import hashlib
import shutil
from typing import Callable
from tqdm import tqdm

Expand All @@ -28,6 +30,8 @@ def main():
if not os.path.exists(RELEASE_DIR):
print("No themes released")
return

shutil.rmtree(PAGES_DIR)

print("Generating READMEs...")

Expand Down Expand Up @@ -75,8 +79,10 @@ def apply_template(path: str, data: dict) -> str:
return template.substitute(data) + "\n"


def format_page_filename(page: int) -> str:
return "index.md" if page == 0 else f"page-{page+1:02}.md"
def format_page_filename(page: int, num_pages: int) -> str:
prefix = f"{num_pages - page:02}"
page_hash = hashlib.shake_128(prefix.encode("utf-8")).hexdigest(2)
return "index.md" if page == 0 else f"index-{prefix}-{page_hash[:2]}.md"


def generate_index(counts: dict):
Expand Down Expand Up @@ -109,7 +115,7 @@ def write_pages(items: list, group_name: str, item_grid_generator: Callable[[lis
num_pages = math.ceil(total / page_size)

for page in tqdm(range(num_pages), desc=PAGE_TITLES[group_name]):
current_path = os.path.join(workdir, format_page_filename(page))
current_path = os.path.join(workdir, format_page_filename(page, num_pages))

index = page * page_size
batch = items[index : index + page_size]
Expand Down Expand Up @@ -139,17 +145,17 @@ def generate_pagination(current_page: int, num_pages: int) -> str:
buffer = ""
buffer += """---\n\n<table align="center"><tr>"""
if current_page > 0:
buffer += f"""<td align="right">\n\n[❮{NB_SPACER}PREV]({format_page_filename(current_page - 1)})\n\n</td>"""
buffer += f"""<td align="right">\n\n[❮{NB_SPACER}PREV]({format_page_filename(current_page - 1, num_pages)})\n\n</td>"""
buffer += f"""<td align="center">\n\n{generate_page_links(current_page, num_pages)}\n\n</td>"""
if current_page < num_pages - 1:
buffer += f"<td>\n\n[NEXT{NB_SPACER}❯]({format_page_filename(current_page + 1)})\n\n</td>"
buffer += f"<td>\n\n[NEXT{NB_SPACER}❯]({format_page_filename(current_page + 1, num_pages)})\n\n</td>"
buffer += "</tr></table>"
return buffer


def generate_page_links(current_page: int, num_pages: int) -> str:
if num_pages <= 9:
return generate_page_link_range(range(num_pages), current_page)
return generate_page_link_range(range(num_pages), current_page, num_pages)

last_page = num_pages - 1
cutoff = 5
Expand All @@ -160,21 +166,21 @@ def generate_page_links(current_page: int, num_pages: int) -> str:
is_high = current_page > last_page - cutoff

buffer = ""
buffer += generate_page_link(0, current_page)
buffer += " " + generate_page_link_range(range(1, cutoff + 2), current_page) if is_low else NB_SPACE + ellipsis
buffer += generate_page_link(0, current_page, num_pages)
buffer += " " + generate_page_link_range(range(1, cutoff + 2), current_page, num_pages) if is_low else NB_SPACE + ellipsis
buffer += " " if is_low or is_high \
else LB_SPACER + generate_page_link_range(range(current_page - half_cut, current_page + half_cut + 1), current_page) + " "
buffer += generate_page_link_range(range(last_page - cutoff - 1, last_page), current_page) + " " if is_high else ellipsis + NB_SPACE
buffer += generate_page_link(last_page, current_page)
else LB_SPACER + generate_page_link_range(range(current_page - half_cut, current_page + half_cut + 1), current_page, num_pages) + " "
buffer += generate_page_link_range(range(last_page - cutoff - 1, last_page), current_page, num_pages) + " " if is_high else ellipsis + NB_SPACE
buffer += generate_page_link(last_page, current_page, num_pages)
return buffer


def generate_page_link_range(rng: range, current_page: int) -> str:
return " ".join(generate_page_link(page, current_page) for page in rng)
def generate_page_link_range(rng: range, current_page: int, num_pages: int) -> str:
return " ".join(generate_page_link(page, current_page, num_pages) for page in rng)


def generate_page_link(page: int, current_page: int) -> str:
return f"{NB_SPACE}**{page + 1}**{NB_SPACE}" if page == current_page else f"[{NB_SPACE}{page + 1}{NB_SPACE}]({format_page_filename(page)})"
def generate_page_link(page: int, current_page: int, num_pages: int) -> str:
return f"{NB_SPACE}**{page + 1}**{NB_SPACE}" if page == current_page else f"[{NB_SPACE}{page + 1}{NB_SPACE}]({format_page_filename(page, num_pages)})"


def generate_table_grid(themes) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-12.md)
[&nbsp;&nbsp;PREV](index-02-6b.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](page-07.md) [&nbsp;8&nbsp;](page-08.md) [&nbsp;9&nbsp;](page-09.md) [&nbsp;10&nbsp;](page-10.md) [&nbsp;11&nbsp;](page-11.md) [&nbsp;12&nbsp;](page-12.md) &nbsp;**13**&nbsp;
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](index-07-fc.md) [&nbsp;8&nbsp;](index-06-21.md) [&nbsp;9&nbsp;](index-05-2a.md) [&nbsp;10&nbsp;](index-04-d8.md) [&nbsp;11&nbsp;](index-03-11.md) [&nbsp;12&nbsp;](index-02-6b.md) &nbsp;**13**&nbsp;

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-11.md)
[&nbsp;&nbsp;PREV](index-03-11.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](page-07.md) [&nbsp;8&nbsp;](page-08.md) [&nbsp;9&nbsp;](page-09.md) [&nbsp;10&nbsp;](page-10.md) [&nbsp;11&nbsp;](page-11.md) &nbsp;**12**&nbsp; [&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](index-07-fc.md) [&nbsp;8&nbsp;](index-06-21.md) [&nbsp;9&nbsp;](index-05-2a.md) [&nbsp;10&nbsp;](index-04-d8.md) [&nbsp;11&nbsp;](index-03-11.md) &nbsp;**12**&nbsp; [&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-13.md)
[NEXT&nbsp;&nbsp;](index-01-79.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-10.md)
[&nbsp;&nbsp;PREV](index-04-d8.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](page-07.md) [&nbsp;8&nbsp;](page-08.md) [&nbsp;9&nbsp;](page-09.md) [&nbsp;10&nbsp;](page-10.md) &nbsp;**11**&nbsp; [&nbsp;12&nbsp;](page-12.md) [&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](index-07-fc.md) [&nbsp;8&nbsp;](index-06-21.md) [&nbsp;9&nbsp;](index-05-2a.md) [&nbsp;10&nbsp;](index-04-d8.md) &nbsp;**11**&nbsp; [&nbsp;12&nbsp;](index-02-6b.md) [&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-12.md)
[NEXT&nbsp;&nbsp;](index-02-6b.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-09.md)
[&nbsp;&nbsp;PREV](index-05-2a.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](page-07.md) [&nbsp;8&nbsp;](page-08.md) [&nbsp;9&nbsp;](page-09.md) &nbsp;**10**&nbsp; [&nbsp;11&nbsp;](page-11.md) [&nbsp;12&nbsp;](page-12.md) [&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](index-07-fc.md) [&nbsp;8&nbsp;](index-06-21.md) [&nbsp;9&nbsp;](index-05-2a.md) &nbsp;**10**&nbsp; [&nbsp;11&nbsp;](index-03-11.md) [&nbsp;12&nbsp;](index-02-6b.md) [&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-11.md)
[NEXT&nbsp;&nbsp;](index-03-11.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-08.md)
[&nbsp;&nbsp;PREV](index-06-21.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](page-07.md) [&nbsp;8&nbsp;](page-08.md) &nbsp;**9**&nbsp; [&nbsp;10&nbsp;](page-10.md) [&nbsp;11&nbsp;](page-11.md) [&nbsp;12&nbsp;](page-12.md) [&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; [&nbsp;7&nbsp;](index-07-fc.md) [&nbsp;8&nbsp;](index-06-21.md) &nbsp;**9**&nbsp; [&nbsp;10&nbsp;](index-04-d8.md) [&nbsp;11&nbsp;](index-03-11.md) [&nbsp;12&nbsp;](index-02-6b.md) [&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-10.md)
[NEXT&nbsp;&nbsp;](index-04-d8.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-07.md)
[&nbsp;&nbsp;PREV](index-07-fc.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; &nbsp;[&nbsp;6&nbsp;](page-06.md) [&nbsp;7&nbsp;](page-07.md) &nbsp;**8**&nbsp; [&nbsp;9&nbsp;](page-09.md) [&nbsp;10&nbsp;](page-10.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; &nbsp;[&nbsp;6&nbsp;](index-08-bc.md) [&nbsp;7&nbsp;](index-07-fc.md) &nbsp;**8**&nbsp; [&nbsp;9&nbsp;](index-05-2a.md) [&nbsp;10&nbsp;](index-04-d8.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-09.md)
[NEXT&nbsp;&nbsp;](index-05-2a.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-06.md)
[&nbsp;&nbsp;PREV](index-08-bc.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; &nbsp;[&nbsp;5&nbsp;](page-05.md) [&nbsp;6&nbsp;](page-06.md) &nbsp;**7**&nbsp; [&nbsp;8&nbsp;](page-08.md) [&nbsp;9&nbsp;](page-09.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; &nbsp;[&nbsp;5&nbsp;](index-09-9d.md) [&nbsp;6&nbsp;](index-08-bc.md) &nbsp;**7**&nbsp; [&nbsp;8&nbsp;](index-06-21.md) [&nbsp;9&nbsp;](index-05-2a.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-08.md)
[NEXT&nbsp;&nbsp;](index-06-21.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-05.md)
[&nbsp;&nbsp;PREV](index-09-9d.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; &nbsp;[&nbsp;4&nbsp;](page-04.md) [&nbsp;5&nbsp;](page-05.md) &nbsp;**6**&nbsp; [&nbsp;7&nbsp;](page-07.md) [&nbsp;8&nbsp;](page-08.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md)&nbsp;&hellip; &nbsp;[&nbsp;4&nbsp;](index-10-be.md) [&nbsp;5&nbsp;](index-09-9d.md) &nbsp;**6**&nbsp; [&nbsp;7&nbsp;](index-07-fc.md) [&nbsp;8&nbsp;](index-06-21.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-07.md)
[NEXT&nbsp;&nbsp;](index-07-fc.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-04.md)
[&nbsp;&nbsp;PREV](index-10-be.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](page-02.md) [&nbsp;3&nbsp;](page-03.md) [&nbsp;4&nbsp;](page-04.md) &nbsp;**5**&nbsp; [&nbsp;6&nbsp;](page-06.md) [&nbsp;7&nbsp;](page-07.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](index-12-ca.md) [&nbsp;3&nbsp;](index-11-8c.md) [&nbsp;4&nbsp;](index-10-be.md) &nbsp;**5**&nbsp; [&nbsp;6&nbsp;](index-08-bc.md) [&nbsp;7&nbsp;](index-07-fc.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-06.md)
[NEXT&nbsp;&nbsp;](index-08-bc.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-03.md)
[&nbsp;&nbsp;PREV](index-11-8c.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](page-02.md) [&nbsp;3&nbsp;](page-03.md) &nbsp;**4**&nbsp; [&nbsp;5&nbsp;](page-05.md) [&nbsp;6&nbsp;](page-06.md) [&nbsp;7&nbsp;](page-07.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](index-12-ca.md) [&nbsp;3&nbsp;](index-11-8c.md) &nbsp;**4**&nbsp; [&nbsp;5&nbsp;](index-09-9d.md) [&nbsp;6&nbsp;](index-08-bc.md) [&nbsp;7&nbsp;](index-07-fc.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-05.md)
[NEXT&nbsp;&nbsp;](index-09-9d.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-02.md)
[&nbsp;&nbsp;PREV](index-12-ca.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](page-02.md) &nbsp;**3**&nbsp; [&nbsp;4&nbsp;](page-04.md) [&nbsp;5&nbsp;](page-05.md) [&nbsp;6&nbsp;](page-06.md) [&nbsp;7&nbsp;](page-07.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](index-12-ca.md) &nbsp;**3**&nbsp; [&nbsp;4&nbsp;](index-10-be.md) [&nbsp;5&nbsp;](index-09-9d.md) [&nbsp;6&nbsp;](index-08-bc.md) [&nbsp;7&nbsp;](index-07-fc.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-04.md)
[NEXT&nbsp;&nbsp;](index-10-be.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@

</td><td align="center">

[&nbsp;1&nbsp;](index.md) &nbsp;**2**&nbsp; [&nbsp;3&nbsp;](page-03.md) [&nbsp;4&nbsp;](page-04.md) [&nbsp;5&nbsp;](page-05.md) [&nbsp;6&nbsp;](page-06.md) [&nbsp;7&nbsp;](page-07.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
[&nbsp;1&nbsp;](index.md) &nbsp;**2**&nbsp; [&nbsp;3&nbsp;](index-11-8c.md) [&nbsp;4&nbsp;](index-10-be.md) [&nbsp;5&nbsp;](index-09-9d.md) [&nbsp;6&nbsp;](index-08-bc.md) [&nbsp;7&nbsp;](index-07-fc.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-03.md)
[NEXT&nbsp;&nbsp;](index-11-8c.md)

</td></tr></table>
4 changes: 2 additions & 2 deletions generated/custom/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@

<table align="center"><tr><td align="center">

&nbsp;**1**&nbsp; [&nbsp;2&nbsp;](page-02.md) [&nbsp;3&nbsp;](page-03.md) [&nbsp;4&nbsp;](page-04.md) [&nbsp;5&nbsp;](page-05.md) [&nbsp;6&nbsp;](page-06.md) [&nbsp;7&nbsp;](page-07.md) &hellip;&nbsp;[&nbsp;13&nbsp;](page-13.md)
&nbsp;**1**&nbsp; [&nbsp;2&nbsp;](index-12-ca.md) [&nbsp;3&nbsp;](index-11-8c.md) [&nbsp;4&nbsp;](index-10-be.md) [&nbsp;5&nbsp;](index-09-9d.md) [&nbsp;6&nbsp;](index-08-bc.md) [&nbsp;7&nbsp;](index-07-fc.md) &hellip;&nbsp;[&nbsp;13&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-02.md)
[NEXT&nbsp;&nbsp;](index-12-ca.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-03.md)
[&nbsp;&nbsp;PREV](index-02-6b.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](page-02.md) [&nbsp;3&nbsp;](page-03.md) &nbsp;**4**&nbsp;
[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](index-03-11.md) [&nbsp;3&nbsp;](index-02-6b.md) &nbsp;**4**&nbsp;

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@

<table align="center"><tr><td align="right">

[&nbsp;&nbsp;PREV](page-02.md)
[&nbsp;&nbsp;PREV](index-03-11.md)

</td><td align="center">

[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](page-02.md) &nbsp;**3**&nbsp; [&nbsp;4&nbsp;](page-04.md)
[&nbsp;1&nbsp;](index.md) [&nbsp;2&nbsp;](index-03-11.md) &nbsp;**3**&nbsp; [&nbsp;4&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-04.md)
[NEXT&nbsp;&nbsp;](index-01-79.md)

</td></tr></table>
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@

</td><td align="center">

[&nbsp;1&nbsp;](index.md) &nbsp;**2**&nbsp; [&nbsp;3&nbsp;](page-03.md) [&nbsp;4&nbsp;](page-04.md)
[&nbsp;1&nbsp;](index.md) &nbsp;**2**&nbsp; [&nbsp;3&nbsp;](index-02-6b.md) [&nbsp;4&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-03.md)
[NEXT&nbsp;&nbsp;](index-02-6b.md)

</td></tr></table>
4 changes: 2 additions & 2 deletions generated/icons_themes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@

<table align="center"><tr><td align="center">

&nbsp;**1**&nbsp; [&nbsp;2&nbsp;](page-02.md) [&nbsp;3&nbsp;](page-03.md) [&nbsp;4&nbsp;](page-04.md)
&nbsp;**1**&nbsp; [&nbsp;2&nbsp;](index-03-11.md) [&nbsp;3&nbsp;](index-02-6b.md) [&nbsp;4&nbsp;](index-01-79.md)

</td><td>

[NEXT&nbsp;&nbsp;](page-02.md)
[NEXT&nbsp;&nbsp;](index-03-11.md)

</td></tr></table>
Loading

0 comments on commit 0fa3973

Please sign in to comment.