The attached patch fixes the following:

- The note inset should only write the label once into the lyx file. This is 
what it expects for reading the file. I believe that this is only a bug fix 
and no file format change, because the current format can be read, only the 
status tag is swallowed.

The rest is lyx2lyx:

- convert_minipage() in lyxconvert_228.py has to convert the status tag now 
too.

- The status tag must be converted between 227 <-> 228 for ERT too, because 
it changed from "status Open" to "status open" etc.

- Back conversion of vspace and frameless box insets from 225 to 224. ERT is 
generated for insets with parameters that have no equivalent in the old 
format. The box conversion is probably extendable to other boxes with 
little effort, but I have no interest in this, because I need this stuff 
only for tex2lyx -> lyx 1.3 conversion, and this works now again.

This was a "python learning by manual reading and copying and modifying 
existing code" project. Although everything seems to work as expected, I am 
not sure wether I fully understood everything.
José and Angus, you seem to be familiar with this stuff, can you please have 
a look and comment wether this is something acceptable?


Georg
Index: development/FORMAT
===================================================================
RCS file: /cvs/lyx/lyx-devel/development/FORMAT,v
retrieving revision 1.21
diff -u -r1.21 FORMAT
--- development/FORMAT	2003/12/29 15:49:48	1.21
+++ development/FORMAT	2003/12/30 14:34:45
@@ -11,9 +11,13 @@
 
 	* format incremented to 228.
 	* Change the output of all insets derived from InsetCollapsable
-	except for InsetERT (which has this output already), changing lines
+	except for InsetERT (which has a similar output already), changing lines
 	"collapsed true" -> "status collapsed"
 	"collapsed false" -> "status open".
+	* Change the output of InsetERT, changing lines
+	"status Collapsed" -> "status collapsed"
+	"status Open" -> "status open".
+	"status Inlined" -> "status inlined".
 
 2003-12-10  Angus Leeming  <[EMAIL PROTECTED]>
 
Index: lib/lyx2lyx/ChangeLog
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/ChangeLog,v
retrieving revision 1.17
diff -u -r1.17 ChangeLog
--- lib/lyx2lyx/ChangeLog	2003/12/29 15:49:49	1.17
+++ lib/lyx2lyx/ChangeLog	2003/12/30 14:34:57
@@ -1,3 +1,12 @@
+2003-12-29  Georg Baum  <[EMAIL PROTECTED]>
+
+	* lyxrevert_228.py: convert ERT status, too
+	* lyxconvert_227.py: ditto, also stop at \\begin_alyout and not
+	\\layout in convert_collapsable()
+	* lyxconvert_228.py (convert_minipage): Add status tag conversion
+	* lyxrevert_225.py: Convert vspace and frameless box insets
+	* lyxrevert_22[3-8].py: fix the 'é' in José's name
+
 2003-12-29  Jürgen Spitzmüller  <[EMAIL PROTECTED]>
 
 	* lyx2lyx: up the format to 229.
Index: lib/lyx2lyx/lyxconvert_227.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxconvert_227.py,v
retrieving revision 1.6
diff -u -r1.6 lyxconvert_227.py
--- lib/lyx2lyx/lyxconvert_227.py	2003/12/29 15:49:49	1.6
+++ lib/lyx2lyx/lyxconvert_227.py	2003/12/30 14:34:58
@@ -16,7 +16,7 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 import sys
-from parser_tools import find_tokens
+from parser_tools import find_token, find_tokens
 
 def convert_collapsable(lines):
     i = 0
@@ -34,8 +34,8 @@
             break
 
         # Seach for a line starting 'collapsed'
-        # If, however, we find a line starting '\layout' (_always_ present)
-        # then break with a warning message
+        # If, however, we find a line starting '\begin_layout'
+        # (_always_ present) then break with a warning message
         i = i + 1
         while 1:
             if (lines[i] == "collapsed false"):
@@ -44,16 +44,44 @@
             elif (lines[i] == "collapsed true"):
                 lines[i] = "status collapsed"
                 break
-            elif (lines[i][:7] == "\\layout"):
+            elif (lines[i][:13] == "\\begin_layout"):
                 sys.stderr.write("Malformed lyx file\n")
                 break
             i = i + 1
 
         i = i + 1
 
