]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_10.py
lyx_0_08.py:
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 """ Convert files generated by lyx 0.10"""
20
21 def regularise_header(document):
22     " Place tokens in their 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, starting from position k, if
35     not existing return last position in line."""
36     l = line.find(' ', j)
37     if l == -1:
38         l = len(line)
39     k = line.find('\\', j)
40     if k == -1:
41         k = len(line)
42
43     if k < l:
44         return k
45     return l
46
47
48 def regularise_body(document):
49     i = 0
50     while i < len(document.body):
51         line = document.body[i]
52         j = 0
53         tmp = []
54         while j < len(line):
55             k = line.find('\\', j)
56
57             if k == -1:
58                 tmp += [line[j:]]
59                 break
60
61             if k != j:
62                 tmp += [line[j: k]]
63                 j = k
64
65             k = find_next_space(line, j+1)
66
67             # These tokens take the rest of the line
68             token = line[j+1:k]
69             if token in ["added_space_bottom", "added_space_top", "align", "layout", "fill_bottom", "fill_top", "labelwidthstring", "pagebreak_top", "pagebreak_bottom", "noindent"]:
70                 tmp += [line[j:]]
71                 break
72
73             # These tokens take no arguments
74             if token in ["backslash", "begin_deeper", "end_deeper", "end_float", "end_inset", "hfill", "newline", "protected_separator"]:
75                 tmp += [line[j:k]]
76                 j = k
77                 continue
78
79             # These tokens take one argument
80             if token in ["bar", "begin_float", "family", "latex", "shape", "size", "series", "cursor"]:
81                 k = find_next_space(line, k + 1)
82                 tmp += [line[j:k]]
83                 j = k
84                 continue
85
86             # Special treatment for insets
87             if token in ["begin_inset"]:
88                 l = find_next_space(line, k + 1)
89                 inset = line[k+1: l]
90
91                 if inset == "Latex":
92                     tmp += [line[j:l]]
93                     j = l
94                     continue
95
96                 if inset in ["LatexCommand", "LatexDel"]:
97                     tmp += [line[j:]]
98                     break
99
100                 if inset == "Quotes":
101                     l = find_next_space(line, l + 1)
102                     tmp += [line[j:l]]
103                     j = l
104                     continue
105
106                 assert(False)
107
108             # We are inside a latex inset, pass the text verbatim
109             tmp += [line[j:]]
110             break
111
112         document.body[i: i+1] = tmp
113         i += len(tmp)
114
115
116 convert = [[210, [regularise_header, regularise_body]]]
117 revert  = []
118
119
120 if __name__ == "__main__":
121     pass
122