diff --git a/devTools/embed_favicon.py b/devTools/embed_favicon.py index 4b59bb93be8597b5a4bdcd45d58dc8d43a00d7f9..79d82ab4ff1c9801d1d0b136c6fa20043c06ae46 100755 --- a/devTools/embed_favicon.py +++ b/devTools/embed_favicon.py @@ -1,14 +1,14 @@ #!/usr/bin/env python3 ''' -Script for embedding favicons into the SugarCube Header. +Script for embedding favicons into a HTML Header. Script file is expected to reside in devTools directory. Note: This does not actually check the image file's contents for size detection. Usage: -python3 embed_favicon.py +python3 embed_favicon.py [FC_pregmod.html] ''' import sys @@ -22,22 +22,28 @@ ext2mimetype = { '.ico': 'image/x-icon' } + # reads a file, turns it into a data uri def data_uri_from_file(filename, mimetype): - data = open(filename,'rb').read() + data = open(filename, 'rb').read() base64data = base64.b64encode(data).decode('ascii') - out = 'data:%s;base64,%s'%(mimetype, base64data) + out = 'data:%s;base64,%s' % (mimetype, base64data) return out + if __name__ == "__main__": # find project root directory path # (script file is expected to reside in devTools) project_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - # path to SugarCube's header.html - header_html_path = os.path.join( - project_root_path, - 'devTools/tweeGo/storyFormats/sugarcube-2/header.html' - ) + html_path = '' + if (len(sys.argv) > 1): + html_path = sys.argv[1] + else: + # path to bin/FC_pregmod.html + html_path = os.path.join( + project_root_path, + 'bin/FC_pregmod.html' + ) # path to directory containing all favicons to embed favicons_source_path = os.path.join( project_root_path, @@ -45,18 +51,19 @@ if __name__ == "__main__": ) # walk directory for all files favicons_paths = [ - os.path.join(dirpath, filename) - for dirpath, dirnames, filenames in os.walk(favicons_source_path) - for filename in filenames + os.path.join(dirpath, filename) + for dirpath, dirnames, filenames in os.walk(favicons_source_path) + for filename in filenames ] # ignore files with unknown extensions - favicons_paths = [f for f in favicons_paths if f[-4:] in ext2mimetype.keys()] - + favicons_paths = [f for f in favicons_paths + if f[-4:] in ext2mimetype.keys()] + # prepare embedded data size_from_filename = re.compile(r'([0-9]+)\....$') favicons_html = [] for fp in favicons_paths: - print('Found favicon source file "%s".'%(fp)) + print('Found favicon source file "%s".' % (fp)) # get mimetype by file extension mimetype = ext2mimetype[fp[-4:]] if (mimetype == 'image/x-icon'): @@ -65,19 +72,19 @@ if __name__ == "__main__": else: # guess icon size from file name size = size_from_filename.search(fp).group(1) - sizes = '%sx%s'%(size, size) + sizes = '%sx%s' % (size, size) data = data_uri_from_file(fp, mimetype) favicons_html.append( # prepare html with favicon data embedded - '<link rel="icon" type="%s" sizes="%s" href="%s">\n'%( + '<link rel="icon" type="%s" sizes="%s" href="%s">\n' % ( mimetype, sizes, data ) ) # modify header file - with open(header_html_path,'r+',encoding='utf-8') as hf: - print('Rewriting "%s"...'%(header_html_path)) - lines_in = hf.readlines() # read whole file + with open(html_path, 'r+', encoding='utf-8') as hf: + print('Rewriting "%s"...' % (html_path)) + lines_in = hf.readlines() # read whole file lines_out = [] for line in lines_in: # embed favicons into head @@ -86,8 +93,8 @@ if __name__ == "__main__": # remove all currently embedded favicons if (not (line.startswith('<link') and 'icon' in line)): lines_out.append(line) - hf.seek(0) # move to beginning of file - hf.write(''.join(lines_out)) # overwrite with new data - hf.truncate() # remove trailing old data - + hf.seek(0) # move to beginning of file + hf.write(''.join(lines_out)) # overwrite with new data + hf.truncate() # remove trailing old data + print('Finished.')