]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_1_5.py
Remove latexdel insets comming from sgml2lyx (ref, url and htmlurl).
[lyx.git] / lib / lyx2lyx / lyx_1_1_5.py
index 29e69c21f8a196719cc975a0cefb27703c5233e5..645c1657d47eb49c7503659991574bfada1bc618 100644 (file)
@@ -24,7 +24,8 @@ from parser_tools import find_token, find_token_backwards, find_re
 layout_exp = re.compile(r"\\layout (\S*)")
 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
 
-def replace_protected_separator(lines):
+def replace_protected_separator(file):
+    lines = file.body
     i=0
     while 1:
         i = find_token(lines, "\\protected_separator", i)
@@ -32,7 +33,11 @@ def replace_protected_separator(lines):
             break
         j = find_token_backwards(lines, "\\layout", i)
         #if j == -1: print error
-        layout = layout_exp.match(lines[j]).group(1)
+        layout_m = layout_exp.match(lines[j])
+        if layout_m:
+            layout = layout_m.group(1)
+        else:
+            layout = "Standard"
 
         if layout == "LyX-Code":
             result = ""
@@ -47,7 +52,8 @@ def replace_protected_separator(lines):
         del lines[i]
 
 
-def merge_formula_inset(lines):
+def merge_formula_inset(file):
+    lines = file.body
     i=0
     while 1:
         i = find_token(lines, "\\begin_inset Formula", i)
@@ -59,7 +65,8 @@ def merge_formula_inset(lines):
 
 
 # Update from tabular format 4 to 5 if necessary
-def update_tabular(lines):
+def update_tabular(file):
+    lines = file.body
     lyxtable_re = re.compile(r".*\\LyXTable$")
     i=0
     while 1:
@@ -90,7 +97,8 @@ def update_tabular(lines):
             i = i + 1
 
 
-def update_toc(lines):
+def update_toc(file):
+    lines = file.body
     i = 0
     while 1:
         i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
@@ -100,13 +108,15 @@ def update_toc(lines):
         i = i + 1
 
 
-def remove_cursor(lines):
+def remove_cursor(file):
+    lines = file.body
     i = find_token(lines, '\\cursor', 0)
     if i != -1:
         del lines[i]
 
 
-def remove_vcid(lines):
+def remove_vcid(file):
+    lines = file.header
     i = find_token(lines, '\\lyxvcid', 0)
     if i != -1:
         del lines[i]
@@ -115,14 +125,16 @@ def remove_vcid(lines):
         del lines[i]
 
 
-def first_layout(lines):
+def first_layout(file):
+    lines = file.body
     while (lines[0] == ""):
         del lines[0]
     if lines[0][:7] != "\\layout":
         lines[:0] = ["\\layout Standard"]
 
 
-def remove_space_in_units(lines):
+def remove_space_in_units(file):
+    lines = file.header
     margins = ["\\topmargin","\\rightmargin",
                "\\leftmargin","\\bottommargin"]
 
@@ -147,20 +159,88 @@ def remove_space_in_units(lines):
             i = i + 1
 
 
-def convert(file):
-    first_layout(file.body)
-    remove_vcid(file.header)
-    remove_cursor(file.body)
-    update_toc(file.body)
-    replace_protected_separator(file.body)
-    merge_formula_inset(file.body)
-    update_tabular(file.body)
-    remove_space_in_units(file.header)
-    file.format = 216
+def latexdel_getargs(file, i):
+    lines = file.body
+
+    # play safe, clean empty lines
+    while 1:
+        if lines[i]:
+            break
+        del lines[i]
+
+    j = find_token(lines, '\\end_inset', i)
+
+    if i == j:
+        del lines[i]
+    else:
+        file.warning("Unexpected end of inset.")
+    j = find_token(lines, '\\begin_inset LatexDel }{', i)
+
+    ref = string.join(lines[i:j])
+    del lines[i:j + 1]
+
+    # play safe, clean empty lines
+    while 1:
+        if lines[i]:
+            break
+        del lines[i]
+
+    j = find_token(lines, '\\end_inset', i - 1)
+    if i == j:
+        del lines[i]
+    else:
+        file.warning("Unexpected end of inset.")
+    j = find_token(lines, '\\begin_inset LatexDel }', i)
+    label = string.join(lines[i:j])
+    del lines[i:j + 1]
+
+    return ref, label
+
+
+def update_ref(file):
+    lines = file.body
+    i = 0
+    while 1:
+        i = find_token(lines, '\\begin_inset LatexCommand', i)
+        if i == -1:
+            return
+
+        if string.split(lines[i])[-1] == "\\ref{":
+            i = i + 1
+            ref, label = latexdel_getargs(file, i)
+            lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label)
+
+        i = i + 1
+
+
+def update_latexdel(file):
+    lines = file.body
+    i = 0
+    latexdel_re = re.compile(r".*\\begin_inset LatexDel")
+    while 1:
+        i = find_re(lines, latexdel_re, i)
+        if i == -1:
+            return
+        lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
+
+        j = string.find(lines[i],'\\begin_inset')
+        lines.insert(i+1, lines[i][j:])
+        lines[i] = string.strip(lines[i][:j])
+        i = i + 1
+
+        if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"):
+            i = i + 1
+
+            ref, label = latexdel_getargs(file, i)
+            lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref)
+
+        i = i + 1
 
 
-def revert(file):
-    file.error("The convertion to an older format (%s) is not implemented." % file.format)
+convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc,
+                  replace_protected_separator, merge_formula_inset,
+                  update_tabular, remove_space_in_units, update_ref, update_latexdel]]]
+revert  = []
 
 if __name__ == "__main__":
     pass