]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_0.py
Fixes for Japanese documents
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
index 6f897d525c30793e3f95c081f4f52e55e9c524e3..480971b5319c38aad3e8ea7ca70e72eacf50293b 100644 (file)
@@ -22,14 +22,15 @@ import re, string
 import unicodedata
 import sys, os
 
-from parser_tools import find_token, find_end_of, find_tokens, \
+from parser_tools import del_complete_lines, \
+  find_token, find_end_of, find_tokens, \
   find_token_exact, find_end_of_inset, find_end_of_layout, \
   find_token_backwards, is_in_inset, get_value, get_quoted_value, \
   del_token, check_token, get_option_value
 
 from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
   put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
-  revert_font_attrs, hex2ratio, str2bool
+  revert_font_attrs, hex2ratio, str2bool, revert_language
 
 ####################################################################
 # Private helper functions
@@ -484,6 +485,15 @@ def revert_printindexall(document):
             document.body[i:k + 1] = subst
         i = i + 1
 
+strikeout_preamble = ['%  for proper underlining',
+                      r'\PassOptionsToPackage{normalem}{ulem}',
+                      r'\usepackage{ulem}']
+
+def convert_strikeout(document):
+    " Remove preamble code loading 'ulem' package. "
+    del_complete_lines(document.preamble,
+                       ['% Added by lyx2lyx']+strikeout_preamble)
+
 
 def revert_strikeout(document):
   " Reverts \\strikeout font attribute "
@@ -491,25 +501,30 @@ def revert_strikeout(document):
   changed = revert_font_attrs(document.body, "\\uwave", "\\uwave") or changed
   changed = revert_font_attrs(document.body, "\\strikeout", "\\sout")  or changed
   if changed == True:
-    insert_to_preamble(document, \
-        ['%  for proper underlining',
-        '\\PassOptionsToPackage{normalem}{ulem}',
-        '\\usepackage{ulem}'])
+    insert_to_preamble(document, strikeout_preamble)
 
 
+ulinelatex_preamble = ['% fix underbar in citations',
+    r'\let\cite@rig\cite',
+    r'\newcommand{\b@xcite}[2][\%]{\def\def@pt{\%}\def\pas@pt{#1}',
+    r'  \mbox{\ifx\def@pt\pas@pt\cite@rig{#2}\else\cite@rig[#1]{#2}\fi}}',
+    r'\renewcommand{\underbar}[1]{{\let\cite\b@xcite\uline{#1}}}']
+
+def convert_ulinelatex(document):
+    " Remove preamble code for \\uline font attribute. "
+    del_complete_lines(document.preamble,
+                       ['% Added by lyx2lyx']+ulinelatex_preamble)
+
 def revert_ulinelatex(document):
-    " Reverts \\uline font attribute "
+    " Add preamble code for \\uline font attribute in citations. "
     i = find_token(document.body, '\\bar under', 0)
     if i == -1:
         return
-    insert_to_preamble(document,\
-            ['%  for proper underlining',
-            '\\PassOptionsToPackage{normalem}{ulem}',
-            '\\usepackage{ulem}',
-            '\\let\\cite@rig\\cite',
-            '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}',
-            '  \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}',
-            '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}'])
+    try:
+        document.preamble.index(r'\usepackage{ulem}')
+    except ValueError:
+        insert_to_preamble(document, strikeout_preamble)
+    insert_to_preamble(document, ulinelatex_preamble)
 
 
 def revert_custom_processors(document):
@@ -818,9 +833,12 @@ def revert_suppress_date(document):
     del document.header[i]
 
 
+mhchem_preamble = [r"\PassOptionsToPackage{version=3}{mhchem}",
+                   r"\usepackage{mhchem}"]
+
 def convert_mhchem(document):
     "Set mhchem to off for versions older than 1.6.x"
-    if document.start < 277:
+    if document.initial_format < 277:
         # LyX 1.5.x and older did never load mhchem.
         # Therefore we must switch it off: Documents that use mhchem have
         # a manual \usepackage anyway, and documents not using mhchem but
@@ -835,47 +853,44 @@ def convert_mhchem(document):
         # pre-1.5.x document
         i = find_token(document.header, "\\use_amsmath", 0)
     if i == -1:
-        document.warning("Malformed LyX document: Could not find amsmath os esint setting.")
+        document.warning("Malformed LyX document: "
+                         "Could not find amsmath or esint setting.")
         return
     document.header.insert(i + 1, "\\use_mhchem %d" % mhchem)
+    # remove LyX-inserted preamble 
+    if mhchem != 0:
+        del_complete_lines(document.preamble,
+                           ['% Added by lyx2lyx']+mhchem_preamble)
 
 
 def revert_mhchem(document):
-    "Revert mhchem loading to preamble code"
-
-    mhchem = "off"
-    i = find_token(document.header, "\\use_mhchem", 0)
-    if i == -1:
-        document.warning("Malformed LyX document: Could not find mhchem setting.")
-        mhchem = "auto"
-    else:
-        val = get_value(document.header, "\\use_mhchem", i)
-        if val == "1":
-            mhchem = "auto"
-        elif val == "2":
-            mhchem = "on"
-        del document.header[i]
-
-    if mhchem == "off":
-      # don't load case
-      return
+    "Revert mhchem loading to preamble code."
 
