Skip to content
Snippets Groups Projects
Commit ed1d11b7 authored by ezsh's avatar ezsh
Browse files

Embed favicons into the result file

Modify embed_favicon.py to pass HTML file name (or use
bin/FC_pregmod.html by default) to embed favicon into. An HTML
header for the story format was replaced by a format.js file, and
therefore we can't embed favicon there anymore, hence we switch
over to the story file itself.

Closes #461.
parent f69400b7
No related branches found
No related tags found
1 merge request!3495Embed favicons into the result file
#!/usr/bin/env python3 #!/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. Script file is expected to reside in devTools directory.
Note: This does not actually check the image file's contents for size detection. Note: This does not actually check the image file's contents for size detection.
Usage: Usage:
python3 embed_favicon.py python3 embed_favicon.py [FC_pregmod.html]
''' '''
import sys import sys
...@@ -22,22 +22,28 @@ ext2mimetype = { ...@@ -22,22 +22,28 @@ ext2mimetype = {
'.ico': 'image/x-icon' '.ico': 'image/x-icon'
} }
# reads a file, turns it into a data uri # reads a file, turns it into a data uri
def data_uri_from_file(filename, mimetype): def data_uri_from_file(filename, mimetype):
data = open(filename,'rb').read() data = open(filename, 'rb').read()
base64data = base64.b64encode(data).decode('ascii') base64data = base64.b64encode(data).decode('ascii')
out = 'data:%s;base64,%s'%(mimetype, base64data) out = 'data:%s;base64,%s' % (mimetype, base64data)
return out return out
if __name__ == "__main__": if __name__ == "__main__":
# find project root directory path # find project root directory path
# (script file is expected to reside in devTools) # (script file is expected to reside in devTools)
project_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) project_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# path to SugarCube's header.html html_path = ''
header_html_path = os.path.join( if (len(sys.argv) > 1):
project_root_path, html_path = sys.argv[1]
'devTools/tweeGo/storyFormats/sugarcube-2/header.html' 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 # path to directory containing all favicons to embed
favicons_source_path = os.path.join( favicons_source_path = os.path.join(
project_root_path, project_root_path,
...@@ -45,18 +51,19 @@ if __name__ == "__main__": ...@@ -45,18 +51,19 @@ if __name__ == "__main__":
) )
# walk directory for all files # walk directory for all files
favicons_paths = [ favicons_paths = [
os.path.join(dirpath, filename) os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk(favicons_source_path) for dirpath, dirnames, filenames in os.walk(favicons_source_path)
for filename in filenames for filename in filenames
] ]
# ignore files with unknown extensions # 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 # prepare embedded data
size_from_filename = re.compile(r'([0-9]+)\....$') size_from_filename = re.compile(r'([0-9]+)\....$')
favicons_html = [] favicons_html = []
for fp in favicons_paths: for fp in favicons_paths:
print('Found favicon source file "%s".'%(fp)) print('Found favicon source file "%s".' % (fp))
# get mimetype by file extension # get mimetype by file extension
mimetype = ext2mimetype[fp[-4:]] mimetype = ext2mimetype[fp[-4:]]
if (mimetype == 'image/x-icon'): if (mimetype == 'image/x-icon'):
...@@ -65,19 +72,19 @@ if __name__ == "__main__": ...@@ -65,19 +72,19 @@ if __name__ == "__main__":
else: else:
# guess icon size from file name # guess icon size from file name
size = size_from_filename.search(fp).group(1) 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) data = data_uri_from_file(fp, mimetype)
favicons_html.append( favicons_html.append(
# prepare html with favicon data embedded # 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 mimetype, sizes, data
) )
) )
# modify header file # modify header file
with open(header_html_path,'r+',encoding='utf-8') as hf: with open(html_path, 'r+', encoding='utf-8') as hf:
print('Rewriting "%s"...'%(header_html_path)) print('Rewriting "%s"...' % (html_path))
lines_in = hf.readlines() # read whole file lines_in = hf.readlines() # read whole file
lines_out = [] lines_out = []
for line in lines_in: for line in lines_in:
# embed favicons into head # embed favicons into head
...@@ -86,8 +93,8 @@ if __name__ == "__main__": ...@@ -86,8 +93,8 @@ if __name__ == "__main__":
# remove all currently embedded favicons # remove all currently embedded favicons
if (not (line.startswith('<link') and 'icon' in line)): if (not (line.startswith('<link') and 'icon' in line)):
lines_out.append(line) lines_out.append(line)
hf.seek(0) # move to beginning of file hf.seek(0) # move to beginning of file
hf.write(''.join(lines_out)) # overwrite with new data hf.write(''.join(lines_out)) # overwrite with new data
hf.truncate() # remove trailing old data hf.truncate() # remove trailing old data
print('Finished.') print('Finished.')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment