Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit 3c65ec6

Browse files
committed
Fix: use the real starting page for PreTeXt books.
1 parent 7462f6f commit 3c65ec6

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

controllers/books.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import logging
2323
import datetime
2424
import importlib
25+
import pathlib
2526
import random
27+
import re
2628

2729
logger = logging.getLogger(settings.logger)
2830
logger.setLevel(settings.log_level)
@@ -455,7 +457,14 @@ def index():
455457
book_info["section"] = "Computer Science"
456458
logger.debug(f"{book} is in section {book_info['section']}")
457459

458-
book_info["url"] = "/{}/books/published/{}/index.html".format(bks, book)
460+
if book_info["source"] == "Runestone":
461+
book_info["url"] = "/{}/books/published/{}/index.html".format(bks, book)
462+
else:
463+
book_info["url"] = "/{}/books/published/{}/{}".format(
464+
bks,
465+
book,
466+
_find_real_url(f"applications/{request.application}/books", book),
467+
)
459468
book_info["regname"] = book
460469
sections.sort()
461470
if hasattr(config, "is_private") and config.is_private == True:
@@ -464,3 +473,17 @@ def index():
464473
res.append(book_info)
465474

466475
return dict(book_list=res, sections=sections)
476+
477+
478+
#
479+
# PreTeXt books are set up with an index.thml that meta refreshes to the real start page.
480+
# This is great for flexibility but not good for ?mode=browsing or for SEO scores.
481+
# We can parse the "real" home page for the book from index.html
482+
def _find_real_url(libdir, book):
483+
idx = pathlib.Path(libdir, book, "published", book, "index.html")
484+
if idx.exists():
485+
with open(idx, "r") as idxf:
486+
for line in idxf:
487+
if g := re.search(r"refresh.*URL='(.*?)'", line):
488+
return g.group(1)
489+
return "index.html"

0 commit comments

Comments
 (0)