Skip to content

Instantly share code, notes, and snippets.

@Sovetnikov
Created November 26, 2020 10:13
Show Gist options
  • Save Sovetnikov/80afc4250a976270e43a81b173229717 to your computer and use it in GitHub Desktop.
Save Sovetnikov/80afc4250a976270e43a81b173229717 to your computer and use it in GitHub Desktop.
Remove all unmodified css from customized buefy theme
// Import Bulma's core
@import "../../node_modules/bulma/sass/utilities/_all";
// Import Bulma and Buefy styles
@import "../../node_modules/bulma/bulma.sass";
@import "../../node_modules/buefy/src/scss/buefy";
# Python 3.8
import hashlib
import re
import shutil
import sys
from collections import defaultdict
from copy import deepcopy
import tinycss2
from tinycss2 import parse_stylesheet_bytes
from tinycss2.ast import QualifiedRule, AtRule
original_scss = 'original_theme.scss'
theme_scss = 'theme.scss'
def _convert(scss_file):
import subprocess
result = subprocess.run([shutil.which('npx'), 'node-sass', scss_file], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if result.stderr:
print(result.stderr.decode(), file=sys.stderr)
raise Exception(f'Error converting {scss_file}')
return result.stdout
theme_css = _convert(theme_scss)
original_css = _convert(original_scss)
theme = parse_stylesheet_bytes(theme_css, skip_comments=True, skip_whitespace=True)
original = parse_stylesheet_bytes(original_css, skip_comments=True, skip_whitespace=True)
def _clean_str(content):
return re.sub('[\r\n\t]', '', re.sub(' +', ' ', content)).strip()
def _get_rule_id(prelude):
c = tinycss2.serialize(prelude)
return _clean_str(c)
def _get_content(content):
c = tinycss2.serialize(content)
c = _clean_str(c)
return hashlib.md5(c.encode()).hexdigest()
original_rules = defaultdict(list)
for item in original[0]:
if isinstance(item, QualifiedRule):
id = _get_rule_id(item.prelude)
content_hash = _get_content(item.content)
original_rules[id].append(content_hash)
elif isinstance(item, AtRule):
rule_id = _get_rule_id(item.prelude)
for inner_item in tinycss2.parse_rule_list(item.content, skip_comments=True, skip_whitespace=True):
inner_id = _get_rule_id(inner_item.prelude)
content_hash = _get_content(inner_item.content)
original_rules[(rule_id, inner_id)].append(content_hash)
modified_items = []
for item in theme[0]:
if isinstance(item, QualifiedRule):
id = _get_rule_id(item.prelude)
if id in original_rules:
content_hash = _get_content(item.content)
if content_hash not in original_rules[id]:
modified_items.append(item)
else:
modified_items.append(item)
elif isinstance(item, AtRule):
rule_item = item
rule_id = _get_rule_id(rule_item.prelude)
empty_rule_item = None
for inner_item in tinycss2.parse_rule_list(rule_item.content, skip_comments=True, skip_whitespace=True):
inner_id = _get_rule_id(inner_item.prelude)
content_hash = _get_content(inner_item.content)
if content_hash not in original_rules[(rule_id, inner_id)]:
if empty_rule_item is None:
empty_rule_item = deepcopy(rule_item)
empty_rule_item.content = []
modified_items.append(empty_rule_item)
empty_rule_item.content.append(inner_item)
print(tinycss2.serialize(modified_items))
// https://buefy.org/documentation/customization/
// Import Bulma's core
@import "~bulma/sass/utilities/_all";
// Set your colors
$primary: #8c67ef;
$primary-light: findLightColor($primary);
$primary-dark: findDarkColor($primary);
$primary-invert: findColorInvert($primary);
$twitter: #4099FF;
$twitter-invert: findColorInvert($twitter);
// Lists and maps
$custom-colors: null !default;
$custom-shades: null !default;
// Setup $colors to use as bulma classes (e.g. 'is-twitter')
$colors: mergeColorMaps(
(
"white": (
$white,
$black,
),
"black": (
$black,
$white,
),
"light": (
$light,
$light-invert,
),
"dark": (
$dark,
$dark-invert,
),
"primary": (
$primary,
$primary-invert,
$primary-light,
$primary-dark,
),
"link": (
$link,
$link-invert,
$link-light,
$link-dark,
),
"info": (
$info,
$info-invert,
$info-light,
$info-dark,
),
"success": (
$success,
$success-invert,
$success-light,
$success-dark,
),
"warning": (
$warning,
$warning-invert,
$warning-light,
$warning-dark,
),
"danger": (
$danger,
$danger-invert,
$danger-light,
$danger-dark,
),
),
$custom-colors
);
// Links
$link: $primary;
$link-invert: $primary-invert;
$link-focus-border: $primary;
// Import Bulma and Buefy styles
@import "~bulma";
@import "~buefy/src/scss/buefy";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment