]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_3.py
New lyx2lyx tools.
[lyx.git] / lib / lyx2lyx / lyx_2_3.py
index f2689fbf45581cb7ef96d6af5abf004ccd0c20a1..625db086044139e7afcc4c7e762333e24271b5b7 100644 (file)
@@ -24,9 +24,10 @@ import sys, os
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import find_end_of, find_token_backwards, find_end_of_layout, \
-    find_token, find_end_of_inset, get_value,  get_bool_value, \
-    get_containing_layout, get_quoted_value, del_token, find_re
+from parser_tools import del_token, del_value, del_complete_lines, \
+    find_end_of, find_end_of_layout, find_end_of_inset, find_re, \
+    find_token, find_token_backwards, get_containing_layout, \
+    get_bool_value, get_value, get_quoted_value
 #  find_tokens, find_token_exact, is_in_inset, \
 #  check_token, get_option_value
 
@@ -1302,7 +1303,7 @@ def revert_biblatex(document):
                       "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",\
                       "footcitet", "footcitep", "footcitealt", "footcitealp",\
                       "footciteauthor", "footciteyear", "footciteyearpar",\
-                     "citefield", "citetitle", "cite*" ]
+                      "citefield", "citetitle", "cite*" ]
 
     i = 0
     while (True):
@@ -1842,18 +1843,16 @@ def revert_chapterbib(document):
 
 def convert_dashligatures(document):
     "Set 'use_dash_ligatures' according to content."
-    use_dash_ligatures = None
-    # eventually remove preamble code from 2.3->2.2 conversion:
-    for i, line in enumerate(document.preamble):
-        if i > 1 and line == r'\renewcommand{\textemdash}{---}':
-            if (document.preamble[i-1] == r'\renewcommand{\textendash}{--}'
-                and document.preamble[i-2] == '% Added by lyx2lyx'):
-                del document.preamble[i-2:i+1]
-                use_dash_ligatures = True
+    # Look for and remove dashligatures workaround from 2.3->2.2 reversion,
+    # set use_dash_ligatures to True if found, to None else.
+    use_dash_ligatures = del_complete_lines(document.preamble,
+                                ['% Added by lyx2lyx',
+                                 r'\renewcommand{\textendash}{--}',
+                                 r'\renewcommand{\textemdash}{---}']) or None
+
     if use_dash_ligatures is None:
-        # Look for dashes:
-        # (Documents by LyX 2.1 or older have "\twohyphens\n" or "\threehyphens\n"
-        # as interim representation for dash ligatures in 2.2.)
+        # Look for dashes (Documents by LyX 2.1 or older have "\twohyphens\n"
+        # or "\threehyphens\n" as interim representation for -- an ---.)
         has_literal_dashes = False
         has_ligature_dashes = False
         j = 0
@@ -1861,23 +1860,32 @@ def convert_dashligatures(document):
             # Skip some document parts where dashes are not converted
             if (i < j) or line.startswith("\\labelwidthstring"):
                 continue
-            words = line.split()
-            if len(words) > 1 and words[0] == "\\begin_inset" and \
-            words[1] in ["CommandInset", "ERT", "External", "Formula",
-                         "FormulaMacro", "Graphics", "IPA", "listings"]:
-                j = find_end_of_inset(document.body, i)
+            if line.startswith("\\begin_inset"):
+                try:
+                    it = line.split()[1]
+                except IndexError:
+                    continue
+                if (it in ["CommandInset", "ERT", "External", "Formula",
+                           "FormulaMacro", "Graphics", "IPA", "listings"]
+                    or line.endswith("Flex Code")):
+                    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." % (itype, i))
+                        continue
+            if line == "\\begin_layout LyX-Code":
+                j = find_end_of_layout(document.body, i)
                 if j == -1:
                     document.warning("Malformed LyX document: "
-                        "Can't find end of %s inset at line %d" % (words[1],i))
+                       "Can't find end of %s layout at line %d" % (words[1],i))
                 continue
             # literal dash followed by a word or no-break space:
             if re.search(u"[\u2013\u2014]([\w\u00A0]|$)", line,
                          flags=re.UNICODE):
                 has_literal_dashes = True
             # ligature dash followed by word or no-break space on next line:
-            if re.search(u"(\\\\twohyphens|\\\\threehyphens)", line,
-                            flags=re.UNICODE) and re.match(u"[\w\u00A0]",
-                            document.body[i+1], flags=re.UNICODE):
+            if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
+                re.match(u"[\w\u00A0]", document.body[i+1], flags=re.UNICODE)):
                 has_ligature_dashes = True
         if has_literal_dashes and has_ligature_dashes:
             # TODO: insert a warning note in the document?
@@ -1890,26 +1898,16 @@ def convert_dashligatures(document):
             use_dash_ligatures = True
     # insert the setting if there is a preferred value
     if use_dash_ligatures is not None:
-        i = find_token(document.header, "\\use_microtype", 0)
-        if i != -1:
-            document.header.insert(i+1, "\\use_dash_ligatures %s"
-                                % str(use_dash_ligatures).lower())
+        i = find_token(document.header, "\\graphics")
+        document.header.insert(i, "\\use_dash_ligatures %s"
+                               % str(use_dash_ligatures).lower())
 
 def revert_dashligatures(document):
     """Remove font ligature settings for en- and em-dashes.
     Revert conversion of \twodashes or \threedashes to literal dashes."""
-    i = find_token(document.header, "\\use_dash_ligatures", 0)
-    if i == -1:
-        return
-    use_dash_ligatures = get_bool_value(document.header, "\\use_dash_ligatures", i)
-    del document.header[i]
-    use_non_tex_fonts = False
-    i = find_token(document.header, "\\use_non_tex_fonts", 0)
-    if i != -1:
-        use_non_tex_fonts = get_bool_value(document.header, "\\use_non_tex_fonts", i)
-    if not use_dash_ligatures or document.backend != "latex":
+    use_dash_ligatures = del_value(document.header, "\\use_dash_ligatures")
+    if use_dash_ligatures != "true" or document.backend != "latex":
         return
-
     j = 0
     new_body = []
     for i, line in enumerate(document.body):
@@ -1917,16 +1915,24 @@ def revert_dashligatures(document):
         if (i < j) or line.startswith("\\labelwidthstring"):
             new_body.append(line)
             continue
-        words = line.split()
-        if len(words) > 1 and words[0] == "\\begin_inset" and \
-           words[1] in ["CommandInset", "ERT", "External", "Formula",
-                        "FormulaMacro", "Graphics", "IPA", "listings"]:
+        if (line.startswith("\\begin_inset ") and
+            line[13:].split()[0] in ["CommandInset", "ERT", "External",
+                "Formula", "FormulaMacro", "Graphics", "IPA", "listings"]
+            or line == "\\begin_inset Flex Code"):
             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))
             new_body.append(line)
             continue
+        if line == "\\begin_layout LyX-Code":
+            j = find_end_of_layout(document.body, i)
+            if j == -1:
+                document.warning("Malformed LyX document: "
+                    "Can't find end of %s layout at line %d" % (words[1],i))
+            new_body.append(line)
+            continue
+        # TODO: skip replacement in typewriter fonts
         line = line.replace(u'\u2013', '\\twohyphens\n')
         line = line.replace(u'\u2014', '\\threehyphens\n')
         lines = line.split('\n')
@@ -2006,8 +2012,8 @@ def revert_mathindent(document):
     else:
         k = find_token(document.header, "\\options", 0)
         if k != -1:
-           document.header[k] = document.header[k].replace("\\options", "\\options fleqn,")
-           del document.header[i]
+            document.header[k] = document.header[k].replace("\\options", "\\options fleqn,")
+            del document.header[i]
         else:
             l = find_token(document.header, "\\use_default_options", 0)
             document.header.insert(l, "\\options fleqn")