]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_0.py
Merge the two noto regular fonts
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
index eefe0b28ea1763b0c96e081f7dc787fb756e93d2..480971b5319c38aad3e8ea7ca70e72eacf50293b 100644 (file)
@@ -30,7 +30,7 @@ from parser_tools import del_complete_lines, \
 
 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
@@ -514,8 +514,6 @@ def convert_ulinelatex(document):
     " Remove preamble code for \\uline font attribute. "
     del_complete_lines(document.preamble,
                        ['% Added by lyx2lyx']+ulinelatex_preamble)
-    for line in document.preamble:
-        print line
 
 def revert_ulinelatex(document):
     " Add preamble code for \\uline font attribute in citations. "
@@ -835,6 +833,9 @@ 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.initial_format < 277:
@@ -852,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]
+    "Revert mhchem loading to preamble code."
 
-    if mhchem == "off":
-      # don't load case
-      return
+    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 == "auto":
+    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):
@@ -1283,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):
@@ -1681,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:
@@ -1716,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"