2006-04-07 16:11:16 +00:00
|
|
|
# This file is part of lyx2lyx
|
2006-08-01 14:38:13 +00:00
|
|
|
# Copyright (C) 2006 José Matos <jamatos@lyx.org>
|
2006-04-07 16:11:16 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2011-08-25 23:10:36 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-04-07 16:11:16 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
""" Convert files to the file format generated by lyx 0.10"""
|
2006-08-01 14:38:13 +00:00
|
|
|
|
2006-08-01 22:54:57 +00:00
|
|
|
def regularise_header(document):
|
2006-08-02 14:19:22 +00:00
|
|
|
" Put each entry in header into a separate line. "
|
2006-08-01 22:54:57 +00:00
|
|
|
i = 0
|
|
|
|
while i < len(document.header):
|
|
|
|
line = document.header[i]
|
|
|
|
if len(line.split('\\')) > 1:
|
|
|
|
tmp = [ '\\'+ token.strip() for token in line.split('\\')][1:]
|
|
|
|
document.header[i: i+1] = tmp
|
|
|
|
i += len(tmp)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
def find_next_space(line, j):
|
2006-08-02 14:19:22 +00:00
|
|
|
""" Return position of next space or backslash, which one comes
|
2018-04-28 13:57:57 +00:00
|
|
|
first, starting from position j, if none exists returns last
|
|
|
|
position in line (+1)."""
|
|
|
|
space_pos = line.find(' ', j)
|
|
|
|
if space_pos == -1:
|
|
|
|
space_pos = len(line)
|
2006-08-01 22:54:57 +00:00
|
|
|
|
2018-04-28 13:57:57 +00:00
|
|
|
bksl_pos = line.find('\\', j)
|
|
|
|
if bksl_pos == -1:
|
|
|
|
bksl_pos = len(line)
|
|
|
|
|
|
|
|
return min(space_pos, bksl_pos)
|
2006-08-01 22:54:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def regularise_body(document):
|
2006-08-02 14:19:22 +00:00
|
|
|
""" Place tokens starting with a backslash into a separate line. """
|
|
|
|
|
|
|
|
getline_tokens = ["added_space_bottom", "added_space_top",
|
|
|
|
"align", "layout", "fill_bottom", "fill_top",
|
|
|
|
"labelwidthstring", "pagebreak_top",
|
|
|
|
"pagebreak_bottom", "noindent"]
|
|
|
|
|
|
|
|
noargs_tokens = ["backslash", "begin_deeper", "end_deeper",
|
|
|
|
"end_float", "end_inset", "hfill", "newline",
|
|
|
|
"protected_separator"]
|
|
|
|
|
|
|
|
onearg_tokens = ["bar", "begin_float", "family", "latex", "shape",
|
|
|
|
"size", "series", "cursor"]
|
|
|
|
|
2006-08-01 22:54:57 +00:00
|
|
|
i = 0
|
|
|
|
while i < len(document.body):
|
|
|
|
line = document.body[i]
|
|
|
|
j = 0
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block = []
|
2006-08-01 22:54:57 +00:00
|
|
|
while j < len(line):
|
|
|
|
k = line.find('\\', j)
|
|
|
|
|
|
|
|
if k == -1:
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block += [line[j:]]
|
2006-08-01 22:54:57 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if k != j:
|
2018-04-28 13:57:57 +00:00
|
|
|
#document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k]))
|
|
|
|
new_block += [line[j: k]]
|
2006-08-01 22:54:57 +00:00
|
|
|
j = k
|
|
|
|
|
|
|
|
k = find_next_space(line, j+1)
|
|
|
|
|
|
|
|
token = line[j+1:k]
|
2018-04-28 13:57:57 +00:00
|
|
|
# These tokens take the rest of the line
|
2006-08-02 14:19:22 +00:00
|
|
|
if token in getline_tokens:
|
2018-04-28 13:57:57 +00:00
|
|
|
#document.warning("getline_token:%s\tj=%d\t\t#%s#%s#" % (token,j,line,line[j:]))
|
|
|
|
new_block += [line[j:]]
|
2006-08-01 22:54:57 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# These tokens take no arguments
|
2006-08-02 14:19:22 +00:00
|
|
|
if token in noargs_tokens:
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block += [line[j:k]]
|
2006-08-01 22:54:57 +00:00
|
|
|
j = k
|
|
|
|
continue
|
|
|
|
|
|
|
|
# These tokens take one argument
|
2006-08-02 14:19:22 +00:00
|
|
|
if token in onearg_tokens:
|
2006-08-01 22:54:57 +00:00
|
|
|
k = find_next_space(line, k + 1)
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block += [line[j:k]]
|
2006-08-01 22:54:57 +00:00
|
|
|
j = k
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Special treatment for insets
|
|
|
|
if token in ["begin_inset"]:
|
|
|
|
l = find_next_space(line, k + 1)
|
|
|
|
inset = line[k+1: l]
|
|
|
|
|
|
|
|
if inset == "Latex":
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block += [line[j:l]]
|
2006-08-01 22:54:57 +00:00
|
|
|
j = l
|
|
|
|
continue
|
|
|
|
|
2018-04-28 13:57:57 +00:00
|
|
|
if inset in ["LatexCommand", "LatexDel", "Label", "Figure",
|
|
|
|
"Formula"]:
|
|
|
|
new_block += [line[j:]]
|
2006-08-01 22:54:57 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if inset == "Quotes":
|
|
|
|
l = find_next_space(line, l + 1)
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block += [line[j:l]]
|
2006-08-01 22:54:57 +00:00
|
|
|
j = l
|
|
|
|
continue
|
|
|
|
|
2018-04-28 13:57:57 +00:00
|
|
|
document.warning("unkown inset %s" % inset)
|
2006-08-01 22:54:57 +00:00
|
|
|
assert(False)
|
|
|
|
|
|
|
|
# We are inside a latex inset, pass the text verbatim
|
2018-04-28 13:57:57 +00:00
|
|
|
new_block += [line[j:]]
|
2006-08-01 22:54:57 +00:00
|
|
|
break
|
|
|
|
|
2018-04-28 13:57:57 +00:00
|
|
|
document.body[i: i+1] = new_block
|
|
|
|
i += len(new_block)
|
2006-08-01 22:54:57 +00:00
|
|
|
|
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
|
2006-08-01 22:54:57 +00:00
|
|
|
convert = [[210, [regularise_header, regularise_body]]]
|
2006-04-07 16:11:16 +00:00
|
|
|
revert = []
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|