Hi all,
the list seems really quite. :-)
For sometime I had the idea of separating the handling of the preamble
from the rest of the header. Finally in Paris I have come to the conclusion
that it really should be in a different part of the document.
Even although we are in a deep freeze I think that this patch should go
in. Why?
We should never mess up with the preamble unless we really want to. Until
now the only option was to test explicitly for it. This is tedious and
error prone. We did it for some functions and not for others. This patch
places all the code in a single place, where it belongs. :-)
Although as a side effect I have improved the comments. The code is now
more robust as well as more readable/understandable...
Lars?
--
José Matos
? l2ly_test.sh
Index: LyX.py
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/LyX.py,v
retrieving revision 1.22
diff -u -p -r1.22 LyX.py
--- LyX.py 11 Aug 2005 22:31:05 -0000 1.22
+++ LyX.py 13 Aug 2005 17:56:01 -0000
@@ -111,6 +111,7 @@ class LyX_Base:
self.backend = "latex"
self.textclass = "article"
self.header = []
+ self.preamble = []
self.body = []
self.status = 0
@@ -133,33 +134,50 @@ class LyX_Base:
def read(self):
"""Reads a file into the self.header and self.body parts, from self.input."""
- preamble = 0
while 1:
line = self.input.readline()
if not line:
self.error("Invalid LyX file.")
- line = line[:-1]
- # remove '\r' from line's end, if present
- if line[-1:] == '\r':
+ # remove end of line char(s)
+ if line[-2:-1] == '\r':
+ line = line[:-2]
+ else:
line = line[:-1]
if check_token(line, '\\begin_preamble'):
- preamble = 1
+ while 1:
+ line = self.input.readline()
+ if not line:
+ self.error("Invalid LyX file.")
+
+ # remove end of line char(s)
+ if line[-2:-1] == '\r':
+ line = line[:-2]
+ else:
+ line = line[:-1]
+
+ if check_token(line, '\\end_preamble'):
+ break
+
+ if string.split(line)[:0] in ("\\layout", "\\begin_layout", "\\begin_body"):
+ self.warning("Malformed LyX file: Missing '\\end_preamble'.")
+ self.warning("Adding it now and hoping for the best.")
+
+ self.preamble.append(line)
+
if check_token(line, '\\end_preamble'):
- preamble = 0
+ continue
- if not preamble:
- line = string.strip(line)
+ line = string.strip(line)
- if not preamble:
- if not line:
- continue
-
- if string.split(line)[0] in ("\\layout", "\\begin_layout", "\\begin_body"):
- self.body.append(line)
- break
+ if not line:
+ continue
+
+ if string.split(line)[0] in ("\\layout", "\\begin_layout", "\\begin_body"):
+ self.body.append(line)
+ break
self.header.append(line)
@@ -167,7 +185,7 @@ class LyX_Base:
line = self.input.readline()
if not line:
break
- # remove '\r' from line's end, if present
+ # remove end of line char(s)
if line[-2:-1] == '\r':
self.body.append(line[:-2])
else:
@@ -187,10 +205,17 @@ class LyX_Base:
self.set_version()
self.set_format()
- for line in self.header:
- self.output.write(line+"\n")
- self.output.write("\n")
- for line in self.body:
+ if self.preamble:
+ i = find_token(self.header, '\\textclass', 0) + 1
+ preamble = ['\\begin_preamble'] + self.preamble + ['\\end_preamble']
+ if i == 0:
+ self.error("Malformed LyX file: Missing '\\textclass'.")
+ else:
+ header = self.header[:i] + preamble + self.header[i:]
+ else:
+ header = self.header
+
+ for line in header + [''] + self.body:
self.output.write(line+"\n")
Index: lyx_0_12.py
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/lyx_0_12.py,v
retrieving revision 1.8
diff -u -p -r1.8 lyx_0_12.py
--- lyx_0_12.py 8 Jul 2005 09:13:51 -0000 1.8
+++ lyx_0_12.py 13 Aug 2005 17:56:01 -0000
@@ -193,13 +193,6 @@ def header_update(file):
i = 0
l = len(lines)
while i < l:
- if check_token(lines[i], '\\begin_preamble'):
- i = find_token(lines, '\\end_preamble', i)
- if i == -1:
- file.error('Unfinished preamble')
- i = i + 1
- continue
-
if lines[i][-1:] == ' ':
lines[i] = lines[i][:-1]
Index: lyx_1_1_5.py
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/lyx_1_1_5.py,v
retrieving revision 1.6
diff -u -p -r1.6 lyx_1_1_5.py
--- lyx_1_1_5.py 8 Jul 2005 08:49:29 -0000 1.6
+++ lyx_1_1_5.py 13 Aug 2005 17:56:01 -0000
@@ -140,18 +140,12 @@ def remove_space_in_units(file):
unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
- begin_preamble = find_token(lines,"\\begin_preamble", 0)
- end_preamble = find_token(lines, "\\end_preamble", 0)
for margin in margins:
i = 0
while 1:
i = find_token(lines, margin, i)
if i == -1:
break
-
- if i > begin_preamble and i < end_preamble:
- i = i + 1
- continue
result = unit_rexp.search(lines[i])
if result:
Index: lyx_1_2.py
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/lyx_1_2.py,v
retrieving revision 1.9
diff -u -p -r1.9 lyx_1_2.py
--- lyx_1_2.py 9 Jun 2005 09:58:02 -0000 1.9
+++ lyx_1_2.py 13 Aug 2005 17:56:01 -0000
@@ -730,7 +730,7 @@ def change_infoinset(file):
i = i+5
-def change_preamble(file):
+def change_header(file):
lines = file.header
i = find_token(lines, "\\use_amsmath", 0)
if i == -1:
@@ -739,7 +739,7 @@ def change_preamble(file):
"\use_numerical_citations 0"]
-convert = [[220, [change_preamble, change_listof, fix_oldfloatinset,
+convert = [[220, [change_header, change_listof, fix_oldfloatinset,
update_tabular, update_longtables, remove_pextra,
remove_oldfloat, remove_figinset, remove_oldertinset,
remove_oldert, combine_ert, change_infoinset]]]
Index: lyx_1_4.py
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/lyx2lyx/lyx_1_4.py,v
retrieving revision 1.47
diff -u -p -r1.47 lyx_1_4.py
--- lyx_1_4.py 11 Aug 2005 22:31:05 -0000 1.47
+++ lyx_1_4.py 13 Aug 2005 17:56:01 -0000
@@ -1045,21 +1045,10 @@ def insert_ert(body, i, status, text):
# Add text to the preamble if it is not already there.
# Only the first line is checked!
def add_to_preamble(file, text):
- i = find_token(file.header, '\\begin_preamble', 0)
- if i == -1:
- file.header.extend(['\\begin_preamble'] + text + ['\\end_preamble'])
+ if find_token(file.preamble, text[0]) != -1:
return
- j = find_token(file.header, '\\end_preamble', i)
- if j == -1:
- file.warning("Malformed LyX file: Missing '\\end_preamble'.")
- file.warning("Adding it now and hoping for the best.")
- file.header.append('\\end_preamble')
- j = len(file.header)
-
- if find_token(file.header, text[0], i, j) != -1:
- return
- file.header[j:j] = text
+ file.preamble.extend(text)
def convert_frameless_box(file):
@@ -1874,20 +1863,11 @@ def remove_paperpackage(file):
paperpackage = split(file.header[i])[1]
if paperpackage in ("a4", "a4wide", "widemarginsa4"):
- j = find_token(file.header, '\\begin_preamble', 0)
conv = {"a4":"\\usepackage{a4}","a4wide": "\\usepackage{a4wide}",
"widemarginsa4": "\\usepackage[widemargins]{a4}"}
- if j == -1:
- # Add preamble
- j = len(file.header) - 2
- file.header[j:j]=["\\begin_preamble",
- conv[paperpackage],"\\end_preamble"]
- i = i + 3
- else:
- file.header[j+1:j+1] = [conv[paperpackage]]
- i = i + 1
+ # for compatibility we ensure it is the first entry in preamble
+ file.preamble[0:0] = conv[paperpackage]
- print i, file.header[i]
del file.header[i]
i = find_token(file.header, '\\papersize', 0)