Author: rgheck
Date: Fri Nov  5 18:54:57 2010
New Revision: 36133
URL: http://www.lyx.org/trac/changeset/36133

Log:
Improve how add_to_preamble and insert_to_preamble work, and audit the
various calls to each.

This should more or less complete the lyx2lyx orgy. Some things probably
will have been broken by accident. Hopefully not too many, and more
things that were broken got fixed.

Modified:
   lyx-devel/trunk/lib/lyx2lyx/lyx2lyx_tools.py
   lyx-devel/trunk/lib/lyx2lyx/lyx_2_0.py

Modified: lyx-devel/trunk/lib/lyx2lyx/lyx2lyx_tools.py
==============================================================================
--- lyx-devel/trunk/lib/lyx2lyx/lyx2lyx_tools.py        Fri Nov  5 18:39:21 
2010        (r36132)
+++ lyx-devel/trunk/lib/lyx2lyx/lyx2lyx_tools.py        Fri Nov  5 18:54:57 
2010        (r36133)
@@ -27,12 +27,15 @@
   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
@@ -89,12 +92,13 @@
       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 @@
       # 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
 
 

Modified: lyx-devel/trunk/lib/lyx2lyx/lyx_2_0.py
==============================================================================
--- lyx-devel/trunk/lib/lyx2lyx/lyx_2_0.py      Fri Nov  5 18:39:21 2010        
(r36132)
+++ lyx-devel/trunk/lib/lyx2lyx/lyx_2_0.py      Fri Nov  5 18:54:57 2010        
(r36133)
@@ -297,7 +297,7 @@
         pretext.append('\\defaultfontfeatures{Numbers=OldStyle}')
     pretext.append('\usepackage{xunicode}')
     pretext.append('\usepackage{xltxtra}')
-    insert_to_preamble(0, document, pretext)
+    insert_to_preamble(document, pretext)
 
 
 def revert_outputformat(document):
@@ -320,8 +320,8 @@
     red   = hex2ratio(colorcode[1:3])
     green = hex2ratio(colorcode[3:5])
     blue  = hex2ratio(colorcode[5:7])
