@@ -61,11 +61,15 @@ class Format(str, Enum):
6161
6262
6363# The CLI only needs two values from the publication file. Therefore, this class ignores the vast majority of a publication file's contents, loading and validating only a (small) relevant subset.
64+ # Since we will want to hash the baseurl for generating qr codes, we also load it here.
6465class PublicationSubset (
6566 pxml .BaseXmlModel , tag = "publication" , search_mode = SearchMode .UNORDERED
6667):
6768 external : Path = pxml .wrapped ("source/directories" , pxml .attr ())
6869 generated : Path = pxml .wrapped ("source/directories" , pxml .attr ())
70+ baseurl : t .Optional [str ] = pxml .wrapped (
71+ "html/baseurl" , pxml .attr (name = "href" , default = None )
72+ )
6973
7074
7175class BrailleMode (str , Enum ):
@@ -517,6 +521,12 @@ def generate_asset_table(self) -> pt.AssetTable:
517521 for node in source_assets :
518522 assert isinstance (node , ET ._Element )
519523 hash .update (ET .tostring (node ))
524+ # For QR codes, we also hash the base URL, if it exists.
525+ if asset == "qrcode" :
526+ base_url = self ._read_publication_file_subset ().baseurl
527+ if base_url is not None :
528+ hash .update (base_url .encode ("utf-8" ))
529+ # Finally, we store the hash as a string in the dictionary.
520530 asset_hash_dict [asset ] = hash .hexdigest ()
521531 return asset_hash_dict
522532
@@ -1123,6 +1133,26 @@ def generate_assets(
11231133 log .debug (e , exc_info = True )
11241134 # youtube also requires the play button.
11251135 self .ensure_play_button ()
1136+ if "qrcode" in assets_to_generate :
1137+ try :
1138+ # Warn if trying to generate qrcodes without a base URL.
1139+ base_url = self ._read_publication_file_subset ().baseurl
1140+ if base_url is None :
1141+ log .warning (
1142+ "You are trying to generate qrcodes, but the publication file does not have a base URL. "
1143+ + "This will result in qrcodes that do not point to anything meaningful."
1144+ )
1145+ core .qrcode (
1146+ xml_source = self .source_abspath (),
1147+ pub_file = self .publication_abspath ().as_posix (),
1148+ stringparams = stringparams_copy ,
1149+ xmlid_root = xmlid ,
1150+ dest_dir = self .generated_dir_abspath () / "qrcode" ,
1151+ )
1152+ successful_assets .append ("qrcode" )
1153+ except Exception as e :
1154+ log .error (f"Unable to generate some qrcodes:\n { e } " )
1155+ log .debug (e , exc_info = True )
11261156 if "mermaid" in assets_to_generate :
11271157 try :
11281158 core .mermaid_images (
@@ -1162,20 +1192,6 @@ def generate_assets(
11621192 except Exception as e :
11631193 log .error (f"Unable to generate some datafiles:\n { e } " )
11641194 log .debug (e , exc_info = True )
1165- # Finally, also generate the qrcodes for interactive and youtube assets:
1166- # NOTE: we do not currently check for success of this for saving assets to the asset cache.
1167- if "interactive" in assets_to_generate or "youtube" in assets_to_generate :
1168- try :
1169- core .qrcode (
1170- xml_source = self .source_abspath (),
1171- pub_file = self .publication_abspath ().as_posix (),
1172- stringparams = stringparams_copy ,
1173- xmlid_root = xmlid ,
1174- dest_dir = self .generated_dir_abspath () / "qrcode" ,
1175- )
1176- except Exception as e :
1177- log .error (f"Unable to generate some qrcodes:\n { e } " , exc_info = True )
1178- log .debug (e , exc_info = True )
11791195 # Delete temporary directories left behind by core:
11801196 try :
11811197 core .release_temporary_directories ()
0 commit comments