/** * Zkompiluje blok * * @param Block|string|array * @return void */ protected function compileBlock($block) { if ($block instanceof Block) { if (count($block->properties) == 0) { //zahoď prázdné bloky return; } if ($block instanceof Rule) { echo implode(',' . Compiler::NL, $block->selectors); } elseif ($block instanceof FontFace) { echo '@font-face'; } elseif ($block instanceof Media) { echo '@media ' . $block->media[1]; } else { throw new \Exception("Neimplementováno"); } echo ' {' . Compiler::NL; foreach ($block->properties as $property) { if ($block instanceof Media) { $this->compileBlock($property); continue; } $value = $this->compileValue($property[2], TRUE); if ($property[0] == Compiler::$prefixes['important']) { $value .= ' !important'; } elseif ($property[0] == Compiler::$prefixes['raw']) { if ($property[2][0] == 'string') { $value = Compiler::stringDecode($value); } } echo $property[1] . ': ' . $value . ';' . Compiler::NL; } echo '}' . Compiler::NL; } elseif (is_string($block)) { //surové CSS echo $block; } elseif (is_array($block) && $block[0] == 'charset' && $block[1][0] == 'string') { //@charset echo '@charset ' . $block[1][1] . ';' . Compiler::NL; } elseif (is_array($block) && $block[0] == 'import' && $block[1][0] == 'string' && $block[2][0] == 'raw') { //@import echo '@import ' . $block[1][1] . ' ' . $block[2][1] . ';' . Compiler::NL; } else { throw new \Exception("Neimplementováno"); } }
/** * Výchozí funkce * * @return void */ protected function prepareFunctions() { $this->addFunction('iergba', function (array $value) { if (isset($value[0]) && $value[0] == 'color') { if ($value[4] == 1) { return array('raw', sprintf('#%02x%02x%02x', $value[1], $value[2], $value[3])); } return array('raw', sprintf('#%02x%02x%02x%02x', $value[4] * 255, $value[1], $value[2], $value[3])); } }); $this->addFunction('raw', function (array $value) { if (isset($value[0]) && $value[0] == 'string') { return array('raw', Compiler::stringDecode($value[1])); } }); }
/** * Vložení souboru * * @param string * @param string * @return void */ protected function callInclude(array $include) { $value = $this->reduceValue($include[2]); if ($value[0] !== 'string') { throw new CompileException("Název vkládaného souboru musí být řetězec"); } $file = Compiler::stringDecode($value[1]); $path = ($this->files->isEmpty() ? '' : dirname($this->files->top()) . DIRECTORY_SEPARATOR) . $file; $extension = pathinfo($file, PATHINFO_EXTENSION); if (!is_file($path) || !is_readable($path)) { throw new CompileException("Soubor '{$path}' neexistuje nebo jej nelze přečíst"); } elseif ($extension == 'iss') { $this->addFile($path); try { $tree = $this->parser->parse(file_get_contents($this->getFile())); if ($include[3] !== NULL) { $block = new Media($include[3], $include[4]); $block->properties = $tree->properties; $this->reduceBlock($block); } else { $this->reduceBlock($tree); } } catch (CompileException $e) { throw $e->setFile($this->getFile()); } $this->removeFile($path); return; } elseif ($extension == 'css') { $this->addFile($path); $context =& $this->getReducedContext(); $context[] = file_get_contents($path); $this->removeFile($path); return; } throw new CompileException("Soubor '{$path}' nemá koncovku .css ani .iss"); }