View source
<?php
include 'sassy.admin.inc';
function sassy_preprocessor_info() {
return array(
'sassy' => array(
'label' => 'Sassy PHPSass',
'callback' => 'parse',
'filetypes' => array(
'sass',
'scss',
),
),
);
}
function sassy_parse($file, $local, $global) {
$local += array(
'style' => 'nested',
'debug' => false,
'watchdog' => false,
'errors' => 'watchdog',
);
extract($local);
$path = libraries_get_path('phpsass');
if (!file_exists($path)) {
$path = libraries_get_path('phamlp');
}
$library = $path . '/SassParser.php';
if ($path && file_exists($library)) {
try {
require_once $library;
$options = array(
'style' => $style,
'cache' => FALSE,
'syntax' => $file['extension'],
'debug' => FALSE,
'debug_info' => $debug,
'load_paths' => array(
dirname($file['data']),
),
'filename' => $file['data'],
'load_path_functions' => array(
'sassy_load_callback',
),
'functions' => sassy_get_functions(),
'callbacks' => array(
'warn' => $watchdog ? 'sassy_watchdog_warn' : NULL,
'debug' => $watchdog ? 'sassy_watchdog_debug' : NULL,
),
);
$parser = new SassParser($options);
$result = $parser
->toCss($file['contents'], false);
return $result;
} catch (Exception $e) {
if ($errors == 'watchdog') {
watchdog_exception('sassy', $e);
if (user_access('administer site configuration')) {
drupal_set_message(t('An error occured while processing !stylesheet. Please consult your !watchdog for a detailed error description.', array(
'!stylesheet' => l(basename($file['data']), $file['data']),
'!watchdog' => l('log messages', 'admin/reports/dblog'),
)), 'error');
}
}
else {
if ($errors == 'output') {
return theme_render_template(drupal_get_path('module', 'sassy') . '/sassy_error.tpl.php', array(
'error' => $e,
));
}
}
}
}
}
function sassy_watchdog_warn($message, $context) {
_sassy_watchdog($message, $context, 'warn', WATCHDOG_WARNING);
}
function sassy_watchdog_debug($message, $context) {
_sassy_watchdog($message, $context, 'debug', WATCHDOG_DEBUG);
}
function _sassy_watchdog($message, $context, $type, $level) {
$vars = array(
'%type' => '@' . $type,
'%line' => $context->node->token->line,
'%message' => $message,
);
$message = '%type on line %line: %message';
$file = $context->node->token->filename;
watchdog('Sassy', $message, $vars, $level, l($file, $file));
}
function sassy_get_functions() {
$functions =& drupal_static(__FUNCTION__);
if (!isset($functions)) {
foreach (module_invoke_all('sassy_functions') as $info) {
$info = (object) $info;
$functions[$info->name] = $info->callback;
}
foreach (array_merge($GLOBALS['base_theme_info'], array(
$GLOBALS['theme_info'],
)) as $theme) {
$function = $theme->name . '_sassy_functions';
if (function_exists($function) && ($data = $function())) {
foreach ($data as $info) {
$info = (object) $info;
$functions[$info->name] = $info->callback;
}
}
}
}
return $functions;
}
function sassy_load_callback($file) {
$file = explode('/', $file, 2);
$namespace = '';
if (count($file) >= 2) {
$namespace = preg_replace('/[^0-9a-z]+/', '_', strtolower(array_shift($file)));
}
foreach (module_implements('sassy_resolve_path_' . $namespace) as $module) {
$hook = $module . '_sassy_resolve_path_' . $namespace;
if (function_exists($hook) && ($paths = call_user_func($hook, $file[0]))) {
return (array) $paths;
}
}
foreach (module_implements('sassy_resolve_path') as $module) {
$hook = $module . '_sassy_resolve_path';
if (function_exists($hook) && ($paths = call_user_func($hook, $file, $namespace))) {
return (array) $paths;
}
}
if ($namespace != 'general' && $namespace != 'sprites' && $namespace != 'typography' && $namespace != 'links' && $namespace != 'lists' && $namespace != 'utilities' && $namespace != 'css' && $namespace != 'text' && $namespace != 'css3' && $namespace != 'tables' && $namespace != 'color') {
$module_exists_in_filesystem = (bool) drupal_get_filename('module', $namespace, NULL, FALSE);
if ($module_exists_in_filesystem) {
$path = drupal_get_path('theme', $namespace);
}
else {
$theme_exists_in_filesystem = (bool) drupal_get_filename('theme', $namespace, NULL, FALSE);
if ($theme_exists_in_filesystem) {
$path = drupal_get_path('theme', $namespace);
}
else {
$path = false;
}
}
}
else {
$path = NULL;
}
if (!$path && $namespace == 'public') {
$path = 'public:/';
}
if ($path) {
$path = $path . '/' . $file[0];
if (file_exists($path)) {
$path = drupal_realpath($path);
return array(
$path,
);
}
}
return FALSE;
}
function sassy_sassy_resolve_path_sassy($file) {
return sassy_registered_includes(basename($file));
}
function sassy_registered_includes($base = NULL) {
$includes =& drupal_static(__FUNCTION__);
if (!isset($includes)) {
if ($cache = cache_get('sassy_libraries:' . $GLOBALS['theme_key'])) {
$includes = $cache->data;
}
else {
$includes = array();
foreach (array_merge(module_list(), $GLOBALS['base_theme_info'], array(
$GLOBALS['theme_info'],
)) as $info) {
$type = is_object($info) ? 'theme' : 'module';
$name = $type == 'theme' ? $info->name : $info;
$info = $type == 'theme' ? $info->info : system_get_info('module', $name);
if (!empty($info['sassy'])) {
foreach ($info['sassy'] as $include) {
$path = drupal_get_path($type, $name) . '/' . $include;
if (is_file($path)) {
$includes[basename($path)][] = $path;
}
}
}
}
drupal_alter('sassy_includes', $includes);
cache_set('sassy_includes:' . $GLOBALS['theme_key'], $includes);
}
}
if (isset($base) && isset($includes[$base])) {
return $includes[$base];
}
else {
if (!isset($base)) {
return $includes;
}
}
}