From 9594fa1eeebf260ec1e060fff3cd32450d80be4f Mon Sep 17 00:00:00 2001 From: Alexandar Mechev Date: Tue, 6 Oct 2020 10:09:49 +0200 Subject: [PATCH] adds theme support for reskinning cards the theme.css file can include a .reskin selector the filter.py file will parse the css file and replace the card color accordingly --- app/filter.py | 27 ++++++++++++++++++++++++-- app/static/css/themes/nord-dark.css | 10 ++++++++++ app/static/css/themes/nord-light.css | 10 ++++++++++ app/static/css/themes/whoogle-dark.css | 5 +++++ app/templates/display.html | 2 +- app/templates/index.html | 2 +- requirements.txt | 1 + 7 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app/filter.py b/app/filter.py index 1a83c3f..16fe089 100644 --- a/app/filter.py +++ b/app/filter.py @@ -5,7 +5,7 @@ from cryptography.fernet import Fernet import re import urllib.parse as urlparse from urllib.parse import parse_qs - +import cssutils class Filter: def __init__(self, user_keys: dict, mobile=False, config=None): @@ -30,13 +30,36 @@ class Filter: def elements(self): return self._elements + def parse_theme_colors(self): + """parses the css theme and looks for reskinning colors. Specifically looks for the .reskin CSS selector. + If no selector is found, returns default background 'fff' + + Returns: + dict: Key-value dictionary of the properties + """ + result = {'background-color':'fff'} + if self.theme: + if self.dark: + sheet = cssutils.parseString(open(f'app/static/css/themes/{self.theme}-dark.css').read()) + else: + sheet = cssutils.parseString(open(f'app/static/css/themes/{self.theme}-light.css').read()) + reskin_selector = [i for i in sheet if i.selectorText=='.reskin'] + if len(reskin_selector) == 0: + return result + for css_prop in reskin_selector[0].style: + result[css_prop.name] = css_prop.value + return result + def reskin(self, page): # Aesthetic only re-skinning page = page.replace('>G<', '>Wh<') pattern = re.compile('4285f4|ea4335|fbcc05|34a853|fbbc05', re.IGNORECASE) page = pattern.sub('685e79', page) + + theme_colors = self.parse_theme_colors() + page = page.replace('fff', theme_colors['background-color'].replace("#","")) if self.dark: - page = page.replace('fff', '2E3440').replace('202124', 'ddd').replace('1967D2', '3b85ea') + page = page.replace('202124', 'ddd').replace('1967D2', '3b85ea') return page diff --git a/app/static/css/themes/nord-dark.css b/app/static/css/themes/nord-dark.css index 6eee324..9b91ebb 100644 --- a/app/static/css/themes/nord-dark.css +++ b/app/static/css/themes/nord-dark.css @@ -40,3 +40,13 @@ input { .search-container { background-color: #292E39 !important; } + +footer { + background-color: #292E39 !important; + color: #ECEFF4; +} + +.reskin { + background-color: #2E3440; + color: #ECEFF4; +} \ No newline at end of file diff --git a/app/static/css/themes/nord-light.css b/app/static/css/themes/nord-light.css index ee1d331..573699d 100644 --- a/app/static/css/themes/nord-light.css +++ b/app/static/css/themes/nord-light.css @@ -40,3 +40,13 @@ input { .search-container { background-color: #f9fafb !important; } + +footer { + background-color: #f9fafb !important; + color: #3B4252; +} + +.reskin { + background-color: #ffffff; + color: #3B4252; +} \ No newline at end of file diff --git a/app/static/css/themes/whoogle-dark.css b/app/static/css/themes/whoogle-dark.css index 36cfada..0ffc496 100644 --- a/app/static/css/themes/whoogle-dark.css +++ b/app/static/css/themes/whoogle-dark.css @@ -40,3 +40,8 @@ input { .search-container { background-color: #000 !important; } + +.reskin { + background-color: #000; + color: #fff; +} \ No newline at end of file diff --git a/app/templates/display.html b/app/templates/display.html index 2153346..0cb2b15 100644 --- a/app/templates/display.html +++ b/app/templates/display.html @@ -25,7 +25,7 @@ {{ response|safe }}