Skip to content

Latest commit

History

78 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

CSS Parser

This is a a CSS modeling utility written in pure Python.

Features

StyleSheet

  1. [✓] Create from file
  2. [✓] Create from string
  3. [✓] Write to file
  4. [✓] Write to string
  5. [✓] Get rules by query string
  6. [✓] Remove rule
  7. [✓] Prepend rule
  8. [✓] Append rule
  9. [✓] Add rule before/after existing rule
  10. Comment out rule
  11. [✓] Add rule to media-query
  12. Add rule with new media-query
  13. Pretty-Printing

Rule

  1. [✓] Create Rule from string
  2. [✓] Create multiple rules from a string
  3. [✓] Write Rule to string
  4. [✓] Get declarations by property
  5. [✓] Remove declaration
  6. [✓] Append declaration
  7. [✓] Prepend declaration
  8. [✓] Add declaration before/after existing declaration
  9. Comment out declaration
  10. Pretty-Printing

Dependencies

It has no dependencies outside of the Python standard library.

Python Version

Developed and tested in v2.7.5.

Usage

StyleSheet

fromcss_parser.stylesheetimportStyleSheet# parse css stringtext='body {margin:0;} p.indent{text-indent:1em;}'stylesheet=StyleSheet.from_string(text)
# parse css filestylesheet=StyleSheet.from_file('path/to/css/file.css')
# write to stringtxt=stylesheet.to_string()
# write to filestylesheet.to_file('path/to/css/file.css')
# get all rulesallrules=stylesheet.get_rules()
# get rules by querysomerules=stylesheet.get_rules('p.indent')
# remove rulestylesheet.remove_rule(rule)
# append rulestylesheet.append_rule(newrule)
# append rule after existing rulestylesheet.append_rule(newrule, existingrule)
# prepend rulestylesheet.prepend_rule(newrule)
# prepend rule before existing rulestylesheet.prepend_rule(newrule, existingrule)
# insert rule into media-querymediaquery=existingrule.get_mediaquery()
newrule.set_mediaquery(mediaquery)
stylesheet.append(newrule, mediaquery)
'''If the media-queries don't match, the new rule will be added before/after the media-query, depending on whether prepend_rule or append_rule is used.'''

Rule

fromcss_parser.ruleimportRule# create rule from stringtext='blockquote {margin:1em 5% 1em 5%;}'rule=Rule.from_string(text)
# media-queries are retainedtext='@media all {blockquote {margin:1em 5% 1em 5%;} }'rule=Rule.from_string(text)
# multiple rules can be generated in this way, if desiredtext='/* multiple rules */' \
'@media all {' \
'	blockquote {' \
' margin:1em 5% 1em 5%;' \
'	}' \
'	p {' \
' color:blue;' \
'	}' \
'}'rule=Rule.from_string(text)
'''Note: If it is important that comments and whitespace bepreserved, use StyleSheet.from_string() rather than Rule.from_string().'''# write single rule to string# includes media-query, if applicablerule.to_string()
# get all declarationsalldeclarations=rule.get_declarations()
# get declarations by querysomedeclarations=rule.get_declarations('margin')
# remove declarationrule.remove_declaration(declaration)
# append declarationrule.append_declaration(declaration)
# insert declaration after existing declarationrule.append_declaration(declaration, existingdeclaration)
# prepend declarationrule.append_declaration(declaration)
# insert declaration before existing declarationrule.prepend_declaration(declaration, existingdeclaration)

Declaration

fromcss_parser.ruleimportRule# create declaration from stringtext='margin:1em 5% 1em 5%;'declaration=Declaration.from_string(text)
# get propertyprop_string=declaration.get_property()
# get valueval_string=declaration.get_value()

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages