GitHub

Build Status Coverage Status PyPI Downloads PyPI Version Wheel Status License

Python LESS Compiler.

A compiler written in Python for the LESS language. For those of us not willing or able to have node.js installed in our environment. Not all features of LESS are supported. Some features will probably never be supported (JavaScript evaluation). This program uses PLY (Python Lex-Yacc) to tokenize / parse the input and is considerably slower than the Node.js compiler.

For more information on LESS:

Development files:

Supported features

  • Variables
  • String interpolation
  • Mixins (nested, calls, closures, recursive)
  • Guard expressions
  • Parametered mixins (class / id)
  • @arguments
  • Nesting
  • Escapes ~/e()
  • Expressions
  • Keyframe blocks
  • Color functions (lighten, darken, saturate, desaturate, spin, hue, mix, saturation, lightness)
  • Other functions (round, increment, decrement, format '%(...))

Differences from less.js

  • All colors are auto-formatted to #nnnnnn (for example, #f7e923)
  • Does not preserve CSS comments

Not supported

  • JavaScript evaluation

Requirements

  • Python 3.11+
  • pip

Installation

Install from PyPI:

pip install lesscpy

Local setup

Create and activate a virtual environment, then install test dependencies from pyproject.toml:

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e ".[test]"

Build

Build source and wheel distributions:

python -m pip install --upgrade build
python -m build

Artifacts are created in dist/.

Test

Run unit tests:

pytest -v

Run lint checks:

flake8

Using tox

tox is useful for running the project test workflow in isolated environments and for reproducing the same multi-version checks used in CI.

Run all configured environments:

python -m pip install -e ".[tox]"
tox

Run a single environment:

tox -e py3.12
tox -e flake8

Compiler script usage

usage: lesscpy [-h] [-v] [-I INCLUDE] [-V] [-C] [-x] [-X] [-t] [-s SPACES]
               [-o OUT] [-r] [-f] [-m] [-D] [-g] [-S] [-L] [-N]
               target [output]

Python usage

from io import StringIO
import lesscpy
print(lesscpy.compile(StringIO("a { border-width: 2px * 3; }"), minify=True))

Output:

a{border-width:6px;}

License

See the LICENSE file.

Read the original on github.com ↗