-
Notifications
You must be signed in to change notification settings - Fork 34
/
haml.php
61 lines (47 loc) · 1.21 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
#!/usr/bin/env php
<?php
// from https://github.com/alexluke/grunt-haml-php/blob/master/bin/haml
// Copyright (c) 2013 Alex Luke
require './vendor/autoload.php';
function usage() {
global $argv;
fwrite(STDERR, "Usage: $argv[0] [-t target] [-d] <inputFile> [outputFile]\n");
exit(1);
}
function main($args) {
array_shift($args);
$opts = getopt('t:d');
if (isset($opts['d'])) {
$enable_dynamic_attrs = false;
$args = array_slice($args, 1);
} else {
$enable_dynamic_attrs = true;
}
if (isset($opts['t'])) {
$target = $opts['t'];
$args = array_slice($args, 2);
} else {
$target = 'php';
}
if (count($args) < 1 || count($args) > 2)
usage();
$haml = new MtHaml\Environment($target, array('enable_escaper' => false, 'enable_dynamic_attrs' => $enable_dynamic_attrs));
$input = @file_get_contents($args[0]);
if ($input === false) {
fwrite(STDERR, "Unable to open $args[0]\n");
exit(1);
}
try {
$output = $haml->compileString($input, $args[0]);
} catch (Exception $e) {
fwrite(STDERR, $e->getMessage() . "\n");
exit(1);
}
if (count($args) == 2) {
file_put_contents($args[1], $output);
} else {
echo $output;
}
}
main($argv);
# vim: ft=php