-    insert_to_preamble(0, document, \
-        ['% Commands inserted by lyx2lyx to set the background color',
+    insert_to_preamble(document, \
+        ['% To set the background color',
         '\...@ifundefined{definecolor}{\\usepackage{color}}{}',
         '\\definecolor{page_backgroundcolor}{rgb}{' + red + ',' + green + ',' 
+ blue + '}',
         '\\pagecolor{page_backgroundcolor}'])
@@ -359,7 +359,7 @@
               preamble.append("\\newindex[" + iname + "]{" + ishortcut + "}")
         del document.header[i:k + 1]
     if preamble:
-        insert_to_preamble(0, document, preamble)
+        insert_to_preamble(document, preamble)
         
     # deal with index insets
     # these need to have the argument removed
@@ -487,8 +487,8 @@
   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(0, document, \
-        ['% Commands inserted by lyx2lyx for proper underlining',
+    insert_to_preamble(document, \
+        ['%  for proper underlining',
         '\\PassOptionsToPackage{normalem}{ulem}',
         '\\usepackage{ulem}'])
 
@@ -498,8 +498,8 @@
     i = find_token(document.body, '\\bar under', 0)
     if i == -1:
         return
-    insert_to_preamble(0, document,\
-            ['% Commands inserted by lyx2lyx for proper underlining',
+    insert_to_preamble(document,\
+            ['%  for proper underlining',
             '\\PassOptionsToPackage{normalem}{ulem}',
             '\\usepackage{ulem}',
             '\\let\\c...@rig\\cite',
@@ -557,8 +557,7 @@
         continue
       width = get_quoted_value(document.body, "width", i, j)
       del document.body[l]
-      add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
-      add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + 
"}"])
+      insert_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + 
"}"])
       i = j - 1
 
 
@@ -621,8 +620,7 @@
     if length != "default":
       # handle percent lengths
       length = latex_length(length)[1]
-      add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
-      add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
+      insert_to_preamble(document, ["\\setlength{\\parindent}{" + length + 
"}"])
     del document.header[i]
 
 
@@ -639,8 +637,7 @@
     # handle percent lengths
     percent, length = latex_length(length)
     if percent:
-        add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
-        add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
+        insert_to_preamble(document, ["\\setlength{\\parskip}{" + length + 
"}"])
         # set defskip to medskip as default
         document.header[i] = "\\defskip medskip"
 
@@ -815,7 +812,6 @@
     # when suppress_date was true
     date = str2bool(get_value(document.header, "\\suppress_date", i))
     if date:
-        add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
         add_to_preamble(document, ["\\date{}"])
     del document.header[i]
 
@@ -853,10 +849,9 @@
             i += 1
 
     if mhchem == "on":
-        pre = ["% lyx2lyx mhchem commands", 
-          "\\PassOptionsToPackage{version=3}{mhchem}", 
+        pre = ["\\PassOptionsToPackage{version=3}{mhchem}", 
           "\\usepackage{mhchem}"]
-        add_to_preamble(document, pre) 
+        insert_to_preamble(document, pre) 
 
 
 def revert_fontenc(document):
@@ -1001,8 +996,7 @@
       i = cend
 
     if multirow == True:
-        add_to_preamble(document, 
-          ["% lyx2lyx multirow additions ", "\\usepackage{multirow}"])
+        add_to_preamble(document, ["\\usepackage{multirow}"])
 
 
 def convert_math_output(document):
@@ -1147,7 +1141,7 @@
           i = j + 1
   
     if has_equal_spacing and not has_preamble:
-        add_to_preamble(document, ['% lyx2lyx xymatrix addition', 
'\\usepackage[all]{xy}'])
+        add_to_preamble(document, ['\\usepackage[all]{xy}'])
 
 
 def revert_notefontcolor(document):
@@ -1170,9 +1164,8 @@
     green = hex2ratio(colorcode[3:5])
     blue = hex2ratio(colorcode[5:7])
     # write the preamble
-    insert_to_preamble(0, document,
-      ['% Commands inserted by lyx2lyx to set the font color',
-        '% for greyed-out notes',
+    insert_to_preamble(document,
+      [ '%  for greyed-out notes',
         '\...@ifundefined{definecolor}{\\usepackage{color}}{}'
         '\\definecolor{note_fontcolor}{rgb}{%s,%s,%s}' % (red, green, blue),
         '\\renewenvironment{lyxgreyedout}',
@@ -1212,8 +1205,8 @@
     green = hex2ratio(colorcode[3:5])
     blue = hex2ratio(colorcode[5:7])
     # write the preamble
-    insert_to_preamble(0, document,
-      ['% Commands inserted by lyx2lyx to set the font color',
+    insert_to_preamble(document,
+      ['%  Set the font color',
       '\...@ifundefined{definecolor}{\\usepackage{color}}{}',
       '\\definecolor{document_fontcolor}{rgb}{%s,%s,%s}' % (red, green, blue),
       '\\color{document_fontcolor}'])
@@ -1231,8 +1224,8 @@
     green = hex2ratio(colorcode[3:5])
     blue = hex2ratio(colorcode[5:7])
     # write the preamble
-    insert_to_preamble(0, document,
-      ['% Commands inserted by lyx2lyx to set the color of boxes with shaded 
background',
+    insert_to_preamble(document,
+      ['%  Set the color of boxes with shaded background',
       '\...@ifundefined{definecolor}{\\usepackage{color}}{}',
       "\\definecolor{shadecolor}{rgb}{%s,%s,%s}" % (red, green, blue)])
 
@@ -1610,7 +1603,7 @@
       document.body[stins:endins + 1] = newcontent
 
   if foundone:
-    add_to_preamble(document, "\usepackage{nameref}")
+    add_to_preamble(document, ["\usepackage{nameref}"])
 
 
 def remove_Nameref(document):
@@ -1638,7 +1631,7 @@
     i = 0
     for line in document.body:
       if line.find("\\mathscr{") != -1:
-        add_to_preamble(document, ["% lyx2lyx mathrsfs addition", 
"\\usepackage{mathrsfs}"])
+        add_to_preamble(document, ["\\usepackage{mathrsfs}"])
         return
 
 
@@ -1770,7 +1763,7 @@
         return
       if usedots == 2:
         # force load case
-        add_to_preamble(["% lyx2lyx mathdots addition", 
"\\usepackage{mathdots}"])
+        add_to_preamble(["\\usepackage{mathdots}"])
         return
     
     # so we are in the auto case. we want to load mathdots if \iddots is used.
@@ -1786,8 +1779,7 @@
         continue
       code = "\n".join(document.body[i:j])
       if code.find("\\iddots") != -1:
-        add_to_preamble(document, ["% lyx2lyx mathdots addition", 
-        "\...@ifundefined{iddots}{\\usepackage{mathdots}}"])
+        add_to_preamble(document, 
["\...@ifundefined{iddots}{\\usepackage{mathdots}}"])
         return
       i = j
 
@@ -1890,7 +1882,7 @@
     if lines.find("\\Diagram") == -1:
       i = j
       continue
-    add_to_preamble(document, ["% lyx2lyx feyn package insertion ", 
"\\usepackage{feyn}"])
+    add_to_preamble(document, ["\\usepackage{feyn}"])
     # only need to do it once!
     return
 

Reply via email to