-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessCompiler.module
More file actions
174 lines (147 loc) · 5.74 KB
/
LessCompiler.module
File metadata and controls
174 lines (147 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
class LessCompiler extends WireData implements Module, ConfigurableModule{
/**
* Less compiler
*
* Compiles less files to css using lessphp library.
*
* ProcessWire 2.x
* Copyright (C) 2012 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
protected static $defaultConfigData = array(
'less_folder_path' => 'bootstrap/less',
'css_folder_path' => 'styles',
'less_files_list' => 'bootstrap.less',
'compile_not_minified' => true,
'compile_minified' => true,
);
public static function getModuleInfo() {
return array(
'title' => 'Less Compiler',
'summary' => 'Compiles .less to .css using lessphp library. This is just first and very early version, almost not tested, so may be buggy. Be careful, don\'t use it in production',
'href' => 'http://processwire.com',
'version' => 002,
'permanent' => false,
'autoload' => true,
'singular' => true,
);
}
/**
* Set the default config data
*
*/
public function __construct() {
foreach(self::$defaultConfigData as $key => $value) {
$this->set($key, $value);
}
}
public function init() {
$this->addHookBefore('Page::render', $this, "compileLess");
}
function autoCompileLess($less_fname, $css_fname, $less = null) {
// load the cache
$cache_fname = $css_fname.".cache";
if (file_exists($cache_fname)) {
$cache = unserialize(file_get_contents($cache_fname));
} else {
$cache = $less_fname;
}
try {
$new_cache = lessc::cexecute($cache,false,$less);
}
catch (Exception $e) {
echo "LessPHP parsing error: ".$e->getMessage();
}
if (!is_array($cache) || $new_cache['updated'] > $cache['updated']) {
file_put_contents($cache_fname, serialize($new_cache));
file_put_contents($css_fname, $new_cache['compiled']);
}
}
public function compileLess(HookEvent $event) {
if($event->object->template == "admin") return;
//for now lessc.inc.php is in the same foldes where LessCompiles module
$dirname = dirname(__FILE__);
include($dirname.'/lessc.inc.php');
//path to a folder with .less files, can't have them in subfolders for now
$lesspath = $this->data['less_folder_path'];
//path to a folder to save compiled .css
$csspath = $this->data['css_folder_path'];
$lessfiles = explode(',',$this->data['less_files_list']);
$compile = $this->data['compile_not_minified'];
$compileMinified = $this->data['compile_minified'];
//get lessc instance
$lessc = new lessc();
//go through all the less files specified
foreach ($lessfiles as $lessfile) {
$lessfile = trim($lessfile);
$lessfile = $this->config->paths->templates.$lesspath.'/'.$lessfile;
$filename = pathinfo($lessfile, PATHINFO_FILENAME);
//name our css file the same name that corresponding less file except file extention
$cssfile = $this->config->paths->templates.$csspath."/".$filename.".css";
//filename for minified version
$cssfileMin = $this->config->paths->templates.$csspath."/".$filename.".min.css";
// if chosen to compile not minified
if ($compile) {
//compile with caching
$this->autoCompileLess($lessfile, $cssfile, $lessc);
}
// if chosen to compile minified
if ($compileMinified) {
//set to minify
$lessc->setFormatter("compressed");
//compile with caching
$this->autoCompileLess($lessfile, $cssfileMin, $lessc);
}
}
}
static public function getModuleConfigInputfields(array $data) {
$fields = new InputfieldWrapper();
$fields->description = "Here you can specify plugin's options";
foreach(self::$defaultConfigData as $key => $value) {
if(!isset($data[$key])) $data[$key] = $value;
}
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'less_folder_path');
$f->attr('value', $data['less_folder_path']);
$f->label = "Less Files Path";
$f->description = "Path to less files folder to be compiled. Please specify it relative to /templates folder without starting or ending trailing slashes. For example, default folder is 'bootstrap/less', so in order it to work please place /bootstrap folder with all its content in your /templates folder";
$fields->add($f);
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'less_files_list');
$f->attr('value', $data['less_files_list']);
$f->label = "Less Files List";
$f->description = "List of less files to compile separated by commas (with file extension, for example mixins.less)";
$fields->add($f);
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'css_folder_path');
$f->attr('value', $data['css_folder_path']);
$f->label = "CSS Files Path";
$f->description = "Folder to output compiles CSS files with trailing slash. Please specify it relative to /templates folder with starting or ending trailing slashes.";
$fields->add($f);
$f = wire('modules')->get("InputfieldCheckbox");
$f->name = "compile_not_minified";
$f->label = "Compile not minified";
$f->description = "Create css file without compression.";
$f->value = 1; // providing a "checked" value for the checkbox is necessary
$checked = $data['compile_not_minified'];
$f->attr('checked', $checked ? 'checked' : '');
$fields->add($f);
$f = wire('modules')->get("InputfieldCheckbox");
$f->name = "compile_minified";
$f->label = "Compile minified";
$f->description = "Create compressed css file (will looke like this: original_filename.min.css).";
$f->value = 1; // providing a "checked" value for the checkbox is necessary
$checked = $data['compile_minified'];
$f->attr('checked', $checked ? 'checked' : '');
$fields->add($f);
// since this is a static function, we can't use $this->modules, so get them from the global wire() function
$modules = wire('modules');
return $fields;
}
}
?>