MDL-63086 block_rss_client: Move skip time calculation to task
[moodle.git] / lib / scssphp / Formatter.php
blobb4f90aa9d87842770ea8b2e48b89115c3cabad34
1 <?php
2 /**
3 * SCSSPHP
5 * @copyright 2012-2018 Leaf Corcoran
7 * @license http://opensource.org/licenses/MIT MIT
9 * @link http://leafo.github.io/scssphp
12 namespace Leafo\ScssPhp;
14 use Leafo\ScssPhp\Formatter\OutputBlock;
15 use Leafo\ScssPhp\SourceMap\SourceMapGenerator;
17 /**
18 * Base formatter
20 * @author Leaf Corcoran <leafot@gmail.com>
22 abstract class Formatter
24 /**
25 * @var integer
27 public $indentLevel;
29 /**
30 * @var string
32 public $indentChar;
34 /**
35 * @var string
37 public $break;
39 /**
40 * @var string
42 public $open;
44 /**
45 * @var string
47 public $close;
49 /**
50 * @var string
52 public $tagSeparator;
54 /**
55 * @var string
57 public $assignSeparator;
59 /**
60 * @var boolean
62 public $keepSemicolons;
64 /**
65 * @var \Leafo\ScssPhp\Formatter\OutputBlock
67 protected $currentBlock;
69 /**
70 * @var integer
72 protected $currentLine;
74 /**
75 * @var integer
77 protected $currentColumn;
79 /**
80 * @var \Leafo\ScssPhp\SourceMap\SourceMapGenerator
82 protected $sourceMapGenerator;
84 /**
85 * Initialize formatter
87 * @api
89 abstract public function __construct();
91 /**
92 * Return indentation (whitespace)
94 * @return string
96 protected function indentStr()
98 return '';
102 * Return property assignment
104 * @api
106 * @param string $name
107 * @param mixed $value
109 * @return string
111 public function property($name, $value)
113 return rtrim($name) . $this->assignSeparator . $value . ';';
117 * Strip semi-colon appended by property(); it's a separator, not a terminator
119 * @api
121 * @param array $lines
123 public function stripSemicolon(&$lines)
125 if ($this->keepSemicolons) {
126 return;
129 if (($count = count($lines))
130 && substr($lines[$count - 1], -1) === ';'
132 $lines[$count - 1] = substr($lines[$count - 1], 0, -1);
137 * Output lines inside a block
139 * @param \Leafo\ScssPhp\Formatter\OutputBlock $block
141 protected function blockLines(OutputBlock $block)
143 $inner = $this->indentStr();
145 $glue = $this->break . $inner;
147 $this->write($inner . implode($glue, $block->lines));
149 if (! empty($block->children)) {
150 $this->write($this->break);
155 * Output block selectors
157 * @param \Leafo\ScssPhp\Formatter\OutputBlock $block
159 protected function blockSelectors(OutputBlock $block)
161 $inner = $this->indentStr();
163 $this->write($inner
164 . implode($this->tagSeparator, $block->selectors)
165 . $this->open . $this->break);
169 * Output block children
171 * @param \Leafo\ScssPhp\Formatter\OutputBlock $block
173 protected function blockChildren(OutputBlock $block)
175 foreach ($block->children as $child) {
176 $this->block($child);
181 * Output non-empty block
183 * @param \Leafo\ScssPhp\Formatter\OutputBlock $block
185 protected function block(OutputBlock $block)
187 if (empty($block->lines) && empty($block->children)) {
188 return;
191 $this->currentBlock = $block;
193 $pre = $this->indentStr();
195 if (! empty($block->selectors)) {
196 $this->blockSelectors($block);
198 $this->indentLevel++;
201 if (! empty($block->lines)) {
202 $this->blockLines($block);
205 if (! empty($block->children)) {
206 $this->blockChildren($block);
209 if (! empty($block->selectors)) {
210 $this->indentLevel--;
212 if (empty($block->children)) {
213 $this->write($this->break);
216 $this->write($pre . $this->close . $this->break);
221 * Entry point to formatting a block
223 * @api
225 * @param \Leafo\ScssPhp\Formatter\OutputBlock $block An abstract syntax tree
226 * @param \Leafo\ScssPhp\SourceMap\SourceMapGenerator|null $sourceMapGenerator Optional source map generator
228 * @return string
230 public function format(OutputBlock $block, SourceMapGenerator $sourceMapGenerator = null)
232 $this->sourceMapGenerator = null;
234 if ($sourceMapGenerator) {
235 $this->currentLine = 1;
236 $this->currentColumn = 0;
237 $this->sourceMapGenerator = $sourceMapGenerator;
240 ob_start();
242 $this->block($block);
244 $out = ob_get_clean();
246 return $out;
250 * @param string $str
252 protected function write($str)
254 if ($this->sourceMapGenerator) {
255 $this->sourceMapGenerator->addMapping(
256 $this->currentLine,
257 $this->currentColumn,
258 $this->currentBlock->sourceLine,
259 $this->currentBlock->sourceColumn - 1, //columns from parser are off by one
260 $this->currentBlock->sourceName
263 $lines = explode("\n", $str);
264 $lineCount = count($lines);
265 $this->currentLine += $lineCount-1;
267 $lastLine = array_pop($lines);
269 $this->currentColumn = ($lineCount === 1 ? $this->currentColumn : 0) + strlen($lastLine);
272 echo $str;