2222import logging
2323import datetime
2424import importlib
25+ import pathlib
2526import random
27+ import re
2628
2729logger = logging .getLogger (settings .logger )
2830logger .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