forked from CommonAccord/Site-Org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser2render.pl
96 lines (66 loc) · 2.38 KB
/
parser2render.pl
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
#!/usr/bin/perl -wl
# CommonAccord - bringing the world to agreement
# Written in 2014 by Primavera De Filippi To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
# You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
use warnings;
use strict;
my %remote;
my $remote_cnt = 0;
my $path = "./Doc/";
my $orig;
sub parse {
my($file,$root,$part) = @_; my $f;
ref($file) eq "GLOB" ? $f = $file : open $f, $file or die $!;
$orig = $f unless $orig;
my $content = parse_root($f, $root, $part);
if($content) { expand_fields($f, \$content, $part); return($content) }
return;
}
sub parse_root {
my ($f, $field, $oldpart) = @_; my $root;
seek($f, 0, 0);
while(<$f>) {
return $root if ($root) = $_ =~ /^\Q$field\E\s*=\s*(.*?)$/;
}
seek($f, 0, 0);
while(<$f>) {
my($part,$what, $newfield);
# if( (($part, $what) = $_ =~ /^([^=]*)=\[(.+?)\]/) and ($field =~ s/^\Q$part\E//) ) {
if( (($part, $what) = $_ =~ /^([^=]*)=\[(.+?)\]/) and ($field =~ /^\Q$part\E/ )) {
if ( $part && ($field =~ /^\Q$part\E(.+?)$/) ){ $newfield = $1;}
$part = $oldpart . $part if $oldpart;
if($what =~ s/^\?//) {
if(! $remote{$path.$what}) { $remote_cnt++;
`curl '$what' > '$path/tmp$remote_cnt.cmacc'`;
$remote{$path.$what} = "$path/tmp$remote_cnt.cmacc";
}
$root = parse($remote{$path.$what}, $newfield || $field, $part);
}
else {
$root = parse($path.$what, $newfield || $field, $part);
}
return $root if $root;
}
}
return $root;
}
sub expand_fields {
my($f,$field,$part) = @_;
foreach( $$field =~ /\{([^}]+)\}/g ) {
my $ex = $_;
my $ox = $part ? $part . $ex : $ex;
my $value = parse($orig, $ox);
$$field =~ s/\{\Q$ex\E\}/$value/gg if $value;
}
}
my $output = parse($ARGV[0], "Model.Root");
print $output;
# XXX FIX ME XXX This is horrible - but I'm just dead tired :(
print "<br><br><hr><br><br>";
print "<div id='missing'><p><h3 class='subtitle2'>Missing parameters:</h3>";
my %seen; my @arr = $output=~/\{([^}]+)\}/g;
@arr = grep { ! $seen{$_}++ } @arr;
print "$_=<br>" foreach @arr;
print "</p></div>";
#clean up the temporary files (remote fetching)
`rm $_` for values %remote;