xref: /dokuwiki/vendor/splitbrain/lesserphp/src/FormatterClassic.php (revision e6380ba37d6b3f7dd03146b3c03030ccc8c1b297)
1*e6380ba3SAndreas Gohr<?php
2*e6380ba3SAndreas Gohr/**
3*e6380ba3SAndreas Gohr * http://leafo.net/lessphp
4*e6380ba3SAndreas Gohr *
5*e6380ba3SAndreas Gohr * LESS CSS compiler, adapted from http://lesscss.org
6*e6380ba3SAndreas Gohr *
7*e6380ba3SAndreas Gohr * Copyright 2013, Leaf Corcoran <leafot@gmail.com>
8*e6380ba3SAndreas Gohr * Copyright 2016, Marcus Schwarz <github@maswaba.de>
9*e6380ba3SAndreas Gohr * Licensed under MIT or GPLv3, see LICENSE
10*e6380ba3SAndreas Gohr */
11*e6380ba3SAndreas Gohr
12*e6380ba3SAndreas Gohr
13*e6380ba3SAndreas Gohrnamespace LesserPHP;
14*e6380ba3SAndreas Gohr
15*e6380ba3SAndreas Gohrclass FormatterClassic
16*e6380ba3SAndreas Gohr{
17*e6380ba3SAndreas Gohr    public $indentChar = '  ';
18*e6380ba3SAndreas Gohr
19*e6380ba3SAndreas Gohr    public $break = "\n";
20*e6380ba3SAndreas Gohr    public $open = ' {';
21*e6380ba3SAndreas Gohr    public $close = '}';
22*e6380ba3SAndreas Gohr    public $selectorSeparator = ', ';
23*e6380ba3SAndreas Gohr    public $assignSeparator = ':';
24*e6380ba3SAndreas Gohr
25*e6380ba3SAndreas Gohr    public $openSingle = ' { ';
26*e6380ba3SAndreas Gohr    public $closeSingle = ' }';
27*e6380ba3SAndreas Gohr
28*e6380ba3SAndreas Gohr    public $disableSingle = false;
29*e6380ba3SAndreas Gohr    public $breakSelectors = false;
30*e6380ba3SAndreas Gohr
31*e6380ba3SAndreas Gohr    public $compressColors = false;
32*e6380ba3SAndreas Gohr    protected int $indentLevel;
33*e6380ba3SAndreas Gohr
34*e6380ba3SAndreas Gohr    public function __construct()
35*e6380ba3SAndreas Gohr    {
36*e6380ba3SAndreas Gohr        $this->indentLevel = 0;
37*e6380ba3SAndreas Gohr    }
38*e6380ba3SAndreas Gohr
39*e6380ba3SAndreas Gohr    public function indentStr($n = 0)
40*e6380ba3SAndreas Gohr    {
41*e6380ba3SAndreas Gohr        return str_repeat($this->indentChar, max($this->indentLevel + $n, 0));
42*e6380ba3SAndreas Gohr    }
43*e6380ba3SAndreas Gohr
44*e6380ba3SAndreas Gohr    public function property($name, $value)
45*e6380ba3SAndreas Gohr    {
46*e6380ba3SAndreas Gohr        return $name . $this->assignSeparator . $value . ';';
47*e6380ba3SAndreas Gohr    }
48*e6380ba3SAndreas Gohr
49*e6380ba3SAndreas Gohr    protected function isEmpty($block)
50*e6380ba3SAndreas Gohr    {
51*e6380ba3SAndreas Gohr        if (empty($block->lines)) {
52*e6380ba3SAndreas Gohr            foreach ($block->children as $child) {
53*e6380ba3SAndreas Gohr                if (!$this->isEmpty($child)) return false;
54*e6380ba3SAndreas Gohr            }
55*e6380ba3SAndreas Gohr
56*e6380ba3SAndreas Gohr            return true;
57*e6380ba3SAndreas Gohr        }
58*e6380ba3SAndreas Gohr        return false;
59*e6380ba3SAndreas Gohr    }
60*e6380ba3SAndreas Gohr
61*e6380ba3SAndreas Gohr    public function block($block)
62*e6380ba3SAndreas Gohr    {
63*e6380ba3SAndreas Gohr        if ($this->isEmpty($block)) return;
64*e6380ba3SAndreas Gohr
65*e6380ba3SAndreas Gohr        $inner = $pre = $this->indentStr();
66*e6380ba3SAndreas Gohr
67*e6380ba3SAndreas Gohr        $isSingle = !$this->disableSingle &&
68*e6380ba3SAndreas Gohr            is_null($block->type) && count($block->lines) == 1;
69*e6380ba3SAndreas Gohr
70*e6380ba3SAndreas Gohr        if (!empty($block->selectors)) {
71*e6380ba3SAndreas Gohr            $this->indentLevel++;
72*e6380ba3SAndreas Gohr
73*e6380ba3SAndreas Gohr            if ($this->breakSelectors) {
74*e6380ba3SAndreas Gohr                $selectorSeparator = $this->selectorSeparator . $this->break . $pre;
75*e6380ba3SAndreas Gohr            } else {
76*e6380ba3SAndreas Gohr                $selectorSeparator = $this->selectorSeparator;
77*e6380ba3SAndreas Gohr            }
78*e6380ba3SAndreas Gohr
79*e6380ba3SAndreas Gohr            echo $pre .
80*e6380ba3SAndreas Gohr                implode($selectorSeparator, $block->selectors);
81*e6380ba3SAndreas Gohr            if ($isSingle) {
82*e6380ba3SAndreas Gohr                echo $this->openSingle;
83*e6380ba3SAndreas Gohr                $inner = '';
84*e6380ba3SAndreas Gohr            } else {
85*e6380ba3SAndreas Gohr                echo $this->open . $this->break;
86*e6380ba3SAndreas Gohr                $inner = $this->indentStr();
87*e6380ba3SAndreas Gohr            }
88*e6380ba3SAndreas Gohr        }
89*e6380ba3SAndreas Gohr
90*e6380ba3SAndreas Gohr        if (!empty($block->lines)) {
91*e6380ba3SAndreas Gohr            $glue = $this->break . $inner;
92*e6380ba3SAndreas Gohr            echo $inner . implode($glue, $block->lines);
93*e6380ba3SAndreas Gohr            if (!$isSingle && !empty($block->children)) {
94*e6380ba3SAndreas Gohr                echo $this->break;
95*e6380ba3SAndreas Gohr            }
96*e6380ba3SAndreas Gohr        }
97*e6380ba3SAndreas Gohr
98*e6380ba3SAndreas Gohr        foreach ($block->children as $child) {
99*e6380ba3SAndreas Gohr            $this->block($child);
100*e6380ba3SAndreas Gohr        }
101*e6380ba3SAndreas Gohr
102*e6380ba3SAndreas Gohr        if (!empty($block->selectors)) {
103*e6380ba3SAndreas Gohr            if (!$isSingle && empty($block->children)) echo $this->break;
104*e6380ba3SAndreas Gohr
105*e6380ba3SAndreas Gohr            if ($isSingle) {
106*e6380ba3SAndreas Gohr                echo $this->closeSingle . $this->break;
107*e6380ba3SAndreas Gohr            } else {
108*e6380ba3SAndreas Gohr                echo $pre . $this->close . $this->break;
109*e6380ba3SAndreas Gohr            }
110*e6380ba3SAndreas Gohr
111*e6380ba3SAndreas Gohr            $this->indentLevel--;
112*e6380ba3SAndreas Gohr        }
113*e6380ba3SAndreas Gohr    }
114*e6380ba3SAndreas Gohr}
115