+def convert_ert(lines):
+    i = 0
+    while 1:
+        i = find_token(lines, "\\begin_inset ERT", i)
+        if i == -1:
+            break
+
+        # Seach for a line starting 'status'
+        # If, however, we find a line starting '\begin_layout'
+        # (_always_ present) then break with a warning message
+        i = i + 1
+        while 1:
+            if (lines[i] == "status Open"):
+                lines[i] = "status open"
+                break
+            elif (lines[i] == "status Collapsed"):
+                lines[i] = "status collapsed"
+                break
+            elif (lines[i] == "status Inlined"):
+                lines[i] = "status inlined"
+                break
+            elif (lines[i][:13] == "\\begin_layout"):
+                sys.stderr.write("Malformed lyx file\n")
+                break
+            i = i + 1
+
+        i = i + 1
 
 def convert(header, body):
     convert_collapsable(body)
+    convert_ert(body)
 
 if __name__ == "__main__":
     pass
Index: lib/lyx2lyx/lyxconvert_228.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxconvert_228.py,v
retrieving revision 1.1
diff -u -r1.1 lyxconvert_228.py
--- lib/lyx2lyx/lyxconvert_228.py	2003/12/29 15:49:49	1.1
+++ lib/lyx2lyx/lyxconvert_228.py	2003/12/30 14:34:58
@@ -73,6 +73,15 @@
         else:
             width = ' "0"'
 
+        if lines[i][:9] == "collapsed":
+            if lines[i][9:] == "true":
+		status = "collapsed"
+            else:
+		status = "open"
+            del lines[i]
+        else:
+	    status = "collapsed"
+
         lines.insert(i, 'use_parbox 0')
         i = i + 1
         lines.insert(i, 'width' + width)
@@ -82,6 +91,8 @@
         lines.insert(i, 'height' + height)
         i = i + 1
         lines.insert(i, 'height_special "totalheight"')
+        i = i + 1
+        lines.insert(i, 'status ' + status)
         i = i + 1
 
 def convert(header, body):
Index: lib/lyx2lyx/lyxrevert_223.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxrevert_223.py,v
retrieving revision 1.2
diff -u -r1.2 lyxrevert_223.py
--- lib/lyx2lyx/lyxrevert_223.py	2003/10/06 14:56:23	1.2
+++ lib/lyx2lyx/lyxrevert_223.py	2003/12/30 14:34:58
@@ -1,5 +1,5 @@
 # This file is part of lyx2lyx
-# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
Index: lib/lyx2lyx/lyxrevert_224.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxrevert_224.py,v
retrieving revision 1.1
diff -u -r1.1 lyxrevert_224.py
--- lib/lyx2lyx/lyxrevert_224.py	2003/10/03 17:11:22	1.1
+++ lib/lyx2lyx/lyxrevert_224.py	2003/12/30 14:34:59
@@ -1,5 +1,5 @@
 # This file is part of lyx2lyx
-# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
Index: lib/lyx2lyx/lyxrevert_225.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxrevert_225.py,v
retrieving revision 1.1
diff -u -r1.1 lyxrevert_225.py
--- lib/lyx2lyx/lyxrevert_225.py	2003/10/03 17:11:22	1.1
+++ lib/lyx2lyx/lyxrevert_225.py	2003/12/30 14:35:00
@@ -1,5 +1,6 @@
 # This file is part of lyx2lyx
-# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 Georg Baum <[EMAIL PROTECTED]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -16,8 +17,10 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 import re
-from parser_tools import find_token, find_end_of_inset
-from string import replace
+from parser_tools import find_token, find_end_of_inset, get_next_paragraph, \
+                         get_paragraph, get_value, del_token, is_nonempty_line
+from string import find, replace, split
+import sys
 
 def rm_end_layout(lines):
     i = 0
@@ -65,11 +68,235 @@
         return
     lines[i] = "\\the_end"
 
