]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_3.py
whitespace.
[lyx.git] / lib / lyx2lyx / lyx_2_3.py
index 4d42b41f1b3bc891e058c46fb37344254e29062c..322f495991e508d8088ce018642f56d0e9662940 100644 (file)
@@ -25,9 +25,9 @@ import sys, os
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import find_end_of, find_token_backwards#,
+from parser_tools import find_end_of, find_token_backwards, find_end_of_layout#,
 #  find_token, find_tokens, \
-#  find_token_exact, find_end_of_inset, find_end_of_layout, \
+#  find_token_exact, find_end_of_inset, \
 #  is_in_inset, get_value, get_quoted_value, \
 #  del_token, check_token, get_option_value, get_bool_value
 
@@ -200,47 +200,48 @@ def revert_beamer_article_styles(document):
     if document.textclass == "scrarticle-beamer":
         inclusion = "scrartcl.layout"
 
-    while True:
-        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!")
-                break
-            document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
-            i = find_token(document.header, "\\begin_local_layout", 0)
-        if i != -1:
-            j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
-            if j == -1:
-                # this should not happen
-                break
-
-            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) ###"]
+    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) ###"
+    ]
+
 
 def convert_beamer_article_styles(document):
     " Remove included (scr)article styles in beamer article "
@@ -249,28 +250,30 @@ def convert_beamer_article_styles(document):
     if document.textclass not in beamer_articles:
         return
 
-    while True:
-        i = find_token(document.header, "\\begin_local_layout", 0)
-        if i == -1:
-            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
-            break
+    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!")
-                break
+    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] = []
 
-        return
-
 
 def revert_bosnian(document):
     "Set the document language to English but assure Bosnian output"
@@ -502,7 +505,7 @@ def revert_quotes(document):
     while i < len(document.body):
         words = document.body[i].split()
         if len(words) > 1 and words[0] == "\\begin_inset" and \
-           ( words[1] in ["ERT", "listings"] or words[2] == "URL" ):
+           ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
             j = find_end_of_inset(document.body, i)
             if j == -1:
                 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
@@ -526,11 +529,44 @@ def revert_quotes(document):
             i += 1
             continue
 
+    # Now verbatim layouts
+    i = 0
+    j = 0
+    while i < len(document.body):
+        words = document.body[i].split()
+        if len(words) > 1 and words[0] == "\\begin_layout" and \
+           words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
+            j = find_end_of_layout(document.body, i)
+            if j == -1:
+                document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
+                i += 1
+                continue
+            while True:
+                k = find_token(document.body, '\\begin_inset Quotes', i, j)
+                if k == -1:
+                    i += 1
+                    break
+                l = find_end_of_inset(document.body, k)
+                if l == -1:
+                    document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
+                    i = k
+                    continue
+                replace = "\""
+                if document.body[k].endswith("s"):
+                    replace = "'"
+                document.body[k:l+1] = [replace]
+        else:
+            i += 1
+            continue
+
     # Now handle Hebrew
+    if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
+        return
+
     i = 0
     j = 0
     while True:
-        k = find_token(document.body, '\\begin_inset Quotes', i, j)
+        k = find_token(document.body, '\\begin_inset Quotes', i)
         if k == -1:
             return
         l = find_end_of_inset(document.body, k)
@@ -550,9 +586,65 @@ def revert_quotes(document):
             if document.body[k].endswith("s"):
                 replace = "'"
             document.body[k:l+1] = [replace]
-        i += 1
+        i = l
     
 
+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) ###"
+    ]
+
+
+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] = []
+
 
 ##
 # Conversion hub
@@ -568,10 +660,12 @@ convert = [
            [514, []],
            [515, []],
            [516, [convert_inputenc]],
-           [517, []]  
+           [517, []],
+           [518, [convert_iopart]]
           ]
 
 revert =  [
+           [517, [revert_iopart]],
            [516, [revert_quotes]],
            [515, []],
            [514, [revert_urdu, revert_syriac]],