@@ -84,6 +84,7 @@ class encryptContentPlugin(BasePlugin):
8484 ('selfhost' , config_options .Type (bool , default = False )),
8585 ('selfhost_download' , config_options .Type (bool , default = True )),
8686 ('translations' , config_options .Type (dict , default = {}, required = False )),
87+ ('hash_filenames' , config_options .Type (dict , default = {}, required = False )),
8788 # legacy features, doesn't exist anymore
8889 )
8990
@@ -95,6 +96,13 @@ def __hash_md5__(self, text):
9596 key .update (text .encode ('utf-8' ))
9697 return key .digest ()
9798
99+ def __hash_md5_file__ (self , fname ):
100+ hash_md5 = hashlib .md5 ()
101+ with open (fname , "rb" ) as f :
102+ for chunk in iter (lambda : f .read (4096 ), b"" ):
103+ hash_md5 .update (chunk )
104+ return hash_md5 .hexdigest ()
105+
98106 def __encrypt_text_aes__ (self , text , password ):
99107 """ Encrypts text with AES-256. """
100108 BLOCK_SIZE = 32
@@ -328,6 +336,34 @@ def on_pre_build(self, config, **kwargs):
328336 except Exception as exp :
329337 logger .exception (exp )
330338
339+ def on_files (self , files , config , ** kwargs ):
340+ """
341+ The files event is called after the files collection is populated from the docs_dir.
342+ Use this event to add, remove, or alter files in the collection.
343+ Note that Page objects have not yet been associated with the file objects in the collection.
344+ Use Page Events to manipulate page specific data.
345+ """
346+ if 'extensions' in self .config ['hash_filenames' ]:
347+ for file in files :
348+
349+ if 'except' in self .config ['hash_filenames' ]:
350+ skip = False
351+ for check in self .config ['hash_filenames' ]['except' ]:
352+ if file .src_path .endswith (check ):
353+ skip = True
354+ if skip :
355+ continue
356+
357+ ext = file .src_path .rsplit ('.' ,1 )[1 ].lower ()
358+ if ext in self .config ['hash_filenames' ]['extensions' ]:
359+ hash = self .__hash_md5_file__ (file .abs_src_path )
360+ filename , ext = file .abs_dest_path .rsplit ('.' ,1 )
361+ filename = filename + "_" + hash
362+ file .abs_dest_path = filename + "." + ext
363+ filename , ext = file .url .rsplit ('.' ,1 )
364+ filename = filename + "_" + hash
365+ file .url = filename + "." + ext
366+
331367 def on_page_markdown (self , markdown , page , config , ** kwargs ):
332368 """
333369 The page_markdown event is called after the page's markdown is loaded from file and
@@ -543,8 +579,6 @@ def on_post_page(self, output_content, page, config, **kwargs):
543579 if self .setup ['search_plugin_found' ]:
544580 location = page .url .lstrip ('/' )
545581 self .setup ['locations' ][location ] = page .encryptcontent ['password' ]
546- print (page .title )
547- print (page .encryptcontent )
548582 delattr (page , 'encryptcontent' )
549583
550584 return output_content
0 commit comments