+# Convert backslashes into valid ERT code, append the converted text to
+# lines[i] and return the (maybe incremented) line index i
+def convert_ertbackslash(lines, i, ert):
+    for c in ert:
+	if c == '\\':
+	    lines[i] += '\\backslash '
+	    lines.insert(i, '')
+	    i += 1
+	else:
+	    lines[i] += c
+    return i
+
+def convert_vspace(header, lines):
+
+    # Get default spaceamount
+    i = find_token(header, '\\defskip', 0)
+    if i == -1:
+	defskipamount = 'medskip'
+    else:
+	defskipamount = split(header[i])[1]
+
+    # Convert the insets
+    i = 0
+    while 1:
+        i = find_token(lines, '\\begin_inset VSpace', i)
+        if i == -1:
+            return
+	spaceamount = split(lines[i])[2]
+
+	# Are we at the beginning or end of a paragraph?
+	paragraph_start = 1
+	start = get_paragraph(lines, i) + 1
+	for k in range(start, i):
+	    if is_nonempty_line(lines[k]):
+		paragraph_start = 0
+		break
+	paragraph_end = 1
+	j = find_end_of_inset(lines, i)
+	if j == -1:
+	    sys.stderr.write("Malformed lyx file: Missing '\\end_inset'\n")
+	    i += 1
+	    continue
+	end = get_next_paragraph(lines, i)
+	for k in range(j + 1, end):
+	    if is_nonempty_line(lines[k]):
+		paragraph_end = 0
+		break
+
+	# Convert to paragraph formatting if we are at the beginning or end
+	# of a paragraph and the resulting paragraph would not be empty
+	if ((paragraph_start and not paragraph_end) or
+	    (paragraph_end   and not paragraph_start)):
+	    # The order is important: del and insert invalidate some indices
+	    del lines[j]
+	    del lines[i]
+	    if paragraph_start:
+		lines.insert(start, '\\added_space_top ' + spaceamount + ' ')
+	    else:
+		lines.insert(start, '\\added_space_bottom ' + spaceamount + ' ')
+	    continue
+
+	# Convert to ERT
+	lines[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
+	                '\\layout Standard', '', '\\backslash ']
+	i += 6
+	if spaceamount[-1] == '*':
+	    spaceamount = spaceamount[:-1]
+	    keep = 1
+	else:
+	    keep = 0
+
+	# Replace defskip by the actual value
+	if spaceamount == 'defskip':
+	    spaceamount = defskipamount
+
+	# LaTeX does not know \\smallskip* etc
+	if keep:
+	    if spaceamount == 'smallskip':
+		spaceamount = '\\smallskipamount'
+	    elif spaceamount == 'medskip':
+		spaceamount = '\\medskipamount'
+	    elif spaceamount == 'bigskip':
+		spaceamount = '\\bigskipamount'
+	    elif spaceamount == 'vfill':
+		spaceamount = '\\fill'
+
+	# Finally output the LaTeX code
+	if (spaceamount == 'smallskip' or spaceamount == 'medskip' or
+	    spaceamount == 'bigskip'   or spaceamount == 'vfill'):
+	    lines.insert(i, spaceamount)
+	else :
+	    if keep:
+		lines.insert(i, 'vspace*{')
+	    else:
+		lines.insert(i, 'vspace{')
+	    i = convert_ertbackslash(lines, i, spaceamount)
+            lines[i] += '}'
+        i += 1
+
+# Convert a LyX length into valid ERT code and append it to lines[i]
+# Return the (maybe incremented) line index i
+def convert_ertlen(lines, i, len, special):
+    units = {"text%":"\\textwidth", "col%":"\\columnwidth",
+             "page%":"\\pagewidth", "line%":"\\linewidth",
+             "theight%":"\\textheight", "pheight%":"\\pageheight"}
+
+    # Convert special lengths
+    if special != 'none':
+	len = '%f\\' % len2value(len) + special
+
+    # Convert LyX units to LaTeX units
+    for unit in units.keys():
+	if find(len, unit) != -1:
+	    len = '%f' % (len2value(len) / 100) + units[unit]
+	    break
+
+    # Convert backslashes and insert the converted length into lines
+    return convert_ertbackslash(lines, i, len)
+
+# Return the value of len without the unit in numerical form
+def len2value(len):
+    result = re.search('([+-]?[0-9.]+)', len)
+    if result:
+	return float(result.group(1))
+    # No number means 1.0
+    return 1.0
+
+def convert_frameless_box(lines):
+    pos = ['t', 'c', 'b']
+    inner_pos = ['c', 't', 'b', 's']
+    i = 0
+    while 1:
+        i = find_token(lines, '\\begin_inset Frameless', i)
+        if i == -1:
+            return
+	j = find_end_of_inset(lines, i)
+	if j == -1:
+	    sys.stderr.write("Malformed lyx file: Missing '\\end_inset'\n")
+	    i += 1
+	    continue
+	del lines[i]
+
+	# Gather parameters
+	params = {'position':'0', 'hor_pos':'c', 'has_inner_box':'1',
+                  'inner_pos':'1', 'use_parbox':'0', 'width':'100col%',
+	          'special':'none', 'height':'1in',
+	          'height_special':'totalheight', 'collapsed':'false'}
+	for key in params.keys():
+	    value = replace(get_value(lines, key, i, j), '"', '')
+	    if value != "":
+		if key == 'position':
+		    # convert new to old position: 'position "t"' -> 0
+		    value = find_token(pos, value, 0)
+		    if value != -1:
+			params[key] = value
+		elif key == 'inner_pos':
+		    # convert inner position
+		    value = find_token(inner_pos, value, 0)
+		    if value != -1:
+			params[key] = value
+		else:
+		    params[key] = value
+		j = del_token(lines, key, i, j)
+	i += 1
+
+	# Convert to minipage or ERT?
+	# Note that the inner_position and height parameters of a minipage
+	# inset are ignored and not accessible for the user, although they
+	# are present in the file format and correctly read in and written.
+	# Therefore we convert to ERT if they do not have their LaTeX
+	# defaults. These are:
+	# - the value of "position" for "inner_pos"
+	# - "\totalheight"          for "height"
+	if (params['use_parbox'] != '0' or
+	    params['has_inner_box'] != '1' or
+	    params['special'] != 'none' or
+	    inner_pos[params['inner_pos']] != pos[params['position']] or
+	    params['height_special'] != 'totalheight' or
+	    len2value(params['height']) != 1.0):
+
+	    # Convert to ERT
+	    if params['collapsed'] == 'true':
+		params['collapsed'] = 'Collapsed'
+	    else:
+		params['collapsed'] = 'Open'
+	    lines[i : i] = ['\\begin_inset ERT', 'status ' + params['collapsed'],
+	                    '', '\\layout Standard', '', '\\backslash ']
+	    i += 6
+	    if params['use_parbox'] == '1':
+		lines.insert(i, 'parbox')
+	    else:
+		lines.insert(i, 'begin{minipage}')
+	    lines[i] += '[' + pos[params['position']] + ']['
+	    i = convert_ertlen(lines, i, params['height'], params['height_special'])
+	    lines[i] += '][' + inner_pos[params['inner_pos']] + ']{'
+	    i = convert_ertlen(lines, i, params['width'], params['special'])
+	    lines[i] += '}{'
+	    i += 1
+	    lines[i:i] = ['', '\\end_inset ']
+	    i += 2
+	    j = find_end_of_inset(lines, i)
+	    if j == -1:
+		sys.stderr.write("Malformed lyx file: Missing '\\end_inset'\n")
+		break
+	    lines[j-1:j-1] += ['\\begin_inset ERT', 'status ' + params['collapsed'],
+	                       '', '\\layout Standard', '']
+	    j += 4
+	    if params['use_parbox'] == '1':
+		lines.insert(j, '}')
+	    else:
+		lines[j:j] = ['\\backslash ', 'end{minipage}']
+	else:
+
+	    # Convert to minipage
+	    lines[i:i] = ['\\begin_inset Minipage',
+	                  'position %d' % params['position'],
+			  'inner_position %d' % params['inner_pos'],
+			  'height "' + params['height'] + '"',
+	                  'width "' + params['width'] + '"',
+	                  'collapsed ' + params['collapsed']]
+	    i += 6
+
 def convert(header, body):
     rm_end_layout(body)
     begin_layout2layout(body)
     end_document(body)
     valignment_middle(body)
