-
Notifications
You must be signed in to change notification settings - Fork 0
/
postrtf.pl
executable file
·151 lines (119 loc) · 4 KB
/
postrtf.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
#
# postrtf.pl - postprocessing of rtf-files generated from AFT
#
# Copyright (C) 2001 Eva Maria Krause. All rights reserved.
# usage
(@ARGV == 0) && do {
print (STDERR "Usage: postrtf filename.rtf \n");
exit 2;
};
$rtffile = $ARGV[0] || die "postrtf: no rtffile specified\n";;
open(FILE, "<$rtffile") || die "postrtf: $rtffile couldn't be opened: $!\n";
while (<FILE>) { $STR .= $_; }
close (FILE);
# constants
$stdind = 500; # standard-indention in twips
$stdcolwidth = 1000; # standard-column-width of table columns
$verbrtf = "\\line\\li"; # rtf-verbatim-code
$listind = "\\par\\pard\\li"; # list-indention
$bul = "\{\\f2\\fs30\\bullet\}"; # bullet in bullet-lists
$celldesign = "\\clbrdrt\\brdrw15\\brdrs". # top border
"\\clbrdrl\\brdrw15\\brdrs". # left border
"\\clbrdrb\\brdrw15\\brdrs". # bottom border
"\\clbrdrr\\brdrw15\\brdrs"; # right border
# for no borderlines, substitute by
# $celldesign = "";
##########################
# handle verbatim-blocks #
##########################
$STR=~s/\\verbatim/\\§verbatim/g;
$STR=~s/\n\\endverbatim/\\§endverbatim\n/g;
# special case: {+, {-, }+, }- in verbatim-block
while ($STR=~m/\\§verbatim([^§]*)[^\\]([\{\}][\+\-])/) {
$STR=~s/\\§verbatim([^§]*)([^\\])([\{\}][\+\-])/\\§verbatim$1$2\\$3/g;
}
while ($STR=~m/\\§verbatim([^§]*)[\+\-]([\{\}])/) {
$STR=~s/\\§verbatim([^§]*)([\+\-])([\{\}])/\\§verbatim$1$2\\$3/g;
}
# replace blanks in verbatim-block by \~
while ($STR=~m/\\§verbatim([^§]*) /) {
$STR=~s/\\§verbatim([^§]*) /\\§verbatim$1\\~/g;
}
# indent each line in verbatim-block
while ($STR=~m/\\§verbatim([^§\n]*)\n/) {
$STR=~s/\\§verbatim([^§\n]*)\n/$1\n$verbrtf$stdind \\§verbatim/g;
}
$STR=~s/\\§verbatim//g;
$STR=~s/\\§endverbatim/\\par\\pard/g;
############################
# handle list-environments #
############################
$STR=~s/\\(num|bul)list/\\§beginlist$1/g;
$STR=~s/\\end(num|bul)list/\\§endlist$1/g;
# assign level to each list, starting with 0
$level=0;
$countlevel=0;
while ($STR=~m/\\§beginlist/ || $STR=~m/\\§endlist/) {
if ($STR=~m/\\bl[^§]*\\§endlist/) { # most inner list
$level--;
$STR=~s/\\bl([^§]*)\\§endlist/\\bl$1\\el$level/;
} else {
$STR=~s/\\§beginlist/\\bl$level$1/;
$level++;
if ($level>$countlevel) {$countlevel=$level;}
}
}
# handle bullet-lists
$STR=~s/\\bl(\d*)bul/\\§bl$1/g;
$STR=~s/\\el(\d*)bul/\\§ebl$1/g;
$STR=~s/\\bulitem/%item%/g;
# new paragraph after the most outer list
$STR=~s/\\§ebl0/\\§ebl0\\par\\pard/g;
for ($ii=$countlevel-1; $ii>=0;$ii--) {
$ind = ($ii+1)*$stdind;
while ($STR=~m/\\§bl$ii([^§%]*)%item%/) {
$STR=~s/\\§bl$ii([^§%]*)%item%/$1$listind$ind $bul\\§bl$ii/g;
}
$STR=~s/\\§bl$ii([^§]*)\\§ebl$ii/$1/g;
}
# handle numbered lists
$STR=~s/\\bl(\d*)num/\\§nl$1/g;
$STR=~s/\\el(\d*)num/\\§enl$1/g;
$STR=~s/\\numitem/%item%/g;
# new paragraph after the most outer list
$STR=~s/\\§enl0/\\§enl0\\par\\pard/g;
for ($ii=$countlevel-1; $ii>=0;$ii--) {
$ind = ($ii+1)*$stdind;
$nr=1;
while ($STR=~m/\\§nl$ii([^§%]*)%item%/) {
$STR=~s/\\§nl$ii([^§%]*)%item%/$1$listind$ind $nr.\\§nl$ii/g;
$nr++;
}
$STR=~s/\\§nl$ii([^§]*)\\§enl$ii/$1/g;
}
# handle tables
$STR=~s/\\cellxx/\\§cellx/g;
while ($STR=~m/\\tabcols/) {
$STR=~s/\\tabcols(\d*)\\endtabcols//;
$count = $1; # number of columns
for ($i = 1; $i <= $count; $i++) {
$colwidth = $i*$stdcolwidth;
$STR=~s/\\§cellx/\n$celldesign\\cellx$colwidth\\§cellx/;
}
$STR=~s/\\§cellx//;
}
# join lines with intermediate space if beginning with letters, numbers or left curly braces
$STR=~s/\n(\w|\{)/ $1/g;
# delete leading spaces
$STR=~s/\n /\n/g;
# eliminate multiple blank lines
$STR=~s/\n+/\n/g;
# eliminate multiple spaces
$STR=~s/ +/ /g;
# delete spaces in front of backslashes
$STR=~s/ \\/\\/g;
# delete spaces after left curly brace
$STR=~s/\{ /\{/g;
open(FILE, ">$rtffile") || die "postrtf: $rtffile couldn't be opened: $!\n";
print FILE $STR;
close (FILE);