Separate preamble from from header. Make its treatment more robust.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10398 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
José Matox 2005-08-18 17:33:26 +00:00
parent a798566541
commit 7bd4678da5
6 changed files with 61 additions and 65 deletions

View File

@ -1,3 +1,14 @@
2005-08-18 José Matos <jamatos@lyx.org>
* LyX.py (read, write): add preamble as data member of LyX_Base,
remove if from the header.
* lyx_0_12.py (header_update):
* lyx_1_1_5.py (remove_space_in_units):
* lyx_1_4.py (add_to_preamble, convert_frameless_box): use the new scheme.
* lyx_1_2.py (change_header): change name to reflect its content.
2005-07-29 José Matos <jamatos@lyx.org>
* lyx_1_4.py:

View File

@ -73,6 +73,14 @@ def get_backend(textclass):
return "latex"
def trim_eol(line):
" Remove end of line char(s)."
if line[-2:-1] == '\r':
return line[:-2]
else:
return line[:-1]
##
# Class
#
@ -111,6 +119,7 @@ class LyX_Base:
self.backend = "latex"
self.textclass = "article"
self.header = []
self.preamble = []
self.body = []
self.status = 0
@ -133,33 +142,39 @@ 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':
line = line[:-1]
line = trim_eol(line)
if check_token(line, '\\begin_preamble'):
preamble = 1
while 1:
line = self.input.readline()
if not line:
self.error("Invalid LyX file.")
line = trim_eol(line)
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 line:
continue
if not preamble:
if not line:
continue
if string.split(line)[0] in ("\\layout", "\\begin_layout", "\\begin_body"):
self.body.append(line)
break
if string.split(line)[0] in ("\\layout", "\\begin_layout", "\\begin_body"):
self.body.append(line)
break
self.header.append(line)
@ -167,11 +182,7 @@ class LyX_Base:
line = self.input.readline()
if not line:
break
# remove '\r' from line's end, if present
if line[-2:-1] == '\r':
self.body.append(line[:-2])
else:
self.body.append(line[:-1])
self.body.append(trim_eol(line))
self.textclass = get_value(self.header, "\\textclass", 0)
self.backend = get_backend(self.textclass)
@ -187,10 +198,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")

View File

@ -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]

View File

@ -140,8 +140,6 @@ 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:
@ -149,10 +147,6 @@ def remove_space_in_units(file):
if i == -1:
break
if i > begin_preamble and i < end_preamble:
i = i + 1
continue
result = unit_rexp.search(lines[i])
if result:
lines[i] = margin + " " + result.group(1) + result.group(2)

View File

@ -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]]]

View File

@ -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)