forked from arthurwolf/Octofab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.pl
103 lines (74 loc) · 3.13 KB
/
compile.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
97
98
#!/usr/bin/perl
use strict;
use WWW::Mechanize;
use HTML::TreeBuilder;
use File::Slurp;
use utf8;
# Step 0, set context
my $octoprint = "http://localhost:5000";
# Step 1, get the octoprint page
my $mech = WWW::Mechanize->new();
$mech->get( $octoprint );
# Step 2, get an array of lines
my @lines = split("\n", $mech->content);
# Step 3, find every tag to replace
my @scripts_inlined;
for my $may_be_a_script ( @lines ){
if( $may_be_a_script =~ m{script.*static} ){
# Get the URL for this script
my $url = $octoprint . HTML::TreeBuilder->new_from_content( $may_be_a_script )->look_down( _tag => "script", src => qr/static/ )->attr("src");
# Get the file's contents
$mech->get($url);
# Inline
push @scripts_inlined, "<script type='text/javascript'>\n" . $mech->content . "\n</script>\n";
}elsif( $may_be_a_script =~ m{link.*static.*stylesheet} ){
# Get the HTML element
my $element = HTML::TreeBuilder->new_from_content( $may_be_a_script )->look_down( _tag => "link", href => qr/static/ );
# Get the URL for this stylesheet
my $url = $octoprint . $element->attr("href");
# Get the file's contents
$mech->get($url);
# Inline
push @scripts_inlined, "<style rel='" . $element->attr("rel") . "'>\n" . $mech->content . "\n</style>\n";
}elsif( $may_be_a_script =~ m{</body>} ){
# Insert the worker script
if( 1 ){
# Get the file's content
my $content = read_file("OctoPrint/src/octoprint/static/gcodeviewer/js/Worker.js");
# Make the new tag
my $tag = HTML::Element->new('script', type => "javascript/worker", id => "inlineworker");
$tag->push_content($content);
# Add to the HTML file
push @scripts_inlined, $tag->as_HTML();
}
# Insert all our custom scripts
for my $jsfile ( split("\n", `ls src/js/*`) ){
# Get the file's content
my $content = read_file($jsfile);
# Make the new tag
my $tag = HTML::Element->new('script', type => "text/javascript");
$tag->push_content($content);
# Add to the HTML file
push @scripts_inlined, $tag->as_HTML();
}
push @scripts_inlined, $may_be_a_script;
}else{
push @scripts_inlined, $may_be_a_script;
}
}
my @images_relocated;
for my $line ( @scripts_inlined ){
while( $line =~ s/(url[\(\"\']+\.\.(.+?)[\"\'\)]+)/url(\/sd\/static$2)/gc ){}
while( $line =~ s/(background-image:[\(\"\']+\.\.(.+?)[\"\)\']+)/background-image:'\/sd\/static$2')/gc ){}
while( $line =~ s/\?v=3\.2\.1//gc ){}
# url: API_BASEURL + "files/sdcard",
$line =~ s/API_BASEURL\s\+\s\"files\/sdcard\"/\"\/upload\"/gm;
$line =~ s/href=\"\/static\//href=\".\//gm;
push @images_relocated, $line;
}
# Final step, dump the whole file
open( my $fh, '>', 'index.html' );
binmode $fh, ':encoding(UTF-8)';
print $fh join("\n", @images_relocated);
close $fh;
system("python smoothie-upload.py index.html 192.168.0.20");