document.preamble.extend(text)
+def insert_to_preamble(index, document, text):
+ """ Insert text to the preamble at a given line"""
+
+ document.preamble.insert(index, text)
+
# Convert a LyX length into a LaTeX length
def convert_len(len):
units = {"text%":"\\backslash\ntextwidth", "col%":"\\backslash\ncolumnwidth",
return retval
+#Bug 5022....
+#Might should do latex2ert first, then deal with stuff that DOESN'T
+#end up inside ERT. That routine could be modified so that it returned
+#a list of lines, and we could then skip ERT bits and only deal with
+#the other bits.
def latex2lyx(data):
'''Takes a string, possibly multi-line, and returns the result of
converting LaTeX constructs into LyX constructs. Returns a list of
return retval
-def lyx2latex(lines):
+def lyx2latex(document, lines):
'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
# clean up multiline stuff
content = ""
#skip all that stuff
continue
+ # this needs to be added to the preamble because of cases like
+ # \textmu, \textbackslash, etc.
+ add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
+ '\\@ifundefined{textmu}',
+ ' {\\usepackage{textcomp}}{}'])
# a lossless reversion is not possible
# try at least to handle some common insets and settings
- # do not replace inside ERTs
if ert_end >= curline:
line = line.replace(r'\backslash', r'\\')
else:
+ line = line.replace('&', '\\&{}')
+ line = line.replace('#', '\\#{}')
+ line = line.replace('^', '\\^{}')
+ line = line.replace('%', '\\%{}')
+ line = line.replace('_', '\\_{}')
+ line = line.replace('$', '\\${}')
+
# Do the LyX text --> LaTeX conversion
for rep in reps:
- line = line.replace(rep[1], rep[0])
+ line = line.replace(rep[1], rep[0] + "{}")
line = line.replace(r'\backslash', r'\textbackslash{}')
line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
setupstart = ""
setupend = ""
# write the preamble
- add_to_preamble(document,
- ['% Commands inserted by lyx2lyx for PDF properties',
- '\\usepackage[unicode=true'
+ # babel must be loaded before hyperref and hyperref the first part
+ # of the preamble, like in LyX 1.6
+ insert_to_preamble(0, document,
+ '% Commands inserted by lyx2lyx for PDF properties\n'
+ + '\\usepackage{babel}\n'
+ + '\\usepackage[unicode=true'
+ bookmarks
+ breaklinks
+ pdfborder
+ colorlinks
+ pagemode
+ ']\n'
- ' {hyperref}\n'
+ + ' {hyperref}\n'
+ setupstart
+ title
+ author
+ subject
+ keywords
+ otheroptions
- + setupend])
+ + setupend)
def remove_inzip_options(document):
return
if document.body[i + 1] != "LatexCommand index": # Might also be index_print
return
+ j = find_end_of_inset(document.body, i + 2)
+ if j == -1:
+ document.warning("Unable to find end of index inset at line " + i + "!")
+ i += 2
+ continue
m = r1.match(document.body[i + 2])
if m == None:
document.warning("Unable to match: " + document.body[i+2])
i += 1
continue
fullcontent = m.group(1)
- #document.warning(fullcontent)
- document.body[i:i + 3] = ["\\begin_inset Index",
- "status collapsed",
- "\\begin_layout Standard"]
- i += 3
- # We are now on the blank line preceding "\end_inset"
- # We will write the content here, into the inset.
-
linelist = latex2lyx(fullcontent)
- document.body[i+1:i+1] = linelist
- i += len(linelist)
+ #document.warning(fullcontent)
- document.body.insert(i + 1, "\\end_layout")
- i += 1
+ linelist = ["\\begin_inset Index", "status collapsed", "\\begin_layout Standard", ""] + \
+ linelist + ["\\end_layout"]
+ document.body[i : j] = linelist
+ i += len(linelist) - (j - i)
def revert_latexcommand_index(document):
if j == -1:
return
- content = lyx2latex(document.body[i:j])
+ content = lyx2latex(document, document.body[i:j])
# escape quotes
content = content.replace('"', r'\"')
document.body[i:j] = ["\\begin_inset CommandInset index", "LatexCommand index",