def wrap_into_ert(string, src, dst):
" Wrap a something into an ERT"
- return string.replace(src, '\n\\begin_inset ERT\nstatus collapsed\n\\begin_layout Standard\n'
+ return string.replace(src, '\n\\begin_inset ERT\nstatus collapsed\n\\begin_layout Standard\n'
+ dst + '\n\\end_layout\n\\end_inset\n')
def add_to_preamble(document, text):
####################################################################
+def get_option(document, m, option, default):
+ l = document.body[m].find(option)
+ val = default
+ if l != -1:
+ val = document.body[m][l:].split('"')[1]
+ return val
+
+def remove_option(document, m, option):
+ l = document.body[m].find(option)
+ if l != -1:
+ val = document.body[m][l:].split('"')[1]
+ document.body[m] = document.body[m][:l-1] + document.body[m][l+len(option + '="' + val + '"'):]
+ return l
+
+def set_option(document, m, option, value):
+ l = document.body[m].find(option)
+ if l != -1:
+ oldval = document.body[m][l:].split('"')[1]
+ l = l + len(option + '="')
+ document.body[m] = document.body[m][:l] + value + document.body[m][l+len(oldval):]
+ else:
+ document.body[m] = document.body[m][:-1] + ' ' + option + '="' + value + '">'
+ return l
+
+def convert_tablines(document):
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Tabular", i)
+ if i == -1:
+ # LyX 1.3 inserted an extra space between \begin_inset
+ # and Tabular so let us try if this is the case and fix it.
+ i = find_token(document.body, "\\begin_inset Tabular", i)
+ if i == -1:
+ return
+ else:
+ document.body[i] = "\\begin_inset Tabular"
+ j = find_end_of_inset(document.body, i + 1)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of tabular.")
+ continue
+
+ m = i + 1
+ nrows = int(document.body[i+1].split('"')[3])
+ ncols = int(document.body[i+1].split('"')[5])
+
+ col_info = []
+ for k in range(ncols):
+ m = find_token(document.body, "<column", m)
+ left = get_option(document, m, 'leftline', 'false')
+ right = get_option(document, m, 'rightline', 'false')
+ col_info.append([left, right])
+ remove_option(document, m, 'leftline')
+ remove_option(document, m, 'rightline')
+ m = m + 1
+
+ row_info = []
+ for k in range(nrows):
+ m = find_token(document.body, "<row", m)
+ top = get_option(document, m, 'topline', 'false')
+ bottom = get_option(document, m, 'bottomline', 'false')
+ row_info.append([top, bottom])
+ remove_option(document, m, 'topline')
+ remove_option(document, m, 'bottomline')
+ m = m + 1
+
+ m = i + 1
+ mc_info = []
+ for k in range(nrows*ncols):
+ m = find_token(document.body, "<cell", m)
+ mc_info.append(get_option(document, m, 'multicolumn', '0'))
+ m = m + 1
+ m = i + 1
+ for l in range(nrows):
+ for k in range(ncols):
+ m = find_token(document.body, '<cell', m)
+ if mc_info[l*ncols + k] == '0':
+ r = set_option(document, m, 'topline', row_info[l][0])
+ r = set_option(document, m, 'bottomline', row_info[l][1])
+ r = set_option(document, m, 'leftline', col_info[k][0])
+ r = set_option(document, m, 'rightline', col_info[k][1])
+ elif mc_info[l*ncols + k] == '1':
+ s = k + 1
+ while s < ncols and mc_info[l*ncols + s] == '2':
+ s = s + 1
+ if s < ncols and mc_info[l*ncols + s] != '1':
+ r = set_option(document, m, 'rightline', col_info[k][1])
+ if k > 0 and mc_info[l*ncols + k - 1] == '0':
+ r = set_option(document, m, 'leftline', col_info[k][0])
+ m = m + 1
+ i = j + 1
+
+
+def revert_tablines(document):
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Tabular", i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i + 1)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of tabular.")
+ continue
+
+ m = i + 1
+ nrows = int(document.body[i+1].split('"')[3])
+ ncols = int(document.body[i+1].split('"')[5])
+
+ lines = []
+ for k in range(nrows*ncols):
+ m = find_token(document.body, "<cell", m)
+ top = get_option(document, m, 'topline', 'false')
+ bottom = get_option(document, m, 'bottomline', 'false')
+ left = get_option(document, m, 'leftline', 'false')
+ right = get_option(document, m, 'rightline', 'false')
+ lines.append([top, bottom, left, right])
+ m = m + 1
+
+ m = i + 1
+ col_info = []
+ for k in range(ncols):
+ m = find_token(document.body, "<column", m)
+ left = 'true'
+ for l in range(nrows):
+ left = lines[k*ncols + k][2]
+ if left == 'false':
+ break
+ set_option(document, m, 'leftline', left)
+ right = 'true'
+ for l in range(nrows):
+ right = lines[k*ncols + k][3]
+ if right == 'false':
+ break
+ set_option(document, m, 'rightline', right)
+ m = m + 1
+
+ row_info = []
+ for k in range(nrows):
+ m = find_token(document.body, "<row", m)
+ top = 'true'
+ for l in range(ncols):
+ top = lines[k*ncols + l][0]
+ if top == 'false':
+ break
+ set_option(document, m, 'topline', top)
+ bottom = 'true'
+ for l in range(ncols):
+ bottom = lines[k*ncols + l][1]
+ if bottom == 'false':
+ break
+ set_option(document, m, 'bottomline', bottom)
+ m = m + 1
+
+ i = j + 1
+
+
def fix_wrong_tables(document):
i = 0
while True:
document.warning("Malformed LyX document: show_label neither false nor true.")
else:
document.warning("Malformed LyX document: show_label missing in CharStyle.")
-
+
i += 1
def convert_inset_command(document):
"""
Convert:
- \begin_inset LatexCommand cmd
- to
+ \begin_inset LatexCommand cmd
+ to
\begin_inset CommandInset InsetType
LatexCommand cmd
"""
Convert:
\begin_inset CommandInset InsetType
LatexCommand cmd
- to
- \begin_inset LatexCommand cmd
+ to
+ \begin_inset LatexCommand cmd
Some insets may end up being converted to insets earlier versions of LyX
will not be able to recognize. Not sure what to do about that.
"""
def convert_latexcommand_index(document):
"Convert from LatexCommand form to collapsable form."
- i = 0
+ i = 0
while True:
i = find_token(document.body, "\\begin_inset CommandInset index", i)
if i == -1:
document.header[j] = "\\inputencoding JIS"
k = 0
k = find_token(document.header, "\\inputencoding SJIS-plain", 0)
- if k != -1: # convert to UTF8 since there is currently no SJIS encoding
+ if k != -1: # convert to UTF8 since there is currently no SJIS encoding
document.header[k] = "\\inputencoding UTF8"
i = k
continue
newstuff = ["\\begin_inset Flex URL",
- "status collapsed", "",
+ "status collapsed", "",
"\\begin_layout Standard",
"",
target,
cmd = m.group(1)
fn = m.group(2)
opt = m.group(3)
- insertion = ["\\begin_inset CommandInset include",
+ insertion = ["\\begin_inset CommandInset include",
"LatexCommand " + cmd, previewline,
"filename \"" + fn + "\""]
newlines = 2
i = i + 1
continue
l = find_token(document.body, '\tsubcaptionText', i, j)
- caption = get_value(document.body, '\tsubcaptionText', i, j).strip('"')
+ caption = document.body[l][16:].strip('"')
savestr = document.body[i]
+ del document.body[l]
+ del document.body[k]
document.body[i] = '\\begin_inset Float figure\nwide false\nsideways false\n' \
'status open\n\n\\begin_layout PlainLayout\n\\begin_inset Caption\n\n\\begin_layout PlainLayout\n' \
+ caption + '\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout PlainLayout\n' + savestr
savestr = document.body[j]
document.body[j] = '\n\\end_layout\n\n\\end_inset\n' + savestr
- del document.body[k]
- del document.body[l]
def revert_subfig(document):
def revert_wrapplacement(document):
- "Revert placement options wrap floats (wrapfig)."
+ " Revert placement options wrap floats (wrapfig). "
i = 0
while True:
i = find_token(document.body, "lines", i)
i = i + 1
+def remove_extra_embedded_files(document):
+ " Remove \extra_embedded_files from buffer params "
+ i = find_token(document.header, '\\extra_embedded_files', 0)
+ if i == -1:
+ document.warning("Malformed lyx document: Missing '\\extra_embedded_files'.")
+ return
+ document.header.pop(i)
+
+
+def convert_spaceinset(document):
+ " Convert '\\InsetSpace foo' to '\\begin_inset Space foo\n\\end_inset' "
+ for i in range(len(document.body)):
+ if re.search(r'\InsetSpace', document.body[i]):
+ document.body[i] = document.body[i].replace('\\InsetSpace', '\n\\begin_inset Space')
+ document.body[i] = document.body[i] + "\n\\end_inset"
+
+
+def revert_spaceinset(document):
+ " Revert '\\begin_inset Space foo\n\\end_inset' to '\\InsetSpace foo' "
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Space", i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of space inset.")
+ continue
+ document.body[i] = document.body[i].replace('\\begin_inset Space', '\\InsetSpace')
+ del document.body[j]
+
+
+def convert_hfill(document):
+ " Convert hfill to space inset "
+ i = 0
+ while True:
+ i = find_token(document.body, "\\hfill", i)
+ if i == -1:
+ return
+ document.body[i] = document.body[i].replace('\\hfill', '\n\\begin_inset Space \\hfill{}\n\\end_inset')
+
+
+def revert_hfills(document):
+ ' Revert \\hfill commands '
+ for i in range(len(document.body)):
+ document.body[i] = document.body[i].replace('\\InsetSpace \\hfill{}', '\\hfill')
+ document.body[i] = document.body[i].replace('\\InsetSpace \\dotfill{}', \
+ '\\begin_inset ERT\nstatus collapsed\n\n' \
+ '\\begin_layout Standard\n\n\n\\backslash\n' \
+ 'dotfill{}\n\\end_layout\n\n\\end_inset\n\n')
+ document.body[i] = document.body[i].replace('\\InsetSpace \\hrulefill{}', \
+ '\\begin_inset ERT\nstatus collapsed\n\n' \
+ '\\begin_layout Standard\n\n\n\\backslash\n' \
+ 'hrulefill{}\n\\end_layout\n\n\\end_inset\n\n')
+
+
+def revert_hspace(document):
+ ' Revert \\InsetSpace \\hspace{} to ERT '
+ i = 0
+ while True:
+ i = find_token(document.body, "\\InsetSpace \\hspace", i)
+ if i == -1:
+ return
+ length = get_value(document.body, '\\length', i+1)
+ if length == '':
+ document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
+ return
+ del document.body[i+1]
+ document.body[i] = document.body[i].replace('\\InsetSpace \\hspace*{}', \
+ '\\begin_inset ERT\nstatus collapsed\n\n' \
+ '\\begin_layout Standard\n\n\n\\backslash\n' \
+ 'hspace*{' + length + '}\n\\end_layout\n\n\\end_inset\n\n')
+ document.body[i] = document.body[i].replace('\\InsetSpace \\hspace{}', \
+ '\\begin_inset ERT\nstatus collapsed\n\n' \
+ '\\begin_layout Standard\n\n\n\\backslash\n' \
+ 'hspace{' + length + '}\n\\end_layout\n\n\\end_inset\n\n')
+
+
+def revert_protected_hfill(document):
+ ' Revert \\begin_inset Space \\hspace*{\\fill} to ERT '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\begin_inset Space \\hspace*{\\fill}', i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of space inset.")
+ continue
+ del document.body[j]
+ document.body[i] = document.body[i].replace('\\begin_inset Space \\hspace*{\\fill}', \
+ '\\begin_inset ERT\nstatus collapsed\n\n' \
+ '\\begin_layout Standard\n\n\n\\backslash\n' \
+ 'hspace*{\n\\backslash\nfill}\n\\end_layout\n\n\\end_inset\n\n')
+
+
+def revert_local_layout(document):
+ ' Revert local layout headers.'
+ i = 0
+ while True:
+ i = find_token(document.header, "\\begin_local_layout", i)
+ if i == -1:
+ return
+ j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
+ if j == -1:
+ # this should not happen
+ break
+ document.header[i : j + 1] = []
+
+
+def convert_pagebreaks(document):
+ ' Convert inline Newpage insets to new format '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\newpage', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newpage newpage',
+ '\\end_inset']
+ i = 0
+ while True:
+ i = find_token(document.body, '\\pagebreak', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newpage pagebreak',
+ '\\end_inset']
+ i = 0
+ while True:
+ i = find_token(document.body, '\\clearpage', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newpage clearpage',
+ '\\end_inset']
+ i = 0
+ while True:
+ i = find_token(document.body, '\\cleardoublepage', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newpage cleardoublepage',
+ '\\end_inset']
+
+
+def revert_pagebreaks(document):
+ ' Revert \\begin_inset Newpage to previous inline format '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\begin_inset Newpage', i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of Newpage inset.")
+ continue
+ del document.body[j]
+ document.body[i] = document.body[i].replace('\\begin_inset Newpage newpage', '\\newpage')
+ document.body[i] = document.body[i].replace('\\begin_inset Newpage pagebreak', '\\pagebreak')
+ document.body[i] = document.body[i].replace('\\begin_inset Newpage clearpage', '\\clearpage')
+ document.body[i] = document.body[i].replace('\\begin_inset Newpage cleardoublepage', '\\cleardoublepage')
+
+
+def convert_linebreaks(document):
+ ' Convert inline Newline insets to new format '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\newline', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newline newline',
+ '\\end_inset']
+ i = 0
+ while True:
+ i = find_token(document.body, '\\linebreak', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newline linebreak',
+ '\\end_inset']
+
+
+def revert_linebreaks(document):
+ ' Revert \\begin_inset Newline to previous inline format '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\begin_inset Newline', i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of Newline inset.")
+ continue
+ del document.body[j]
+ document.body[i] = document.body[i].replace('\\begin_inset Newline newline', '\\newline')
+ document.body[i] = document.body[i].replace('\\begin_inset Newline linebreak', '\\linebreak')
+
+
+def convert_japanese_plain(document):
+ "Set language japanese-plain to japanese"
+ i = 0
+ if document.language == "japanese-plain":
+ document.language = "japanese"
+ i = find_token(document.header, "\\language", 0)
+ if i != -1:
+ document.header[i] = "\\language japanese"
+ j = 0
+ while True:
+ j = find_token(document.body, "\\lang japanese-plain", j)
+ if j == -1:
+ return
+ document.body[j] = document.body[j].replace("\\lang japanese-plain", "\\lang japanese")
+ j = j + 1
+
+
##
# Conversion hub
#
[314, []],
[315, []],
[316, [convert_subfig]],
- [317, []]
+ [317, []],
+ [318, []],
+ [319, [convert_spaceinset, convert_hfill]],
+ [320, []],
+ [321, [convert_tablines]],
+ [322, []],
+ [323, [convert_pagebreaks]],
+ [324, [convert_linebreaks]],
+ [325, [convert_japanese_plain]],
]
-revert = [[316, [revert_wrapplacement]],
+revert = [[324, []],
+ [323, [revert_linebreaks]],
+ [322, [revert_pagebreaks]],
+ [321, [revert_local_layout]],
+ [320, [revert_tablines]],
+ [319, [revert_protected_hfill]],
+ [318, [revert_spaceinset, revert_hfills, revert_hspace]],
+ [317, [remove_extra_embedded_files]],
+ [316, [revert_wrapplacement]],
[315, [revert_subfig]],
[314, [revert_colsep]],
[313, []],