]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_4.py
Cmake tests: Added check to inspect created pdf in test "AMS-import"
[lyx.git] / lib / lyx2lyx / lyx_2_4.py
index 43542866c1f41baea30edb2c1fce156388aa3249..2b03ada0d89b6877973d33e5cc290a8b896e0e5e 100644 (file)
@@ -24,12 +24,13 @@ import sys, os
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, get_bool_value, get_value) 
+from parser_tools import (find_end_of_inset, find_end_of_layout, find_token,
+get_bool_value, get_value, get_quoted_value) 
 #    del_token, del_value, del_complete_lines,
 #    find_complete_lines, find_end_of, 
 #    find_re, find_substring, find_token_backwards,
-#    get_containing_inset, get_containing_layout, get_value,
-#    get_quoted_value, is_in_inset, set_bool_value
+#    get_containing_inset, get_containing_layout,
+#    is_in_inset, set_bool_value
 #    find_tokens, find_token_exact, check_token, get_option_value
 
 from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
@@ -182,6 +183,150 @@ def revert_lscape(document):
         # no need to reset i
 
 
+def convert_fontenc(document):
+    " Convert default fontenc setting "
+
+    i = find_token(document.header, "\\fontencoding global", 0)
+    if i == -1:
+        return
+
+    document.header[i] = document.header[i].replace("global", "auto")
+
+
+def revert_fontenc(document):
+    " Revert default fontenc setting "
+
+    i = find_token(document.header, "\\fontencoding auto", 0)
+    if i == -1:
+        return
+
+    document.header[i] = document.header[i].replace("auto", "global")
+
+
+def revert_nospellcheck(document):
+    " Remove nospellcheck font info param "
+
+    i = 0
+    while True:
+        i = find_token(document.body, '\\nospellcheck', i)
+        if i == -1:
+            return
+        del document.body[i]
+
+
+def revert_floatpclass(document):
+    " Remove float placement params 'document' and 'class' "
+
+    i = 0
+    i = find_token(document.header, "\\float_placement class", 0)
+    if i != -1:
+        del document.header[i]
+
+    i = 0
+    while True:
+        i = find_token(document.body, '\\begin_inset Float', i)
+        if i == -1:
+            break
+        j = find_end_of_inset(document.body, i)
+        k = find_token(document.body, 'placement class', i, i + 2)
+        if k == -1:
+            k = find_token(document.body, 'placement document', i, i + 2)
+            if k != -1:
+                del document.body[k]
+            i = j
+            continue
+        del document.body[k]
+
+
+def revert_floatalignment(document):
+    " Remove float alignment params "
+
+    i = 0
+    i = find_token(document.header, "\\float_alignment", 0)
+    galignment = ""
+    if i != -1:
+        galignment = get_value(document.header, "\\float_alignment", i)
+        del document.header[i]
+
+    i = 0
+    while True:
+        i = find_token(document.body, '\\begin_inset Float', i)
+        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 inset at line " + str(i))
+            i += 1
+        k = find_token(document.body, 'alignment', i, i + 4)
+        if k == -1:
+            i = j
+            continue
+        alignment = get_value(document.body, "alignment", k)
+        if alignment == "document":
+            alignment = galignment
+        del document.body[k]
+        l = find_token(document.body, "\\begin_layout Plain Layout", i, j)
+        if l == -1:
+            document.warning("Can't find float layout!")
+            i = j
+            continue
+        alcmd = []
+        if alignment == "left":
+            alcmd = put_cmd_in_ert("\\raggedright{}")
+        elif alignment == "center":
+            alcmd = put_cmd_in_ert("\\centering{}")
+        elif alignment == "right":
+            alcmd = put_cmd_in_ert("\\raggedleft{}")
+        if len(alcmd) > 0:
+            document.body[l+1:l+1] = alcmd
+        i = j 
+
+
+def revert_tuftecite(document):
+    " Revert \cite commands in tufte classes "
+
+    tufte = ["tufte-book", "tufte-handout"]
+    if document.textclass not in tufte:
+        return
+
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_inset CommandInset citation", i)
+        if i == -1:
+            break
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Can't find end of citation inset at line %d!!" %(i))
+            i += 1
+            continue
+        k = find_token(document.body, "LatexCommand", i, j)
+        if k == -1:
+            document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
+            i = j + 1
+            continue
+        cmd = get_value(document.body, "LatexCommand", k)
+        if cmd != "cite":
+            i = j + 1
+            continue
+        pre = get_quoted_value(document.body, "before", i, j)
+        post = get_quoted_value(document.body, "after", i, j)
+        key = get_quoted_value(document.body, "key", i, j)
+        if not key:
+            document.warning("Citation inset at line %d does not have a key!" %(i))
+            key = "???"
+        # Replace command with ERT
+        res = "\\cite"
+        if pre:
+            res += "[" + pre + "]"
+        if post:
+            res += "[" + post + "]"
+        elif pre:
+            res += "[]"
+        res += "{" + key + "}"
+        document.body[i:j+1] = put_cmd_in_ert([res])
+        i = j + 1
+
+
 ##
 # Conversion hub
 #
@@ -192,10 +337,18 @@ convert = [
            [546, []],
            [547, []],
            [548, []],
-           [549, []]
+           [549, []],
+           [550, [convert_fontenc]],
+           [551, []],
+           [552, []],
+           [553, []]
           ]
 
 revert =  [
+           [552, [revert_tuftecite]],
+           [551, [revert_floatpclass, revert_floatalignment]],
+           [550, [revert_nospellcheck]],
+           [549, [revert_fontenc]],
            [548, []],# dummy format change
            [547, [revert_lscape]],
            [546, [revert_xcharter]],