Skip to content

Commit 84a73a7

Browse files
committed
improvement: in watch mode, use hash in graphics filenames to force browser refresh
1 parent ba41d72 commit 84a73a7

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎flm/main/feature_graphics_collection.py‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,27 @@ def prepare_collect_graphics(self, source_info, resolved_src_url, counter):
858858
)
859859
target_ext = converter_info['target_ext']
860860

861-
target_fname = self.collect_graphics_filename_template_obj.substitute({
862-
'basename': os.path.basename(resolved_src_url),
863-
'basenoext': basenoext,
864-
'ext': target_ext,
865-
'counter': counter,
866-
})
861+
input_hash = None
862+
if source_type == 'file':
863+
with open(resolved_src_url, 'rb') as f:
864+
input_hash = hashlib.file_digest(f, 'sha256').hexdigest()
865+
866+
try:
867+
target_fname = self.collect_graphics_filename_template_obj.substitute({
868+
'basename': os.path.basename(resolved_src_url),
869+
'basenoext': basenoext,
870+
'ext': target_ext,
871+
'counter': counter,
872+
'hash': input_hash if input_hash is not None else 'NOHASH',
873+
'hash6': input_hash[:6] if input_hash is not None else 'NOHASH',
874+
})
875+
except KeyError as e:
876+
raise ValueError(
877+
f"Invalid file name template in feature_graphics_collection: ‘"
878+
+ self.collect_graphics_filename_template
879+
+ f"’ refers to invalid key ‘{e.args[0]}’"
880+
)
881+
867882
target_path = os.path.join(
868883
self.reference_output_dir,
869884
self.collect_graphics_to_output_folder,
@@ -880,6 +895,7 @@ def prepare_collect_graphics(self, source_info, resolved_src_url, counter):
880895
'source_url': source_url,
881896
'src_url_resolved': resolved_src_url,
882897
'converter_info': converter_info,
898+
'input_hash': input_hash,
883899
'target_path': target_path,
884900
'target_relative_path': target_relative_path,
885901
'from_ext': ext,
@@ -902,10 +918,7 @@ def collect_graphics(self, source_key, collect_info, *, cache_info):
902918

903919
logger.debug('converter_info = %r', converter_info)
904920

905-
input_hash = None
906-
if source_type == 'file':
907-
with open(src_url, 'rb') as f:
908-
input_hash = hashlib.file_digest(f, 'sha256').hexdigest()
921+
input_hash = collect_info['input_hash']
909922

910923
if os.path.exists(target_path):
911924
# check if the input file was updated since last run

‎flm/main/watch.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def workflow_postprocess_actions_fn(rendered_content, document, render_context):
120120
inline_default_config = None
121121
if computed_format == 'html':
122122
inline_default_config = { 'flm': {
123+
'features': {
124+
'flm.main.feature_graphics_collection': {
125+
'collect_graphics_filename_template': "gr${counter}-${hash6}${ext}"
126+
}
127+
},
123128
'renderer': {
124129
'html': {
125130
'include_node_data_attrs_fn': include_node_data_attrs_fn_src_line_col,

0 commit comments

Comments
 (0)