{"id":17332895,"url":"https://github.com/client9/csstool","last_synced_at":"2025-04-14T17:30:57.752Z","repository":{"id":57514768,"uuid":"123620509","full_name":"client9/csstool","owner":"client9","description":"CSS filters and formatters in golang","archived":false,"fork":false,"pushed_at":"2021-11-26T20:32:42.000Z","size":308,"stargazers_count":29,"open_issues_count":4,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-01T05:32:55.919Z","etag":null,"topics":["css","golang"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/client9.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-02T19:17:49.000Z","updated_at":"2024-09-17T16:55:24.000Z","dependencies_parsed_at":"2022-09-26T18:00:46.258Z","dependency_job_id":null,"html_url":"https://github.com/client9/csstool","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/client9%2Fcsstool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/client9%2Fcsstool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/client9%2Fcsstool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/client9%2Fcsstool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/client9","download_url":"https://codeload.github.com/client9/csstool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223639403,"owners_count":17177816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","golang"],"created_at":"2024-10-15T14:59:11.032Z","updated_at":"2024-11-08T06:03:21.648Z","avatar_url":"https://github.com/client9.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# csstool\nCSS filters and formatters in golang\n\n[![Build Status](https://travis-ci.org/client9/csstool.svg?branch=master)](https://travis-ci.org/client9/csstool)\n\n## css cut \n\nUse awesome CSS frameworks without the weight by cutting out unused rules.\n\n`css cut` is similar to [purgecss](https://www.purgecss.com) ([GitHub](https://github.com/FullHuman/purgecss)). It scans your HTML for elements, classes and identifiers and then cuts out any CSS rule that doesn't apply. The results for a [small site](https://www.client9.com/) using a framework like [bootstrap](https://getbootstrap.com) can be dramatic:\n\n|                | Bootstrap | css cut  | savings |\n|----------------|-----------|----------|---------|\n| uncompressed   |   141k    |   5.6k   |   96%   |\n| compressed     |    20k    |   1.8k   |   91%   |\n\n\nSee also [Hugo #4446](https://github.com/gohugoio/hugo/issues/4446#issuecomment-370070252)\n\n### Example\n\nFor use with [hugo](https://gohugo.io) using [bootstrap](https://getbootstrap.com):\n\n```bash\n# install the binary\ngo get github.com/client9/csstool/css\n\n# build your site, by default the output is in `public`\nhugo\n\n# create new minimized CSS file from bootstrap\ncurl -s https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css | \\\n    css cut --html 'public/**/*.html' \u003e static/bootstrap-csscut.min.css\n```\n\nOf course, you'll need to use the new `bootstrap-csscut.min.css` file in your template source.\n\nBe sure to put the HTML file pattern `'public/**/*.html'` in single quotes.\n\n### Usage\n\nTK - likely to change, feedback welcome\n\n### API\n\nTK - likely to change, feedback welcome\n\n### How It Works\n\n#### The Correct Algorithm\n\nThe \"correct way\" to strip out CSS rules might be:\n\n1. Read in all the CSS files, and extract all the selectors\n2. For each HTML file, execute each selector and see if it returns anything\n3. Use that data to the emit each CSS file with only the selectors that were used.\n\nThere are a few problems:\n\n1. Slow, as you are executing _n_ CSS rules against _m_ HTML files.\n2. Need a perfect CSS Level 3 (or 4!) selector library, else you might strip out rules that are actually used.  \n3. Need to know which pseudo-classes matter and which ones do not.  For instance `:hover` can be ignored, but `:last_child` needs to be evaluated.\n\n#### The CSSCut Algorithm\n\nSince the Correct Way seems problematic, csscut does the following:\n\n1. Read each HTML file and keep track of elements, classes and ids found.\n2. Scan the CSS file and convert a selector into a set of \"basic selectors\".  If a rule is `h1 h2 h3` then the list of basic selectors is h1, h2, and h3. Classes and identifiers (ids) are preserved, while pseudo elements and attribute selectors are ignored.\n3. Then if each of the basic selectors is found in out list in the first step, the original selector is preserved.  This not the rule is tossed out.\n\nAs a special case, \"universal selectors\" are passed through: `*, ::before, ::after, ::root`. Pure attribute selectors are also passed through: `[hidden]`.\n\nIn practice this works well for \"flat\" CSS frameworks such as [bootstrap](https://getbootstrap.com).  For highly specified CSS it might not work as well. \n\n# css format \n\nMakes minified CSS readable.\n\n```\ncss format \u003c bootstrap.min.css\n```\n\n# css minify\n\n```\ncss minify \u003c bootstrap.min.css\n```\n\nminify is a shortcut of 'css format' with all options selected to produce the smallest output. It is \"conservative\" in that it only removes whitespace and does not do any property value rewriting.\n\n# css count\n\nSee commonly or rarely used CSS classes and identifiers.\n\nWork in progress\n\n## Credits\n\n* The CSS parsing is done by the most excellent [tdewolff/parse](https://github.com/tdewolff/parse) which powers [tdewolff/minify](https://github.com/tdewolff/minify).\n* The [double star](https://www.client9.com/golang-globs-and-the--double-star-glob-operator/) globbing / pattern matching is handled by [mattn/go-zglob](https://github.com/mattn/go-zglob)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclient9%2Fcsstool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclient9%2Fcsstool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclient9%2Fcsstool/lists"}