/
autobuild_brief_html_to_text.pl
executable file
·65 lines (58 loc) · 1.28 KB
/
autobuild_brief_html_to_text.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
#!/usr/bin/perl
use strict; use warnings;
use HTML::Parser;
use Term::ANSIColor;
use HTML::Parser 3.00 ();
my %inside;
my $needs_groupend = 0;
my $gh_actions = ($ENV{GITHUB_ACTIONS} // "") eq "true";
sub tag {
my($tag, $num) = @_;
$inside{$tag} += $num;
if ($tag =~ /h[1-3]/) {
if ($num > 0 && $needs_groupend) {
$needs_groupend = 0;
print "::endgroup::\n";
}
print "\n"; # not for all tags
}
if ($tag eq "br") {
print "\n"; # not for all tags
}
if ($tag eq "body" && $num < 0) {
if ($num < 0 && $needs_groupend) {
$needs_groupend = 0;
print "::endgroup::\n";
}
print "\n"; # not for all tags
}
}
sub text {
return if $inside{script} || $inside{style};
my $esc = 1;
my $line = $_[0];
$line =~ s/\R//g;
if ($inside{h2}) {
print color 'yellow';
}
elsif ($inside{h3}) {
if ($gh_actions && !$needs_groupend) {
$needs_groupend = 1;
print "::group::";
}
print color 'red';
}
else {
$esc = 0;
}
print $line;
print color 'reset' if $esc;
}
HTML::Parser->new(api_version => 3,
handlers => [
start => [\&tag, "tagname, '+1'"],
end => [\&tag, "tagname, '-1'"],
text => [\&text, "dtext"],
],
marked_sections => 1,
)->parse_file(shift) || die "Can't open file: $!\n";;