Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. styles = {
  2. '.text.highlight':{
  3. 'color': 'black',
  4. 'background': 'blue',
  5. 'font-family': 'Garamond',
  6. 'psudo': {
  7. ':not(h1)': {
  8. 'font-size': '1.5rem'
  9. },
  10. ':hover': {
  11. 'background': 'red'
  12. }
  13. },
  14. },
  15. '.underline':{
  16. 'text-decoration': 'underline'
  17. },
  18. '#main':{
  19. 'color': 'red',
  20. "psudo":{
  21. '::after':{
  22. 'height': '4px',
  23. 'width': '100%',
  24. 'background': 'green'
  25. }
  26. }
  27. }
  28. }
  29.  
  30. def get_last(obj):
  31. # get generic last key
  32. last = list(obj.keys())[-1]
  33.  
  34. # check to see if it is psudo
  35. if last == 'psudo':
  36. # if it is jump back one
  37. return list(obj.keys())[-2]
  38. else:
  39. # if not return generic last
  40. return last
  41.  
  42. def create_css_rule_str(obj):
  43. # initialize string
  44. css_rule_str = f''
  45. last = get_last(obj)
  46. # loop through all selectors in object
  47. for prop in obj:
  48. # double check it is not 'psudo'
  49. if prop != 'psudo':
  50. # append rule to string (with correct formatting)
  51. css_rule_str += f'\t{prop}: {obj[prop]};'
  52. if prop != last:
  53. # add return charactor if it is not the last one
  54. css_rule_str += '\n'
  55. # return rules
  56. return css_rule_str
  57.  
  58. def write_rules(selector, rules, sheet):
  59. # create string to append
  60. css_str = selector + '{ \n' + rules + ' \n}; \n\r'
  61.  
  62. # append string to file
  63. sheet.write(css_str)
  64.  
  65. def createCSS(styles, name):
  66. # create file
  67. css = open(f'{name}.css', 'w+')
  68.  
  69. # loop through generic styles
  70. for selector in styles:
  71. css_rule_str = create_css_rule_str(styles[selector])
  72. write_rules(selector, css_rule_str, css)
  73.  
  74. # loop through nested psudo styles
  75. if 'psudo' in styles[selector]:
  76. for psudoselect in styles[selector]["psudo"]:
  77. css_rule_str = create_css_rule_str(styles[selector]["psudo"][psudoselect])
  78. write_rules((selector + psudoselect), css_rule_str, css)
  79.  
  80. css.close()
  81.  
  82. createCSS(styles, 'test')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement