Skip to content
Snippets Groups Projects
Commit ee58a149 authored by Pregmodder's avatar Pregmodder
Browse files

Merge branch 'embed_favicon' into 'pregmod-master'

Embedded favicons

See merge request pregmodfan/fc-pregmod!2136
parents e2135fce 31cad589
No related branches found
No related tags found
No related merge requests found
Showing
with 94 additions and 45 deletions
#!/usr/bin/env python3
'''
Script for embedding favicons into the SugarCube 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
'''
import sys
import os
import re
import base64
# file extensions eligible for use as favicons and their mimetype
ext2mimetype = {
'.png': 'image/png',
'.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()
base64data = base64.b64encode(data).decode('ascii')
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(__file__))
# path to SugarCube's header.html
header_html_path = os.path.join(
project_root_path,
'devTools/tweeGo/storyFormats/sugarcube-2/header.html'
)
# path to directory containing all favicons to embed
favicons_source_path = os.path.join(
project_root_path,
'resources/raster/favicon/'
)
# 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
]
# ignore files with unknown extensions
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:
# get mimetype by file extension
mimetype = ext2mimetype[fp[-4:]]
if (mimetype == 'image/x-icon'):
# assume sizes in ico
sizes = '16x16 32x32 64x64'
else:
# guess icon size from file name
size = size_from_filename.search(fp).group(1)
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'%(
mimetype, sizes, data
)
)
# modify header file
with open(header_html_path,'r+') as hf:
lines_in = hf.readlines() # read whole file
lines_out = []
for line in lines_in:
# remove all currently embedded favicons
if (not (line.startswith('<link') and 'icon' in line)):
lines_out.append(line)
# embed favicons into head
if (line.startswith('<head>')):
lines_out.extend(favicons_html)
hf.seek(0) # move to beginning of file
hf.write(''.join(lines_out)) # overwrite with new data
hf.truncate() # remove trailing old data
This diff is collapsed.
<link rel="shortcut icon" href="resources/vector/favicon/arcologyVector.ico?v=2.4.5">
<link rel="icon" sizes="16x16 32x32 64x64" href="resources/vector/favicon/arcologyVector.ico?v=2.4.5">
<link rel="icon" type="image/png" sizes="196x196" href="resources/vector/favicon/arcologyVector-192.png?v=2.4.5">
<link rel="icon" type="image/png" sizes="160x160" href="resources/vector/favicon/arcologyVector-160.png?v=2.4.5">
<link rel="icon" type="image/png" sizes="96x96" href="resources/vector/favicon/arcologyVector-96.png?v=2.4.5">
<link rel="icon" type="image/png" sizes="64x64" href="resources/vector/favicon/arcologyVector-64.png?v=2.4.5">
<link rel="icon" type="image/png" sizes="32x32" href="resources/vector/favicon/arcologyVector-32.png?v=2.4.5">
<link rel="icon" type="image/png" sizes="16x16" href="resources/vector/favicon/arcologyVector-16.png?v=2.4.5">
<link rel="apple-touch-icon" href="resources/vector/favicon/arcologyVector-57.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="114x114" href="resources/vector/favicon/arcologyVector-114.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="72x72" href="resources/vector/favicon/arcologyVector-72.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="144x144" href="resources/vector/favicon/arcologyVector-144.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="60x60" href="resources/vector/favicon/arcologyVector-60.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="120x120" href="resources/vector/favicon/arcologyVector-120.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="76x76" href="resources/vector/favicon/arcologyVector-76.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="152x152" href="resources/vector/favicon/arcologyVector-152.png?v=2.4.5">
<link rel="apple-touch-icon" sizes="180x180" href="resources/vector/favicon/arcologyVector-180.png?v=2.4.5">
<meta name="msapplication-TileColor" content="#FFFFFF">
<meta name="msapplication-TileImage" content="resources/vector/favicon/arcologyVector-144.png?v=2.4.5">
<meta name="msapplication-config" content="resources/vector/favicon/browserconfig.xml">
\ No newline at end of file
I don't know how to link these files when the files aren't in the same
directory as the HTML file, so hopefully someone will be able to figure it out
\ No newline at end of file
resources/vector/favicon/arcologyVector-114.png

5.88 KiB

resources/vector/favicon/arcologyVector-120.png

6.08 KiB

resources/vector/favicon/arcologyVector-150.png

7.41 KiB

resources/vector/favicon/arcologyVector-152.png

7.52 KiB

resources/vector/favicon/arcologyVector-160.png

6.93 KiB

resources/vector/favicon/arcologyVector-180.png

8.88 KiB

resources/vector/favicon/arcologyVector-192.png

8.66 KiB

resources/vector/favicon/arcologyVector-57.png

3.08 KiB

resources/vector/favicon/arcologyVector-60.png

3.23 KiB

resources/vector/favicon/arcologyVector-64.png

3.41 KiB

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