Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- styles = {
- '.text.highlight':{
- 'color': 'black',
- 'background': 'blue',
- 'font-family': 'Garamond',
- 'psudo': {
- ':not(h1)': {
- 'font-size': '1.5rem'
- },
- ':hover': {
- 'background': 'red'
- }
- },
- },
- '.underline':{
- 'text-decoration': 'underline'
- },
- '#main':{
- 'color': 'red',
- "psudo":{
- '::after':{
- 'height': '4px',
- 'width': '100%',
- 'background': 'green'
- }
- }
- }
- }
- def get_last(obj):
- # get generic last key
- last = list(obj.keys())[-1]
- # check to see if it is psudo
- if last == 'psudo':
- # if it is jump back one
- return list(obj.keys())[-2]
- else:
- # if not return generic last
- return last
- def create_css_rule_str(obj):
- # initialize string
- css_rule_str = f''
- last = get_last(obj)
- # loop through all selectors in object
- for prop in obj:
- # double check it is not 'psudo'
- if prop != 'psudo':
- # append rule to string (with correct formatting)
- css_rule_str += f'\t{prop}: {obj[prop]};'
- if prop != last:
- # add return charactor if it is not the last one
- css_rule_str += '\n'
- # return rules
- return css_rule_str
- def write_rules(selector, rules, sheet):
- # create string to append
- css_str = selector + '{ \n' + rules + ' \n}; \n\r'
- # append string to file
- sheet.write(css_str)
- def createCSS(styles, name):
- # create file
- css = open(f'{name}.css', 'w+')
- # loop through generic styles
- for selector in styles:
- css_rule_str = create_css_rule_str(styles[selector])
- write_rules(selector, css_rule_str, css)
- # loop through nested psudo styles
- if 'psudo' in styles[selector]:
- for psudoselect in styles[selector]["psudo"]:
- css_rule_str = create_css_rule_str(styles[selector]["psudo"][psudoselect])
- write_rules((selector + psudoselect), css_rule_str, css)
- css.close()
- createCSS(styles, 'test')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement