From 1ef6b8397917c4c31470fd14fa2b773651e40a3c Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 27 Feb 2022 12:44:47 +0000 Subject: [PATCH] Reading pwa colors into the meta html headers --- daemon.py | 32 ++++---------------------------- webapp_utils.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/daemon.py b/daemon.py index 2bcf31b30..649cf4335 100644 --- a/daemon.py +++ b/daemon.py @@ -167,6 +167,7 @@ from webapp_utils import html_hashtag_blocked from webapp_utils import html_following_list from webapp_utils import set_blog_address from webapp_utils import html_show_share +from webapp_utils import get_pwa_theme_colors from webapp_calendar import html_calendar_delete_confirm from webapp_calendar import html_calendar from webapp_about import html_about @@ -6878,35 +6879,10 @@ class PubServer(BaseHTTPRequestHandler): getreq_start_time) -> None: """gets the PWA manifest """ - default_pwa_theme_color = 'apple-mobile-web-app-status-bar-style' - default_pwa_theme_background_color = 'black-translucent' - pwa_theme_color = default_pwa_theme_color - pwa_theme_background_color = default_pwa_theme_background_color - css_filename = base_dir + '/epicyon.css' - if os.path.isfile(css_filename): - css_str = '' - with open(css_filename, 'r') as fp_css: - css_str = fp_css.read() - if '--pwa-theme-color:' in css_str: - pwa_theme_color = css_str.split('--pwa-theme-color:')[1] - if ';' in pwa_theme_color: - pwa_theme_color = pwa_theme_color.split(';')[0].strip() - pwa_theme_color = remove_html(pwa_theme_color) - if ' ' in pwa_theme_color: - pwa_theme_color = \ - default_pwa_theme_color - if '--pwa-theme-background-color:' in css_str: - pwa_theme_background_color = \ - css_str.split('--pwa-theme-background-color:')[1] - if ';' in pwa_theme_background_color: - pwa_theme_background_color = \ - pwa_theme_background_color.split(';')[0].strip() - pwa_theme_background_color = \ - remove_html(pwa_theme_background_color) - if ' ' in pwa_theme_background_color: - pwa_theme_background_color = \ - default_pwa_theme_background_color + pwa_theme_color, pwa_theme_background_color = \ + get_pwa_theme_colors(css_filename) + app1 = "https://f-droid.org/en/packages/eu.siacs.conversations" app2 = "https://staging.f-droid.org/en/packages/im.vector.app" app3 = \ diff --git a/webapp_utils.py b/webapp_utils.py index 3ea3d5c06..ed6103d79 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -538,11 +538,56 @@ def get_right_image_file(base_dir: str, account_dir, nickname, domain, theme) +def _get_variable_from_css(css_str: str, variable: str) -> str: + """Gets a variable value from the css file text + """ + if '--' + variable + ':' not in css_str: + return None + value = css_str.split('--' + variable + ':')[1] + if ';' in value: + value = value.split(';')[0].strip() + value = remove_html(value) + if ' ' in value: + value = None + return value + + +def get_pwa_theme_colors(css_filename: str) -> (str, str): + """Gets the theme/statusbar color for progressive web apps + """ + default_pwa_theme_color = 'apple-mobile-web-app-status-bar-style' + pwa_theme_color = default_pwa_theme_color + + default_pwa_theme_background_color = 'black-translucent' + pwa_theme_background_color = default_pwa_theme_background_color + + if not os.path.isfile(css_filename): + return pwa_theme_color, pwa_theme_background_color + + css_str = '' + with open(css_filename, 'r') as fp_css: + css_str = fp_css.read() + + pwa_theme_color = \ + _get_variable_from_css(css_str, 'pwa-theme-color') + if not pwa_theme_color: + pwa_theme_color = default_pwa_theme_color + + pwa_theme_background_color = \ + _get_variable_from_css(css_str, 'pwa-theme-background-color') + if not pwa_theme_background_color: + pwa_theme_background_color = default_pwa_theme_background_color + + return pwa_theme_color, pwa_theme_background_color + + def html_header_with_external_style(css_filename: str, instance_title: str, metadata: str, lang='en') -> str: if metadata is None: metadata = '' css_file = '/' + css_filename.split('/')[-1] + pwa_theme_color, pwa_theme_background_color = \ + get_pwa_theme_colors(css_filename) html_str = \ '\n' + \ '\n' + \ @@ -557,8 +602,10 @@ def html_header_with_external_style(css_filename: str, instance_title: str, ' \n' + \ ' \n' + \ - ' \n' + \ + ' \n' + \ metadata + \ + ' ' + \ ' ' + instance_title + '\n' + \ ' \n' + \ ' \n'