]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_10.py
Small improvement to pre-historic file format conversion.
[lyx.git] / lib / lyx2lyx / lyx_0_10.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2006 José Matos <jamatos@lyx.org>
4 #
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.
9 #
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.
14 #
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
18
19 """ Convert files to the file format generated by lyx 0.10"""
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 = ["added_space_bottom", "added_space_top",
52                       "align", "layout", "fill_bottom", "fill_top",
53                       "labelwidthstring", "pagebreak_top",
54                       "pagebreak_bottom", "noindent"]
55
56     noargs_tokens = ["backslash", "begin_deeper", "end_deeper",
57                      "end_float", "end_inset", "hfill", "newline",
58                      "protected_separator"]
59
60     onearg_tokens = ["bar", "begin_float", "family", "latex", "shape",
61                      "size", "series", "cursor"]
62
63     i = 0
64     while i < len(document.body):
65         line = document.body[i]
66         j = 0
67         new_block = []
68         while j < len(line):
69             k = line.find('\\', j)
70
71             if k == -1:
72                 new_block += [line[j:]]
73                 break
74
75             if k != j:
76                 #document.warning("j=%d\tk=%d\t#%s#%s#" % (j,k,line,line[j: k]))
77                 new_block += [line[j: k]]
78                 j = k
79
80             k = find_next_space(line, j+1)
81
82             token = line[j+1:k]
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:]]
87                 break
88
89             # These tokens take no arguments
90             if token in noargs_tokens:
91                 new_block += [line[j:k]]
92                 j = k
93                 continue
94
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]]
99                 j = k
100                 continue
101
102             # Special treatment for insets
103             if token in ["begin_inset"]:
104                 l = find_next_space(line, k + 1)
105                 inset = line[k+1: l]
106
107                 if inset == "Latex":
108                     new_block += [line[j:l]]
109                     j = l
110                     continue
111
112                 if inset in ["LatexCommand", "LatexDel", "Label", "Figure",
113                              "Formula"]:
114                     new_block += [line[j:]]
115                     break
116
117                 if inset == "Quotes":
118                     l = find_next_space(line, l + 1)
119                     new_block += [line[j:l]]
120                     j = l
121                     continue
122
123                 document.warning("unkown inset %s" % inset)
124                 assert(False)
125
126             # We are inside a latex inset, pass the text verbatim
127             new_block += [line[j:]]
128             break
129
130         document.body[i: i+1] = new_block
131         i += len(new_block)
132
133
134 supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
135 convert = [[210, [regularise_header, regularise_body]]]
136 revert  = []
137
138
139 if __name__ == "__main__":
140     pass