Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fc-pregmod
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pregmodfan
fc-pregmod
Commits
a6b22ab5
Commit
a6b22ab5
authored
6 years ago
by
prndev
Browse files
Options
Downloads
Patches
Plain Diff
Added tool for embedding favicons.
parent
45f3b356
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!2136
Embedded favicons
,
!2047
RA rework
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
devTools/embed_favicon.py
+89
-0
89 additions, 0 deletions
devTools/embed_favicon.py
with
89 additions
and
0 deletions
devTools/embed_favicon.py
0 → 100644
+
89
−
0
View file @
a6b22ab5
#!/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.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment