]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_4.py
Collapsable -> Collapsible (part 1)
[lyx.git] / lib / lyx2lyx / lyx_1_4.py
index 06c6a23e1f3112c9155390acd51a8efa75fa20b3..9c7c7a594e5e29b2a4cc4cbcb390f7ac98e55bf1 100644 (file)
@@ -16,7 +16,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 """ Convert files to the file format generated by lyx 1.4"""
 
@@ -24,7 +24,7 @@ import re
 from os import access, F_OK
 import os.path
 from parser_tools import check_token, find_token, \
-                         get_value, del_token, is_nonempty_line, \
+                         get_value, is_nonempty_line, \
                          find_tokens, find_end_of, find_beginning_of, find_token_exact, find_tokens_exact, \
                          find_re, find_tokens_backwards
 from sys import stdin
@@ -84,13 +84,28 @@ def find_end_of_inset(lines, i):
     "Finds the matching \end_inset"
     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
 
+def del_token(lines, token, start, end):
+    """ del_token(lines, token, start, end) -> int
+
+    Find the lower line in lines where token is the first element and
+    delete that line.
+
+    Returns the number of lines remaining."""
+
+    k = find_token_exact(lines, token, start, end)
+    if k == -1:
+        return end
+    else:
+        del lines[k]
+        return end - 1
+
 # End of helper functions
 ####################################################################
 
 def remove_color_default(document):
     " Remove \color default"
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\color default", i)
         if i == -1:
             return
@@ -164,7 +179,7 @@ def revert_spaces(document):
     " \InsetSpace ~ -> \SpecialChar ~"
     regexp = re.compile(r'(.*)(\\InsetSpace\s+)(\S+)')
     i = 0
-    while 1:
+    while True:
         i = find_re(document.body, regexp, i)
         if i == -1:
             break
@@ -221,7 +236,7 @@ def revert_eqref(document):
     "\\begin_inset LatexCommand \\eqref -> ERT"
     regexp = re.compile(r'^\\begin_inset\s+LatexCommand\s+\\eqref')
     i = 0
-    while 1:
+    while True:
         i = find_re(document.body, regexp, i)
         if i == -1:
             break
@@ -249,7 +264,7 @@ def revert_bibtex(document):
 def remove_insetparent(document):
     " Remove \lyxparent"
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_inset LatexCommand \\lyxparent", i)
         if i == -1:
             break
@@ -261,7 +276,7 @@ def convert_external(document):
     external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
     external_header = "\\begin_inset External"
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, external_header, i)
         if i == -1:
             break
@@ -296,7 +311,7 @@ def revert_external_1(document):
     " Revert inset External."
     external_header = "\\begin_inset External"
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, external_header, i)
         if i == -1:
             break
@@ -321,7 +336,7 @@ def revert_external_2(document):
     " Revert inset External. (part II)"
     draft_token = '\tdraft'
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_inset External', i)
         if i == -1:
             break
@@ -339,7 +354,7 @@ def convert_comment(document):
     " Convert \\layout comment"
     i = 0
     comment = "\\layout Comment"
-    while 1:
+    while True:
         i = find_token(document.body, comment, i)
         if i == -1:
             return
@@ -350,7 +365,7 @@ def convert_comment(document):
                         '\\layout %s' % document.default_layout]
         i = i + 7
 
-        while 1:
+        while True:
                 old_i = i
                 i = find_token(document.body, "\\layout", i)
                 if i == -1:
@@ -403,7 +418,7 @@ def convert_comment(document):
 def revert_comment(document):
     " Revert comments"
     i = 0
-    while 1:
+    while True:
         i = find_tokens(document.body, ["\\begin_inset Comment", "\\begin_inset Greyedout"], i)
 
         if i == -1:
@@ -422,7 +437,7 @@ def add_end_layout(document):
     i = i + 1
     struct_stack = ["\\layout"]
 
-    while 1:
+    while True:
         i = find_tokens(document.body, ["\\begin_inset", "\\end_inset", "\\layout",
                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
 
@@ -489,7 +504,7 @@ def add_end_layout(document):
 def rm_end_layout(document):
     " Remove \end_layout"
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\end_layout', i)
 
         if i == -1:
@@ -520,7 +535,7 @@ def rm_tracking_changes(document):
 def rm_body_changes(document):
     " Remove body changes."
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\change_", i)
         if i == -1:
             return
@@ -531,7 +546,7 @@ def rm_body_changes(document):
 def layout2begin_layout(document):
     " \layout -> \begin_layout "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\layout', i)
         if i == -1:
             return
@@ -543,7 +558,7 @@ def layout2begin_layout(document):
 def begin_layout2layout(document):
     " \begin_layout -> \layout "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_layout', i)
         if i == -1:
             return
@@ -563,7 +578,7 @@ def convert_table_valignment_middle(document):
     " Convert table  valignment, center -> middle"
     regexp = re.compile(r'^\\begin_inset\s+Tabular')
     i = 0
-    while 1:
+    while True:
         i = find_re(document.body, regexp, i)
         if i == -1:
             return
@@ -587,7 +602,7 @@ def revert_valignment_middle(document):
     " Convert table  valignment, middle -> center"
     regexp = re.compile(r'^\\begin_inset\s+Tabular')
     i = 0
-    while 1:
+    while True:
         i = find_re(document.body, regexp, i)
         if i == -1:
             return
@@ -662,7 +677,7 @@ parskip}
     attribute_values = ['default', 'default', 'default', 'default',
                         'default', 'default', 'default', 'none', document.language]
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_layout", i)
         if i == -1:
             return
@@ -820,7 +835,7 @@ parskip}
 def convert_note(document):
     " Convert Notes. "
     i = 0
-    while 1:
+    while True:
         i = find_tokens(document.body, ["\\begin_inset Note",
                                 "\\begin_inset Comment",
                                 "\\begin_inset Greyedout"], i)
@@ -835,7 +850,7 @@ def revert_note(document):
     " Revert Notes. "
     note_header = "\\begin_inset Note "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, note_header, i)
         if i == -1:
             break
@@ -847,7 +862,7 @@ def revert_note(document):
 def convert_box(document):
     " Convert Boxes. "
     i = 0
-    while 1:
+    while True:
         i = find_tokens(document.body, ["\\begin_inset Boxed",
                                 "\\begin_inset Doublebox",
                                 "\\begin_inset Frameless",
@@ -865,7 +880,7 @@ def revert_box(document):
     " Revert Boxes."
     box_header = "\\begin_inset Box "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, box_header, i)
         if i == -1:
             break
@@ -874,10 +889,10 @@ def revert_box(document):
         i = i + 1
 
 
-def convert_collapsable(document):
+def convert_collapsible(document):
     " Convert collapsed insets. "
     i = 0
-    while 1:
+    while True:
         i = find_tokens_exact(document.body, ["\\begin_inset Box",
                                 "\\begin_inset Branch",
                                 "\\begin_inset CharStyle",
@@ -894,7 +909,7 @@ def convert_collapsable(document):
         # If, however, we find a line starting '\begin_layout'
         # (_always_ present) then break with a warning message
         i = i + 1
-        while 1:
+        while True:
             if (document.body[i] == "collapsed false"):
                 document.body[i] = "status open"
                 break
@@ -909,10 +924,10 @@ def convert_collapsable(document):
         i = i + 1
 
 
-def revert_collapsable(document):
+def revert_collapsible(document):
     " Revert collapsed insets. "
     i = 0
-    while 1:
+    while True:
         i = find_tokens_exact(document.body, ["\\begin_inset Box",
                                 "\\begin_inset Branch",
                                 "\\begin_inset CharStyle",
@@ -929,7 +944,7 @@ def revert_collapsable(document):
         # If, however, we find a line starting '\begin_layout'
         # (_always_ present) then break with a warning message
         i = i + 1
-        while 1:
+        while True:
             if (document.body[i] == "status open"):
                 document.body[i] = "collapsed false"
                 break
@@ -948,7 +963,7 @@ def revert_collapsable(document):
 def convert_ert(document):
     " Convert ERT. "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_inset ERT", i)
         if i == -1:
             break
@@ -957,7 +972,7 @@ def convert_ert(document):
         # If, however, we find a line starting '\begin_layout'
         # (_always_ present) then break with a warning message
         i = i + 1
-        while 1:
+        while True:
             if (document.body[i] == "status Open"):
                 document.body[i] = "status open"
                 break
@@ -978,7 +993,7 @@ def convert_ert(document):
 def revert_ert(document):
     " Revert ERT. "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_inset ERT", i)
         if i == -1:
             break
@@ -987,7 +1002,7 @@ def revert_ert(document):
         # If, however, we find a line starting '\begin_layout'
         # (_always_ present) then break with a warning message
         i = i + 1
-        while 1:
+        while True:
             if (document.body[i] == "status open"):
                 document.body[i] = "status Open"
                 break
@@ -1013,7 +1028,7 @@ def convert_minipage(document):
     inner_pos = ["c","t","b","s"]
 
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_inset Minipage", i)
         if i == -1:
             return
@@ -1184,7 +1199,7 @@ def revert_breaks(document):
 
     # Convert the insets
     i = 0
-    while 1:
+    while True:
         i = find_tokens(document.body, tokens, i)
         if i == -1:
             return
@@ -1480,7 +1495,7 @@ def convert_len(len, special):
         len = '%f\\' % len2value(len) + special
 
     # Convert LyX units to LaTeX units
-    for unit in units.keys():
+    for unit in list(units.keys()):
         if len.find(unit) != -1:
             len = '%f' % (len2value(len) / 100) + units[unit]
             break
@@ -1539,7 +1554,7 @@ def convert_frameless_box(document):
     pos = ['t', 'c', 'b']
     inner_pos = ['c', 't', 'b', 's']
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_inset Frameless', i)
         if i == -1:
             return
@@ -1556,7 +1571,7 @@ def convert_frameless_box(document):
                   'inner_pos':1, 'use_parbox':'0', 'width':'100col%',
                   'special':'none', 'height':'1in',
                   'height_special':'totalheight', 'collapsed':'false'}
-        for key in params.keys():
+        for key in list(params.keys()):
             value = get_value(document.body, key, i, j).replace('"', '')
             if value != "":
                 if key == 'position':
@@ -1718,7 +1733,7 @@ def convert_frameless_box(document):
 def remove_branches(document):
     " Remove branches. "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.header, "\\branch", i)
         if i == -1:
             break
@@ -1730,7 +1745,7 @@ def remove_branches(document):
         del document.header[i:j+1]
 
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_inset Branch", i)
         if i == -1:
             return
@@ -1745,7 +1760,7 @@ def remove_branches(document):
         # If, however, we find a line starting '\layout'
         # (_always_ present) then break with a warning message
         collapsed_found = 0
-        while 1:
+        while True:
             if (document.body[i][:9] == "collapsed"):
                 del document.body[i]
                 collapsed_found = 1
@@ -1806,7 +1821,7 @@ def revert_bibtopic(document):
 def convert_float(document):
     " Convert sideway floats. "
     i = 0
-    while 1:
+    while True:
         i = find_token_exact(document.body, '\\begin_inset Float', i)
         if i == -1:
             return
@@ -1814,7 +1829,7 @@ def convert_float(document):
         # If, however, we find a line starting '\begin_layout'
         # (_always_ present) then break with a warning message
         i = i + 1
-        while 1:
+        while True:
             if (document.body[i][:4] == "wide"):
                 document.body.insert(i + 1, 'sideways false')
                 break
@@ -1826,20 +1841,38 @@ def convert_float(document):
 
 
 def revert_float(document):
-    " Revert sideway floats. "
+    " Revert sideways floats. "
     i = 0
-    while 1:
+    while True:
         i = find_token_exact(document.body, '\\begin_inset Float', i)
         if i == -1:
             return
+        line = document.body[i]
+        r = re.compile(r'\\begin_inset Float (.*)$')
+        m = r.match(line)
+        floattype = m.group(1)
+        if floattype != "figure" and floattype != "table":
+            i = i + 1
+            continue
         j = find_end_of_inset(document.body, i)
         if j == -1:
             document.warning("Malformed lyx document: Missing '\\end_inset'.")
             i = i + 1
             continue
         if get_value(document.body, 'sideways', i, j) != "false":
-            document.warning("Conversion of 'sideways true' not yet implemented.")
-            # Don't remove 'sideways' so that people will get warnings by lyx
+            l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
+            if l == -1:
+                document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Float inset.")
+                return
+            document.body[j] = '\\layout Standard\n\\begin_inset ERT\nstatus Collapsed\n\n' \
+            '\\layout Standard\n\n\n\\backslash\n' \
+            'end{sideways' + floattype + '}\n\n\\end_inset\n'
+            del document.body[i+1:l-1]
+            document.body[i] = '\\begin_inset ERT\nstatus Collapsed\n\n' \
+            '\\layout Standard\n\n\n\\backslash\n' \
+            'begin{sideways' + floattype + '}\n\n\\end_inset\n\n'
+            add_to_preamble(document,
+                            ['\\usepackage{rotfloat}\n'])
             i = i + 1
             continue
         del_token(document.body, 'sideways', i, j)
@@ -1850,7 +1883,7 @@ def convert_graphics(document):
     """ Add extension to documentnames of insetgraphics if necessary.
     """
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_inset Graphics", i)
         if i == -1:
             return
@@ -1860,8 +1893,7 @@ def convert_graphics(document):
             return
         i = i + 1
         filename = document.body[j].split()[1]
-        absname = os.path.normpath(os.path.join(document.dir, filename))
-        if document.input == stdin and not os.path.isabs(filename):
+        if document.dir == u'' and not os.path.isabs(filename):
             # We don't know the directory and cannot check the document.
             # We could use a heuristic and take the current directory,
             # and we could try to find out if documentname has an extension,
@@ -1872,6 +1904,7 @@ def convert_graphics(document):
          You may need to correct the document manually or run
          lyx2lyx again with the .lyx document as commandline argument.""" % filename)
             continue
+        absname = os.path.normpath(os.path.join(document.dir, filename))
         # This needs to be the same algorithm as in pre 233 insetgraphics
         if access(absname, F_OK):
             continue
@@ -1891,7 +1924,7 @@ def convert_names(document):
 
     i = 0
 
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_layout Author", i)
         if i == -1:
             return
@@ -2059,7 +2092,7 @@ def revert_paperpackage(document):
 def convert_bullets(document):
     " Convert bullets. "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.header, "\\bullet", i)
         if i == -1:
             return
@@ -2077,7 +2110,7 @@ def convert_bullets(document):
 def revert_bullets(document):
     " Revert bullets. "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.header, "\\bullet", i)
         if i == -1:
             return
@@ -2202,13 +2235,13 @@ def normalize_paragraph_params(document):
                          "\\leftindent"
 
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_layout', i)
         if i == -1:
             return
 
         i = i + 1
-        while 1:
+        while True:
             if body[i].strip() and body[i].split()[0] not in allowed_parameters:
                 break
 
@@ -2249,7 +2282,7 @@ def convert_ert_paragraphs(document):
                           '\\emph', '\\numeric', '\\bar', '\\noun',
                           '\\color', '\\lang']
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_inset ERT', i)
         if i == -1:
             return
@@ -2261,7 +2294,7 @@ def convert_ert_paragraphs(document):
 
         # convert non-standard paragraphs to standard
         k = i
-        while 1:
+        while True:
             k = find_token(document.body, "\\begin_layout", k, j)
             if k == -1:
                 break
@@ -2281,7 +2314,7 @@ def convert_ert_paragraphs(document):
         # insert an empty paragraph before each paragraph but the first
         k = i
         first_pagraph = 1
-        while 1:
+        while True:
             k = find_token(document.body, "\\begin_layout", k, j)
             if k == -1:
                 break
@@ -2296,7 +2329,7 @@ def convert_ert_paragraphs(document):
 
         # convert \\newline to new paragraph
         k = i
-        while 1:
+        while True:
             k = find_token(document.body, "\\newline", k, j)
             if k == -1:
                 break
@@ -2314,7 +2347,7 @@ def convert_ert_paragraphs(document):
 def revert_ert_paragraphs(document):
     " Remove double paragraph breaks. "
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, '\\begin_inset ERT', i)
         if i == -1:
             return
@@ -2326,7 +2359,7 @@ def revert_ert_paragraphs(document):
 
         # replace paragraph breaks with \newline
         k = i
-        while 1:
+        while True:
             k = find_token(document.body, "\\end_layout", k, j)
             l = find_token(document.body, "\\begin_layout", k, j)
             if k == -1 or l == -1:
@@ -2337,7 +2370,7 @@ def revert_ert_paragraphs(document):
 
         # replace double \newlines with paragraph breaks
         k = i
-        while 1:
+        while True:
             k = find_token(document.body, "\\newline", k, j)
             if k == -1:
                 break
@@ -2369,7 +2402,7 @@ def convert_french(document):
     # Change language in the document body
     regexp = re.compile(r'^\\lang\s+frenchb')
     i = 0
-    while 1:
+    while True:
         i = find_re(document.body, regexp, i)
         if i == -1:
             break
@@ -2415,7 +2448,7 @@ def convert_sgml_paragraphs(document):
         return
 
     i = 0
-    while 1:
+    while True:
         i = find_token(document.body, "\\begin_layout SGML", i)
 
         if i == -1:
@@ -2430,7 +2463,7 @@ def convert_sgml_paragraphs(document):
         i = i + 10
 
 ##
-# Convertion hub
+# Conversion hub
 #
 
 supported_versions = ["1.4.%d" % i for i in range(3)] + ["1.4"]
@@ -2441,7 +2474,7 @@ convert = [[222, [insert_tracking_changes, add_end_header, convert_amsmath]],
                   convert_table_valignment_middle, convert_breaks]],
            [226, [convert_note]],
            [227, [convert_box]],
-           [228, [convert_collapsable, convert_ert]],
+           [228, [convert_collapsible, convert_ert]],
            [229, [convert_minipage]],
            [230, [convert_jurabib]],
            [231, [convert_float]],
@@ -2479,7 +2512,7 @@ revert =  [[244, []],
            [230, [revert_float]],
            [229, [revert_jurabib]],
            [228, []],
-           [227, [revert_collapsable, revert_ert]],
+           [227, [revert_collapsible, revert_ert]],
            [226, [revert_box, revert_external_2]],
            [225, [revert_note]],
            [224, [rm_end_layout, begin_layout2layout, revert_end_document,