Skip to content

Commit

Permalink
Improve pagination (now truncates more than 9 pages)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aemiii91 committed May 18, 2024
1 parent 0fad040 commit c65bdb1
Show file tree
Hide file tree
Showing 24 changed files with 1,644 additions and 325 deletions.
30 changes: 27 additions & 3 deletions .github/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def rel_path(path: str, from_path: str) -> str:

THEME_DIR = from_src("../themes")
RELEASE_DIR = from_src("../release")
PAGES_DIR = from_src("../generated")
TEMP_DIR = from_src("temp")

FEATURED_ORDERING = from_src("data/featured.txt")
Expand All @@ -19,7 +20,6 @@ def rel_path(path: str, from_path: str) -> str:
README_TEST = ["readme.md", "README.md", "readme.txt", "README.txt"]
REL_PATH = os.path.abspath(os.path.join(SRC_DIR, ".."))

PAGES_DIR = from_src("../pages")
PAGES_ICONS_DIR = os.path.join(PAGES_DIR, "icons")
README_PATH = from_src("../README.md")
INDEX_TEMPLATE = from_src("template/index.template.md")
Expand All @@ -41,6 +41,10 @@ def rel_path(path: str, from_path: str) -> str:

PREVIEW_ICON = f"<img src=\"{HAS_ICONPACK_ICON_URL}\" height=\"16\" title=\"Show full preview\">"

NB_SPACE = "&nbsp;"
NB_SPACER = NB_SPACE * 2
LB_SPACER = " " + NB_SPACE

_pages = {
"custom": "Custom Themes",
"remixed": "Remixed Themes",
Expand All @@ -49,7 +53,7 @@ def rel_path(path: str, from_path: str) -> str:
}

HEADER_LINKS = {
**{name: [text.replace(" ", "&nbsp;"), os.path.join(PAGES_DIR, name, "index.md")] for name, text in _pages.items()},
**{name: [text.replace(" ", NB_SPACE), os.path.join(PAGES_DIR, name, "index.md")] for name, text in _pages.items()},
"contributing": ["Contributing", from_src("../CONTRIBUTING.md")]
}

Expand All @@ -60,4 +64,24 @@ def rel_path(path: str, from_path: str) -> str:
MAX_RECENTS = 3

COLUMN_WIDTH = 46
COLUMN_SPANNER = "&nbsp;" * COLUMN_WIDTH
COLUMN_SPANNER = NB_SPACE * COLUMN_WIDTH

WARN_GENERATED_FILE = f"""<!--{'\n' * 20}
=================================================================================
---------------------------------------------------------------------------------
██████╗ ██████╗ ███╗ ██╗ ██████╗ ████████╗ ███████╗██████╗ ██╗████████╗
██╔══██╗██╔═══██╗ ████╗ ██║██╔═══██╗╚══██╔══╝ ██╔════╝██╔══██╗██║╚══██╔══╝
██║ ██║██║ ██║ ██╔██╗ ██║██║ ██║ ██║ █████╗ ██║ ██║██║ ██║
██║ ██║██║ ██║ ██║╚██╗██║██║ ██║ ██║ ██╔══╝ ██║ ██║██║ ██║
██████╔╝╚██████╔╝ ██║ ╚████║╚██████╔╝ ██║ ███████╗██████╔╝██║ ██║
╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚══════╝╚═════╝ ╚═╝ ╚═╝
---------------------------------------------------------------------------------
=================================================================================
Note: This file was automatically generated.
Run `python .github/generate.py` to regenerate the pages.
{'\n' * 20}-->
"""
69 changes: 38 additions & 31 deletions .github/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def main():
write_pages(themes_custom, "custom", "Custom Themes", generate_table_grid)
write_pages(themes_remixed, "remixed", "Remixed Themes", generate_table_grid)

write_pages(themes_icon_packs, "icons_themes", "Theme Icon Packs", generate_icon_pack_table)
write_pages(standalone_icon_packs, "icons_standalone", "Standalone Icon Packs", generate_icon_pack_table)
write_pages(themes_icon_packs, "icons_themes", "Theme Icon Packs", generate_icon_pack_table, page_size=9)
write_pages(standalone_icon_packs, "icons_standalone", "Standalone Icon Packs", generate_icon_pack_table, page_size=9)

