26 KiB
lass
0.2.0Lisp Augmented Style Sheets. Compiles LASS to CSS.
About LASS
Writing CSS files comes with a lot of repetition and is generally much too verbose. With lispy syntax, shortcuts, and improvements, LASS aims to help you out in writing CSS quick and easy. LASS was largely inspired by SASS.
How To
LASS supports two modes, one being directly in your lisp code, the other in pure LASS files. Adding LASS into your code is easy:
(lass:compile-and-write
'(div
:background black))
"div{
background: black;
}"
LASS works on the following simple principles: A list is a block. The first argument in the list is a selector. The body of the list makes up the properties and sub-blocks. A property is started with a keyword that is used as the property name. Following is a bunch of property arguments until a new keyword, list, or the end is reached. A list inside a block is, again, a block with the twist that the parent block's selector is prepended to the sub-block's selector.
(lass:compile-and-write
'(nav
(ul
:list-style none
(li
:margin 0 :padding 0
:display inline-block)))))
"nav ul{
list-style: none;
}
nav ul li{
margin: 0;
padding: 0;
display: inline-block;
}"
Since LASS' COMPILE-SHEET
simply takes a bunch of lists as its argument, you can use the backquote and comma to integrate variables from your lisp environment:
(let ((color "#0088EE"))
(lass:compile-and-write
`(div
:background ,color))))
"div{
background: #0088EE;
}"
Alternatively however, and this is especially useful in pure LASS files, you can use the LET
block to create LASS-specific bindings:
(lass:compile-and-write
'(:let ((color "#0088EE"))
(div
:background #(color))))
"div{
background: #0088EE;
}"
LASS' selector mechanism is very flexible and allows for some complex logic to reduce duplication:
(lass:compile-and-write
'(article
((:or p blockquote)
:margin 0 :padding 0
(a
:color black)
((:and a :hover)
:color darkred))))
"article p, article blockquote{
margin: 0;
padding: 0;
}
article p a, article blockquote a{
color: black;
}
article p a:hover, article blockquote a:hover{
color: darkred;
}"
But it can go even further:
(lass:compile-and-write
'((:and
(:or article section)
(:= data-author (:or yukari ran chen))
(:nth-child (:or 1 2 3)))
:display none))
"article[data-author=\"yukari\"]:nth-child(1),
article[data-author=\"yukari\"]:nth-child(2),
article[data-author=\"yukari\"]:nth-child(3),
article[data-author=\"ran\"]:nth-child(1),
article[data-author=\"ran\"]:nth-child(2),
article[data-author=\"ran\"]:nth-child(3),
article[data-author=\"chen\"]:nth-child(1),
article[data-author=\"chen\"]:nth-child(2),
article[data-author=\"chen\"]:nth-child(3),
section[data-author=\"yukari\"]:nth-child(1),
section[data-author=\"yukari\"]:nth-child(2),
section[data-author=\"yukari\"]:nth-child(3),
section[data-author=\"ran\"]:nth-child(1),
section[data-author=\"ran\"]:nth-child(2),
section[data-author=\"ran\"]:nth-child(3),
section[data-author=\"chen\"]:nth-child(1),
section[data-author=\"chen\"]:nth-child(2),
section[data-author=\"chen\"]:nth-child(3){
display: none;
}"
Whoa nelly!
Some CSS properties are not fully specified yet and require browser-specific prefixes. LASS can help you with that, too:
(lass:compile-and-write
'(.fun
:linear-gradient "deg(45)" black 0% darkgray 100%
:transform rotate -45deg))
".fun{
background: -moz-linear-gradient(deg(45), black 0%, darkgray 100%);
background: -o-linear-gradient(deg(45), black 0%, darkgray 100%);
background: -webkit-linear-gradient(deg(45), black 0%, darkgray 100%);
background: -ms-linear-gradient(deg(45), black 0%, darkgray 100%);
background: linear-gradient(deg(45), black 0%, darkgray 100%);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
}"
LASS also supports the various @QUERY
operator blocks:
(lass:compile-and-write
'(:media "(max-width: 800px)"
(div
:margin 0)))
"@media (max-width: 800px){
div{
margin: 0;
}
}"
By default LASS activates pretty-printing and inserts newlines and spaces where appropriate in order to make the result readable and easy to debug. However, you can also deactivate that and directly produce minified CSS:
(let ((lass:*pretty* NIL))
(lass:compile-and-write
'(:media "(max-width: 800px)"
(div
:margin 0))))
"@media (max-width: 800px){div{margin:0;}}"
As mentioned above you can write pure LASS files to compile down to a CSS file. To do that, simply use GENERATE
:
Extending LASS
Pretty much every part of LASS is extensible through methods. Most useful will however probably be the DEFINE-SPECIAL-PROPERTY
, DEFINE-BROWSER-PROPERTY
and DEFINE-SPECIAL-SELECTOR
helper-macros. Here's some examples from the SPECIAL.LISP
file that defines some standard special handlers:
(define-special-property font-family (&rest faces)
(list (make-property "font-family" (format NIL "~{~a~^, ~}" (mapcar #'resolve faces)))))
(define-browser-property linear-gradient (direction &rest colors)
(:default (property)
(make-property "background" (format NIL "~a(~a~{, ~a ~a~})"
property (resolve direction) (mapcar #'resolve colors)))))
For more control, have a look at the various COMPILE-*
generic functions.
Emacs Support
LASS includes a tiny elisp file, lass.el
. Add LASS' directory to your emacs LOAD-PATH
and REQUIRE
lass.
(add-to-list 'load-path "[path-to-lass-source-dir]/")
(require 'lass)
Once you visit a .lass
file, it will automatically start in the LASS
major-mode, which is a derived-mode from COMMON-LISP-MODE
. Whenever you save, it will automatically try to compile the lass file to its CSS equivalent. If slime is connected, it will try to quickload LASS and evaluate GENERATE
. If slime is not connected, it instead executes a shell command. In order for that to work, the lass
binary must be in your path.
If your operating system is not directly supported with a binary, you can build it yourself using a build tool like Buildapp, the ASDF system BINARY-LASS
and the entry-point BINARY-LASS:CMD-WRAPPER
.
Copyright
lass is licensed under the Artistic license and ©2014 Nicolas Hafner <shinmera@tymoon.eu>. This library can be obtained on https://github.com/Shinmera/LASS.Package Index
-
LASS (ORG.TYMOONNEXT.LASS)
-
FUNCTION COMPILE-AND-WRITE
(&REST FORMS)Shortcut for (WRITE-SHEET (COMPILE-SHEET FORMS*))
-
FUNCTION COMPILE-SHEET
(&REST BLOCKS)Compiles a LASS sheet composed of BLOCKS. Each BLOCK is passed to COMPILE-BLOCK. The results thereof are appended together into one list of blocks and properties.
-
FUNCTION GENERATE
(IN &KEY (OUT (MERGE-PATHNAMES (MAKE-PATHNAME TYPE css) IN)) (PRETTY NIL) (IF-EXISTS SUPERSEDE))Generate a CSS file from a LASS file. IN --- The LASS input file. Has to be READable. OUT --- The target file, by default a file of same location and name, but with CSS type. PRETTY --- Whether to minify or not. See WRITE-SHEET. IF-EXISTS --- See WITH-OPEN-FILE Returns OUT
-
FUNCTION INDENT
Returns a string of the appropriate number of spaces depending on *PRETTY* and *INDENT-LEVEL*
-
FUNCTION MAKE-BLOCK
(SELECTOR VALUES)Creates a block object with SELECTOR and VALUES.
-
FUNCTION MAKE-PROPERTY
(PROPERTY &OPTIONAL VALUE)Creates a property object with PROPERTY as its key and VALUE as its value.
-
FUNCTION WRITE-SHEET
(SHEET &KEY (STREAM NIL) (PRETTY *PRETTY*))Writes the compiled SHEET object to STREAM. If PRETTY is non-NIL, spaces and newlines are inserted as appropriate in order to create a human-readable stylesheet. Otherwise whitespace is only used where necessary, producing a minified version. STREAM can be a STREAM, T for *STANDARD-OUTPUT*, or NIL for a STRING.
-
FUNCTION WRITE-SHEET-PART
(STREAM BLOCK CP AP)Wrapper around WRITE-SHEET-OBJECT so that we can call it from FORMAT. Calls WRITE-SHEET-OBJECT with (CAR BLOCK) (CDR BLOCK) STREAM.
-
GENERIC COMPILE-BLOCK
(HEADER FIELDS)Compiles the block with given HEADER and FIELDS list. By default, the following case is handled: (T T) Blocks are handled in the following way: The HEADER is used as a selector and compiled through COMPILE-SELECTOR. Fields are semantically segregated through KEYWORDS and LISTS. Every time a KEYWORD is encountered, it is taken as the current property and all following objects until either a LIST or a KEYWORD is encountered are gathered as the property's values. Every time a LIST is encountered, it is taken as a SUB-BLOCK and is passed to COMPILE-BLOCK with the HEADER being the current block's selector prepended to the selector of the sub-block. Special handling of blocks may occur. See DEFINE-SPECIAL-BLOCK.
-
GENERIC COMPILE-CONSTRAINT
(FUNC ARGS)Compiles a constraint of type FUNC with arguments ARGS to a list of alternative selectors. By default, the following cases are handled: (T T) Concatenates its ARGS together with spaces. Preserves OR combinations. (NULL NULL) Returns NIL (T NULL) Returns FUNC (:OR T) Passes all ARGS to COMPILE-SELECTOR individually and then APPENDS all the results together. (:AND T) Concatenates its ARGS together without spaces. Preserves OR combinations. Special handling of constraints may occur. See DEFINE-SPECIAL-SELECTOR.
-
GENERIC COMPILE-PROPERTY
(KEY VALUE)Compile a property of KEY and VALUE to a list of property objects. By default, the following cases are handled: (T LIST) A list is created with one property object, wherein the property-value is the Space-concatenated list of RESOLVEd VALUEs. The KEY is DOWNCASEd. (T T) A list is created with one property object, wherein the property-value is the RESOLVEd VALUE. The KEY is DOWNCASEd. Special handling of properties may occur. See DEFINE-SPECIAL-PROPERTY
-
GENERIC COMPILE-SELECTOR
(SELECTOR)Compiles the SELECTOR form into a list of alternative selectors. By default, the following cases are handled: (NULL) Returns NIL. (LIST) Calls COMPILE-CONSTRAINT with the SELECTOR's CAR and CDR. (T) Returns a list with the RESOLVEd SELECTOR.
-
GENERIC RESOLVE
(THING)Resolves THING to a value that makes sense for LASS. By default the following types are handled: NULL: NIL STRING: the THING itself ARRAY: the variable stored in *VARS* under THING KEYWORD: Colon-prefixed, downcased symbol-name of THING SYMBOL: Downcased symbol-name of THING T: PRINC-TO-STRING of THING
-
GENERIC WRITE-SHEET-OBJECT
(TYPE OBJECT STREAM)Writes the OBJECT of type TYPE to STREAM. By default the following TYPEs are handled: :BLOCK (SELECTOR-LIST OBJECTS*) Prints the SELECTOR-LIST separated by commas, followed by an opening brace and the printed list of OBJECTS using WRITE-SHEET-PART. Finally the body is closed off with a closing brace. Newlines and spaces may be inserted where necessary if *PRETTY* is non-NIL. :PROPERTY (KEY VALUE) Prints the KEY. If VALUE is non-NIL, a colon is printed followed by the VALUE. Finally a semicolon is printed. Spaces may be inserted where necessary if *PRETTY* is non-NIL.
-
MACRO DEFINE-BROWSER-PROPERTY
(NAME ARGS &BODY BROWSER-OPTIONS)Helper macro to define properties that have browser-dependant versions. NAME --- The base name of the property name or value. ARGS --- Property arguments, see DEFINE-SPECIAL-PROPERTY. BROWSER-OPTIONS ::= (OPTION (symbol) FORM*) OPTION ::= :MOZ | :O | :WEBKIT | :MS | :W3C | :DEFAULT Each browser-option body should return a single property. The SYMBOL in the option definition is bound to the computed property name (eg -moz-NAME for the :MOZ option). You can define special handling of the browsers by defining options specifically for them. If no handling is defined, the DEFAULT option is used as a fallback.
-
MACRO DEFINE-SINGLE-ARG-SELECTOR
(NAME)Helper macro to define a single-argument pseudo-selector like NOT or NTH-CHILD.
-
MACRO DEFINE-SPECIAL-BLOCK
(NAME ARGS &BODY BODY)Define handling of a special block type. In order for the block to be recognised, it has to begin with the NAME as a keyword. The ARGS is a lambda-list that parses the block contents. Expected as a return value is a /list/ of either attributes or blocks. See COMPILE-BLOCK
-
MACRO DEFINE-SPECIAL-PROPERTY
(NAME ARGS &BODY BODY)Define handling of a special property. The ARGS is a lambda-list that parses the arguments passed to the property. Expected as a return value is a /list/ of either attributes or blocks. See COMPILE-PROPERTY
-
MACRO DEFINE-SPECIAL-SELECTOR
(NAME ARGS &BODY BODY)Define handling of a special selector type. In order for the selector to be recognised, it has to begin with the NAME as a keyword. The ARGS is a lambda-list that parses the selector contents. Expected as a return value is a /list/ of alternate versions of selectors. See COMPILE-CONSTRAINT.
-
SPECIAL *INDENT-LEVEL*
Directs the current amount of spaces used to indent.
-
SPECIAL *PRETTY*
Directs whether to pretty-print using whitespace or not.
-
SPECIAL *VARS*
Special variable containing LASS-environment variables. See the definition of the LET block.
-