-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess-code.pl
More file actions
executable file
·60 lines (49 loc) · 1.44 KB
/
Copy pathpreprocess-code.pl
File metadata and controls
executable file
·60 lines (49 loc) · 1.44 KB
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
#!/usr/bin/perl
use strict;
use warnings;
# Preprocess .ms files to handle code snippets for groff
# Usage: ./preprocess-code.pl input.ms > output.ms
if (@ARGV != 1) {
die "Usage: $0 input.ms\n";
}
my $input_file = $ARGV[0];
if (!-f $input_file) {
die "Error: Input file $input_file not found\n";
}
open(my $fh, '<', $input_file) or die "Cannot open $input_file: $!\n";
my $in_code_block = 0;
my $language = "";
my @code_lines;
while (my $line = <$fh>) {
chomp($line);
if ($line =~ /^\.CODE(?:\s+(\S+))?$/) {
$in_code_block = 1;
$language = $1 // "none";
@code_lines = ();
print ".HTML <pre class=\"language-$language line-numbers\"><code class=\"language-$language\">\n";
next;
}
elsif ($line =~ /^\.ENDCODE$/) {
$in_code_block = 0;
$language = "";
# Emit each line individually via .HTML to avoid groff swallowing
# embedded newlines in a single multi-line .HTML argument.
for my $code_line (@code_lines) {
# Escape HTML entities: & first, then < and >
$code_line =~ s/&/&/g;
$code_line =~ s/</</g;
$code_line =~ s/>/>/g;
print ".HTML $code_line\n";
}
print ".HTML </code></pre>\n";
print ".PP\n";
@code_lines = ();
next;
}
if ($in_code_block) {
push @code_lines, $line;
} else {
print "$line\n";
}
}
close($fh);