# do not replace inside ERTs
if ert_end < k:
line = line.replace(u'ä', r'\\\"a').replace(u'ö', r'\\\"o').replace(u'ü', r'\\\"u')
- 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{}')
- line = line.replace(r'\\shape slanted', r'\\slshape{}').replace(r'\\shape default', r'\\upshape{}')
- line = line.replace(r'\\emph on', r'\\em{}').replace(r'\\emph default', r'\\em{}')
- line = line.replace(r'\\noun on', r'\\scshape{}').replace(r'\\noun default', r'\\upshape{}')
- line = line.replace(r'\\bar under', r'\\underbar{').replace(r'\\bar default', r'}')
- line = line.replace(r'\\family sans', r'\\sffamily{}').replace(r'\\family default', r'\\normalfont{}')
- line = line.replace(r'\\family typewriter', r'\\ttfamily{}').replace(r'\\family roman', r'\\rmfamily{}')
- line = line.replace(r'\\InsetSpace ', r'').replace(r'\\SpecialChar ', r'')
+ 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{}')
+ line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
+ line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
+ line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
+ line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
+ line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
+ line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
+ line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
else:
- line = line.replace(r'\\backslash', r'\\')
+ line = line.replace(r'\backslash', r'\\')
content = content + line;
document.body[i + 3] = "name " + '"' + content + '"'
for k in range(i + 4, j - 2):
del document.body[i+1:k]
else:
document.body[i] = document.body[i].replace("\\begin_inset Box Shaded", "\\begin_inset Box Frameless")
- document.body.insert(l + 1, "\\begin_inset Note Shaded\n" + status + "\n\\begin_layout Standard\n")
- document.body.insert(m + 1, "\\end_layout\n\\end_inset")
+ subst1 = [document.body[l],
+ "\\begin_inset Note Shaded",
+ status,
+ '\\begin_layout Standard']
+ document.body[l:l + 1] = subst1
+ subst2 = [document.body[m], "\\end_layout", "\\end_inset"]
+ document.body[m:m + 1] = subst2
i = i + 1
def revert_slash(document):
'Revert \\SpecialChar \\slash{} to ERT'
- for i in range(len(document.body)):
- document.body[i] = document.body[i].replace('\\SpecialChar \\slash{}', \
- '\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Standard\n\n\n\\backslash\n' \
- 'slash{}\n\\end_layout\n\n\\end_inset\n\n')
+ r = re.compile(r'\\SpecialChar \\slash{}')
+ i = 0
+ while i < len(document.body):
+ m = r.match(document.body[i])
+ if m:
+ subst = ['\\begin_inset ERT',
+ 'status collapsed', '',
+ '\\begin_layout Standard',
+ '', '', '\\backslash',
+ 'slash{}',
+ '\\end_layout', '',
+ '\\end_inset', '']
+ document.body[i: i+1] = subst
+ i = i + len(subst)
+ else:
+ i = i + 1
def revert_nobreakdash(document):
'Revert \\SpecialChar \\nobreakdash- to ERT'
- found = 0
- for i in range(len(document.body)):
+ i = 0
+ while i < len(document.body):
line = document.body[i]
r = re.compile(r'\\SpecialChar \\nobreakdash-')
m = r.match(line)
if m:
- found = 1
- document.body[i] = document.body[i].replace('\\SpecialChar \\nobreakdash-', \
- '\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout Standard\n\n\n\\backslash\n' \
- 'nobreakdash-\n\\end_layout\n\n\\end_inset\n\n')
- if not found:
- return
- j = find_token(document.header, "\\use_amsmath", 0)
- if j == -1:
- document.warning("Malformed LyX document: Missing '\\use_amsmath'.")
- return
- document.header[j] = "\\use_amsmath 2"
+ subst = ['\\begin_inset ERT',
+ 'status collapsed', '',
+ '\\begin_layout Standard', '', '',
+ '\\backslash',
+ 'nobreakdash-',
+ '\\end_layout', '',
+ '\\end_inset', '']
+ document.body[i:i+1] = subst
+ i = i + len(subst)
+ j = find_token(document.header, "\\use_amsmath", 0)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing '\\use_amsmath'.")
+ return
+ document.header[j] = "\\use_amsmath 2"
+ else:
+ i = i + 1
def revert_nocite_key(body, start, end):
- 'key "..." -> \nocite{...}'
- for i in range(start, end):
- if (body[i][0:5] == 'key "'):
- body[i] = body[i].replace('key "', "\\backslash\nnocite{")
- body[i] = body[i].replace('"', "}")
+ 'key "..." -> \nocite{...}'
+ r = re.compile(r'^key "(.*)"')
+ i = start
+ j = end
+ while i <= j:
+ m = r.match(body[i])
+ if m:
+ body[i:i+1] = ["\\backslash", "nocite{" + m.group(1) + "}"]
+ j = j + 1 # because we added a line
+ i = i + 2 # skip that line
else:
body[i] = ""
+ i = i + 1
+ return j - end # how many lines we added
def revert_nocite(document):
if i == -1:
return
i = i + 1
- if (document.body[i] == "LatexCommand nocite"):
- j = find_end_of_inset(document.body, i + 1)
- if j == -1:
- #this should not happen
- document.warning("End of CommandInset citation not found in revert_nocite!")
- revert_nocite_key(document.body, i + 1, len(document.body))
- return
- revert_nocite_key(document.body, i + 1, j)
- document.body[i-1] = "\\begin_inset ERT"
- document.body[i] = "status collapsed\n\n" \
- "\\begin_layout Standard"
- document.body.insert(j, "\\end_layout\n");
- i = j
+ if (document.body[i] != "LatexCommand nocite"):
+ # note that we already incremented i
+ continue
+ j = find_end_of_inset(document.body, i + 1)
+ if j == -1:
+ #this should not happen
+ document.warning("End of CommandInset citation not found in revert_nocite!")
+ return
+ # NOTE The order of these is important.
+ document.body[i-1] = "\\begin_inset ERT"
+ # Do this before we insert lines before j+1
+ document.body[j+1:j+1] = ["\\end_layout", ""]
+ # Do this before we insert lines before i+1
+ addedlines = revert_nocite_key(document.body, i + 1, j)
+ # Nothing has been inserted before this one
+ document.body[i:i+1] = ["status collapsed", "", "\\begin_layout Standard"]
+ i = j + 3 + addedlines
def revert_btprintall(document):
for k in range(i, j):
if (document.body[k] == 'btprint "btPrintAll"'):
del document.body[k]
- document.body.insert(i, "\\begin_inset ERT\n" \
- "status collapsed\n\n\\begin_layout Standard\n\n" \
- "\\backslash\nnocite{*}\n" \
- "\\end_layout\n\\end_inset\n")
+ subst = ["\\begin_inset ERT",
+ "status collapsed", "",
+ "\\begin_layout Standard", "",
+ "\\backslash",
+ "nocite{*}",
+ "\\end_layout",
+ "\\end_inset"]
+ document.body[i:i] = subst
i = j
if floattype == "algorithm":
add_to_preamble(document,
['% Commands inserted by lyx2lyx for sideways algorithm float',
- '\\usepackage{rotfloat}\n'
- '\\floatstyle{ruled}\n'
- '\\newfloat{algorithm}{tbp}{loa}\n'
- '\\floatname{algorithm}{Algorithm}\n'])
+ '\\usepackage{rotfloat}',
+ '\\floatstyle{ruled}',
+ '\\newfloat{algorithm}{tbp}{loa}',
+ '\\floatname{algorithm}{Algorithm}'])
else:
document.warning("Cannot create preamble definition for custom float" + floattype + ".")
i = i + 1
def convert_spaceinset(document):
- " Convert '\\InsetSpace foo' to '\\begin_inset Space foo\n\\end_inset' "
- for i in range(len(document.body)):
- m = re.match(r'(.*)\\InsetSpace (.*)', document.body[i])
- if m:
- before = m.group(1)
- after = m.group(2)
- subst = [before, "\\begin_inset Space " + after, "\\end_inset"]
- document.body[i: i+1] = subst
+ " Convert '\\InsetSpace foo' to '\\begin_inset Space foo\n\\end_inset' "
+ i = 0
+ while i < len(document.body):
+ m = re.match(r'(.*)\\InsetSpace (.*)', document.body[i])
+ if m:
+ before = m.group(1)
+ after = m.group(2)
+ subst = [before, "\\begin_inset Space " + after, "\\end_inset"]
+ document.body[i: i+1] = subst
+ i = i + len(subst)
+ else:
+ i = i + 1
def revert_spaceinset(document):