diff --git a/WebLoader/Filter/PHPCoffeeScriptFilter.php b/WebLoader/Filter/PHPCoffeeScriptFilter.php new file mode 100644 index 0000000..de71e6a --- /dev/null +++ b/WebLoader/Filter/PHPCoffeeScriptFilter.php @@ -0,0 +1,49 @@ +compileCoffee($code, $file); + } + + return $code; + } + + + /** + * @param $source $string + * @param $file bool|NULL + * @throws \WebLoader\WebLoaderException + * @return string + */ + public function compileCoffee($source, $file) + { + try { + return Compiler::compile($source, array('filename' => $file)); + } catch (\Exception $e) { + throw new \WebLoader\WebLoaderException('CoffeeScript Filter Error: ' . $e->getMessage(), 0, $e); + } + } +} diff --git a/composer.json b/composer.json index da93c11..9823dba 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "suggest": { "oyejorge/less.php": "LESS compiler written in PHP.", "leafo/scssphp": "SCSS compiler written in PHP.", - "joseki/webloader-filters": "CSSMin & JSMin filters written in PHP." + "joseki/webloader-filters": "CSSMin & JSMin filters written in PHP.", + "coffeescript/coffeescript": "CoffeeScript compiler written in PHP." }, "require-dev": { "nette/application": "~2.3@dev", @@ -50,6 +51,7 @@ "oyejorge/less.php": "~1.5", "leafo/scssphp": "~0.1", + "coffeescript/coffeescript": "1.3.*", "mockery/mockery": "0.7.*", "phpunit/phpunit": "3.7.*", diff --git a/tests/Filter/PHPCoffeeScriptFilterTest.php b/tests/Filter/PHPCoffeeScriptFilterTest.php new file mode 100644 index 0000000..09b0c68 --- /dev/null +++ b/tests/Filter/PHPCoffeeScriptFilterTest.php @@ -0,0 +1,47 @@ +object = new PHPCoffeeScriptFilter(); + } + + public function testSimpleLoadAndParse() + { + if (!class_exists('CoffeeScript\Compiler')) { + $this->markTestSkipped('Missing CoffeeScript compiler.'); + } + + $compiler = new PHPCoffeeScriptFilter(); + $coffee = $compiler->compileCoffee("number = -42 if opposite", null); + + $version = COFFEESCRIPT_VERSION; + $expected = <<assertEquals($expected, $coffee); + } +}