1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2006 José Matos <jamatos@lyx.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 """ 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. """
51 getline_tokens = ["added_space_bottom", "added_space_top",
52 "align", "layout", "fill_bottom", "fill_top",
53 "labelwidthstring", "pagebreak_top",
54 "pagebreak_bottom", "noindent"]
56 noargs_tokens = ["backslash", "begin_deeper", "end_deeper",
57 "end_float", "end_inset", "hfill", "newline",
58 "protected_separator"]
60 onearg_tokens = ["bar", "begin_float", "family", "latex", "shape",
61 "size", "series", "cursor"]
64 while i < len(document.body):
65 line = document.body[i]
69 k = line.find('\\', j)
72 new_block += [line[j:]]
76 #document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k]))
77 new_block += [line[j: k]]
80 k = find_next_space(line, j+1)
83 # These tokens take the rest of the line
84 if token in getline_tokens:
85 #document.warning("getline_token:%s\tj=%d\t\t#%s#%s#" % (token,j,line,line[j:]))
86 new_block += [line[j:]]
89 # These tokens take no arguments
90 if token in noargs_tokens:
91 new_block += [line[j:k]]
95 # These tokens take one argument
96 if token in onearg_tokens:
97 k = find_next_space(line, k + 1)
98 new_block += [line[j:k]]
102 # Special treatment for insets
103 if token in ["begin_inset"]:
104 l = find_next_space(line, k + 1)
108 new_block += [line[j:l]]
112 if inset in ["LatexCommand", "LatexDel", "Label", "Figure",
114 new_block += [line[j:]]
117 if inset == "Quotes":
118 l = find_next_space(line, l + 1)
119 new_block += [line[j:l]]
123 document.warning("unkown inset %s" % inset)
126 # We are inside a latex inset, pass the text verbatim
127 new_block += [line[j:]]
130 document.body[i: i+1] = new_block
134 supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
135 convert = [[210, [regularise_header, regularise_body]]]
139 if __name__ == "__main__":