]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_3.py
Fix cantarell reversion
[lyx.git] / lib / lyx2lyx / lyx_2_3.py
index 8b834c1abc4130ebec9dc8b45d60f461fa9dd209..5f80d23fcae3e08270aa96feff21bb7b4f6a0304 100644 (file)
@@ -26,13 +26,14 @@ import sys, os
 
 from parser_tools import (del_token, del_value, del_complete_lines,
     find_complete_lines, find_end_of, find_end_of_layout, find_end_of_inset,
-    find_re, find_substring, find_token, find_token_backwards,
+    find_re, find_substring, find_token, find_token_backwards, find_across_lines,
     get_containing_inset, get_containing_layout, get_bool_value, get_value,
     get_quoted_value, is_in_inset, set_bool_value)
 #  find_tokens, find_token_exact, check_token, get_option_value
 
 from lyx2lyx_tools import (add_to_preamble, put_cmd_in_ert, revert_font_attrs,
-                           insert_to_preamble, latex_length, revert_language)
+    insert_to_preamble, latex_length, is_document_option,
+    insert_document_option, remove_document_option, revert_language)
 
 ####################################################################
 # Private helper functions
@@ -149,14 +150,38 @@ def revert_ibranches(document):
         if j == -1:
             document.warning("Malformed LyX document! Can't find end of branch " + old)
             continue
-        # ourbranches[old] - 1 inverts the selection status of the old branch
         lines = ["\\branch " + new,
-                 "\\selected " + str(ourbranches[old] - 1)]
+                 "\\selected %d" % (not ourbranches[old])]
         # these are the old lines telling us color, etc.
         lines += document.header[i+2 : j+1]
         document.header[i:i] = lines
 
 
+beamer_article_styles = [
+    "### Inserted by lyx2lyx (more [scr]article styles) ###",
+    "Input article.layout",
+    "Input beamer.layout",
+    "Provides geometry 0",
+    "Provides hyperref 0",
+    "DefaultFont",
+    "     Family                Roman",
+    "     Series                Medium",
+    "     Shape                 Up",
+    "     Size                  Normal",
+    "     Color                 None",
+    "EndFont",
+    "Preamble",
+    "     \\usepackage{beamerarticle,pgf}",
+    "     % this default might be overridden by plain title style",
+    "     \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
+    "     \\AtBeginDocument{",
+    "             \\let\\origtableofcontents=\\tableofcontents",
+    "             \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
+    "             \\def\\gobbletableofcontents#1{\\origtableofcontents}",
+    "     }",
+    "EndPreamble",
+    "### End of insertion by lyx2lyx (more [scr]article styles) ###"]
+
 def revert_beamer_article_styles(document):
     " Include (scr)article styles in beamer article "
 
@@ -164,52 +189,9 @@ def revert_beamer_article_styles(document):
     if document.textclass not in beamer_articles:
         return
 
-    inclusion = "article.layout"
     if document.textclass == "scrarticle-beamer":
-        inclusion = "scrartcl.layout"
-
-    i = find_token(document.header, "\\begin_local_layout", 0)
-    if i == -1:
-        k = find_token(document.header, "\\language", 0)
-        if k == -1:
-            # this should not happen
-            document.warning("Malformed LyX document! No \\language header found!")
-            return
-        document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
-        i = k - 1
-
-    j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-    if j == -1:
-        # this should not happen
-        document.warning("Malformed LyX document: Can't find end of local layout!")
-        return
-
-    document.header[i+1 : i+1] = [
-        "### Inserted by lyx2lyx (more [scr]article styles) ###",
-        "Input " + inclusion,
-        "Input beamer.layout",
-        "Provides geometry 0",
-        "Provides hyperref 0",
-        "DefaultFont",
-        "     Family                Roman",
-        "     Series                Medium",
-        "     Shape                 Up",
-        "     Size                  Normal",
-        "     Color                 None",
-        "EndFont",
-        "Preamble",
-        "     \\usepackage{beamerarticle,pgf}",
-        "     % this default might be overridden by plain title style",
-        "     \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
-        "     \\AtBeginDocument{",
-        "             \\let\\origtableofcontents=\\tableofcontents",
-        "             \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
-        "             \\def\\gobbletableofcontents#1{\\origtableofcontents}",
-        "     }",
-        "EndPreamble",
-        "### End of insertion by lyx2lyx (more [scr]article styles) ###"
-    ]
-
+        beamer_article_styles[1] = "Input scrartcl.layout"
+    document.append_local_layout(beamer_article_styles)
 
 def convert_beamer_article_styles(document):
     " Remove included (scr)article styles in beamer article "
@@ -218,29 +200,9 @@ def convert_beamer_article_styles(document):
     if document.textclass not in beamer_articles:
         return
 
-    i = find_token(document.header, "\\begin_local_layout", 0)
-    if i == -1:
-        return
-
-    j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-    if j == -1:
-        # this should not happen
-        document.warning("Malformed LyX document: Can't find end of local layout!")
-        return
-
-    k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
-    if k != -1:
-        l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
-        if l == -1:
-            # this should not happen
-            document.warning("End of lyx2lyx local layout insertion not found!")
-            return
-
-        if k == i + 1 and l == j - 1:
-            # that was all the local layout there was
-            document.header[i : j + 1] = []
-        else:
-            document.header[k : l + 1] = []
+    if document.textclass == "scrarticle-beamer":
+        beamer_article_styles[1] = "Input scrartcl.layout"
+    document.del_local_layout(beamer_article_styles)
 
 
 def revert_new_babel_languages(document):
@@ -259,12 +221,12 @@ def revert_new_babel_languages(document):
             # These are supported by babel and polyglossia
             revert_language(document, lang, lang, lang)
 
-
 # TODO:
 # def convert_new_babel_languages(document)
 # set to native support if get_value(document.header, "\\options") in
 # ["bosnian", "friulan", "macedonian", "piedmontese", "romansh"]
-# and "\\language_package babel".
+# and Babel is used.
+
 
 def revert_amharic(document):
     "Set the document language to English but assure Amharic output"
@@ -396,61 +358,22 @@ def revert_quotes(document):
         i = l
 
 
+iopart_local_layout = ["### Inserted by lyx2lyx (stdlayouts) ###",
+                       "Input stdlayouts.inc",
+                       "### End of insertion by lyx2lyx (stdlayouts) ###"""]
+
 def revert_iopart(document):
     " Input new styles via local layout "
     if document.textclass != "iopart":
         return
-
-    i = find_token(document.header, "\\begin_local_layout", 0)
-    if i == -1:
-        k = find_token(document.header, "\\language", 0)
-        if k == -1:
-            # this should not happen
-            document.warning("Malformed LyX document! No \\language header found!")
-            return
-        document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
-        i = k-1
-
-    j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-    if j == -1:
-        # this should not happen
-        document.warning("Malformed LyX document! Can't find end of local layout!")
-        return
-
-    document.header[i+1 : i+1] = [
-        "### Inserted by lyx2lyx (stdlayouts) ###",
-        "Input stdlayouts.inc",
-        "### End of insertion by lyx2lyx (stdlayouts) ###"
-    ]
+    document.append_local_layout(iopart_local_layout)
 
 
 def convert_iopart(document):
     " Remove local layout we added, if it is there "
     if document.textclass != "iopart":
         return
-
-    i = find_token(document.header, "\\begin_local_layout", 0)
-    if i == -1:
-        return
-
-    j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-    if j == -1:
-        # this should not happen
-        document.warning("Malformed LyX document! Can't find end of local layout!")
-        return
-
-    k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
-    if k != -1:
-        l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
-        if l == -1:
-            # this should not happen
-            document.warning("End of lyx2lyx local layout insertion not found!")
-            return
-        if k == i + 1 and l == j - 1:
-            # that was all the local layout there was
-            document.header[i : j + 1] = []
-        else:
-            document.header[k : l + 1] = []
+    document.del_local_layout(iopart_local_layout)
 
 
 def convert_quotestyle(document):
@@ -1192,27 +1115,12 @@ def revert_biblatex(document):
 
     # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
     if biblatex:
-        i = find_token(document.header, "\\begin_local_layout", 0)
-        if i == -1:
-            k = find_token(document.header, "\\language", 0)
-            if k == -1:
-                # this should not happen
-                document.warning("Malformed LyX document! No \\language header found!")
-                return
-            document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
-            i = k-1
-
-        j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-        if j == -1:
-            # this should not happen
-            document.warning("Malformed LyX document! Can't find end of local layout!")
-            return
-
-        document.header[i+1 : i+1] = [
+        biblatex_emulation = [
             "### Inserted by lyx2lyx (biblatex emulation) ###",
             "Provides natbib 1",
             "### End of insertion by lyx2lyx (biblatex emulation) ###"
         ]
+        document.append_local_layout(biblatex_emulation)
 
 
 def revert_citekeyonly(document):
@@ -1273,28 +1181,12 @@ def revert_bibpackopts(document):
         # Nothing to do for empty options
         return
 
-    i = find_token(document.header, "\\begin_local_layout", 0)
-    if i == -1:
-        k = find_token(document.header, "\\language", 0)
-        if k == -1:
-            # this should not happen
-            document.warning("Malformed LyX document! No \\language header found!")
-            return
-        document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
-        i = k - 1
-
-    j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-    if j == -1:
-        # this should not happen
-        document.warning("Malformed LyX document! Can't find end of local layout!")
-        return
-
-    document.header[i+1 : i+1] = [
+    bibliography_package_options = [
         "### Inserted by lyx2lyx (bibliography package options) ###",
         "PackageOptions " + engine + " " + biblio_options,
         "### End of insertion by lyx2lyx (bibliography package options) ###"
     ]
-
+    document.append_local_layout(bibliography_package_options)
 
 def revert_qualicites(document):
     " Revert qualified citation list commands to ERT "
@@ -1416,9 +1308,9 @@ def convert_literalparam(document):
             i += 1
         # href is already fully latexified. Here we can switch off literal.
         if inset == "href":
-            document.body.insert(i, "literal \"false\"")
+            document.body.insert(i, 'literal "false"')
         else:
-            document.body.insert(i, "literal \"true\"")
+            document.body.insert(i, 'literal "true"')
         i = j + 1
 
 
@@ -1428,19 +1320,14 @@ def revert_literalparam(document):
     for inset in command_insets:
         i = 0
         while True:
-            i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
+            i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i+1)
             if i == -1:
                 break
             j = find_end_of_inset(document.body, i)
             if j == -1:
                 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
-                i += 1
                 continue
-            k = find_token(document.body, 'literal', i, j)
-            if k == -1:
-                i += 1
-                continue
-            del document.body[k]
+            del_token(document.body, 'literal', i, j)
 
 
 def revert_multibib(document):
@@ -1981,35 +1868,23 @@ def revert_allowbreak(document):
 
 def convert_mathnumberpos(document):
     " add the \\math_number_before tag "
+    i = find_token(document.header, "\\quotes_style")
     # check if the document uses the class option "leqno"
-    i = find_token(document.header, "\\options")
-    k = find_token(document.header, "\\quotes_style")
-    if 'leqno' in document.header[i]:
-        document.header.insert(k, "\\math_number_before 1")
-        # delete the found option
-        document.header[i] = document.header[i].replace(",leqno", "")
-        document.header[i] = document.header[i].replace(", leqno", "")
-        document.header[i] = document.header[i].replace("leqno,", "")
-        if 'leqno' in document.header[i]:
-            # then we have leqno as the only option
-            del document.header[i]
+    if is_document_option(document, "leqno"):
+        remove_document_option(document, "leqno")
+        document.header.insert(i, "\\math_number_before 1")
     else:
-        document.header.insert(k, "\\math_number_before 0")
+        document.header.insert(i, "\\math_number_before 0")
 
 
 def revert_mathnumberpos(document):
     """Remove \\math_number_before tag,
     add the document class option leqno if required.
     """
-    math_number_before = get_bool_value(document.header,
-                                        '\\math_number_before', delete=True)
+    math_number_before = get_bool_value(document.header, '\\math_number_before',
+                                        delete=True)
     if math_number_before:
-        i = find_token(document.header, "\\options")
-        if i != -1 and 'leqno' not in document.header[i]:
-            document.header[i] = document.header[i].replace("\\options", "\\options leqno,")
-        else:
-            i = find_token(document.header, "\\use_default_options")
-            document.header.insert(i, "\\options leqno")
+        insert_document_option(document, "leqno")
 
 
 def convert_mathnumberingname(document):