pug-php/pug-filter-less

View on GitHub
src/Pug/Filter/Less.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace Pug\Filter;

use NodejsPhpFallback\Less as LessWrapper;

class Less extends AbstractFilter
{
    protected $tag = 'style';
    protected $textType = 'css';

    public function replaceVariable($matches)
    {
        return '~"' . addcslashes($matches[0], '"') . '"';
    }

    public function parse($code)
    {
        $code = preg_replace('/#\{\$?(.*?)\}/', '<?php echo $$1; ?>', $code);
        $code = preg_replace_callback(
            '/<\?php.*\?>/',
            array($this, 'replaceVariable'),
            $code
        );

        return new LessWrapper($code);
    }
}