1 # This file is part of lyx2lyx
2 # Copyright (C) 2006 José Matos <jamatos@lyx.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 """Convert files to the file format generated by lyx 0.10"""
21 def regularise_header(document):
22 "Put each entry in header into a separate line."
24 while i < len(document.header):
25 line = document.header[i]
26 if len(line.split("\\")) > 1:
27 tmp = ["\\" + token.strip() for token in line.split("\\")][1:]
28 document.header[i : i + 1] = tmp
33 def find_next_space(line, j):
34 """Return position of next space or backslash, which one comes
35 first, starting from position j, if none exists returns last
36 position in line (+1)."""
37 space_pos = line.find(" ", j)
41 bksl_pos = line.find("\\", j)
45 return min(space_pos, bksl_pos)
48 def regularise_body(document):
49 """Place tokens starting with a backslash into a separate line."""
72 "protected_separator",
87 while i < len(document.body):
88 line = document.body[i]
92 k = line.find("\\", j)
95 new_block += [line[j:]]
99 # document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k]))
100 new_block += [line[j:k]]
103 k = find_next_space(line, j + 1)
105 token = line[j + 1 : k]
106 # These tokens take the rest of the line
107 if token in getline_tokens:
108 # document.warning("getline_token:%s\tj=%d\t\t#%s#%s#" % (token,j,line,line[j:]))
109 new_block += [line[j:]]
112 # These tokens take no arguments
113 if token in noargs_tokens:
114 new_block += [line[j:k]]
118 # These tokens take one argument
119 if token in onearg_tokens:
120 k = find_next_space(line, k + 1)
121 new_block += [line[j:k]]
125 # Special treatment for insets
126 if token in ["begin_inset"]:
127 l = find_next_space(line, k + 1)
128 inset = line[k + 1 : l]
131 new_block += [line[j:l]]
135 if inset in ["LatexCommand", "LatexDel", "Label", "Figure", "Formula"]:
136 new_block += [line[j:]]
139 if inset == "Quotes":
140 l = find_next_space(line, l + 1)
141 new_block += [line[j:l]]
145 document.warning("unkown inset %s" % inset)
148 # We are inside a latex inset, pass the text verbatim
149 new_block += [line[j:]]
152 document.body[i : i + 1] = new_block
156 supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
157 convert = [[210, [regularise_header, regularise_body]]]
161 if __name__ == "__main__":