Css.PpPrinter
CSS Pretty Printer
A minification-aware printer for CSS that uses direct buffer writing for performance. This module provides formatting combinators that can produce both minified and formatted CSS output.
The core abstraction is the formatter type 'a t = ctx -> 'a -> unit which writes values of type 'a directly to a buffer based on the context.
Design principles:
type ctx = {minify : bool;Whether to produce minified output
*)indent : int;Current indentation level
*)buf : Buffer.t;Output buffer
*)inline : bool;Whether to inline variables or not
*)}Formatter context containing output configuration
type 'a t = ctx -> 'a -> unitCore formatter type: writes values of type 'a to a buffer
val to_string : ?minify:bool -> ?inline:bool -> 'a t -> 'a -> stringto_string ~minify ~inline formatter value runs the formatter and returns a string. Creates a fresh buffer internally. Defaults: minify=false, inline=false.
val nop : 'a tnop is a no-op formatter that writes nothing and ignores its input.
val string : string tstring writes a string value to the buffer.
val quoted : string tquoted writes a double-quoted string value to the buffer.
val char : char tchar writes a single character to the buffer.
val quoted_string : string tquoted_string writes a double-quoted string with proper escaping of quotes and backslashes.
These formatters control whitespace and indentation for readable output. They respect the minification setting - producing no output when minifying.
val sp : unit tsp writes a space character when not minifying (layout whitespace).
val cut : unit tcut writes a newline when not minifying.
nest n formatter runs formatter with indentation increased by n levels.
Functions for combining and transforming formatters
pair ~sep f g formats a pair using f for first, g for second, with optional separator between them.
triple ~sep f g h formats a triple using f, g, h for the three components, with optional separator between them.
list ~sep formatter formats a list with separator between elements.
option ~none formatter formats an option, using none formatter for None.
CSS number formatters that handle minification rules like dropping leading zeros and avoiding scientific notation
val float : float tfloat formats floating point numbers with CSS rules:
val float_n : int -> float tfloat_n n formats float to exactly n decimal places using round-half-up. Used for CSS color channels and opacity where precision matters.
val int : int tint formats integers.
val hex : int thex formats integers as uppercase hexadecimal.
val unit : ctx -> float -> string -> unitunit ctx f suffix formats a number with a unit suffix, e.g. "3.5px" or "0" for zero.
val pct : ?always:bool -> ctx -> float -> unitpct ?always ctx f formats a percentage value with the % suffix. The value is expected to be in the range 0-100. When always is true, always includes the unit even for zero values (required for CSS property initial-value).
val comma : unit tcomma outputs "," when minifying, ", " when formatting.
val semicolon : unit tsemicolon always outputs ";".
val slash : unit tslash always outputs "/" (mandatory separator, no spacing control).
val space : unit tspace always outputs " " (mandatory lexical space, not layout).
val block_open : unit tblock_open outputs "{" (block formatting controlled elsewhere).
val block_close : unit tblock_close outputs "}" (block formatting controlled elsewhere).
val minified : ctx -> boolminified ctx queries whether context is in minification mode.
cond predicate then_fmt else_fmt conditionally chooses formatter based on context predicate.
val space_if_pretty : unit tspace_if_pretty is an alias for sp - outputs space when not minifying.
val op_char : char top_char outputs a character with spaces around it when not minifying. Useful for operators like +, -, *, / in expressions.
braces formatter wraps formatter in braces with proper spacing and indentation: { <indented content> } when formatting, {<content>} when minifying.
call_list name item formats a function call with a comma-separated list of items: name(a, b, c).
call_2 name a b formats a 2-arg function call: name(a, b).
call_3 name a b c formats a 3-arg function call: name(a, b, c).
val url : string turl formats a CSS url with quotes: url("s").