-
Notifications
You must be signed in to change notification settings - Fork 1
/
rmHTMLement.pl
executable file
·239 lines (228 loc) · 8.69 KB
/
rmHTMLement.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/perl -w
use strict;
# HTML regexes
my $HEAD= qr/^<(?>[a-z]\w*+|!DOCTYPE)[^>]*+>/; my $pstv=qr/[1-9]\d*+/;
my $A=qr/(?>[^<>]|<(?>meta|link|input|img|hr|base)\b[^>]*+>)/; # Asymetric tag/text content
# Node that may have the content or head closing
my $ND= qr{(<([a-z]\w*+)(?>[^/>]*+/>|[^>]*+>(?>$A|(?-2))*+</\g-1>))};
my $C= qr/(?>$A|$ND)/; # Content of node
sub getNthEAtt { # $_[0] searched el $_[1]=nth/nth backw $_[2] el under which to search $_[5] nth bkw
# obtain max nth +1 to solve backward nth
if ($_[5]) {
my $i=1; $_[2]=~/$HEAD(?:$C*?(?=<$_[0]\b)$ND(?{ ++$i }))+/;
return 1 if ($_[1]=$i-$_[1]) <1 }
return not $_[2]=~ /($HEAD(?:$C*?(?=<$_[0]\b)($ND)){$_[1]})(?{ @{$_[3]}=[$_[4].substr($1,0,-length($4)), $4] })/
}
sub getAllEAtt { # $_[1] el under which to search $_[2] its offset $_[4]) nth range pos $_[5] attr $_[6] aatt $_[7] all nodes
my ($tn, $at, $aatt, $all, $a,$b,$i, $OFF,$off)= ($_[0],$_[5],$_[6],,$_[7], 1, '+');
if ($_[4]) {
my ($lt,$e,$n)= $_[4]=~ /(?>(<)|>)(=)?(\d+)/;
$b= $lt? $e? "{$n}" : '{'.--$n.'}' : ($a= $e? $n : $n+1, $b) }
my $search=
$tn ? qr/(?=<$tn\b$at)/
: $aatt ? qr/(?=<[a-z]\w*+$aatt)/
:'';
return not $_[1]=~
/($HEAD) (?{ $OFF=$_[2].$1 })
(?: ($C*?) $search ($ND)
(?{ if (++$i>=$a) { push (@{$_[3]}, [$OFF.($off.=$2), $5]); $off.=$5 } }) )$b /x
}
# Node regex to count depth
my ($MAX,$DTH);
my $N= qr{ (?{ $MAX=0 }) (
<(\w++)[^>]*+> (?>[^/>]*+/> | (?{ $MAX=$DTH if ++$DTH>$MAX }) (?>$A|(?-2))*+ </\g-1> (?{--$DTH})) ) }x;
sub getAllDepth { # $_[1] nth/nth bckwrd $_[2] search space element
# $_[4] its offset $_[5] depth $_[6] nth bckw flag
my ($nth, $ret, $min, $onref, @nd,$offset,$off, $valRng) = ($_[1], $_[3], $_[5]);
my @curNode=[$_[4], $_[2]];
while (@curNode) {
for $onref (@curNode) {
if ($_[6]) {
my $i; $onref->[1]=~ /$HEAD(?:$C*?(?=<$_[0]\b)$ND(?{ ++$i }))+/;
$nth=0 if ($nth=++$i-$_[1]) <1 }
$onref->[1]=~
/($HEAD) (?{ $offset=$1 })
(?(?{$nth})(?:
(?: ($A*+) (?{$offset.=$2 })
(?: (?!<$_[0]\b) (?'n'$N)
(?{ push (@nd, [$onref->[0].$offset, $+{n}]) if $MAX>$min;
$offset.=$+{n} })
)? )*+
(?=<$_[0]\b) (?'nd'$N)
(?{ push (@nd, [$onref->[0].$offset, $+{nd}]) if $MAX>$min;
$off=$offset; $offset.=$+{nd} })
){$nth}
(?{ push (@$ret, [$onref->[0].$off, $+{nd}]) if $MAX>=$min}) | )
(?: (?'a'$A*+) (?{ $offset.=$+{a} })
(?'z'$N) (?{ push (@nd, [$onref->[0].$offset, $+{z}]) if $MAX>$min; $offset.=$+{z} })
)* /x
}
@curNode=@nd; @nd=();
}
return !@$ret
}
sub getAllDepNthRnAtt { # in every nth or positon within range
my @curNode=[$_[2], $_[1]];
my ($ret, $min, $att, $all, $M, $onref, @nd, $offset) = ($_[3], $_[4], $_[6], $_[7]);
while (@curNode) {
for $onref (@curNode) {
my ($a,$b,$i)= (1, '+');
if ($_[5]) {
my ($lt,$e,$n)= $_[5]=~ /(?>(<)|>)(=)?(\d+)/;
$b = $lt? $e? "{$n}" : '{'.--$n.'}': ($a=$e? $n : $n+1, $b) }
$onref->[1]=~
/($HEAD)(?{$offset=$1}) (?:
($A*+)
(?{$offset.=$2})
(?:(?=<$_[0]\b$att (?{$M=1}))?
(?'n'$N) (?{ if ($MAX>=$min) {
push (@$ret, [$onref->[0].$offset, $+{n}]) if $M and ++$i>=$a;
push (@nd, [$onref->[0].$offset, $+{n}]) if $MAX>$min;
$M=0}
$offset.=$+{n} })
)?) $b /x
}
@curNode=@nd; @nd=();
}
return !@$ret
}
sub getAllDepthAatt { # $_[0] attribute $_[1] el under which to search $_[2] its offset $_[4] depth
my ($att, $ret, $min, $M, $onref, @nd,$offset)= ($_[0], $_[3], $_[4]);
my @curNode=[$_[2], $_[1]];
while (@curNode) {
for $onref (@curNode) {
$onref->[1]=~
/($HEAD)(?{$offset=$1}) (?:
($A*+)
(?{$offset.=$2})
(?: (?=<\w+$att (?{$M=1}) )?
(?'n'$N)
(?{ if ($MAX>=$min) {
push (@$ret, [$onref->[0].$offset, $+{n}]) if $M;
push (@nd, [$onref->[0].$offset, $+{n}]) if $MAX>$min;
$M=0}
$offset.=$+{n} })
)?)* /x
}
@curNode=@nd; @nd=();
}
return !@$ret
}
# Above subs' $_[0] : searched el tag or att. Returns 1 on failure finding, else 0 and offset & node pairs in the 4rd arg, $_[3]
my @res;
sub getE_Path_Rec { # path, offset - node pair
my ($AllDepth, $tag ,$nrev,$nth, $range, $attg, $aatt, $allnode, $path) = $_[0] =~
m{ ^/ (/)? (?> ([^/@*[]+) (?> \[ (?> (last\(\)-)? ($pstv) | position\(\)(?!<1)([<>]=? $pstv) | @(\*| [^]]+) ) \] )? | @([a-z]\w*[^/]* |\*) | (\*) ) (.*) }x;
$attg=$attg? '\s+'.($attg eq '*'? '\S' : $attg) :'';
$aatt=$aatt? '\s+'.($aatt eq '*'? '\S' : $aatt) :'';
my $depth=split '/', $path=~ s/^\/\h*//r;
my @OffNode;
for (@{$_[1]}) {
if ($AllDepth ?
$tag? $nth?
getAllDepth ($tag, $nth, $_->[1], \@OffNode, $_->[0], $depth, $nrev) # offset-node pair return is in @OffNode..
: getAllDepNthRnAtt ($tag, $_->[1], $_->[0], \@OffNode, $depth, $range, $attg)
: getAllDepthAatt ($aatt, $_->[1], $_->[0], \@OffNode, $depth)
:
$nth ?
getNthEAtt ($tag, $nth, $_->[1], \@OffNode, $_->[0], $nrev)
: getAllEAtt ($tag, $_->[1], $_->[0], \@OffNode, $range, $attg, $aatt, $allnode)){ # if true, means
next if \$_ != \${$_[1]}[-1]; # fail to find but if it's not the last in loop, go on iterating
return !@res} # otherwise return bool 1 if overall fails, 0 if any finding
if ($path) {
my $R=getE_Path_Rec ($path, \@OffNode); # ..to always propagate to the next
return $R if \$_ == \${$_[1]}[-1]
} else {
push (@res, @OffNode)} # 2D arr OffNode is flatten into res and its inner arr is preserved
}
return
}
my ($whole, $trPath, @valid, $O, $CP);
if (@ARGV) {
$trPath=shift; $O=shift;
undef local $/;$whole=<>
}else {
print "Element path is of Xpath form e.g:\n\thtml/body/div[1]//div[1]/div[2]\nmeans find in a given HTML or XML file, the second div tag element that is under the first\ndiv element anywhere under the first div element, under any body element,\nunder any html element.\n\nTo put multiply at once, put one after another delimited by ; or |\nPut element path: ";
die "No any Xpath given\n" if chomp($trPath=<>)=~/^\s*$/;
my $xpath=qr{
^ \h* (?> /?/? (([a-z]\w*+) (?: \h*\[ \h*(?> (?:last\(\)-)? $pstv | position\(\)(?!<1)[<>]=? $pstv | @((?>(?2)(?:=(?2))? | \*)) ) \])? | @(?-1) | \*) | \.\.? ) (?://?(?1))*+ \h*$ }x;
for (split /[|;]/,$trPath) {
if (/$xpath/) {
s#\h$##g;
if (/^[^\/]/) {
if(!$CP){
print "\n'$_'\nis relative to base/current path which is now empty, specify one:\n";
print "\n'$CP' is not a valid xpath" while (($CP=<>=~s#\s|/$##gr) !~ $xpath);
$CP=~s#\h$##g
}
s#^\./##;
if (/^\.\./) { $CP=~s#/?[^/]+$##; s#^../## }
$_="$CP/$_"
}
push (@valid, $_);
}else {
print "'$_' is invalid Xpath\nSkip or abort it? (S: skip. any else: abort) ";my $y=<>;
die "Aborting\n" unless $y=~/^s/i
}}
print "\nHTML/XML file name to process: ";
my $file=<>=~s/^\h+//r=~ s/\s+$//r;
$!=1;-e $file or die "\n'$file' not exist\n";
$!=2;open R,"$file" or die "\nCannot open '$file'\n";
undef local $/;$whole=<R>;close R;
$|=1; print "\nChecking HTML document '$file'... "
}
# Validating ML document
die "can't parse due to the ill-form ML (likely unbalanced tag pair)\n" unless $whole=~ /^
(\s* (?:<\?xml\b[^>]*+>\s*)?) (<(!DOCTYPE)[^>]*+>[^<]* $ND \s*$ ) /x;
my @in=[ $1, $2."</$3>" ];
my ($ER, @path, @fpath, @miss, @short);
for (sort{length $a cmp length $b} @valid) {
if ($ER) {
print "\nSkip it to process the next path? (Y/Enter: yes. any else: Abort) ";
<>=~/^(?:\h*y)?$/i or die "Aborting\n"}
@res=();
if ( $ER = getE_Path_Rec ($_, \@in)) {
push(@miss,$_);
print "\nCan't find '$_'"
}else {
push (@path, [$_, [@res]]);
my $cur=$_; # Optimize removal: filter out longer path whose head is the same as shorter one
for (@short) {
goto P if $cur =~ /^\Q$_\E/}
push (@fpath, @res);
P:
push (@short, $_)
}
}
if (@miss){
if (@path){
print "\nKeep processing ones found? (Y/Enter: yes. any else: Abort) ";
<>=~s/^\h+//r =~/^y?$/i or die "Aborting\n";
}else{ print "\n\nNothing was done";exit
}}
unless (@ARGV){
print "\n\nWhich operation will be done :\n- Remove\n- Get\n(R: remove Else key: just get it) ";
$O=<>=~s/^\h+//r=~ s/\s+$//r;
print 'File name to save the result: (hit Enter to standard output) ';
my $of=<>=~s/^\h+//r=~ s/\s+$//r;
open W,">","$of" or die "Cannot open '$of'\n" if $of
}
if($#path) {print "\nProcessing the path:";print "\n$_->[0]" for(@path)}
for ($O){
if (! /^r/i) {
my $o;for (@path) {
$o.="\n$_->[0]:";
$o.="\n-------------\n$_->[1]\n" for @{$_->[1]}
}
fileno W? print W $o:print $o;
last
}
@fpath=sort {length $b->[0] <=> length $a->[0]} @fpath;
$whole=~ s/\A(\Q$_->[0]\E)\Q$_->[1]\E(.*)\Z/$1$2/s for (@fpath);
fileno W? print W $whole:print "\n\nRemoval result:\n$whole"
}
close W;
#$r=qr/ fo(?:o|k|l)\h*bar/ ; $s = 'fo(?:o|k|l)\h*bar' ;cmpthese( 0, {
#re , sub { 'foobar'=~$r },
#str , sub { 'foobar'=~/$r/ },
#})