+    convert_vspace(header, body)
+    convert_frameless_box(body)
 
 if __name__ == "__main__":
     pass
Index: lib/lyx2lyx/lyxrevert_226.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxrevert_226.py,v
retrieving revision 1.1
diff -u -r1.1 lyxrevert_226.py
--- lib/lyx2lyx/lyxrevert_226.py	2003/12/10 17:28:11	1.1
+++ lib/lyx2lyx/lyxrevert_226.py	2003/12/30 14:35:00
@@ -1,5 +1,5 @@
 # This file is part of lyx2lyx
-# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
Index: lib/lyx2lyx/lyxrevert_227.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxrevert_227.py,v
retrieving revision 1.2
diff -u -r1.2 lyxrevert_227.py
--- lib/lyx2lyx/lyxrevert_227.py	2003/12/11 11:16:19	1.2
+++ lib/lyx2lyx/lyxrevert_227.py	2003/12/30 14:35:00
@@ -1,5 +1,5 @@
 # This file is part of lyx2lyx
-# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
Index: lib/lyx2lyx/lyxrevert_228.py
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/lyx2lyx/lyxrevert_228.py,v
retrieving revision 1.4
diff -u -r1.4 lyxrevert_228.py
--- lib/lyx2lyx/lyxrevert_228.py	2003/12/19 21:38:07	1.4
+++ lib/lyx2lyx/lyxrevert_228.py	2003/12/30 14:35:01
@@ -1,5 +1,5 @@
 # This file is part of lyx2lyx
-# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
+# Copyright (C) 2003 José Matos <[EMAIL PROTECTED]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -16,7 +16,7 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 import sys
-from parser_tools import find_tokens
+from parser_tools import find_token, find_tokens
 
 def convert_collapsable(lines):
     i = 0
@@ -33,14 +33,13 @@
         if i == -1:
             break
 
-        # Seach for a line starting 'collapsed'
-        # If, however, we find a line starting '\layout' (_always_ present)
-        # then break with a warning message
+        # Seach for a line starting 'status'
+        # If, however, we find a line starting '\begin_layout'
+        # (_always_ present) then break with a warning message
         i = i + 1
         while 1:
             if (lines[i] == "status open"):
                 lines[i] = "collapsed false"
-                lines[i] = "collapsed false"
                 break
             elif (lines[i] == "status collapsed" or
                   lines[i] == "status inlined"):
@@ -53,8 +52,37 @@
 
         i = i + 1
 
+def convert_ert(lines):
+    i = 0
+    while 1:
+        i = find_token(lines, "\\begin_inset ERT", i)
+        if i == -1:
+            break
+
+        # Seach for a line starting 'status'
+        # If, however, we find a line starting '\begin_layout'
+        # (_always_ present) then break with a warning message
+        i = i + 1
+        while 1:
+            if (lines[i] == "status open"):
+                lines[i] = "status Open"
+                break
+            elif (lines[i] == "status collapsed"):
+                lines[i] = "status Collapsed"
+                break
+            elif (lines[i] == "status inlined"):
+                lines[i] = "status Inlined"
+                break
+            elif (lines[i][:13] == "\\begin_layout"):
+                sys.stderr.write("Malformed lyx file\n")
+                break
+            i = i + 1
+
+        i = i + 1
+
 def convert(header, body):
     convert_collapsable(body)
+    convert_ert(body)
 
 if __name__ == "__main__":
     pass
Index: src/insets/ChangeLog
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.953
diff -u -r1.953 ChangeLog
--- src/insets/ChangeLog	2003/12/29 13:55:44	1.953
+++ src/insets/ChangeLog	2003/12/30 14:37:35
@@ -1,3 +1,6 @@
+2003-12-30  Georg Baum  <[EMAIL PROTECTED]>
+
+	* insetnote.C (InsetNoteParams::write): write label only once
 
 2003-12-22  Michael Schmitt  <[EMAIL PROTECTED]>
 
Index: src/insets/insetnote.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetnote.C,v
retrieving revision 1.69
diff -u -r1.69 insetnote.C
--- src/insets/insetnote.C	2003/12/11 15:23:15	1.69
+++ src/insets/insetnote.C	2003/12/30 14:37:36
@@ -82,7 +82,6 @@
 {
 	string const label = notetranslator().find(type);
 	os << "Note " << label << "\n";
-	os << label << "\n";
 }
 
 

Reply via email to