Skip to content

Commit

Permalink
fixed #1, #2
Browse files Browse the repository at this point in the history
  • Loading branch information
lesstif committed Jul 12, 2016
1 parent ab35d31 commit c6c1170
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ info:
@echo ${PHP_MODS_AVAILABLE}
@echo ${PHP_CONFIG_DIR}

${OBJECTS}:
${OBJECTS}: ${SOURCES}
${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}

install:
Expand Down
48 changes: 35 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Compile .scss and .sass files using PHP.

## compile sass string.

### Nesting

```php
<?php

Expand All @@ -33,26 +31,50 @@ $str = $sass->compile($str);
var_dump($str);
```

### Variables
## compile sass file and returing string.

```php
<?php

$file = 'input.sass';

$sass = new Sass();

$str = <<<'SASS'
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
$css = $sass->compileFile($file);

body {
font: 100% $font-stack;
color: $primary-color;
}
SASS;
var_dump($css);
```

## compile sass file to css file.

```php
<?php

$file = 'input.sass';

$sass = new Sass();

// saved 'css-output-dir/input.css'
$css = $sass->compileFile($file, 'css-output-dir');

var_dump($css);
```

## set style

```php
<?php

$file = 'input.sass';

$sass = new Sass();

$sass->setOutputStyle(Sass::STYLE_COMPRESSED);

$ret = $sass->compile($str);
// saved 'css-output-dir/input.css'
$css = $sass->compileFile($file, 'css-output-dir');

var_dump($ret);
var_dump($css);
```

# Installation
Expand Down
183 changes: 168 additions & 15 deletions src/php_sass.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Libraries used.
*/
#include <fstream>

#include "php_sass.h"
#include "phpsass_version.h"

Expand All @@ -20,7 +22,6 @@ vector<string> &split(const string &s, char delim, vector<string> &elems) {
return elems;
}


vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
Expand All @@ -38,17 +39,24 @@ Lesstif::Sass::Sass()
// read in the "sass.plugin path" variable from the php.ini file
std::string plugin_path = Php::ini_get("sass.plugin_path");

this->vpluginPath = split(plugin_path, ',');
vector<string> vPath = split(plugin_path, ',');
for(vector<string>::iterator it = vPath.begin(); it != vPath.end(); ++it) {
sass_option_push_plugin_path(this->options, it->c_str());
}

// read in the "variables_order" variable
// read in the "include_path" variable
std::string include_path = Php::ini_get("sass.include_path");
Php::notice << "sass.include_path:" << include_path << endl;
vPath = split(include_path, ',');
for(vector<string>::iterator it = vPath.begin(); it != vPath.end(); ++it) {
sass_option_push_include_path(this->options, it->c_str());
}

this->vincludePath = split(include_path, ',');

for (std::vector<std::string>::const_iterator i = this->vincludePath.begin(); i != this->vincludePath.end(); ++i) {
Php::notice << *i << ' ';
}
/*
Php::notice << "sass.include_path:" << include_path << endl;
Php::notice << "sass.plugin_path" << Php::ini_get("sass.plugin_path") << endl;
Php::notice << "include_path:" << Php::ini_get("include_path") << endl;
Php::notice << "session.save_handler:" << Php::ini_get("session.save_handler") << endl;
*/
}


Expand All @@ -67,6 +75,11 @@ Lesstif::Sass::~Sass()
*/
Php::Value Lesstif::Sass::compile(Php::Parameters &params)
{
if (params.size() == 0)
{
throw Php::Exception("Parameters must great than 1. ");
}

std::string source_string = params[0];

char * buffer = new char[source_string.size() + 1];
Expand All @@ -78,10 +91,8 @@ Php::Value Lesstif::Sass::compile(Php::Parameters &params)
Sass_Data_Context* ctx = sass_make_data_context(buffer);
Sass_Context* ctx_out = sass_data_context_get_context(ctx);

// default option
Sass_Options* options = sass_make_options();

sass_data_context_set_options(ctx, options);
// set option
sass_data_context_set_options(ctx, this->options);
int status = sass_compile_data_context(ctx);

// Check the context for any errors...
Expand All @@ -95,8 +106,8 @@ Php::Value Lesstif::Sass::compile(Php::Parameters &params)
Php::Value ret = sass_context_get_output_string(ctx_out);

sass_delete_data_context(ctx);
return ret;

return ret;
}

Php::Value Lesstif::Sass::version()
Expand All @@ -117,5 +128,147 @@ Php::Value Lesstif::Sass::version()

