-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize-html.pl
More file actions
38 lines (32 loc) · 957 Bytes
/
Copy pathnormalize-html.pl
File metadata and controls
38 lines (32 loc) · 957 Bytes
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
#!/usr/bin/perl
use strict;
use warnings;
# Normalize HTML output from groff to fix extra newlines within <code> tags
# Usage: ./normalize-html.pl input.html > output.html
my $in_code_block = 0;
my $code_block = "";
my @output_lines = ();
while (my $line = <>) {
chomp $line;
if ($line =~ /<code class="language-[^"]*">/) {
$in_code_block = 1;
$code_block = "$line\n";
next;
}
elsif ($line =~ /<\/code>/) {
$in_code_block = 0;
$code_block .= "$line\n";
# Normalize newlines: collapse three or more consecutive newlines into two
$code_block =~ s/\n{3,}/\n\n/g;
# Remove leading/trailing newlines within the code block, but ensure one trailing newline
$code_block =~ s/^\n+//;
$code_block =~ s/\n+$/\n/;
print $code_block;
next;
}
if ($in_code_block) {
$code_block .= "$line\n";
} else {
print "$line\n";
}
}