forked from mikestowe/php-ramlMerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ramlMerge.php
52 lines (40 loc) · 1.31 KB
/
ramlMerge.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
<?php
function doInclude ($file, $tabIndex = '') {
$contents = @file_get_contents($file);
if (!$contents) {
$contents = @file_get_contents(BASE_PATH . $file);
}
if (!$contents) {
return "\n\n# Unable to Include" . $file . "\n\n";
}
if ($tabIndex) {
$contents = $tabIndex . str_replace("\n", "\n" . $tabIndex, $contents);
}
$contents = preg_replace_callback('/(([\s\t]*)([a-z0-9_\/\-]+)):[\s]+\!include ([^\s]+)/i',
function($matches) {
$property = $matches[3];
$spacing = $matches[2];
$file = $matches[4];
if (!preg_match("/^((https?:\/\/)|\/)/i", $file)) {
$file = BASE_PATH . "/" . $file;
}
$i = 0;
$cap = ": | \n";
$subContent = doInclude($file, $spacing . " ");
$subLines = explode("\n", $subContent);
while (isset($subLines[$i]) && !preg_match("/[^\s]/i", $subLines[$i])) {
$i++;
}
if (strpos($subLines[$i], ':') && preg_match("/(:\s*('|\")(.+)('|\"))*/", $subLines[$i])) {
$cap = ":\n";
}
return $spacing . $property . $cap . $subContent;
},
$contents);
return $contents;
}
$file = $argv[1];
$outputFile = $argv[2];
define('BASE_PATH', dirname($file));
file_put_contents($outputFile, doInclude($file));
file_put_contents($outputFile, preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", file_get_contents($outputFile)));