-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
63 lines (46 loc) · 1.34 KB
/
parser.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
<?php
$filename = $argv[1];
$contents = file_get_contents( $filename );
$lines = explode( "\n", $contents );
$result = array();
foreach ( $lines as $line ) {
$matches = array();
if ( preg_match( '@^//\s*components_theme\s+file:([\w.]+)\s+location:([\w]+)\s*$@', $line, $matches ) ) {
// add last fragment to result
if ( $file ) {
$result[ $file ][ $location ][] = concat_fragment_array( $fragment );
}
// start new fragment
$fragment = array();
$file = $matches[1];
$location = $matches[2];
} else {
$fragment[] = $line;
}
}
// add final fragment
if ( $file ) {
$result[ $file ][ $location ][] = concat_fragment_array( $fragment );
}
function concat_fragment_array( $fragment_array ) {
$tags_to_strip = array(
"<?php",
"?>",
"<script>",
"</script>",
);
$scalar = implode( "\n", $fragment_array );
$trimmed_array = explode( "\n", trim( $scalar ) );
$first_line = sizeof( $trimmed_array ) > 0 ? $trimmed_array[0] : "";
if ( in_array( trim( $first_line ), $tags_to_strip ) ) {
array_shift( $trimmed_array );
}
$array_size = sizeof( $trimmed_array );
$last_line = $array_size > 0 ? $trimmed_array[ $array_size - 1 ] : "";
if ( in_array( trim( $last_line ), $tags_to_strip ) ) {
array_pop( $trimmed_array );
}
return implode( "\n", $trimmed_array );
}
print_r( $result );
//print_r( JSON_encode( $result ) );