Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for Eco templates #12

Merged
merged 5 commits into from
May 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version 0.4 (2012-??-??):
- Support for eco templates
- Also write statically precompressed files for use with MultiViews
in Apache or gzip_static in nginx
- Support for inline-content-tags
Expand Down
5 changes: 5 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Sacy turns
<script type="text/javascript" src="/jslib/file1.js"></script>
<script type="text/coffeescript" src="/jslib/file2.coffee"></script>
<script type="text/javascript" src="/jslib/file3.js"></script>
<script type="text/x-eco" src="/jslib/sometemplate.eco"></script>
{/asset_compile}

into
Expand Down Expand Up @@ -527,6 +528,10 @@ define()'ing some constants pointing sacy to the path of the respecive tools:
requires a working ruby installation (1.8.7 or later) and you would install
it using `gem install sass`, giving you an executable at `/usr/bin/sass`

- `SACY_TRANSFORMER_ECO` specifies the path to the eco compiler. You can
install it using `npm install -g eco`, which will likely put the eco
executable in `/usr/local/bin/eco`.

If you are not sure what you are doing, always install these utilities to
their global locations. If you install them as a user account, chances are
that the user the web server runs as will not be able to find them or if they
Expand Down
39 changes: 32 additions & 7 deletions src/sacy/ext-translators.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
namespace sacy;

abstract class ExternalProcessor{
abstract protected function getCommandLine($filename);
abstract protected function getCommandLine($filename, $opts=array());

function transform($in, $filename){
function transform($in, $filename, $opts=array()){
$s = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$cmd = $this->getCommandLine($filename);
$cmd = $this->getCommandLine($filename, $opts);
$p = proc_open($cmd, $s, $pipes);
if (!is_resource($p))
throw new \Exception("Failed to execute $cmd");
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function getCompressorForType($type){
}

class ProcessorUglify extends ExternalProcessor{
protected function getCommandLine($filename){
protected function getCommandLine($filename, $opts=array()){
if (!is_executable(SACY_COMPRESSOR_UGLIFY)){
throw new Exception('SACY_COMPRESSOR_UGLIFY defined but not executable');
}
Expand All @@ -84,21 +84,42 @@ protected function getCommandLine($filename){
}

class ProcessorCoffee extends ExternalProcessor{
protected function getCommandLine($filename){
protected function getCommandLine($filename, $opts=array()){
if (!is_executable(SACY_TRANSFORMER_COFFEE)){
throw new Exception('SACY_TRANSFORMER_COFFEE defined but not executable');
}
return sprintf('%s -c -s', SACY_TRANSFORMER_COFFEE);
}
}

class ProcessorEco extends ExternalProcessor{
protected function getType(){
return 'text/x-eco';
}

protected function getCommandLine($filename, $opts=array()){
if (!is_executable(SACY_TRANSFORMER_ECO)){
throw new Exception('SACY_TRANSFORMER_ECO defined but not executable');
}
// Calling eco with the filename here. Using stdin wouldn't
// cut it, as eco uses the filename to figure out the name of
// the js function it outputs.
$eco_root = $opts['eco-root'];
return sprintf('%s %s -p %s',
SACY_TRANSFORMER_ECO,
$eco_root ? sprintf('-i %s', escapeshellarg($eco_root)) : '',
escapeshellarg($filename)
);
}
}

class ProcessorSass extends ExternalProcessor{

protected function getType(){
return 'text/x-sass';
}

protected function getCommandLine($filename){
protected function getCommandLine($filename, $opts=array()){
if (!is_executable(SACY_TRANSFORMER_SASS)){
throw new Exception('SACY_TRANSFORMER_SASS defined but not executable');
}
Expand All @@ -118,7 +139,7 @@ protected function getType(){
}

class ProcessorLess extends ExternalProcessor{
protected function getCommandLine($filename){
protected function getCommandLine($filename, $opts=array()){
if (!is_executable(SACY_TRANSFORMER_LESS)){
throw new \Exception('SACY_TRANSFORMER_LESS defined but not executable');
}
Expand All @@ -140,6 +161,10 @@ protected function getCommandLine($filename){
ExternalProcessorRegistry::registerTransformer('text/coffeescript', 'sacy\ProcessorCoffee');
}

if (defined('SACY_TRANSFORMER_ECO')){
ExternalProcessorRegistry::registerTransformer('text/x-eco', 'sacy\ProcessorEco');
}

if (defined('SACY_TRANSFORMER_SASS')){
ExternalProcessorRegistry::registerTransformer('text/x-sass', 'sacy\ProcessorSass');
ExternalProcessorRegistry::registerTransformer('text/x-scss', 'sacy\ProcessorScss');
Expand Down
33 changes: 30 additions & 3 deletions src/sacy/sacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ function workUnitFromTag($tag, $attrdata, $content){
}

private function extract_attrs($attstr){
$attextract = '#([a-z]+)\s*=\s*(["\'])\s*(.*?)\s*\2#';
// The attribute name regex is too relaxed, but let's
// compromise and keep it simple.
$attextract = '#([a-z\-]+)\s*=\s*(["\'])\s*(.*?)\s*\2#';
if (!preg_match_all($attextract, $attstr, $m)) return false;
$res = array();
foreach($m[1] as $idx => $name){
Expand Down Expand Up @@ -178,12 +180,28 @@ private function extract_script_unit($tag, $attrdata, $content){
'group' => '',
'content' => $content,
'file' => $path,
'data' => $this->parseDataAttrs($attrs),
'type' => $attrs['type']
);
}
return false;
}

private function parseDataAttrs($attrs){
$data = array();

foreach($attrs as $key => $value){
// Compromising again here on the valid
// format of the attr key, to keep the
// regex simple.
if(preg_match('#^data-([a-z\-]+)$#', $key, $match)){
$name = $match[1];
$data[$name] = $value;
}
}

return $data;
}
}

class Config{
Expand Down Expand Up @@ -449,9 +467,15 @@ static public function willTransformType($type){

class JavaScriptRenderHandler extends ConfiguredRenderHandler{
static function supportedTransformations(){
$supported = array();

if (function_exists('CoffeeScript\compile') || ExternalProcessorRegistry::typeIsSupported('text/coffeescript'))
return array('text/coffeescript');
return array();
$supported[] = 'text/coffeescript';

if (ExternalProcessorRegistry::typeIsSupported('text/x-eco'))
$supported[] = 'text/x-eco';

return $supported;
}

static function willTransformType($type){
Expand Down Expand Up @@ -485,6 +509,9 @@ function getOutput($work_unit){
$js = ExternalProcessorRegistry::typeIsSupported('text/coffeescript') ?
ExternalProcessorRegistry::getTransformerForType('text/coffeescript')->transform($js, $source_file) :
\Coffeescript::build($js);
} else if ($work_unit['type'] == 'text/x-eco'){
$eco = ExternalProcessorRegistry::getTransformerForType('text/x-eco');
$js = $eco->transform($js, $source_file, $work_unit['data']);
}
if ($debug){
return $js;
Expand Down