Skip to content
Browse files

Merge

  • Loading branch information...
2 parents a676f2e + 2d86d35 commit ef66d32a55a463713110491791673f9419d117e6 @stefanoverna stefanoverna committed Mar 20, 2012
View
134 wordless/helpers/image_helper.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ * This module provides functions to interact with images.
+ *
+ * @ingroup helperclass
+ */
+class ImageHelper {
+ /**
+ * Resizes the specified image to the specified dimensions.
+ *
+ * The crop will be centered relative to the image.
+ * If the files do not exists will be created and saved into the tmp/ folder.
+ * After first creation, the same file will be served in response to calling
+ * this function.
+ *
+ * @param string $src
+ * The path to the image to be resized.
+ * @param int $width
+ * The width at which the image will be cropped.
+ * @param int $height
+ * The height at which the image will be cropped.
+ * @return string
+ * The valid URL to the resized image.
+ *
+ * @ingroup helperfunc
+ */
+ function resize_image($src, $width, $height){
+ // initializing
+ $save_path = get_theme_path() . '/tmp/';
+ $img_filename = $save_path . md5($width . 'x' . $height . '_' . basename($src)) . '.jpg';
+
+ // if file doesn't exists, create it ( else simply returns the image )
+ if (!file_exists($img_filename)) {
+ $to_scale = FALSE;
+ $to_crop = FALSE;
+
+ // Get orig dimensions
+ list ($width_orig, $height_orig, $type_orig) = getimagesize($src);
+
+ // get original image ... to improve!
+ switch ($type_orig) {
+ case IMAGETYPE_JPEG:
+ $image = imagecreatefromjpeg($src);
+ break;
+ case IMAGETYPE_PNG:
+ $image = imagecreatefrompng($src);
+ break;
+ case IMAGETYPE_GIF:
+ $image = imagecreatefromgif($src);
+ break;
+ default:
+ return;
+ }
+
+ // which is the new smallest?
+ if ($width < $height)
+ $min_dim = $width;
+ else
+ $min_dim = $height;
+
+ // which is the orig smallest?
+ if ($width_orig < $height_orig)
+ $min_orig = $width_orig;
+ else
+ $min_orig = $height_orig;
+
+ // image of the right size
+ if ($height_orig == $height && $width_orig == $width) ; // nothing to do
+ // if something smaller => scale
+ else if ($width_orig < $width) {
+ $to_scale = TRUE;
+ $ratio = $width / $width_orig;
+ }
+ else if ($height_orig < $height) {
+ $to_scale = TRUE;
+ $ratio = $height / $height_orig;
+ }
+ // if both bigger => scale
+ else if ($height_orig > $height && $width_orig > $width) {
+ $to_scale = TRUE;
+ $ratio_dest = $width / $height;
+ $ratio_orig = $width_orig / $height_orig;
+ if ($ratio_dest > $ratio_orig)
+ $ratio = $width / $width_orig;
+ else
+ $ratio = $height / $height_orig;
+ }
+ // one equal one bigger
+ else if (($width == $width_orig && $height_orig > $height) || ($height == $height_orig && width_orig > $width))
+ $to_crop = TRUE;
+ // some problem...
+ else
+ echo "ALARM";
+
+ // we need to zoom to get the right size
+ if ($to_scale) {
+ $new_width = $width_orig * $ratio;
+ $new_height = $height_orig * $ratio;
+ $image_scaled = imagecreatetruecolor($new_width, $new_height);
+ // scaling!
+ imagecopyresampled($image_scaled, $image, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
+ $image = $image_scaled;
+
+ if($new_width > $width || $new_height > $height)
+ $to_crop = TRUE;
+ }
+ else {
+ $new_width = $width_orig;
+ $new_height = $height_orig;
+ }
+
+ // we need to crop the image
+ if ($to_crop) {
+ $image_cropped = imagecreatetruecolor($width, $height);
+
+ // find margins for images
+ $margin_x = ($new_width - $width) / 2;
+ $margin_y = ($new_height - $height) / 2;
+
+ // cropping!
+ imagecopy($image_cropped, $image, 0, 0, $margin_x, $margin_y, $width, $height);
+ $image = $image_cropped;
+ }
+
+ // Save image
+ imagejpeg($image, $img_filename, 95);
+ }
+
+ // Return image URL
+ return get_bloginfo("template_url") . '/tmp/' . basename($img_filename);
+ }
+}
+
+Wordless::register_helper("ImageHelper");
View
9 wordless/helpers/query_helper.php
@@ -72,11 +72,14 @@ function latest_post_of_type($type, $order = 'date') {
*
* @ingroup helperfunc
*/
- function latest_posts_of_category($category, $limit, $offset = 0, $post_type = 'post', $taxonomy = 'category',$order = 'date', $ord = 'ASC') {
+ function latest_posts_of_category($category, $limit, $offset = 0, $post_type = 'post', $taxonomy = 'category', $order = 'date', $ord = 'ASC') {
return query_posts(array(
'posts_per_page' => $limit,
- 'taxonomy' => $taxonomy,
- 'term' => $category,
+ 'tax_query' => array(
+ 'taxonomy' => $taxonomy,
+ 'field' => 'slug',
+ 'terms' => $category,
+ ),
'offset' => $offset,
'post_type' => $post_type,
'orderby' => $order,
View
12 wordless/helpers/render_helper.php
@@ -7,7 +7,7 @@ function render_error($title, $description) {
die();
}
- function render_template($name) {
+ function render_template($name, $locals = array()) {
$valid_filenames = array("$name.html.haml", "$name.haml", "$name.html.php", "$name.php");
foreach ($valid_filenames as $filename) {
$path = Wordless::join_paths(Wordless::theme_views_path(), $filename);
@@ -22,6 +22,8 @@ function render_template($name) {
render_error("Template missing", "<strong>Ouch!!</strong> It seems that <code>$name.html.haml</code> or <code>$name.html.php</code> doesn't exist!");
}
+ extract($locals);
+
switch ($format) {
case 'haml':
$tmp_dir = Wordless::theme_temp_path();
@@ -48,20 +50,20 @@ function render_template($name) {
}
- function get_partial_content($name) {
+ function get_partial_content($name, $locals = array()) {
ob_start();
- render_partial($name);
+ render_partial($name, $locals);
$partial_content = ob_get_contents();
ob_end_clean();
return $partial_content;
}
- function render_partial($name) {
+ function render_partial($name, $locals = array()) {
$parts = preg_split("/\//", $name);
if (!preg_match("/^_/", $parts[sizeof($parts)-1])) {
$parts[sizeof($parts)-1] = "_" . $parts[sizeof($parts)-1];
}
- render_template(implode($parts, "/"));
+ render_template(implode($parts, "/"), $locals);
}
function yield() {
View
1 wordless/helpers/theme_helper.php
@@ -1,5 +1,4 @@
<?php
-
/**
* This module provides functions to get information from the curren theme.
*
View
33 wordless/helpers/url_helper.php
@@ -7,13 +7,35 @@
class UrlHelper {
/**
+ * Returns URL arguments
+ *
+ * @param int $index (optional)
+ * The number (counting from zero) of the argument in the list. If is not
+ * specified all arguments will be returned as an array.
+ * @return array|mixed
+ * If $index was specified returns the relative URL argument, elsewhere
+ * returns an array with all available URL arguments.
+ *
+ * @ingroup helperfunc
+ */
+ function arg($index = NULL) {
+ $args = array_values(array_filter(explode('/', $_SERVER['REQUEST_URI'])));
+
+ if (isset($index))
+ return $args[$index];
+
+ return $args;
+ }
+
+ /**
* Returns the URL path to the spcified folder in the assets directory.
*
* @param string $path
* The path inside the @e {theme}/assets/ folder.
- *
* @return string
* The complete URL path to the specified folder.
+ *
+ * @ingroup helperfunc
*/
function asset_url($path) {
return get_bloginfo('stylesheet_directory') . "/assets/$path";
@@ -24,9 +46,10 @@ function asset_url($path) {
*
* @param string $path
* The path inside the @e {theme}/assets/images/ folder.
- *
* @return string
* The complete URL path to the specified folder.
+ *
+ * @ingroup helperfunc
*/
function image_url($path) {
return asset_url("images/$path");
@@ -37,9 +60,10 @@ function image_url($path) {
*
* @param string $path
* The path inside the @e {theme}/assets/stylesheet/ folder.
- *
* @return string
* The complete URL path to the specified folder.
+ *
+ * @ingroup helperfunc
*/
function stylesheet_url($path) {
return asset_url("stylesheets/$path");
@@ -50,9 +74,10 @@ function stylesheet_url($path) {
*
* @param string $path
* The path inside the @e {theme}/assets/javascript/ folder.
- *
* @return string
* The complete URL path to the specified folder.
+ *
+ * @ingroup helperfunc
*/
function javascript_url($path) {
return asset_url("javascripts/$path");
View
1 wordless/preprocessors.php
@@ -4,3 +4,4 @@
require_once "preprocessors/compile_exception.php";
require_once "preprocessors/compass_preprocessor.php";
require_once "preprocessors/sprockets_preprocessor.php";
+require_once "preprocessors/less_preprocessor.php";
View
127 wordless/preprocessors/less_preprocessor.php
@@ -0,0 +1,127 @@
+<?php
+
+require_once "wordless_preprocessor.php";
+
+/**
+ * Compile LESS files using the `lessc` executable.
+ *
+ * LessPreprocessor relies on some preferences to work:
+ * - css.lessc_path (defaults to "/usr/bin/lessc"): the path to the lessc executable
+ * - css.output_style (defaults to "compressed"): the output style used to render css files
+ * (check LESS source for more details: https://github.com/cloudhead/less.js/blob/master/bin/lessc)
+ *
+ * You can specify different values for this preferences using the Wordless::set_preference() method.
+ *
+ * @copyright welaika &copy; 2011 - MIT License
+ * @see WordlessPreprocessor
+ */
+class LessPreprocessor extends WordlessPreprocessor {
+
+ public function __construct() {
+ parent::__construct();
+
+ $this->set_preference_default_value("css.lessc_path", "/usr/bin/lessc");
+ $this->set_preference_default_value("css.compress", false);
+ }
+
+ /**
+ * Overrides WordlessPreprocessor::asset_hash()
+ * @attention This is raw code. Right now all we do is find all the *.{sass,scss} files, concat
+ * them togheter and generate an hash. We should find exacty the sass files required by
+ * $file_path asset file.
+ */
+ protected function asset_hash($file_path) {
+ $hash = array(parent::asset_hash($file_path));
+ $base_path = dirname($file_path);
+ $files = $this->folder_tree(dirname($base_path), "*.less");
+ sort($files);
+ $hash_seed = array();
+ foreach ($files as $file) {
+ $hash_seed[] = $file . date("%U", filemtime($file));
+ }
+ return md5(join($hash_seed));
+ }
+
+ /**
+ * Overrides WordlessPreprocessor::comment_line()
+ */
+ protected function comment_line($line) {
+ return "/* $line */\n";
+ }
+
+ /**
+ * Overrides WordlessPreprocessor::content_type()
+ */
+ protected function content_type() {
+ return "text/css";
+ }
+
+ /**
+ * Overrides WordlessPreprocessor::die_with_error()
+ */
+ protected function die_with_error($description) {
+ echo "/************************\n";
+ echo $description;
+ echo "************************/\n\n";
+ echo sprintf(
+ 'body::before { content: "%s"; font-family: monospace; white-space: pre; display: block; background: #eee; padding: 20px; }',
+ 'Damn, we\'re having problems compiling the Less. Check the CSS source code for more infos!'
+ );
+ die();
+ }
+
+
+ /**
+ * Process a file, executing lessc executable.
+ *
+ * Execute the lessc executable, overriding the no-op function inside
+ * WordlessPreprocessor.
+ *
+ * If using php-fpm, remember to pass the PATH environment variable
+ * in php-fpm.ini (e.g. env[PATH]=/usr/local/bin:/usr/bin:/bin)
+ */
+ protected function process_file($file_path, $result_path, $temp_path) {
+
+ $this->validate_executable_or_die($this->preference("css.lessc_path"));
+
+ // On cache miss, we build the file from scratch
+ $pb = new ProcessBuilder(array(
+ $this->preference("css.lessc_path"),
+ $file_path
+ ));
+
+ // Since the official lessc executable relies on node.js, we need to
+ // inherit env to get access to $PATH so we can find the node executable
+ $pb->inheritEnvironmentVariables();
+
+ if ($this->preference("css.compress")) {
+ $pb->add("-compress");
+ }
+
+ $proc = $pb->getProcess();
+ $code = $proc->run();
+
+ if (0 < $code) {
+ $this->die_with_error($proc->getErrorOutput());
+ }
+
+ return $proc->getOutput();
+ }
+
+ /**
+ * Overrides WordlessPreprocessor::supported_extensions()
+ */
+ public function supported_extensions() {
+ return array("less");
+ }
+
+
+ /**
+ * Overrides WordlessPreprocessor::to_extension()
+ */
+ public function to_extension() {
+ return "css";
+ }
+
+}
+
View
4 wordless/process.php
@@ -23,14 +23,14 @@ class Process
* @param integer $timeout The timeout in seconds
* @param array $options An array of options for proc_open
*
- * @throws \RuntimeException When proc_open is not installed
+ * @throws RuntimeException When proc_open is not installed
*
* @api
*/
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
if (!function_exists('proc_open')) {
- throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
+ throw new RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
}
$this->commandline = $commandline;
View
2 wordless/process_builder.php
@@ -84,7 +84,7 @@ public function setOption($name, $value)
public function getProcess()
{
if (!count($this->arguments)) {
- throw new \LogicException('You must add() command arguments before calling getProcess().');
+ throw new LogicException('You must add() command arguments before calling getProcess().');
}
$options = $this->options;
View
8 wordless/theme_builder/vanilla_theme/config/initializers/wordless_preferences.php
@@ -4,12 +4,16 @@
* Configure Wordless preferences here.
*/
+Wordless::set_preference("assets.preprocessors", array("SprocketsPreprocessor", "CompassPreprocessor"));
+// Wordless::set_preference("assets.cache_enabled", true);
+
// Wordless::set_preference("css.compass_path", "/usr/bin/compass");
// Wordless::set_preference("css.output_style", "compressed");
// Wordless::set_preference("css.require_libs", array());
+// Wordless::set_preference("css.lessc_path", "/usr/bin/lessc");
+// Wordless::set_preference("css.compress", false);
+
// Wordless::set_preference("js.ruby_path", "/usr/bin/ruby");
// Wordless::set_preference("js.yui_compress", false);
// Wordless::set_preference("js.yui_munge", false);
-
-// Wordless::set_preference("assets.cache_enabled", true);
View
5 wordless/wordless.php
@@ -18,7 +18,7 @@ public static function initialize() {
self::require_helpers();
self::require_theme_initializers();
self::register_activation();
- self::register_preprocessors("SprocketsPreprocessor", "CompassPreprocessor");
+ self::register_preprocessors();
self::register_preprocessor_actions();
}
self::load_admin_page();
@@ -45,7 +45,8 @@ public static function register_helper($class_name) {
}
public static function register_preprocessors() {
- foreach (func_get_args() as $preprocessor_class) {
+ $preprocessors = self::preference("assets.preprocessors", array());
+ foreach ($preprocessors as $preprocessor_class) {
self::$preprocessors[] = new $preprocessor_class();
}
}

0 comments on commit ef66d32

Please sign in to comment.
Something went wrong with that request. Please try again.