]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_0.py
Math.lyx: describe new supported font \mathscr
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
index 05903ed33b3cc5a6b74198aca724a1eae0717fc4..02cc1f0fca0fed11505156d2f187cd620965c9dd 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 # This file is part of lyx2lyx
 # -*- coding: utf-8 -*-
-# Copyright (C) 2008 José Matos  <jamatos@lyx.org>
+# Copyright (C) 2010 The LyX team
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -1568,7 +1568,6 @@ def revert_fontcolor(document):
                            + ', ' + str(blueout) + '}\n'
                            + '\\color{document_fontcolor}\n')
 
-
 def revert_shadedboxcolor(document):
     " Reverts shaded box color to preamble code "
     i = 0
@@ -2057,6 +2056,153 @@ def revert_mathrsfs(document):
       i += 1
 
 
+def convert_mathdots(document):
+    " Load mathdots automatically "
+    while True:
+      i = find_token(document.header, "\\use_esint" , 0)
+      if i != -1:
+        document.header.insert(i + 1, "\\use_mathdots 1")
+      break
+
+
+def revert_mathdots(document):
+    " Load mathdots if used in the document "
+    i = 0
+    ddots = re.compile(r'\\begin_inset Formula .*\\ddots', re.DOTALL)
+    vdots = re.compile(r'\\begin_inset Formula .*\\vdots', re.DOTALL)
+    iddots = re.compile(r'\\begin_inset Formula .*\\iddots', re.DOTALL)
+    mathdots = find_token(document.header, "\\use_mathdots" , 0)
+    no = find_token(document.header, "\\use_mathdots 0" , 0)
+    auto = find_token(document.header, "\\use_mathdots 1" , 0)
+    yes = find_token(document.header, "\\use_mathdots 2" , 0)
+    if mathdots != -1:
+      del document.header[mathdots]
+    while True:
+      i = find_token(document.body, '\\begin_inset Formula', i)
+      if i == -1:
+        return
+      j = find_end_of_inset(document.body, i)
+      if j == -1:
+        document.warning("Malformed LyX document: Can't find end of Formula inset.")
+        return 
+      k = ddots.search("\n".join(document.body[i:j]))
+      l = vdots.search("\n".join(document.body[i:j]))
+      m = iddots.search("\n".join(document.body[i:j]))
+      if (yes == -1) and ((no != -1) or (not k and not l and not m) or (auto != -1 and not m)):
+        i += 1
+        continue
+      # use \@ifundefined to catch also the "auto" case
+      add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+      add_to_preamble(document, ["\\@ifundefined{iddots}{\\usepackage{mathdots}}\n"])
+      return
+
+
+def convert_rule(document):
+    " Convert \\lyxline to CommandInset line "
+    i = 0
+    while True:
+      i = find_token(document.body, "\\lyxline" , i)
+      if i != -1:
+        j = find_token(document.body, "\\color" , i - 2)
+        if j == i - 2:
+          color = document.body[j] + '\n'
+        else:
+          color = ''
+        k = find_token(document.body, "\\begin_layout Standard" , i - 4)
+        # we need to handle the case that \lyxline is in a separate paragraph and that it is colored
+        # the result is then an extra empty paragraph which we get by adding an empty ERT inset
+        if k == i - 4 and j == i - 2 and document.body[i - 1] == '':
+          layout = '\\begin_inset ERT\nstatus collapsed\n\n\\begin_layout Plain Layout\n\n\n\\end_layout\n\n\\end_inset\n' \
+            + '\\end_layout\n\n' \
+            + '\\begin_layout Standard\n'
+        elif k == i - 2 and document.body[i - 1] == '':
+          layout = ''
+        else:
+          layout = '\\end_layout\n\n' \
+            + '\\begin_layout Standard\n'
+        l = find_token(document.body, "\\begin_layout Standard" , i + 4)
+        if l == i + 4 and document.body[i + 1] == '':
+          layout2 = ''
+        else:
+          layout2 = '\\end_layout\n' \
+            + '\n\\begin_layout Standard\n'
+        subst = layout \
+          + '\\noindent\n\n' \
+          + color \
+          + '\\begin_inset CommandInset line\n' \
+          + 'LatexCommand rule\n' \
+          + 'offset "0.5ex"\n' \
+          + 'width "100line%"\n' \
+          + 'height "1pt"\n' \
+          + '\n\\end_inset\n\n\n' \
+          + layout2
+        document.body[i] = subst
+        i += 1
+      else:
+        return
+
+
+def revert_rule(document):
+    " Revert line insets to Tex code "
+    i = 0
+    while 1:
+      i = find_token(document.body, "\\begin_inset CommandInset line" , i)
+      if i != -1:
+        # find end of inset
+        j = find_token(document.body, "\\end_inset" , i)
+        # assure we found the end_inset of the current inset
+        if j > i + 6 or j == -1:
+          document.warning("Malformed LyX document: Can't find end of line inset.")
+          return
+        # determine the optional offset
+        k = find_token(document.body, 'offset', i)
+        if k != -1 and k < j:
+          offset = document.body[k][8:]
+        else:
+          offset = '0"'
+        # determine the width
+        l = find_token(document.body, 'width', k + 1)
+        width = document.body[l][7:]
+        # determine the height
+        m = find_token(document.body, 'height', l + 1)
+        height = document.body[m][8:]
+        # remove trailing '"'
+        offset = offset[:-1]
+        width = width[:-1]
+        height = height[:-1]
+        # output the \rule command
+        if offset <> "0":
+          subst = "\\rule[" + offset + "]{" + width + "}{" + height + "}"
+        else:
+          subst = "\\rule{" + width + "}{" + height + "}"
+        document.body[i:j + 1] = put_cmd_in_ert(subst)
+        i += 1
+      else:
+        return
+
+def revert_diagram(document):
+  " Add the feyn package if \\Diagram is used in math "
+  i = 0
+  re_diagram = re.compile(r'\\begin_inset Formula .*\\Diagram', re.DOTALL)
+  while True:
+    i = find_token(document.body, '\\begin_inset Formula', i)
+    if i == -1:
+      return
+    j = find_end_of_inset(document.body, i)
+    if j == -1:
+        document.warning("Malformed LyX document: Can't find end of Formula inset.")
+        return 
+    m = re_diagram.search("\n".join(document.body[i:j]))
+    if not m:
+      i += 1
+      continue
+    add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+    add_to_preamble(document, "\\usepackage{feyn}")
+    # only need to do it once!
+    return
+
+
+
 ##
 # Conversion hub
 #
@@ -2114,10 +2260,16 @@ convert = [[346, []],
            [395, []],
            [396, []],
            [397, [remove_Nameref]],
-           [398, []]
+           [398, []],
+           [399, [convert_mathdots]],
+           [400, [convert_rule]],
+           [401, []]
           ]
 
-revert =  [[397, [revert_mathrsfs]],
+revert =  [[400, [revert_diagram]],
+           [399, [revert_rule]],
+           [398, [revert_mathdots]],
+           [397, [revert_mathrsfs]],
            [396, []],
            [395, [revert_nameref]],
            [394, [revert_DIN_C_pagesizes]],