-
Notifications
You must be signed in to change notification settings - Fork 0
/
Haml.php
209 lines (182 loc) · 7.86 KB
/
Haml.php
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
class Haml {
const LITERAL = '[-\w\$_0-9\[\]]+|<\?.*?\?>';
const _PHP_VAR = '/^<\?.*?\?>$/';
const _VAR = '/^(\$[\w_0-9\[\]]+|<\?.*?\?>)$/';
const ELEMENT = '/^[\w_][-\w_0-9]*$/';
const _PHP = '<PHP>';
protected $_;
protected $_stack;
protected $_context;
protected $_unparse;
protected $_was_php;
protected $_deep;
protected $_eval_context;
public function __construct($context = null, $eval_context = null) {
$this->_context = $context;
$this->_eval_context = $eval_context;
$this->init();
}
public function init() {
$this->_ = '';
$this->_stack = array();
$this->_unparse = false;
$this->_deep = 0;
}
public function parse($text) {
$this->init();
$lines = array_map(array($this,'calcLine'),explode("\n",$text));
array_push($lines,array(0,''));
$count = count($lines);
for ($i = 0; $i < $count; ++$i) {
$increase = false;
if (!$this->_unparse) {
$this->popStack($lines[$i][0]);
$increase = ($i < $count - 1) && ($lines[$i + 1][0] > $this->_deep);
}
$this->_ .= $this->parseLine($lines[$i][1],$increase);
}
return $this->_;
}
public static function parse2($text, $context = null, $eval_context = null) {
$h = new Haml($context,$eval_context);
return $h->parse($text);
}
protected function popStack($current_deep) {
$deep = $this->_deep;
$this->_deep = $current_deep;
while ($deep >= $this->_deep) {
if (empty($this->_stack)) return;
$element = array_pop($this->_stack);
$tag = $element[1];
$deep = $element[0];
if ($deep < $this->_deep) {
$this->pushStack($tag,$deep);
return;
}
$this->_ .= ($tag == self::_PHP) ? '<?php }?>' : ('</'.$tag.'>');
}
}
protected function calcLine($line) {
return array(strlen($line) - strlen(ltrim($line)),preg_replace("/\r+$/",'',$line));
}
protected function parseLine($line,$increase = false) {
if ($this->_unparse && ltrim($line) != $this->_unparse) return $line."\n";
if ($this->_unparse) {
if ($this->_unparse == '?>') $this->_ .= '?>';
$this->_unparse = false;
return '';
}
if (empty($this->_) && substr($line,0,3) == '!!!') return $this->parseLineDoctype($line)."\n";
#check unparse
if (preg_match('|^\s*<<<(.+)$|',$line,$res) != 0) {
$this->_unparse = $res[1];
$this->_ .= "\n";
return '';
}
#check PHP injection
if (preg_match('|^\s*<\?php$|', $line) != 0) {
$this->_unparse = '?>';
$this->_ .= "<?php\n";
return '';
}
if (preg_match('|^\s*//|',$line) != 0) return '';
if (preg_match('/^\s*\- (.*)$/',$line) != 0) {
if ($increase) $this->pushStack(self::_PHP);
return '<?php '.substr(ltrim($line),2).($increase ? '{' : '').'?>';
}
if (preg_match('/^\s*\-= (.*)$/',$line,$arr) != 0) return $this->_echo($arr[1]);
if (preg_match('|^\s*\-/$|',$line) != 0) return "\n";
if (preg_match('/^(\s*)\-e (.*)$/',$line,$arr) != 0) return $this->parseLine($arr[1].($this->_eval($arr[2])),$increase);
if (preg_match('/^\s*\\\\/',$line) != 0) return substr(ltrim($line),1);
if (preg_match('/^\s*(?:%('.self::LITERAL.'))?((?:\.(?:'.self::LITERAL.'))*)(?:#('.self::LITERAL.'))?(?:\{([^\}]*)\})?(\=)?(.*)$/',$line,$arr) != 0 && (!empty($arr[1]) || !empty($arr[2]) || !empty($arr[3]) || !empty($arr[4]) || !empty($arr[5]))) return ($this->parseLineCommon($line,$arr));
if (preg_match('|^\s*/|',$line) != 0) return $this->parseLineComment($line);
return ltrim($line);
}
protected function parseLineCommon($line,$arr) {
$line = ltrim($line);
$tag = empty($arr[1]) ? 'div' : $this->parseElement($arr[1]);
if ($tag === false) return $line;
$classes = empty($arr[2]) ? false : explode('.',substr($arr[2],1));
if ($classes) {
$classes = array_map(array($this,'parseElement'),$classes);
foreach ($classes as &$class)
if ($class === false) return $line;
$classes = implode(' ',$classes);
}
if (!empty($arr[3])) {
$id = $this->parseElement($arr[3]);
if ($id === false) return $line;
} else
$id = false;
$params = ltrim($arr[4]);
if (!empty($params)) {
$params_result = array();
$regexp = '/^('.self::LITERAL.')(?:\="([^"]*)")?/';
while (preg_match($regexp,$params,$param) != 0) {
$attr = $this->parseElement($param[1]);
if ($attr === false) return $line;
if (array_key_exists(2, $param)) {
$value = $param[2];
if (preg_match(self::_VAR,$value) != 0) $value = $this->getVariable($value);
array_push($params_result,' '.$attr.'="'.$value.'"');
} else
array_push($params_result,' '.$attr);
$params = ltrim(preg_replace($regexp,'',$params));
}
if (!empty($params)) return $line;
$params = $params_result;
}else
$params = false;
$close_tag = in_array($tag,array('br', 'hr', 'link', 'meta', 'img', 'input', 'param'));
$is_echo = ($arr[5] == '=');
$content = ltrim($arr[6]);
if ($close_tag && ($is_echo || !empty($content))) return $line;
if ($is_echo) $content = $this->_echo($content);
$result = '<'.$tag.($id ? (' id="'.$id.'"') : '').($classes ? (' class="'.$classes.'"') : '').($params ? implode('',$params) : '');
if ($close_tag)
$result .= ' />';
else {
$this->pushStack($tag);
$result .= '>'.$content;
}
return $result;
}
protected function parseLineDoctype($line) {
if (trim(substr($line,0,4)) == '!!!') {
$temp = trim(substr($line,3));
if ($temp == 'XML') return '<?xml version="1.0" encoding="utf-8" ?>';
if ($temp == '1.1') return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
if ($temp == 'Strict') return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
if ($temp == '5') return '<!DOCTYPE html>';
if ($temp == '') return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
} elseif (trim(substr($line,0,5)) == '!!!!') {
$temp = trim(substr($line,4));
if ($temp == 'Transitional') return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
if ($temp == 'Frameset') return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
if ($temp == '') return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
}
return $line;
}
protected function parseLineComment($line) { return '<!--'.substr(ltrim($line),1).'-->'; }
protected function parseElement($element) {
if (preg_match(self::_VAR,$element) != 0) $element = $this->getVariable($element);
elseif (preg_match(self::ELEMENT,$element) == 0) return false;
return $element;
}
protected function getVariable($var) {
if (preg_match(self::_PHP_VAR,$var) != 0) return $var;
$str = is_null($this->_context) ? $var : call_user_func_array($this->_context,array(substr($var,1)));
return $this->_echo($str);
}
protected function _echo($str) {
return '<?php echo '.$str.';?>';
}
protected function _eval($str) {
return is_null($this->_eval_context) ? eval($str) : call_user_func_array($this->_eval_context,array($str));
}
protected function pushStack($element, $deep = null) {
array_push($this->_stack,array(is_null($deep) ? $this->_deep : $deep,$element));
}
}
?>