]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_10.py
Move DrawStrategy enum to update_flags.h.
[lyx.git] / lib / lyx2lyx / lyx_0_10.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2006 José Matos <jamatos@lyx.org>
3 #
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.
8 #
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.
13 #
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
17
18 """Convert files to the file format generated by lyx 0.10"""
19
20
21 def regularise_header(document):
22     "Put each entry in header into a separate line."
23     i = 0
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
29             i += len(tmp)
30         i += 1
31
32
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)
38     if space_pos == -1:
39         space_pos = len(line)
40
41     bksl_pos = line.find("\\", j)
42     if bksl_pos == -1:
43         bksl_pos = len(line)
44
45     return min(space_pos, bksl_pos)
46
47
48 def regularise_body(document):
49     """Place tokens starting with a backslash into a separate line."""
50
51     getline_tokens = [
52         "added_space_bottom",
53         "added_space_top",
54         "align",
55         "layout",
56         "fill_bottom",
57         "fill_top",
58         "labelwidthstring",
59         "pagebreak_top",
60         "pagebreak_bottom",
61         "noindent",
62     ]
63
64     noargs_tokens = [
65         "backslash",
66         "begin_deeper",
67         "end_deeper",
68         "end_float",
69         "end_inset",
70         "hfill",
71         "newline",
72         "protected_separator",
73     ]
74
75     onearg_tokens = [
76         "bar",
77         "begin_float",
78         "family",
79         "latex",
80         "shape",
81         "size",
82         "series",
83         "cursor",
84     ]
85
86     i = 0
87     while i < len(document.body):
88         line = document.body[i]
89         j = 0
90         new_block = []
91         while j < len(line):
92             k = line.find("\\", j)
93
94             if k == -1:
95                 new_block += [line[j:]]
96                 break
97
98             if k != j:
99                 # document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k]))
100                 new_block += [line[j:k]]
101                 j = k
102
103             k = find_next_space(line, j + 1)
104
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:]]
110                 break
111
112             # These tokens take no arguments
113             if token in noargs_tokens:
114                 new_block += [line[j:k]]
115                 j = k
116                 continue
117
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]]
122                 j = k
123                 continue
124
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]
129
130                 if inset == "Latex":
131                     new_block += [line[j:l]]
132                     j = l
133                     continue
134
135                 if inset in ["LatexCommand", "LatexDel", "Label", "Figure", "Formula"]:
136                     new_block += [line[j:]]
137                     break
138
139                 if inset == "Quotes":
140                     l = find_next_space(line, l + 1)
141                     new_block += [line[j:l]]
142                     j = l
143                     continue
144
145                 document.warning("unkown inset %s" % inset)
146                 assert False
147
148             # We are inside a latex inset, pass the text verbatim
149             new_block += [line[j:]]
150             break
151
152         document.body[i : i + 1] = new_block
153         i += len(new_block)
154
155
156 supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
157 convert = [[210, [regularise_header, regularise_body]]]
158 revert = []
159
160
161 if __name__ == "__main__":
162     pass