-
Notifications
You must be signed in to change notification settings - Fork 9
/
lyxcodelinewrapper.pl
executable file
·173 lines (151 loc) · 4.64 KB
/
lyxcodelinewrapper.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/perl
#
# $Id$
#
# LyX codeline wrapper tool
#
# (P) & (C) 2002-2009 by Peter Bieringer <pb@bieringer.de>
#
# Published under the GNU GPL licence
#
# Takes LyX code from stdin and pipes a modified to stdout.
# Wraps length of code lines to a given limit.
# Marks second and next lines with a given character.
#
# Attention: no warranty that the new LyX code is very well!
# Use diff to check proper work
#
# Changes:
# 20020118: Initial try
# 20020119: Improve tool, do not touch code lines including LyX tags
# 20090214/PB: adjustments for LyX format 345
#
# Known bugs:
# * Sure some
#
# Known limitations:
# * Code lines containing LyX tags are currently not supported and skipped
my $lyx_format_supported = "474";
sub print_long_line ($);
my $TAG_CODE = 0;
my $line_carry = "";
my $lines_original = "";
my $line_limit = 79;
#my $linebreakchar = chr(172);
my $linebreakchar = "¬"; # UTF-8 support
#my $debug = 1;
my $debug = 0;
while (<STDIN>) {
my $line = $_;
chomp $line;
#print "$line";
#print "\n";
#
if ($line =~ /^\\lyxformat ([0-9]+)$/) {
if ($1 ne $lyx_format_supported) {
die "Lyx format not supported: $1";
};
} elsif ($line =~ /^\\begin_layout Code$/ && $TAG_CODE != 1) {
print STDERR "INF: Code tag starts\n";
$TAG_CODE = 1;
$line_carry = "";
$lines_original = "";
next;
} elsif ($line =~ /^\\end_layout$/ && $TAG_CODE == 1) {
print STDERR "INF: Code tag ends\n";
if ($lines_original =~ /\\/) {
# Ooops, lines contain a LyX tag, currently not supported, so let it be
#print STDERR "WARN: Lines contain LyX code tag, let it like it is\n: '$lines_original'";
print STDERR "WARN: Lines contain LyX code tag, let it like it is\n";
if ($debug & 0x01) { print "G"; };
print "\\begin_layout Code" . "\n" . $lines_original . "\\end_layout" . "\n";
} elsif ($lines_original =~ /$linebreakchar/) {
# Code line already wrapped
print STDERR "INF: Code line already wrapped, let it like it is\n";
if ($debug & 0x01) { print "F"; };
print "\\begin_layout Code" . "\n" . $lines_original . "\\end_layout" . "\n";
} else {
print_long_line $line_carry;
};
$TAG_CODE = 0;
$line_carry = "";
$lines_original = "";
next;
} elsif ($line =~ /^\\layout/ && $TAG_CODE == 1) {
die "unsupported";
print STDERR "INF: Code tag ends\n";
if ($lines_original =~ /\\/) {
# Ooops, lines contain a LyX tag, currently not supported, so let it be
#print STDERR "WARN: Lines contain LyX code tag, let it like it is\n: '$lines_original'";
print STDERR "WARN: Lines contain LyX code tag, let it like it is\n";
if ($debug & 0x01) { print "A"; };
print "\\begin_layout Code" . "\n" . $lines_original;
} elsif ($lines_original =~ /$linebreakchar/) {
# Code line already wrapped
print STDERR "INF: Code line already wrapped, let it like it is\n";
if ($debug & 0x01) { print "B"; };
print "\\begin_layout Code" . "\n" . $lines_original;
} else {
print_long_line $line_carry;
};
$line_carry = "";
$lines_original = "";
$TAG_CODE = 0;
print $line . "\n";
next;
};
if ($TAG_CODE != 1) {
print $line . "\n";
} else {
$lines_original .= $line . "\n";
if ($line eq "") {
# empty lines are skipped here
next;
};
print STDERR "INF: Found code line: '" . $line . "'\n";
$line_carry .= $line;
};
};
sub print_long_line ($){
my $line = shift;
print STDERR "INF: Print code line: '" . $line . "'\n";
my $l = 0;
my $c = "";
if (length($line) == 0) {
if ($debug & 0x01) { print "C"; };
my $s = "\\begin_layout Code" . "\n" . "\n" . "\\end_layout" . "\n";
print $s;
return;
};
while ($l < length($line)) {
my $t = 0;
print STDERR "INF: Step: $l\n";
if ($l == 0) {
$c = "";
} else {
$c =$linebreakchar;
}
if (length($line) - $l <= $line_limit) {
if ($debug & 0x01) { print "D"; };
my $s = "\\begin_layout Code" . "\n" . $c . substr($line, $l) . "\n" . "\\end_layout" . "\n";
print STDERR "INF: Step end\n";
print $s;
print STDERR $s;
last;
};
for ($t = $line_limit; $t > 0; $t--) {
if (substr($line, $l + $t, 1) eq " ") {
print STDERR "INF: Found <space> at pos: $t\n";
last;
};
};
if ($t == 0) { $t = $line_limit };
print STDERR "INF: Start printing l=$l t=$t\n";
if ($debug & 0x01) { print "E"; };
my $s = "\\begin_layout Code" . "\n" . $c . substr($line, $l, $t) . "\n" . "\\end_layout" . "\n\n";
print $s;
print STDERR $s;
$l = $l + $t;
};
};
print STDERR "\n\nWARNING: check code using utility 'diff' for proper LyX output!\n";