summaryrefslogtreecommitdiff
path: root/support/chktex/MakeChkTeXRC.pl
blob: 4e7e9208ba95466c8656bade3583ea6db997b2a0 (plain)
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
# Used to convert chktexrc.in (which see) into chktexrc and ChkTeXRC.tex

use strict;
use warnings;

my $chktexrc_output;
if ( $ARGV[0] eq '--chktexrc' ) {
    $chktexrc_output = 1;
} elsif ( $ARGV[0] eq '--latex' ) {
    $chktexrc_output = 0;
} else {
    exit 1;
}


# Filter the lines of the file
open(my $fh, '<:encoding(UTF-8)', $ARGV[1])
    or die "Could not open file '$ARGV[1]' $!";

while ( <$fh> ) {
    if (/^#### END$/) {
        exit 0;
    }

    if ($chktexrc_output) {
        # chktexrc output, so discard all lines over 2
        next if /^#{3,}/;

        s/\@verb\@/`/g;
        s/\@endverb\@/'/g;
        s/\@emph\@/*/g;
        s/\@endemph\@/*/g;
        s/\@ref\@/ /g;
        s/\@endref\@//g;

        s/\@TeX@/TeX/g;
        s/\@LaTeX@/LaTeX/g;
        s/\@ChkTeX@/ChkTeX/g;
        s/\@\\{2}\@//g;         # \\ in LaTeX
        s/\\\@ */ /g;           # \@ in LaTeX
        s/\@bf //g;
        s/\@\&\@//g;
        print;

    } else {

        # LaTeX output, so discard lines except with 1,3
        next if /^#{4,}/;
        next if /^#{2} /;
        # next unless /^#/;
        s/^#+[ ]*//;

        s/\@emph\@/\\emph{/g;
        s/\@endemph\@/}/g;
        s/\@ref\@(.*)\@endref\@/~\\hyperref[rc:$1]{$1}/g;


        s/\@TeX@/\\TeX{}/g;
        s/\@LaTeX@/\\LaTeX{}/g;
        s/\@ChkTeX@/\\chktex{}/g;
        s/\@verb\@/\\verb@/g;
        s/\@endverb\@/@/g;
        s/\@\\{2}\@/\\\\/g;
        s/\@bf/\\bf/g;
        s/\@\&\@/&/g;

        print;
    }
}
# TODO:
# \ChkTeX