### Install csscompressor Source: https://github.com/sprymix/csscompressor/blob/master/README.rst Install the package using pip. ```bash $ pip install csscompressor ``` -------------------------------- ### Color Optimizations Source: https://context7.com/sprymix/csscompressor/llms.txt Demonstrates automatic color optimizations, including converting RGB to hex and shortening long hex codes. Ensure the `csscompressor` library is installed. ```python from csscompressor import compress # Color optimizations: RGB to hex, long hex to short hex css = ''' .colors { color: rgb(255, 0, 0); background: #AABBCC; border-color: #FF0000; } ''' print(compress(css)) ``` -------------------------------- ### Access Command Line Help Source: https://github.com/sprymix/csscompressor/blob/master/README.rst Display the help menu for the csscompressor command line interface. ```bash $ python3 -m csscompressor --help ``` -------------------------------- ### Command Line Interface Source: https://context7.com/sprymix/csscompressor/llms.txt The csscompressor module can be run directly from the command line to compress CSS files. It supports compressing multiple input files, specifying output files, and setting line break columns. ```APIDOC ## Command Line Interface ### Description Provides a command-line interface for compressing CSS files directly. ### Usage `python -m csscompressor [options] input [input ...]` ### Options - `--help` or `-h`: Display help and version information. - `--line-break ` or `-l `: Specify the column number for line breaks. - `--output ` or `-o `: Specify the output file path. ### Examples #### Display help and version information ```bash python -m csscompressor --help ``` ``` CSS Compressor 0.9.4 usage: csscompressor [-h] [--line-break ] [-o ] input [input ...] ``` #### Compress a single CSS file and output to stdout ```bash python -m csscompressor styles.css ``` #### Compress multiple CSS files (concatenated) to stdout ```bash python -m csscompressor reset.css base.css components.css ``` #### Compress and write to output file ```bash python -m csscompressor styles.css -o styles.min.css ``` #### Compress with line breaks after 80 characters ```bash python -m csscompressor styles.css --line-break 80 -o styles.min.css ``` ``` -------------------------------- ### Build Script Integration Source: https://context7.com/sprymix/csscompressor/llms.txt Integrate CSS compression into your build process using this bash script. It iterates through CSS files in a source directory and outputs minified versions to a distribution directory. ```bash #!/bin/bash for file in src/css/*.css; do filename=$(basename "$file" .css) python -m csscompressor "$file" -o "dist/css/${filename}.min.css" done ``` -------------------------------- ### Compress CSS via Command Line Source: https://context7.com/sprymix/csscompressor/llms.txt Execute the module directly from the terminal to process files, concatenate outputs, or specify line breaks. ```bash # Display help and version information python -m csscompressor --help # Output: CSS Compressor 0.9.4 # usage: csscompressor [-h] [--line-break ] [-o ] input [input ...] # Compress a single CSS file and output to stdout python -m csscompressor styles.css # Compress multiple CSS files (concatenated) to stdout python -m csscompressor reset.css base.css components.css # Compress and write to output file python -m csscompressor styles.css -o styles.min.css # Compress with line breaks after 80 characters python -m csscompressor styles.css --line-break 80 -o styles.min.css ``` -------------------------------- ### Programmatic API - compress_partitioned Source: https://context7.com/sprymix/csscompressor/llms.txt Compresses CSS and splits the output into multiple chunks based on a maximum number of rules per file. This is essential for Internet Explorer 9 and earlier which have an artificial limit of 4096 CSS rules per stylesheet. Returns a list of compressed CSS strings. ```APIDOC ## compress_partitioned ### Description Compresses CSS and partitions the output into chunks, each containing a maximum number of rules. This is useful for compatibility with older browsers like IE9 that have a limit on CSS rules per file. ### Method `compress_partitioned(css_input, max_rules_per_file=4096, max_linelen=0)` ### Parameters #### Arguments - **css_input** (string) - Required - The CSS string to compress and partition. - **max_rules_per_file** (integer) - Optional - The maximum number of CSS rules allowed in each output chunk. Defaults to 4096. - **max_linelen** (integer) - Optional - Maximum line length for output within each chunk. If 0, no line breaks are inserted. Defaults to 0. ### Request Example ```python from csscompressor import compress_partitioned css_input = ''' a {content: '}}'} b {content: '}'} c {content: '{'} ''' chunks = compress_partitioned(css_input, max_rules_per_file=2) print(chunks) ``` ### Response #### Success Response (list of strings) - **chunks** (list) - A list where each element is a compressed CSS string representing a chunk. #### Response Example ``` ["a{content:'}}'}b{content:'}'}", "c{content:'{'} "] ``` ### Additional Examples #### Handling @media blocks ```python css_media = ''' @media screen { a {padding: 1px} b {padding: 2px} x {padding: 2px} } @media print { c {padding: 1px} d {padding: 2px} y {padding: 2px} } @media handheld { e {padding: 1px} f {padding: 2px} z {padding: 2px} } ''' chunks = compress_partitioned(css_media, max_rules_per_file=2) print(chunks) ``` ``` ['@media screen{a{padding:1px}b{padding:2px}x{padding:2px}}', '@media print{c{padding:1px}d{padding:2px}y{padding:2px}}', '@media handheld{e{padding:1px}f{padding:2px}z{padding:2px}}'] ``` #### Selector lists count each selector ```python css_selectors = ''' a, a1, a2 {color: red} b, b2, b3 {color: red} c, c3, c4, c5 {color: red} d {color: red} ''' chunks = compress_partitioned(css_selectors, max_rules_per_file=2) print(chunks) ``` ``` ['a,a1,a2{color:red}', 'b,b2,b3{color:red}', 'c,c3,c4,c5{color:red}', 'd{color:red}'] ``` #### Combined with line length limit ```python chunks = compress_partitioned(css_media, max_rules_per_file=2, max_linelen=6) print(chunks[0]) ``` ``` '@media screen{a{padding:1px}\nb{padding:2px}x{padding:2px}\n}' ``` ``` -------------------------------- ### Zero Value Optimizations Source: https://context7.com/sprymix/csscompressor/llms.txt Shows how the compressor removes units from zero values (e.g., 0px to 0) and handles 'none' properties. This reduces CSS size without altering layout. ```python from csscompressor import compress # Zero value optimizations css = ''' .zeros { margin: 0px 0em 0% 0in; padding: 0.5em 0 1.0em 0; border: none; outline: none; } ''' print(compress(css)) ``` -------------------------------- ### Modern @supports Directive Source: https://context7.com/sprymix/csscompressor/llms.txt Demonstrates the preservation of modern CSS directives like `@supports`. This ensures compatibility and allows for feature detection in stylesheets. ```python from csscompressor import compress # Modern @supports directive css = ''' @supports (aspect-ratio: 16/9) { .video { aspect-ratio: 16/9; } } ''' print(compress(css)) ``` -------------------------------- ### Handle Malformed calc() Expressions Source: https://context7.com/sprymix/csscompressor/llms.txt Demonstrates error handling for malformed `calc()` expressions, specifically missing closing parentheses. The `compress` function raises a `ValueError` in such cases. ```python from csscompressor import compress, compress_partitioned # Handling malformed calc() expressions try: css = 'a { width: calc(10px-10% }' # Missing closing parenthesis compress(css) except ValueError as e: print(f"Compression failed: {e}") ``` -------------------------------- ### Minify CSS with Programmatic API Source: https://context7.com/sprymix/csscompressor/llms.txt Use the compress function to minify CSS strings, with options for line length limits and preserving important comments. ```python from csscompressor import compress # Basic CSS compression css_input = ''' .container { margin: 0px 0px 0px 0px; padding: 10px; background-color: #AABBCC; } /* This comment will be removed */ .button { color: rgb(255, 0, 0); border: none; } ''' result = compress(css_input) print(result) # Output: .container{margin:0;padding:10px;background-color:#abc}.button{color:red;border:0} # Compression with line length limit (useful for version control) css_multiline = ''' a {content: '}}'} b {content: '}'} c {content: '{'} ''' result_with_breaks = compress(css_multiline, max_linelen=20) print(result_with_breaks) # Output: a{content:'}}'}\nb{content:'}'}\nc{content:'{'} # Preserve important comments (marked with /*!) css_with_license = ''' /*! Copyright 2024 - Do not remove */ body { margin: 0px; } ''' result_preserved = compress(css_with_license, preserve_exclamation_comments=True) print(result_preserved) # Output: /*! Copyright 2024 - Do not remove */body{margin:0} # Remove all comments including important ones result_no_comments = compress(css_with_license, preserve_exclamation_comments=False) print(result_no_comments) # Output: body{margin:0} ``` -------------------------------- ### Handle Unbalanced Braces in Partitioned Compression Source: https://context7.com/sprymix/csscompressor/llms.txt Shows error handling for unbalanced curly braces when using `compress_partitioned`. This is important for ensuring valid CSS output, especially in partitioned scenarios. ```python from csscompressor import compress, compress_partitioned # Handling unbalanced braces in partitioned compression try: css = ''' @media { a { padding: 1px } ''' # Missing closing brace compress_partitioned(css, max_rules_per_file=2) except ValueError as e: print(f"Partition failed: {e}") ``` -------------------------------- ### Partition CSS for IE Compatibility Source: https://context7.com/sprymix/csscompressor/llms.txt Use compress_partitioned to split CSS into multiple chunks, ensuring compliance with the 4096-rule limit per file in older Internet Explorer versions. ```python from csscompressor import compress_partitioned # CSS with multiple rules css_input = ''' a {content: '}}'} b {content: '}'} c {content: '{'} ''' # Split into chunks with max 2 rules per file chunks = compress_partitioned(css_input, max_rules_per_file=2) print(chunks) # Output: ["a{content:'}}'}b{content:'}'}", "c{content:'{'}"] # Handling @media blocks (counted as single rules) css_media = ''' @media screen { a {padding: 1px} b {padding: 2px} x {padding: 2px} } @media print { c {padding: 1px} d {padding: 2px} y {padding: 2px} } @media handheld { e {padding: 1px} f {padding: 2px} z {padding: 2px} } ''' chunks = compress_partitioned(css_media, max_rules_per_file=2) print(chunks) # Output: ['@media screen{a{padding:1px}b{padding:2px}x{padding:2px}}', # '@media print{c{padding:1px}d{padding:2px}y{padding:2px}}', # '@media handheld{e{padding:1px}f{padding:2px}z{padding:2px}}'] # Selector lists count each selector (a,b,c counts as 3 rules) css_selectors = ''' a, a1, a2 {color: red} b, b2, b3 {color: red} c, c3, c4, c5 {color: red} d {color: red} ''' chunks = compress_partitioned(css_selectors, max_rules_per_file=2) print(chunks) # Output: ['a,a1,a2{color:red}', 'b,b2,b3{color:red}', 'c,c3,c4,c5{color:red}', 'd{color:red}'] # Combined with line length limit chunks = compress_partitioned(css_media, max_rules_per_file=2, max_linelen=6) print(chunks[0]) # Output: '@media screen{a{padding:1px}\nb{padding:2px}x{padding:2px}\n}' ``` -------------------------------- ### Preserve calc() Expressions Source: https://context7.com/sprymix/csscompressor/llms.txt Illustrates the compressor's ability to preserve `calc()` expressions, including vendor-prefixed versions, while maintaining correct spacing. This is crucial for dynamic layouts. ```python from csscompressor import compress # Preserves calc() expressions with proper spacing css = ''' .calc-example { width: calc( (100vh - 100px) / 4 + 30px ); height: -webkit-calc(100% + 30px); margin: -moz-calc(50% - 10px); } ''' print(compress(css)) ``` -------------------------------- ### Compress CSS in Python Source: https://github.com/sprymix/csscompressor/blob/master/README.rst Use the compress function to minify CSS strings within a Python environment. ```pycon >>> from csscompressor import compress >>> compress(''' ... your css { ... content: "!"; ... } ... ''') 'your css{content:"!"}' ``` -------------------------------- ### Programmatic API - compress Source: https://context7.com/sprymix/csscompressor/llms.txt The primary function for minifying CSS stylesheets. It removes unnecessary whitespace, comments, and optimizes various CSS constructs while preserving functionality. Supports optional line length limits and preservation of important comments marked with `/*!`. ```APIDOC ## compress ### Description Minifies CSS stylesheets by removing unnecessary whitespace, comments, and optimizing CSS constructs. Preserves functionality while offering options for line length limits and important comment preservation. ### Method `compress(css_input, max_linelen=0, preserve_exclamation_comments=False)` ### Parameters #### Arguments - **css_input** (string) - Required - The CSS string to compress. - **max_linelen** (integer) - Optional - Maximum line length for output. If 0, no line breaks are inserted. - **preserve_exclamation_comments** (boolean) - Optional - If True, preserves comments starting with `/*!`. ### Request Example ```python from csscompressor import compress css_input = ''' .container { margin: 0px 0px 0px 0px; padding: 10px; background-color: #AABBCC; } /* This comment will be removed */ .button { color: rgb(255, 0, 0); border: none; } ''' result = compress(css_input) print(result) ``` ### Response #### Success Response (string) - **result** (string) - The minified CSS string. #### Response Example ``` .container{margin:0;padding:10px;background-color:#abc}.button{color:red;border:0} ``` ### Additional Examples #### Compression with line length limit ```python css_multiline = ''' a {content: '}}'} b {content: '}'} c {content: '{'} ''' result_with_breaks = compress(css_multiline, max_linelen=20) print(result_with_breaks) ``` ``` a{content:'}}'}\nb{content:'}'}\nc{content:'{'} ``` #### Preserve important comments ```python css_with_license = ''' /*! Copyright 2024 - Do not remove */ body { margin: 0px; } ''' result_preserved = compress(css_with_license, preserve_exclamation_comments=True) print(result_preserved) ``` ``` /*! Copyright 2024 - Do not remove */body{margin:0} ``` #### Remove all comments including important ones ```python result_no_comments = compress(css_with_license, preserve_exclamation_comments=False) print(result_no_comments) ``` ``` body{margin:0} ``` ``` -------------------------------- ### HSL and HSLA Color Preservation Source: https://context7.com/sprymix/csscompressor/llms.txt Shows that HSL and HSLA color formats are preserved by the compressor. This is useful for maintaining color definitions that rely on these color spaces. ```python from csscompressor import compress # HSL and HSLA color preservation css = ''' .hsl-colors { color: hsl(0, 100%, 50%); background: hsla(120, 50%, 50%, 0.5); } ''' print(compress(css)) ``` -------------------------------- ### Safe Compression Wrapper Function Source: https://context7.com/sprymix/csscompressor/llms.txt A utility function to safely compress CSS, returning the original CSS if compression fails due to a `ValueError`. This prevents build failures from minor CSS errors. ```python from csscompressor import compress, compress_partitioned # Safe compression wrapper function def safe_compress(css, **kwargs): try: return compress(css, **kwargs) except ValueError as e: print(f"Warning: CSS compression failed - {e}") return css # Return original on failure # Usage in build pipeline original_css = load_css_file('styles.css') minified = safe_compress(original_css, max_linelen=500) save_css_file('styles.min.css', minified) ``` -------------------------------- ### Handle Nested Parenthesis Errors Source: https://context7.com/sprymix/csscompressor/llms.txt Illustrates error handling for unbalanced nested parentheses within `calc()` expressions. A `ValueError` is raised when parentheses do not match. ```python from csscompressor import compress, compress_partitioned # Handling nested parenthesis errors try: css = 'a { width: calc( ((10vh - 100px) / 4 + 30px ) }' # Unbalanced parens compress(css) except ValueError as e: print(f"Compression failed: {e}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.