-    if mhchem == "auto":
+    mhchem = get_value(document.header, "\\use_mhchem", delete=True)
+    try:
+        mhchem = int(mhchem)
+    except ValueError:
+        document.warning("Malformed LyX document: "
+                         "Could not find mhchem setting.")
+        mhchem = 1 # "auto"
+    # mhchem in {0: "off", 1: "auto", 2: "on"}
+
+    if mhchem == 1: # "auto"
         i = 0
-        while True:
+        while i != 1 and mhchem == 1:
             i = find_token(document.body, "\\begin_inset Formula", i)
-            if i == -1:
-               break
-            line = document.body[i]
-            if line.find("\\ce{") != -1 or line.find("\\cf{") != -1:
-              mhchem = "on"
-              break
+            j = find_end_of_inset(document.body, i)
+            if j == -1:
+                break
+            if (True for line in document.body[i:j]
+                if r"\ce{" in line or r"\cf{" in line):
+                mhchem = 2
+                break
             i += 1
 
-    if mhchem == "on":
-        pre = ["\\PassOptionsToPackage{version=3}{mhchem}",
-          "\\usepackage{mhchem}"]
-        insert_to_preamble(document, pre)
+    if (mhchem == 2 # on
+        and find_token(document.preamble, r"\usepackage{mhchem}") == -1):
+        insert_to_preamble(document, mhchem_preamble)
 
 
 def revert_fontenc(document):
@@ -1266,19 +1281,7 @@ def revert_notefontcolor(document):
 def revert_turkmen(document):
     "Set language Turkmen to English"
 
-    if document.language == "turkmen":
-        document.language = "english"
-        i = find_token(document.header, "\\language", 0)
-        if i != -1:
-            document.header[i] = "\\language english"
-
-    j = 0
-    while True:
-        j = find_token(document.body, "\\lang turkmen", j)
-        if j == -1:
-            return
-        document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english")
-        j += 1
+    revert_language(document, "turkmen", "turkmen", "turkmen")
 
 
 def revert_fontcolor(document):
@@ -1516,17 +1519,16 @@ def revert_makebox(document):
       document.warning("Malformed LyX document: Can't find layout in box.")
       i = z
       continue
-    # by looking before the layout we make sure we're actually finding
-    # an option, not text.
-    j = find_token(document.body, 'use_makebox', i, blay)
-    if j == -1:
-        i = z
-        continue
-
+    j = find_token(document.body, 'use_makebox', i)
+    if j == -1 or j != i +6:
+      document.warning("Malformed LyX document: Can't find use_makebox statement in box.")
+      i = z
+      continue
+    # delete use_makebox
     if not check_token(document.body[i], "\\begin_inset Box Frameless") \
       or get_value(document.body, 'use_makebox', j) != 1:
         del document.body[j]
-        i = z
+        i += 1
         continue
     bend = find_end_of_layout(document.body, blay)
     if bend == -1 or bend > z:
@@ -1553,26 +1555,14 @@ def convert_use_makebox(document):
     i = find_token(document.body, '\\begin_inset Box', i)
     if i == -1:
       return
-    # all of this is to make sure we actually find the use_parbox
-    # that is an option for this box, not some text elsewhere.
-    z = find_end_of_inset(document.body, i)
-    if z == -1:
-      document.warning("Can't find end of box inset!!")
-      i += 1
-      continue
-    blay = find_token(document.body, "\\begin_layout", i, z)
-    if blay == -1:
-      document.warning("Can't find layout in box inset!!")
-      i = z
-      continue
-    # so now we are looking for use_parbox before the box's layout
-    k = find_token(document.body, 'use_parbox', i, blay)
-    if k == -1:
+    k = find_token(document.body, 'use_parbox', i)
+    if k == -1 or k != i + 5:
       document.warning("Malformed LyX document: Can't find use_parbox statement in box.")
-      i = z
+      i += 1
       continue
-    document.body.insert(k + 1, "use_makebox 0")
-    i = blay + 1 # not z + 1 (box insets may be nested)
+    if k == i + 5:
+      document.body.insert(k + 1, "use_makebox 0")
+    i += 1
 
 
 def revert_IEEEtran(document):
@@ -1677,12 +1667,10 @@ def revert_nameref(document):
       i += 1
       # Make sure it is actually in an inset!
       # A normal line could begin with "LatexCommand nameref"!
-      val = is_in_inset(document.body, cmdloc, \
-          "\\begin_inset CommandInset ref")
-      if not val:
+      stins, endins = is_in_inset(document.body, cmdloc,
+                                  "\\begin_inset CommandInset ref")
+      if endins == -1:
           continue
-      stins, endins = val
-
       # ok, so it is in an InsetRef
       refline = find_token(document.body, "reference", stins, endins)
       if refline == -1:
@@ -1712,10 +1700,9 @@ def remove_Nameref(document):
       break
     cmdloc = i
     i += 1
-
     # Make sure it is actually in an inset!
-    val = is_in_inset(document.body, cmdloc, \
-        "\\begin_inset CommandInset ref")
+    val = is_in_inset(document.body, cmdloc,
+                      "\\begin_inset CommandInset ref", default=False)
     if not val:
       continue
     document.body[cmdloc] = "LatexCommand nameref"
@@ -2481,9 +2468,9 @@ convert = [[346, []],
            [352, [convert_splitindex]],
            [353, []],
            [354, []],
-           [355, []],
+           [355, [convert_strikeout]],
            [356, []],
-           [357, []],
+           [357, [convert_ulinelatex]],
            [358, []],
            [359, [convert_nomencl_width]],
            [360, []],