]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_1.py
064b938ad1bb5e862a4a6fbe57c663603c419a8e
[lyx.git] / lib / lyx2lyx / lyx_2_1.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 The LyX team
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
20 """ Convert files to the file format generated by lyx 2.1"""
21
22 import re, string
23 import unicodedata
24 import sys, os
25
26 # Uncomment only what you need to import, please.
27
28 from parser_tools import find_token, find_end_of_inset, get_value, \
29    del_token
30
31 #from parser_tools import find_token, find_end_of, find_tokens, \
32   #find_token_exact, find_end_of_inset, find_end_of_layout, \
33   #find_token_backwards, is_in_inset, get_value, get_quoted_value, \
34   #del_token, check_token, get_option_value
35
36 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
37
38 #from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
39 #  put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
40 #  revert_font_attrs, hex2ratio, str2bool
41
42 ####################################################################
43 # Private helper functions
44
45 #def remove_option(lines, m, option):
46     #''' removes option from line m. returns whether we did anything '''
47     #l = lines[m].find(option)
48     #if l == -1:
49         #return False
50     #val = lines[m][l:].split('"')[1]
51     #lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
52     #return True
53
54
55 ###############################################################################
56 ###
57 ### Conversion and reversion routines
58 ###
59 ###############################################################################
60
61 def revert_visible_space(document):
62     "Revert InsetSpace visible into its ERT counterpart"
63     i = 0
64     while True:
65       i = find_token(document.body, "\\begin_inset space \\textvisiblespace{}", i)
66       if i == -1:
67         return
68       end = find_end_of_inset(document.body, i)
69       subst = put_cmd_in_ert("\\textvisiblespace{}")
70       document.body[i:end + 1] = subst
71
72
73 def convert_undertilde(document):
74     " Load undertilde automatically "
75     i = find_token(document.header, "\\use_mathdots" , 0)
76     if i != -1:
77       document.header.insert(i + 1, "\\use_undertilde 1")
78
79
80 def revert_undertilde(document):
81     " Load undertilde if used in the document "
82     undertilde = find_token(document.header, "\\use_undertilde" , 0)
83     if undertilde == -1:
84       document.warning("No \\use_undertilde line. Assuming auto.")
85     else:
86       val = get_value(document.header, "\\use_undertilde", undertilde)
87       del document.header[undertilde]
88       try:
89         usetilde = int(val)
90       except:
91         document.warning("Invalid \\use_undertilde value: " + val + ". Assuming auto.")
92         # probably usedots has not been changed, but be safe.
93         usetilde = 1
94
95       if usetilde == 0:
96         # do not load case
97         return
98       if usetilde == 2:
99         # force load case
100         add_to_preamble(document, ["\\usepackage{undertilde}"])
101         return
102     
103     # so we are in the auto case. we want to load undertilde if \utilde is used.
104     i = 0
105     while True:
106       i = find_token(document.body, '\\begin_inset Formula', i)
107       if i == -1:
108         return
109       j = find_end_of_inset(document.body, i)
110       if j == -1:
111         document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
112         i += 1
113         continue
114       code = "\n".join(document.body[i:j])
115       if code.find("\\utilde") != -1:
116         add_to_preamble(document, ["\\@ifundefined{utilde}{\\usepackage{undertilde}}"])
117         return
118       i = j
119
120
121 def revert_negative_space(document):
122     "Revert InsetSpace negmedspace and negthickspace into its TeX-code counterpart"
123     i = 0
124     j = 0
125     reverted = False
126     while True:
127       i = find_token(document.body, "\\begin_inset space \\negmedspace{}", i)
128       if i == -1:
129         j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
130         if j == -1:
131           # load amsmath in the preamble if not already loaded if we are at the end of checking
132           if reverted == True:
133             i = find_token(document.header, "\\use_amsmath 2", 0)
134             if i == -1:
135               add_to_preamble(document, ["\\@ifundefined{negthickspace}{\\usepackage{amsmath}}"])
136           return
137       if i == -1:
138         return
139       end = find_end_of_inset(document.body, i)
140       subst = put_cmd_in_ert("\\negmedspace{}")
141       document.body[i:end + 1] = subst
142       j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
143       if j == -1:
144         return
145       end = find_end_of_inset(document.body, j)
146       subst = put_cmd_in_ert("\\negthickspace{}")
147       document.body[j:end + 1] = subst
148       reverted = True
149
150
151 def revert_math_spaces(document):
152     "Revert formulas with protected custom space and protected hfills to TeX-code"
153     i = 0
154     while True:
155       i = find_token(document.body, "\\begin_inset Formula", i)
156       if i == -1:
157         return
158       j = document.body[i].find("\\hspace*")
159       if j != -1:
160         end = find_end_of_inset(document.body, i)
161         subst = put_cmd_in_ert(document.body[i][21:])
162         document.body[i:end + 1] = subst
163       i = i + 1
164
165
166 def convert_japanese_encodings(document):
167     " Rename the japanese encodings to names understood by platex "
168     jap_enc_dict = {
169         "EUC-JP-pLaTeX": "euc",
170         "JIS-pLaTeX":    "jis",
171         "SJIS-pLaTeX":   "sjis"
172     }
173     i = find_token(document.header, "\\inputencoding" , 0)
174     if i == -1:
175         return
176     val = get_value(document.header, "\\inputencoding", i)
177     if val in jap_enc_dict.keys():
178         document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
179
180
181 def revert_japanese_encodings(document):
182     " Revert the japanese encodings name changes "
183     jap_enc_dict = {
184         "euc":  "EUC-JP-pLaTeX",
185         "jis":  "JIS-pLaTeX",
186         "sjis": "SJIS-pLaTeX"
187     }
188     i = find_token(document.header, "\\inputencoding" , 0)
189     if i == -1:
190         return
191     val = get_value(document.header, "\\inputencoding", i)
192     if val in jap_enc_dict.keys():
193         document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
194
195
196 def revert_justification(document):
197     " Revert the \\justification buffer param"
198     if not del_token(document.header, '\\justification', 0):
199         document.warning("Malformed LyX document: Missing \\justification.")
200
201     
202
203 ##
204 # Conversion hub
205 #
206
207 supported_versions = ["2.1.0","2.1"]
208 convert = [
209            [414, []],
210            [415, [convert_undertilde]],
211            [416, []],
212            [417, [convert_japanese_encodings]],
213            [418, []],
214           ]
215
216 revert =  [
217            [417, [revert_justification]],
218            [416, [revert_japanese_encodings]],
219            [415, [revert_negative_space,revert_math_spaces]],
220            [414, [revert_undertilde]],
221            [413, [revert_visible_space]]
222           ]
223
224
225 if __name__ == "__main__":
226     pass