]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_10.py
Length.cpp: add new unit representing \baselineskip
[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 k, if not existing return last
36     position in line."""
37     l = line.find(' ', j)
38     if l == -1:
39         l = len(line)
40     k = line.find('\\', j)
41     if k == -1:
42         k = len(line)
43
44     if k < l:
45         return k
46     return l
47
48
49 def regularise_body(document):
50     """ Place tokens starting with a backslash into a separate line. """
51
52     getline_tokens = ["added_space_bottom", "added_space_top",
53                       "align", "layout", "fill_bottom", "fill_top",
54                       "labelwidthstring", "pagebreak_top",
55                       "pagebreak_bottom", "noindent"]
56
57     noargs_tokens = ["backslash", "begin_deeper", "end_deeper",
58                      "end_float", "end_inset", "hfill", "newline",
59                      "protected_separator"]
60
61     onearg_tokens = ["bar", "begin_float", "family", "latex", "shape",
62                      "size", "series", "cursor"]
63
64     i = 0
65     while i < len(document.body):
66         line = document.body[i]
67         j = 0
68         tmp = []
69         while j < len(line):
70             k = line.find('\\', j)
71
72             if k == -1:
73                 tmp += [line[j:]]
74                 break
75
76             if k != j:
77                 tmp += [line[j: k]]
78                 j = k
79
80             k = find_next_space(line, j+1)
81
82             # These tokens take the rest of the line
83             token = line[j+1:k]
84             if token in getline_tokens:
85                 tmp += [line[j:]]
86                 break
87
88             # These tokens take no arguments
89             if token in noargs_tokens:
90                 tmp += [line[j:k]]
91                 j = k
92                 continue
93
94             # These tokens take one argument
95             if token in onearg_tokens:
96                 k = find_next_space(line, k + 1)
97                 tmp += [line[j:k]]
98                 j = k
99                 continue
100
101             # Special treatment for insets
102             if token in ["begin_inset"]:
103                 l = find_next_space(line, k + 1)
104                 inset = line[k+1: l]
105
106                 if inset == "Latex":
107                     tmp += [line[j:l]]
108                     j = l
109                     continue
110
111                 if inset in ["LatexCommand", "LatexDel"]:
112                     tmp += [line[j:]]
113                     break
114
115                 if inset == "Quotes":
116                     l = find_next_space(line, l + 1)
117                     tmp += [line[j:l]]
118                     j = l
119                     continue
120
121                 document.warning("unkown inset %s" % line)
122                 assert(False)
123
124             # We are inside a latex inset, pass the text verbatim
125             tmp += [line[j:]]
126             break
127
128         document.body[i: i+1] = tmp
129         i += len(tmp)
130
131
132 supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
133 convert = [[210, [regularise_header, regularise_body]]]
134 revert  = []
135
136
137 if __name__ == "__main__":
138     pass