Php::Value Lesstif::Sass::compileFile(Php::Parameters &params)
{
return "not implmentated, yet";
if (params.size() == 0)
{
throw Php::Exception("Parameters must great than 1. ");
}

std::string inputFile = params[0];
std::string outPath;
std::string fileName; // only filename, not include extension.

if (params.size() > 1) {
outPath = params[1].stringValue();
}

//Php::notice << "inputFile :''" << inputFile << "',outPath:'" << outPath << "'" << std::flush;

Sass_File_Context* ctx = sass_make_file_context(inputFile.c_str());
Sass_Context* ctx_out = sass_file_context_get_context(ctx);

if (outPath.length() > 0){
sass_option_set_output_path(this->options, outPath.c_str());

// extract filename
unsigned int idx = inputFile.rfind('.');

if(idx != std::string::npos) {
fileName = inputFile.substr(0, idx );
} else {
fileName = inputFile;
}
}

sass_option_set_input_path(this->options, inputFile.c_str());
sass_file_context_set_options(ctx, this->options);

int status = sass_compile_file_context(ctx);

// Check the context for any errors...
if (status != 0)
{
std::string errMsg = sass_context_get_error_message(ctx_out);
sass_delete_file_context(ctx);
throw Php::Exception("sass context compile error() " + errMsg);
}

std::string outString = sass_context_get_output_string(ctx_out);

// write compiled css into directory
if (outPath.length() > 0)
{
// write file
ofstream sassFile(outPath + "/" + fileName + ".css");

sassFile << outString;
sassFile.close();
}

std::string srcMap;

// std::string constructor does not accept null parameter
const char* tmpStr = sass_option_get_source_map_file(this->options);
if (tmpStr != NULL) {
srcMap = tmpStr;
}

//Php::notice << "srcMapString: " << srcMap << endl;

std::string srcMapString = sass_context_get_source_map_string(ctx_out);

//Php::notice << "sass_context_get_source_map_string:" << srcMapString << std::flush;

std::string outMapfile = sass_option_get_source_map_file(this->options);

if (outMapfile.length() > 0)
{
// write file
ofstream mapFile(outMapfile + "/" + fileName + ".map");

mapFile << srcMapString;
mapFile.close();
}

Php::Value ret = outString;

sass_delete_file_context(ctx);

return ret;
}

void Lesstif::Sass::setSourceMapPath(Php::Parameters &params)
{
std::string mapPath = params[0].stringValue();

sass_option_set_source_map_file(this->options, mapPath.c_str());
}

void Lesstif::Sass::setSourceComment(Php::Parameters &params)
{
bool val = params[0].boolValue();

sass_option_set_source_comments(this->options, val);
}

void Lesstif::Sass::setOutputStyle(Php::Parameters &params)
{
Sass_Output_Style style = static_cast<Sass_Output_Style>(params[0].numericValue());

sass_option_set_output_style(this->options, style);
}

void Lesstif::Sass::setOmitSourceMapUrl(Php::Parameters &params)
{
bool val = params[0].boolValue();

sass_option_set_omit_source_map_url(this->options, val);
}

void Lesstif::Sass::setPrecision(Php::Parameters &params)
{
int val = params[0].numericValue();

sass_option_set_precision(this->options, val);
}

void Lesstif::Sass::setLoadPath(Php::Parameters &params)
{
string val = params[0].stringValue();

vector<string> vPath = split(val, ',');
for(vector<string>::iterator it = vPath.begin(); it != vPath.end(); ++it) {
//Php::notice << "sass_option_push_include_path:" << *it << endl;
sass_option_push_include_path(this->options, it->c_str());
}
}

void Lesstif::Sass::setPluginPath(Php::Parameters &params)
{
std::string val = params[0].stringValue();

vector<string> vPath = split(val, ',');
for(vector<string>::iterator it = vPath.begin(); it != vPath.end(); ++it) {
//Php::notice << "sass_option_push_plugin_path:" << *it << endl;
sass_option_push_plugin_path(this->options, it->c_str());
}
}
13 changes: 10 additions & 3 deletions src/php_sass.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class Sass : public Php::Base
*/
Sass_Options* options;

std::vector<std::string> vpluginPath;
std::vector<std::string> vincludePath;

public:
/**
* C++ constructor and destructor
Expand All @@ -32,6 +29,16 @@ class Sass : public Php::Base

// return php-sass & 3rd party library Version
Php::Value version();

// setter method
void setSourceMapPath(Php::Parameters &params);
void setSourceComment(Php::Parameters &params);

void setOutputStyle(Php::Parameters &params);
void setOmitSourceMapUrl(Php::Parameters &params);
void setPrecision(Php::Parameters &params);
void setLoadPath(Php::Parameters &params);
void setPluginPath(Php::Parameters &params);
};

} // end of namespace
Expand Down
Loading

0 comments on commit c6c1170

Please sign in to comment.