Am Samstag, 28. Januar 2006 14:12 schrieb Martin Vermeer: > Couldn't it be done like this: > > 1) modify lib/configure.m4 to issue the command > > lyx2lyx -t 221 -Y $PWD/layouts \$\$i >\$\$o
$PWD would not work. We also need to consider the user directory. And I don't like to pass information by relying on the current directory. > 2) modify lyx2lyx to use the layouts directory from -Y to load the > layout file? > > I understand that this should be traightforward. Well, if one actually implements it it is not so straightforward anymore. This patch uses the already existing code to read layout files from layout2layout.py to extract the text class in lyx2lyx itself. It works well for me, but two things should be thought of: 1) lyx2lyx has no hardcoded userdir/sysdir as LyX has. This is only relevant for standalone usage. Should we change that? 2) Currently lyx2lyx refuses to run if it can't find the layout file, even if is not necessary at all. This could be refined to only bail out if the default layout is really needed. Given the fact that 1.3.7 is already out with the broken lyx2lyx I tend now to polish this and put it in 1.4.1, and for 1.4.0 to tell users to do s/^\\begin_layout Standard/\\begin_layout whatever_the_default_layout_is/ on their files if they are broken after converting. Georg
diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/ChangeLog lyx-1.4-cvs/lib/ChangeLog --- lyx-1.4-clean/lib/ChangeLog 2006-01-26 20:22:46.000000000 +0100 +++ lyx-1.4-cvs/lib/ChangeLog 2006-01-28 19:34:09.000000000 +0100 @@ -1,3 +1,7 @@ +2006-01-28 Georg Baum <[EMAIL PROTECTED]> + + * scripts/layout2layout.py: factor out code to lyx2lyx/Layout.py + 2006-01-26 Jürgen Spitzmüller <[EMAIL PROTECTED]> * chkconfig.ltx: add missing tests for bibtopic and jurabib; diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/ChangeLog lyx-1.4-cvs/lib/lyx2lyx/ChangeLog --- lyx-1.4-clean/lib/lyx2lyx/ChangeLog 2005-12-03 18:45:38.000000000 +0100 +++ lyx-1.4-cvs/lib/lyx2lyx/ChangeLog 2006-01-28 19:33:28.000000000 +0100 @@ -1,3 +1,11 @@ +2006-01-28 Georg Baum <[EMAIL PROTECTED]> + + * Layout.py: New, factored out from ../scripts/layout2layout.py + * LyX.py: Use the new layout class to determine the default text class + * lyx_1_1_5.py, lyx_1_1_6.py, lyx_1_2.py, lyx_1_4.py: Replace all + occurences of the "Standard" layout with file.default_layout (bug 2026) + * lyx2lyx: New commandline options --userdir and --sysdir + 2005-12-01 José Matos <[EMAIL PROTECTED]> * LyX.py (choose_io): replace open and make the choice more transparent. diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/Layout.py lyx-1.4-cvs/lib/lyx2lyx/Layout.py --- lyx-1.4-clean/lib/lyx2lyx/Layout.py 2006-01-28 19:18:15.000000000 +0100 +++ lyx-1.4-cvs/lib/lyx2lyx/Layout.py 2006-01-28 19:33:28.000000000 +0100 @@ -0,0 +1,211 @@ +#! /usr/bin/env python +# -*- coding: iso-8859-1 -*- + +# file Layout.py +# This file is part of LyX, the document processor. +# Licence details can be found in the file COPYING. + +# author Georg Baum + +# Full author contact details are available in file CREDITS + + +import os, re, string, sys + + +def trim_eol(line): + " Remove end of line char(s)." + if line[-2:-1] == '\r': + return line[:-2] + elif line[-1:] == '\r' or line[-1:] == '\n': + return line[:-1] + else: + # file with no EOL in last line + return line + + +def file_search(path, name, ext): + # if `name' is an absolute path, we ignore the setting of `path' + # Expand Environmentvariables in 'name' + tmpname = os.path.expandvars(name) + fullname = os.path.join(path, tmpname) + # search first without extension, then with it. + if os.path.isfile(fullname): + return fullname + if ext == "": + return "" + fullname = fullname + '.' + ext + if os.path.isfile(fullname): + return fullname + return "" + + +def lib_file_search(userdir, sysdir, dir, name, ext): + fullname = file_search(os.path.join(userdir, dir), name, ext) + if fullname != "": + return fullname + builddir = os.path.join(os.path.dirname(sys.argv[0])) + builddir = os.path.dirname(sys.argv[0]) + if builddir == '': + builddir = '.' + builddir = os.path.join(builddir, "..") + fullname = file_search(os.path.join(builddir, dir), name, ext) + if fullname != "": + return fullname + return file_search(os.path.join(sysdir, dir), name, ext) + + +class Layout: + """This class carries all the information of a layout file.""" + + + def __init__(self, err): + self.lines = list() + self.default_layout = "" + self.userdir = "" + self.sysdir = "" + self.err = err + + + def warning(self, message): + self.err.write("Warning: " + message + "\n") + + + def error(self, message): + self.warning(message) + self.warning("Quiting.") + sys.exit(1) + + + def read(self, input, read_includes = 0): + " Read input file and strip lineendings." + re_DefaultStyle = re.compile(r'^(\s*)(DefaultStyle)(\s+)(\S+)', re.IGNORECASE) + re_Input = re.compile(r'^(\s*)(Input)(\s+)(\S+)', re.IGNORECASE) + self.lines = list() + while 1: + line = input.readline() + if not line: + break + match = re_DefaultStyle.match(line) + if match: + self.default_layout = match.group(4) + if read_includes: + match = re_Input.match(line) + if match: + layout = Layout(self.err) + layout.read_class(match.group(4), self.userdir, self.sysdir) + self.lines.append(layout.lines) + else: + self.lines.append(trim_eol(line)) + else: + self.lines.append(trim_eol(line)) + + + def read_class(self, classname, userdir, sysdir): + "Search layout file of text class, read it and strip lineendings." + self.userdir = userdir + self.sysdir = sysdir + layoutfilename = lib_file_search(userdir, sysdir, "layouts", classname, "layout") + if layoutfilename == "": + self.error("Could not find layout file for text class '" + classname + "'.") + layoutfile = open(layoutfilename, 'rb') + self.read(layoutfile, 1) + layoutfile.close() + + + def write(self, output): + " Write output file with native lineendings." + for line in self.lines: + output.write(line + os.linesep) + + + def convert(self): + " Convert to new format." + re_Comment = re.compile(r'^(\s*)#') + re_Empty = re.compile(r'^(\s*)$') + re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE) + re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE) + re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE) + re_MaxCounter = re.compile(r'^\s*MaxCounter', re.IGNORECASE) + re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE) + re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE) + re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE) + re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE) + + i = 0 + only_comment = 1 + label = "" + space1 = "" + latextype = "" + style = "" + while i < len(self.lines): + + # Skip comments and empty lines + if re_Comment.match(self.lines[i]) or re_Empty.match(self.lines[i]): + i = i + 1 + continue + + # insert file format if not already there + if (only_comment): + match = re_Format.match(self.lines[i]) + if match: + format = match.group(4) + if format == '2': + # nothing to do + return + error('Cannot convert file format %s' % format) + else: + self.lines.insert(i, "Format 2") + only_comment = 0 + continue + + # Don't get confused by LaTeX code + if re_Preamble.match(self.lines[i]): + i = i + 1 + while i < len(self.lines) and not re_EndPreamble.match(self.lines[i]): + i = i + 1 + continue + + # Delete MaxCounter + if re_MaxCounter.match(self.lines[i]): + del self.lines[i] + continue + + # Replace line + # + # LabelType Counter_EnumI + # + # with two lines + # + # LabelType Counter + # LabelCounter EnumI + # + match = re_LabelType.match(self.lines[i]) + if match: + label = match.group(4) + space1 = match.group(1) + if string.lower(label[:8]) == "counter_": + counter = label[8:] + self.lines[i] = re_LabelType.sub(r'\1\2\3Counter', self.lines[i]) + # use the same indentation + self.lines.insert(i + 1, "%sLabelCounter %s" % (space1, counter)) + + # Add a line "LatexType Bib_Environment" if LabelType is Bibliography + # (or change the existing LatexType) + match = re_LatexType.match(self.lines[i]) + if match: + latextype_line = i + match = re_Style.match(self.lines[i]) + if match: + style = match.group(4) + label = "" + space1 = "" + latextype_line = -1 + if re_End.match(self.lines[i]) and string.lower(label) == "bibliography": + if (latextype_line < 0): + self.lines.insert(i, "%sLatexType Bib_Environment" % space1) + i = i + 1 + else: + self.lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', self.lines[latextype_line]) + + i = i + 1 diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/lyx_1_1_5.py lyx-1.4-cvs/lib/lyx2lyx/lyx_1_1_5.py --- lyx-1.4-clean/lib/lyx2lyx/lyx_1_1_5.py 2005-08-19 10:47:41.000000000 +0200 +++ lyx-1.4-cvs/lib/lyx2lyx/lyx_1_1_5.py 2006-01-28 19:33:28.000000000 +0100 @@ -37,7 +37,7 @@ def replace_protected_separator(file): if layout_m: layout = layout_m.group(1) else: - layout = "Standard" + layout = file.default_layout if layout == "LyX-Code": result = "" @@ -130,7 +130,7 @@ def first_layout(file): while (lines[0] == ""): del lines[0] if lines[0][:7] != "\\layout": - lines[:0] = ["\\layout Standard"] + lines[:0] = ["\\layout %s" % file.default_layout] def remove_space_in_units(file): diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/lyx_1_1_6.py lyx-1.4-cvs/lib/lyx2lyx/lyx_1_1_6.py --- lyx-1.4-clean/lib/lyx2lyx/lyx_1_1_6.py 2005-01-06 13:49:48.000000000 +0100 +++ lyx-1.4-cvs/lib/lyx2lyx/lyx_1_1_6.py 2006-01-28 19:33:28.000000000 +0100 @@ -146,7 +146,7 @@ def update_tabular(file): tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="%s" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],leftline,rightline,cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8])) tmp.append('\\begin_inset Text') tmp.append('') - tmp.append('\\layout Standard') + tmp.append('\\layout %s' % file.default_layout) tmp.append('') if cell_info[m][0] != '2': diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/lyx_1_2.py lyx-1.4-cvs/lib/lyx2lyx/lyx_1_2.py --- lyx-1.4-clean/lib/lyx2lyx/lyx_1_2.py 2005-09-20 20:13:24.000000000 +0200 +++ lyx-1.4-cvs/lib/lyx2lyx/lyx_1_2.py 2006-01-28 19:33:28.000000000 +0100 @@ -179,7 +179,7 @@ def remove_pextra(file): if hfill: start = ["","\hfill",""]+start else: - start = ["\\layout Standard"] + start + start = ["\\layout %s" % file.default_layout] + start j0 = find_token_backwards(lines,"\\layout", i-1) j = get_next_paragraph(lines, i, file.format + 1) @@ -219,7 +219,7 @@ spchar_rexp = re.compile(r"(.*)(\\Specia ert_begin = ["\\begin_inset ERT", "status Collapsed", "", - "\\layout Standard"] + "\\layout %s" % file.default_layout] def remove_oldert(file): @@ -248,8 +248,7 @@ def remove_oldert(file): new = [] new2 = [] if check_token(lines[i], "\\layout LaTeX"): - new = ["\layout Standard", "", ""] - # We have a problem with classes in which Standard is not the default layout! + new = ["\layout %s" % file.default_layout, "", ""] k = i+1 while 1: @@ -349,8 +348,8 @@ def remove_oldertinset(file): i = i+1 -def is_ert_paragraph(lines, i): - if not check_token(lines[i], "\\layout Standard"): +def is_ert_paragraph(lines, i, default_layout): + if not check_token(lines[i], "\\layout %s" % default_layout): return 0 i = find_nonempty_line(lines, i+1) @@ -372,7 +371,7 @@ def combine_ert(file): j = get_paragraph(lines, i, file.format + 1) count = 0 text = [] - while is_ert_paragraph(lines, j): + while is_ert_paragraph(lines, j, file.default_layout): count = count+1 i2 = find_token(lines, "\\layout", j+1) @@ -721,7 +720,7 @@ def change_infoinset(file): note_lines = [txt]+note_lines for line in note_lines: - new = new + ["\layout Standard", ""] + new = new + ["\layout %s" % file.default_layout, ""] tmp = string.split(line, '\\') new = new + [tmp[0]] for x in tmp[1:]: diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/lyx_1_4.py lyx-1.4-cvs/lib/lyx2lyx/lyx_1_4.py --- lyx-1.4-clean/lib/lyx2lyx/lyx_1_4.py 2005-11-21 20:41:19.000000000 +0100 +++ lyx-1.4-cvs/lib/lyx2lyx/lyx_1_4.py 2006-01-28 19:33:28.000000000 +0100 @@ -81,7 +81,7 @@ def revert_spaces(file): file.body[i+1:i+1] = '' if space == "\\space": space = "\\ " - i = insert_ert(file.body, i+1, 'Collapsed', space, file.format - 1) + i = insert_ert(file.body, i+1, 'Collapsed', space, file.format - 1, file.default_layout) ## # \InsetSpace \, -> \InsetSpace \thinspace{} @@ -129,7 +129,7 @@ def revert_eqref(file): break eqref = lyx_support_escape(regexp.sub("", file.body[i])) file.body[i:i+1] = ["\\begin_inset ERT", "status Collapsed", "", - "\\layout Standard", "", "\\backslash ", + "\\layout %s" % file.default_layout, "", "\\backslash ", "eqref" + eqref] i = i + 7 @@ -251,10 +251,10 @@ def convert_comment(file): if i == -1: return - file.body[i:i+1] = ["\\layout Standard","","", + file.body[i:i+1] = ["\\layout %s" % file.default_layout,"","", "\\begin_inset Comment", "collapsed true","", - "\\layout Standard"] + "\\layout %s" % file.default_layout] i = i + 7 while 1: @@ -303,7 +303,7 @@ def convert_comment(file): file.body[i:i] = ["\\end_inset"] i = i + 1 break - file.body[i:i+1] = ["\\layout Standard"] + file.body[i:i+1] = ["\\layout %s" % file.default_layout] i = i + 1 @@ -570,7 +570,11 @@ def convert_breaks(file): i = find_token(file.body, "\\begin_layout", i) if i == -1: return - layout = split(file.body[i])[1] + tokens = split(file.body[i]) + if len(tokens) > 1: + layout = split(file.body[i])[1] + else: + layout = file.default_layout i = i + 1 # Merge all paragraph parameters into a single line @@ -597,7 +601,7 @@ def convert_breaks(file): # We want to avoid new paragraphs if possible becauase we want to # inherit font sizes. nonstandard = 0 - if (layout != "Standard" or find(file.body[i],"\\align") != -1 or + if (layout != file.default_layout or find(file.body[i],"\\align") != -1 or find(file.body[i],"\\labelwidthstring") != -1 or find(file.body[i],"\\noindent") != -1): nonstandard = 1 @@ -640,7 +644,7 @@ def convert_breaks(file): paragraph_above = list() if nonstandard: # We need to create an extra paragraph for nonstandard environments - paragraph_above = ['\\begin_layout Standard', ''] + paragraph_above = ['\\begin_layout %s' % file.default_layout, ''] if pb_top != -1: paragraph_above.extend(['\\newpage ','']) @@ -655,7 +659,7 @@ def convert_breaks(file): # We can't use the vspace inset because it does not know \parskip. paragraph_above.extend(['\\lyxline ', '', '']) insert_ert(paragraph_above, len(paragraph_above) - 1, 'Collapsed', - '\\vspace{-1\\parskip}\n', file.format + 1) + '\\vspace{-1\\parskip}\n', file.format + 1, file.default_layout) paragraph_above.extend(['']) if nonstandard: @@ -692,7 +696,7 @@ def convert_breaks(file): paragraph_below = list() if nonstandard: # We need to create an extra paragraph for nonstandard environments - paragraph_below = ['', '\\begin_layout Standard', ''] + paragraph_below = ['', '\\begin_layout %s' % file.default_layout, ''] else: for a in range(len(font_attributes)): if find_token(file.body, font_attributes[a], i, k) != -1: @@ -993,7 +997,7 @@ def convert_minipage(file): # ------------------------------------------------------------------------------------------- # Convert backslashes and '\n' into valid ERT code, append the converted # text to body[i] and return the (maybe incremented) line index i -def convert_ertbackslash(body, i, ert, format): +def convert_ertbackslash(body, i, ert, format, default_layout): for c in ert: if c == '\\': body[i] = body[i] + '\\backslash ' @@ -1004,7 +1008,7 @@ def convert_ertbackslash(body, i, ert, f body[i+1:i+1] = ['\\newline ', ''] i = i + 2 else: - body[i+1:i+1] = ['\\end_layout', '', '\\begin_layout Standard', ''] + body[i+1:i+1] = ['\\end_layout', '', '\\begin_layout %s' % default_layout, ''] i = i + 4 else: body[i] = body[i] + c @@ -1313,7 +1317,7 @@ def revert_breaks(file): # The others are converted in the next loop runs (if they exist) if insets[0] == "vspace": file.body[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '', - '\\layout Standard', '', '\\backslash '] + '\\layout %s' % file.default_layout, '', '\\backslash '] i = i + 6 if spaceamount[0][-1] == '*': spaceamount[0] = spaceamount[0][:-1] @@ -1345,7 +1349,7 @@ def revert_breaks(file): file.body.insert(i, 'vspace*{') else: file.body.insert(i, 'vspace{') - i = convert_ertbackslash(file.body, i, spaceamount[0], file.format - 1) + i = convert_ertbackslash(file.body, i, spaceamount[0], file.format - 1, file.default_layout) file.body[i] = file.body[i] + '}' i = i + 1 elif insets[0] == "lyxline": @@ -1356,7 +1360,7 @@ def revert_breaks(file): latexsize = '\\normalsize' i = insert_ert(file.body, i, 'Collapsed', '\\lyxline{%s}' % latexsize, - file.format - 1) + file.format - 1, file.default_layout) # We use \providecommand so that we don't get an error if native # lyxlines are used (LyX writes first its own preamble and then # the user specified one) @@ -1368,7 +1372,7 @@ def revert_breaks(file): elif insets[0] == "newpage": file.body[i] = '' i = insert_ert(file.body, i, 'Collapsed', '\\newpage{}', - file.format - 1) + file.format - 1, file.default_layout) # Convert a LyX length into a LaTeX length @@ -1392,9 +1396,9 @@ def convert_len(len, special): # Convert a LyX length into valid ERT code and append it to body[i] # Return the (maybe incremented) line index i -def convert_ertlen(body, i, len, special, format): +def convert_ertlen(body, i, len, special, format, default_layout): # Convert backslashes and insert the converted length into body - return convert_ertbackslash(body, i, convert_len(len, special), format) + return convert_ertbackslash(body, i, convert_len(len, special), format, default_layout) # Return the value of len without the unit in numerical form @@ -1408,15 +1412,15 @@ def len2value(len): # Convert text to ERT and insert it at body[i] # Return the index of the line after the inserted ERT -def insert_ert(body, i, status, text, format): +def insert_ert(body, i, status, text, format, default_layout): body[i:i] = ['\\begin_inset ERT', 'status ' + status, ''] i = i + 3 if format <= 224: - body[i:i] = ['\\layout Standard', ''] + body[i:i] = ['\\layout %s' % default_layout, ''] else: - body[i:i] = ['\\begin_layout Standard', ''] + body[i:i] = ['\\begin_layout %s' % default_layout, ''] i = i + 1 # i points now to the just created empty line - i = convert_ertbackslash(body, i, text, format) + 1 + i = convert_ertbackslash(body, i, text, format, default_layout) + 1 if format > 224: body[i:i] = ['\\end_layout'] i = i + 1 @@ -1557,7 +1561,7 @@ def convert_frameless_box(file): ert = ert + '\\let\\endminipage\\endlyxtolyxminipage%\n' old_i = i - i = insert_ert(file.body, i, 'Collapsed', ert, file.format - 1) + i = insert_ert(file.body, i, 'Collapsed', ert, file.format - 1, file.default_layout) j = j + i - old_i - 1 file.body[i:i] = ['\\begin_inset Minipage', @@ -1572,21 +1576,21 @@ def convert_frameless_box(file): # Restore the original minipage environment since we may have # minipages inside this box. # Start a new paragraph because the following may be nonstandard - file.body[i:i] = ['\\layout Standard', '', ''] + file.body[i:i] = ['\\layout %s' % file.default_layout, '', ''] i = i + 2 j = j + 3 ert = '\\let\\minipage\\lyxtolyxrealminipage%\n' ert = ert + '\\let\\endminipage\\lyxtolyxrealendminipage%' old_i = i - i = insert_ert(file.body, i, 'Collapsed', ert, file.format - 1) + i = insert_ert(file.body, i, 'Collapsed', ert, file.format - 1, file.default_layout) j = j + i - old_i - 1 # Redefine the minipage end before the inset end. # Start a new paragraph because the previous may be nonstandard - file.body[j:j] = ['\\layout Standard', '', ''] + file.body[j:j] = ['\\layout %s' % file.default_layout, '', ''] j = j + 2 ert = '\\let\\endminipage\\endlyxtolyxminipage' - j = insert_ert(file.body, j, 'Collapsed', ert, file.format - 1) + j = insert_ert(file.body, j, 'Collapsed', ert, file.format - 1, file.default_layout) j = j + 1 file.body.insert(j, '') j = j + 1 @@ -1597,7 +1601,7 @@ def convert_frameless_box(file): ert = '}%\n' else: ert = '\\end{lyxtolyxrealminipage}%\n' - j = insert_ert(file.body, j, 'Collapsed', ert, file.format - 1) + j = insert_ert(file.body, j, 'Collapsed', ert, file.format - 1, file.default_layout) # We don't need to restore the original minipage after the inset # end because the scope of the redefinition is the original box. @@ -1796,7 +1800,7 @@ def convert_names(file): "\\begin_inset CharStyle Firstname", "status inlined", "", - "\\begin_layout Standard", + "\\begin_layout %s" % file.default_layout, "", "%s" % firstname, "\end_layout", @@ -1807,7 +1811,7 @@ def convert_names(file): "\\begin_inset CharStyle Surname", "status inlined", "", - "\\begin_layout Standard", + "\\begin_layout %s" % file.default_layout, "", "%s" % surname, "\\end_layout", @@ -2134,7 +2138,7 @@ def convert_ert_paragraphs(file): k = find_token(file.body, "\\begin_layout", k, j) if k == -1: break - file.body[k] = "\\begin_layout Standard" + file.body[k] = "\\begin_layout %s" % file.default_layout k = k + 1 # remove all paragraph parameters and font settings @@ -2151,14 +2155,14 @@ def convert_ert_paragraphs(file): k = i first_pagraph = 1 while 1: - k = find_token(file.body, "\\begin_layout Standard", k, j) + k = find_token(file.body, "\\begin_layout %s" % file.default_layout, k, j) if k == -1: break if first_pagraph: first_pagraph = 0 k = k + 1 continue - file.body[k:k] = ["\\begin_layout Standard", "", + file.body[k:k] = ["\\begin_layout %s" % file.default_layout, "", "\\end_layout", ""] k = k + 5 j = j + 4 @@ -2169,7 +2173,7 @@ def convert_ert_paragraphs(file): k = find_token(file.body, "\\newline", k, j) if k == -1: break - file.body[k:k+1] = ["\\end_layout", "", "\\begin_layout Standard"] + file.body[k:k+1] = ["\\end_layout", "", "\\begin_layout %s" % file.default_layout] k = k + 4 j = j + 3 i = i + 1 @@ -2212,7 +2216,7 @@ def revert_ert_paragraphs(file): l = l + 1 if strip(file.body[l]) and split(file.body[l])[0] == "\\newline": file.body[k:l+1] = ["\\end_layout", "", - "\\begin_layout Standard"] + "\\begin_layout %s" % file.default_layout] j = j - l + k + 2 k = k + 3 else: diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/lyx2lyx lyx-1.4-cvs/lib/lyx2lyx/lyx2lyx --- lyx-1.4-clean/lib/lyx2lyx/lyx2lyx 2005-07-08 21:15:43.000000000 +0200 +++ lyx-1.4-cvs/lib/lyx2lyx/lyx2lyx 2006-01-28 19:33:28.000000000 +0100 @@ -36,18 +36,22 @@ Options: -t, --to version final version (optional) -o, --output name name of the output file or else goes to stdout -n, --try-hard try hard (ignore any convertion errors) + -u, --userdir dir try to set user directory to dir + -s, --sysdir dir try to set system directory to dir -q, --quiet same as --debug=0""" def parse_options(argv): - _options = ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "try-hard", "quiet"] + _options = ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "try-hard", "quiet", "userdir=", "sysdir="] try: - opts, args = getopt.getopt(argv[1:], "d:e:f:hlno:qt:v", _options) + opts, args = getopt.getopt(argv[1:], "d:e:f:hlno:qt:vu:s:", _options) except getopt.error: usage() sys.exit(2) end_format, input, output, error, debug, try_hard = 0, "", "", "", LyX.default_debug_level, 0 + userdir = "" + sysdir = "" for o, a in opts: if o in ("-h", "--help"): usage() @@ -71,15 +75,19 @@ def parse_options(argv): error = a if o in ("-n", "--try-hard"): try_hard = 1 + if o in ("-u", "--userdir"): + userdir = a + if o in ("-s", "--sysdir"): + sysdir = a if args: input = args[0] - return end_format, input, output, error, debug, try_hard + return end_format, input, output, error, debug, try_hard, userdir, sysdir def main(argv): - end_format, input, output, error, debug, try_hard = parse_options(argv) - file = LyX.File(end_format, input, output, error, debug, try_hard) + end_format, input, output, error, debug, try_hard, userdir, sysdir = parse_options(argv) + file = LyX.File(end_format, input, output, error, debug, try_hard, userdir, sysdir) file.convert() file.write() diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/LyX.py lyx-1.4-cvs/lib/lyx2lyx/LyX.py --- lyx-1.4-clean/lib/lyx2lyx/LyX.py 2005-12-03 18:45:38.000000000 +0100 +++ lyx-1.4-cvs/lib/lyx2lyx/LyX.py 2006-01-28 19:33:28.000000000 +0100 @@ -24,6 +24,7 @@ import sys import re import string import time +from Layout import Layout version_lyx2lyx = "1.4.0cvs" default_debug_level = 2 @@ -86,7 +87,7 @@ def trim_eol(line): # class LyX_Base: """This class carries all the information of the LyX file.""" - def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0): + def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0, userdir = "", sysdir = ""): """Arguments: end_format: final format that the file should be converted. (integer) input: the name of the input source, if empty resort to standard input. @@ -103,6 +104,8 @@ class LyX_Base: self.debug = debug self.try_hard = try_hard + self.userdir = userdir + self.sysdir = sysdir if end_format: self.end_format = self.lyxformat(end_format) @@ -111,6 +114,7 @@ class LyX_Base: self.backend = "latex" self.textclass = "article" + self.default_layout = "Standard" self.header = [] self.preamble = [] self.body = [] @@ -178,6 +182,9 @@ class LyX_Base: self.body.append(trim_eol(line)) self.textclass = get_value(self.header, "\\textclass", 0) + layout = Layout(self.err) + layout.read_class(self.textclass, self.userdir, self.sysdir) + self.default_layout = layout.default_layout self.backend = get_backend(self.textclass) self.format = self.read_format() self.language = get_value(self.header, "\\language", 0) @@ -452,8 +459,8 @@ class LyX_Base: class File(LyX_Base): " This class reads existing LyX files." - def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0): - LyX_Base.__init__(self, end_format, input, output, error, debug, try_hard) + def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level, try_hard = 0, userdir = "", sysdir = ""): + LyX_Base.__init__(self, end_format, input, output, error, debug, try_hard, userdir, sysdir) self.read() diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/lyx2lyx/Makefile.am lyx-1.4-cvs/lib/lyx2lyx/Makefile.am --- lyx-1.4-clean/lib/lyx2lyx/Makefile.am 2005-07-15 18:38:59.000000000 +0200 +++ lyx-1.4-cvs/lib/lyx2lyx/Makefile.am 2006-01-28 19:34:35.000000000 +0100 @@ -9,6 +9,7 @@ lyx2lyxdir = $(pkgdatadir)/lyx2lyx dist_lyx2lyx_PYTHON = \ lyx2lyx \ parser_tools.py \ + Layout.py \ LyX.py \ lyx_0_12.py \ lyx_1_0_0.py \ diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/lib/scripts/layout2layout.py lyx-1.4-cvs/lib/scripts/layout2layout.py --- lyx-1.4-clean/lib/scripts/layout2layout.py 2006-01-19 21:48:19.000000000 +0100 +++ lyx-1.4-cvs/lib/scripts/layout2layout.py 2006-01-28 19:33:28.000000000 +0100 @@ -12,8 +12,14 @@ # This script will update a .layout file to format 2 -import os, re, string, sys +import os, sys +srcdir = os.path.dirname(sys.argv[0]) +if srcdir == '': + srcdir = '.' +sys.path.insert(0, os.path.join(srcdir, "..", "lyx2lyx")) + +from Layout import Layout def usage(prog_name): return ("Usage: %s inputfile outputfile\n" % prog_name + @@ -25,126 +31,6 @@ def error(message): sys.exit(1) -def trim_eol(line): - " Remove end of line char(s)." - if line[-2:-1] == '\r': - return line[:-2] - elif line[-1:] == '\r' or line[-1:] == '\n': - return line[:-1] - else: - # file with no EOL in last line - return line - - -def read(input): - " Read input file and strip lineendings." - lines = list() - while 1: - line = input.readline() - if not line: - break - lines.append(trim_eol(line)) - return lines - - -def write(output, lines): - " Write output file with native lineendings." - for line in lines: - output.write(line + os.linesep) - - -def convert(lines): - " Convert to new format." - re_Comment = re.compile(r'^(\s*)#') - re_Empty = re.compile(r'^(\s*)$') - re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE) - re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE) - re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE) - re_MaxCounter = re.compile(r'^\s*MaxCounter', re.IGNORECASE) - re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE) - re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE) - re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE) - re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE) - - i = 0 - only_comment = 1 - label = "" - space1 = "" - latextype_line = -1 - style = "" - while i < len(lines): - - # Skip comments and empty lines - if re_Comment.match(lines[i]) or re_Empty.match(lines[i]): - i = i + 1 - continue - - # insert file format if not already there - if (only_comment): - match = re_Format.match(lines[i]) - if match: - format = match.group(4) - if format == '2': - # nothing to do - return - error('Cannot convert file format %s' % format) - else: - lines.insert(i, "Format 2") - only_comment = 0 - continue - - # Don't get confused by LaTeX code - if re_Preamble.match(lines[i]): - i = i + 1 - while i < len(lines) and not re_EndPreamble.match(lines[i]): - i = i + 1 - continue - - # Delete MaxCounter - if re_MaxCounter.match(lines[i]): - del lines[i] - continue - - # Replace line - # - # LabelType Counter_EnumI - # - # with two lines - # - # LabelType Counter - # LabelCounter EnumI - # - match = re_LabelType.match(lines[i]) - if match: - label = match.group(4) - space1 = match.group(1) - if string.lower(label[:8]) == "counter_": - counter = label[8:] - lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i]) - # use the same indentation - lines.insert(i + 1, "%sLabelCounter %s" % (space1, counter)) - - # Add a line "LatexType Bib_Environment" if LabelType is Bibliography - # (or change the existing LatexType) - match = re_LatexType.match(lines[i]) - if match: - latextype_line = i - match = re_Style.match(lines[i]) - if match: - style = match.group(4) - label = "" - space1 = "" - latextype_line = -1 - if re_End.match(lines[i]) and string.lower(label) == "bibliography": - if (latextype_line < 0): - lines.insert(i, "%sLatexType Bib_Environment" % space1) - i = i + 1 - else: - lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line]) - - i = i + 1 - - def main(argv): # Open files @@ -158,9 +44,10 @@ def main(argv): error(usage(argv[0])) # Do the real work - lines = read(input) - convert(lines) - write(output, lines) + layout = Layout(sys.stderr) + layout.read(input) + layout.convert() + layout.write(output) # Close files if len(argv) == 3: diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/src/buffer.C lyx-1.4-cvs/src/buffer.C --- lyx-1.4-clean/src/buffer.C 2006-01-26 20:23:09.000000000 +0100 +++ lyx-1.4-cvs/src/buffer.C 2006-01-28 19:38:05.000000000 +0100 @@ -72,6 +72,7 @@ #endif #include "support/lyxlib.h" #include "support/os.h" +#include "support/package.h" #include "support/path.h" #include "support/textutils.h" #include "support/convert.h" @@ -110,6 +111,7 @@ using lyx::support::MakeDisplayPath; using lyx::support::MakeLatexName; using lyx::support::OnlyFilename; using lyx::support::OnlyPath; +using lyx::support::package; using lyx::support::Path; using lyx::support::QuoteName; using lyx::support::removeAutosaveFile; @@ -642,6 +644,8 @@ bool Buffer::readFile(LyXLex & lex, stri } ostringstream command; command << "python " << QuoteName(lyx2lyx) + << " -u " << QuoteName(package().user_support()) + << " -s " << QuoteName(package().system_support()) << " -t " << convert<string>(LYX_FORMAT) << " -o " << QuoteName(tmpfile) << ' ' << QuoteName(filename); diff -p -r -U 3 -X excl.tmp lyx-1.4-clean/src/ChangeLog lyx-1.4-cvs/src/ChangeLog --- lyx-1.4-clean/src/ChangeLog 2006-01-28 13:26:01.000000000 +0100 +++ lyx-1.4-cvs/src/ChangeLog 2006-01-28 19:33:47.000000000 +0100 @@ -1,3 +1,7 @@ +2006-01-28 Georg Baum <[EMAIL PROTECTED]> + + * buffer.C (readFile): Pass package directories to lyx2lyx + 2006-01-28 Martin Vermeer <[EMAIL PROTECTED]> * text2.C (setCharFont): take inset's own font settings into account