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.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@36133 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2010-11-05 17:54:57 +00:00
parent 86cff08734
commit fb64f0e1af
2 changed files with 35 additions and 38 deletions

View File

@ -27,12 +27,15 @@ add_to_preamble(document, text):
we will handle that properly. we will handle that properly.
The routine checks to see whether the provided material is The routine checks to see whether the provided material is
already in the preamble. If not, it adds it. 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 Here, text can be either a single line or a list of lines. It
is bad practice to pass something with embedded newlines, but is bad practice to pass something with embedded newlines, but
we will handle that properly. 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): put_cmd_in_ert(arg):
Here arg should be a list of strings (lines), which we want to Here arg should be a list of strings (lines), which we want to
@ -89,12 +92,13 @@ def add_to_preamble(document, text):
if matched: if matched:
return return
document.preamble.extend("% Added by lyx2lyx")
document.preamble.extend(text) document.preamble.extend(text)
# Note that text can be either a list of lines or a single line. # Note that text can be either a list of lines or a single line.
# It should really be a list. # 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""" """ Insert text to the preamble at a given line"""
if not type(text) is list: 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 # it'll give us the one element list we want
# if there's no \n, too # if there's no \n, too
text = text.split('\n') text = text.split('\n')
text.insert(0, "% Added by lyx2lyx")
document.preamble[index:index] = text document.preamble[index:index] = text

View File

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