]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx2lyx_tools.py
Don't use widest label for numerical citations.
[lyx.git] / lib / lyx2lyx / lyx2lyx_tools.py
index 35da97f643ab0b4b171f02a90898829c8346d66b..1edd1fbfc85f28c26de2cae4ed08d3f794bf6f9e 100644 (file)
@@ -1,6 +1,6 @@
 # This file is part of lyx2lyx
 # -*- coding: utf-8 -*-
-# Copyright (C) 2010 The LyX team
+# Copyright (C) 2011 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
@@ -14,7 +14,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 '''
 This modules offer several free functions to help with lyx2lyx'ing. 
@@ -27,12 +27,15 @@ add_to_preamble(document, text):
   we will handle that properly.
   The routine checks to see whether the provided material is
   already in the preamble. If not, it adds it.
+  Prepends a comment "% Added by lyx2lyx" to text.
 
-insert_to_preamble(index, document, text):
+insert_to_preamble(document, text[, index]):
   Here, text can be either a single line or a list of lines. It
   is bad practice to pass something with embedded newlines, but
   we will handle that properly.
-  The routine inserts text at document.preamble[index].
+  The routine inserts text at document.preamble[index], where by
+  default index is 0, so the material is inserted at the beginning.
+  Prepends a comment "% Added by lyx2lyx" to text.
 
 put_cmd_in_ert(arg):
   Here arg should be a list of strings (lines), which we want to
@@ -57,12 +60,12 @@ latex_length(slen):
 '''
 
 import string
-from parser_tools import find_token
+from parser_tools import find_token, find_end_of_inset
 from unicode_symbols import unicode_reps
 
 
 # This will accept either a list of lines or a single line.
-# It is bad practice to pass something with embedded newlines, 
+# It is bad practice to pass something with embedded newlines,
 # though we will handle that.
 def add_to_preamble(document, text):
     " Add text to the preamble if it is not already there. "
@@ -89,12 +92,13 @@ def add_to_preamble(document, text):
       if matched:
         return
 
+    document.preamble.extend(["% Added by lyx2lyx"])
     document.preamble.extend(text)
 
 
 # Note that text can be either a list of lines or a single line.
 # It should really be a list.
-def insert_to_preamble(index, document, text):
+def insert_to_preamble(document, text, index = 0):
     """ Insert text to the preamble at a given line"""
     
     if not type(text) is list:
@@ -102,7 +106,8 @@ def insert_to_preamble(index, document, text):
       # it'll give us the one element list we want
       # if there's no \n, too
       text = text.split('\n')
-
+    
+    text.insert(0, "% Added by lyx2lyx")
     document.preamble[index:index] = text
 
 
@@ -112,7 +117,7 @@ def put_cmd_in_ert(arg):
     Returns a list of strings, with the lines so wrapped.
     '''
     
-    ret = ["\\begin_inset ERT", "status collapsed", "\\begin_layout Plain Layout", ""]
+    ret = ["\\begin_inset ERT", "status collapsed", "", "\\begin_layout Plain Layout", ""]
     # It will be faster for us to work with a single string internally. 
     # That way, we only go through the unicode_reps loop once.
     if type(arg) is list:
@@ -123,10 +128,41 @@ def put_cmd_in_ert(arg):
       s = s.replace(rep[1], rep[0].replace('\\\\', '\\'))
     s = s.replace('\\', "\\backslash\n")
     ret += s.splitlines()
-    ret += ["\\end_layout", "\\end_inset"]
+    ret += ["\\end_layout", "", "\\end_inset"]
+    return ret
+
+
+def get_ert(lines, i):
+    'Convert an ERT inset into LaTeX.'
+    if not lines[i].startswith("\\begin_inset ERT"):
+        return ""
+    j = find_end_of_inset(lines, i)
+    if j == -1:
+        return ""
+    while i < j and not lines[i].startswith("status"):
+        i = i + 1
+    i = i + 1
+    ret = ""
+    first = True
+    while i < j:
+        if lines[i] == "\\begin_layout Plain Layout":
+            if first:
+                first = False
+            else:
+                ret = ret + "\n"
+            while i + 1 < j and lines[i+1] == "":
+                i = i + 1
+        elif lines[i] == "\\end_layout":
+            while i + 1 < j and lines[i+1] == "":
+                i = i + 1
+        elif lines[i] == "\\backslash":
+            ret = ret + "\\"
+        else:
+            ret = ret + lines[i]
+        i = i + 1
     return ret
 
-            
+
 def lyx2latex(document, lines):
     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'