An Elixir implementation of the popular PostCSS library, providing CSS parsing, AST manipulation, and stringification capabilities.
- CSS Parsing: Parse CSS strings into an Abstract Syntax Tree (AST)
- AST Manipulation: Create, modify, and traverse CSS nodes
- CSS Generation: Convert AST back to CSS strings
- Node Types: Support for rules, declarations, at-rules, comments, and more
- Source Maps: Preserve original formatting and whitespace
Add postcss
to your list of dependencies in mix.exs
:
def deps do
[
{:postcss, "~> 0.1.3"}
]
end
# Parse CSS string
css = """
.foo {
color: red;
margin: 10px;
}
"""
root = PostCSS.parse(css)
# Convert back to string
PostCSS.stringify(root)
# Create a declaration
decl = PostCSS.decl("color", "blue")
# Create a rule with declarations
rule = PostCSS.rule(".my-class", [decl])
# Create a root with rules
root = PostCSS.root([rule])
# Generate CSS
PostCSS.stringify(root)
# => ".my-class {\n color: blue;\n}"
# Create an at-rule
media = PostCSS.at_rule("media", "screen and (max-width: 600px)", [
PostCSS.rule(".responsive", [
PostCSS.decl("display", "none")
])
])
root = PostCSS.root([media])
PostCSS.stringify(root)
comment = PostCSS.comment("This is a comment")
rule = PostCSS.rule(".foo", [comment, PostCSS.decl("color", "red")])
The main API consists of:
PostCSS.parse/1
- Parse CSS string into ASTPostCSS.stringify/1
- Convert AST to CSS stringPostCSS.decl/2
andPostCSS.decl/3
- Create declaration nodesPostCSS.rule/1
andPostCSS.rule/2
- Create rule nodesPostCSS.root/0
andPostCSS.root/1
- Create root nodesPostCSS.at_rule/1
,PostCSS.at_rule/2
, andPostCSS.at_rule/3
- Create at-rule nodesPostCSS.comment/1
- Create comment nodes
The library supports all major CSS node types:
- Root: The top-level container for all CSS nodes
- Rule: CSS rules with selectors and declarations (e.g.,
.foo { color: red; }
) - Declaration: CSS property-value pairs (e.g.,
color: red
) - AtRule: At-rules like
@media
,@import
,@keyframes
- Comment: CSS comments (e.g.,
/* comment */
)
# Install dependencies
mix deps.get
# Run tests
mix test
# Generate documentation
mix docs
# Check formatting
mix format --check-formatted
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -am 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.