write_file(README_PATH, generate_index({
"custom": len(themes_custom),
Expand All @@ -67,7 +67,7 @@ def main():

def write_file(path: str, buffer: str):
with open(path, "w+", encoding="utf-8") as outfile:
outfile.write(buffer)
outfile.write(WARN_GENERATED_FILE + buffer)


def apply_template(path: str, data: dict) -> str:
Expand Down Expand Up @@ -114,16 +114,10 @@ def write_pages(items: list, group_name: str, group_header: str, item_grid_gener

index = page * page_size
batch = items[index : index + page_size]
last_item = min(index + page_size, total)

buffer = ""
buffer += apply_template(HEADER_TEMPLATE, { "LINKS": generate_header_links(current_path, current_group=group_name) })
buffer += f"""
## {group_header}
**Page {page + 1} of {num_pages}**
*Showing {index + 1}{last_item} of {total} items*
"""
buffer += f"\n## {group_header}\n\n*Page {page + 1} of {num_pages}{total} items available*\n"
buffer += item_grid_generator(batch, **opts) + "\n\n"

if num_pages > 1:
Expand All @@ -139,35 +133,48 @@ def generate_header_links(current_path: str, current_group: str = None):
links.append(f"**{text}**")
else:
links.append(f"[{text}]({rel_path(link, current_path)})")
return " &nbsp;•&nbsp; ".join(links)
return f"{LB_SPACER}{LB_SPACER}".join(links)


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[◀&nbsp;PREV PAGE]({format_page_filename(current_page - 1)})\n\n</td>"""
buffer += f"""<td align="right">\n\n[❮{NB_SPACER}PREV PAGE]({format_page_filename(current_page - 1)})\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 PAGE{NB_SPACER}❯]({format_page_filename(current_page + 1)})\n\n</td>"
buffer += "</tr></table>"
return buffer

buffer += """<td align="center">\n\n"""

def generate_page_links(current_page: int, num_pages: int) -> str:
if num_pages <= 9:
return LB_SPACER.join(generate_page_link(page, current_page) for page in range(num_pages))

for page in range(num_pages):
if page == current_page:
buffer += f"**{page + 1:02}**"
else:
buffer += f"[{page + 1:02}]({format_page_filename(page)})"
if page < num_pages - 1:
buffer += " &nbsp;"
last_page = num_pages - 1
cutoff = 5
half_cut = math.floor(cutoff / 2)

buffer += "\n\n</td>"
is_low = current_page < cutoff
is_high = current_page > last_page - cutoff

if current_page < num_pages - 1:
buffer += f"<td>\n\n[NEXT PAGE&nbsp;▶]({format_page_filename(current_page + 1)})\n\n</td>"
buffer = ""
buffer += generate_page_link(0, current_page) + LB_SPACER
buffer += generate_page_link_range(range(1, cutoff + 2), current_page) if is_low else "&hellip;"
buffer += LB_SPACER 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) + LB_SPACER
buffer += generate_page_link_range(range(last_page - cutoff - 1, last_page), current_page) if is_high else "&hellip;"
buffer += LB_SPACER + generate_page_link(last_page, current_page)
return buffer

buffer += "</tr></table>"

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(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_table_grid(themes) -> str:
Expand Down Expand Up @@ -244,10 +251,10 @@ def generate_item(theme: str, index: int = 0, collect_data: bool = False) -> str
"NAME": name + " ★" if is_featured else name,
"AUTHOR": author or "&nbsp;",
"TITLE": title,
"HAS_BGM": f"&nbsp;&nbsp;<a href=\"https://onionui.github.io/bgm_preview.html?theme={urlencode(theme_subdirs[0][7:])}\">{BGM_ICON}</a>" if has_bgm else "",
"HAS_ICONPACK": f"&nbsp; <a href=\"{generate_icon_pack_url(theme, theme_subdirs)}\">{HAS_ICONPACK_ICON}</a>" if has_icon_pack else "",
"README": f"&nbsp;&nbsp;<a href=\"{urlencode(readme_path)}\">{README_ICON}</a>" if len(readme_path) != 0 else "",
"AUTHOR_BTN": f"&nbsp;&nbsp;<a href=\"https://github.com/search?l=ZIP&q=filename%3A%22{urlencode(author)}%22+repo%3AOnionUI%2FThemes\">{AUTHOR_ICON}</a>" if author else "",
"HAS_BGM": f"{NB_SPACER}<a href=\"https://onionui.github.io/bgm_preview.html?theme={urlencode(theme_subdirs[0][7:])}\">{BGM_ICON}</a>" if has_bgm else "",
"HAS_ICONPACK": f"{LB_SPACER}<a href=\"{generate_icon_pack_url(theme, theme_subdirs)}\">{HAS_ICONPACK_ICON}</a>" if has_icon_pack else "",
"README": f"{NB_SPACER}<a href=\"{urlencode(readme_path)}\">{README_ICON}</a>" if len(readme_path) != 0 else "",
"AUTHOR_BTN": f"{NB_SPACER}<a href=\"https://github.com/search?l=ZIP&q=filename%3A%22{urlencode(author)}%22+repo%3AOnionUI%2FThemes\">{AUTHOR_ICON}</a>" if author else "",
"UPDATED": f"{last_updated} (v{commit_count})" if commit_count > 1 else last_updated,
"PREVIEW_URL": preview_url,
"RELEASE_URL": release_url,
Expand Down
Loading

0 comments on commit c65bdb1

Please sign in to comment.